Sign in to follow this  
  • entries
    455
  • comments
    639
  • views
    422456

Voodoo!

Sign in to follow this  
_the_phantom_

99 views

I've just obtained more proof that Boost is pure voodoo in computer form!

GTL makes use of a boost::multi_index_container in order to peform id -> creator and string -> creator lookups. The same system is staying in place, with a few name changes, in GTL3.

This code is in GTL2;

namespace mi = boost::multi_index;

struct FilterCreatorDetails
{
FileTypes id_;
std::string key_;
FilterCreator_t creator_;

FilterCreatorDetails(FileTypes id, std::string const &key, FilterCreator_t creator) : id_(id), key_(key), creator_(creator)
{
}
FilterCreatorDetails(FilterCreatorDetails const &lhs) : id_(lhs.id_), key_(lhs.key_), creator_(lhs.creator_)
{
}
bool operator<(FilterCreatorDetails const &lhs) const
{
return id_ < lhs.id_;
};
};

struct key{};
struct id{};

class FilterRegister
{
public:
typedef mi::multi_index_container<
FilterCreatorDetails,
mi::indexed_by<
mi::ordered_unique< mi::tag , mi::member >,
mi::ordered_non_unique< mi::tag , mi::member >
>
> texmap_t;
// rest of class definition
}

FilterCreator_t FilterRegister::getCreator(std::string const &val)
{
typedef FilterRegister::texmap_t::index::type tex_by_name;
tex_by_name::iterator it = texturefilters->get().find(val);
if(it == texturefilters->get().end())
throw LoaderNotFoundException("Texture Loader not found for texture type : " + val);

return (*it).creator_;
}



This code compiles perfectly.

GTL had this code;

namespace mi = boost::multi_index;

struct DecoderCreatorDetails
{
GameTextureLoader3::FileTypes id_;
std::string key_;
GameTextureLoader3::DecoderCreator_t creator_;

DecoderCreatorDetails(GameTextureLoader3::FileTypes id, std::string const &key, GameTextureLoader3::DecoderCreator_t creator) : id_(id), key_(key), creator_(creator)
{
}
DecoderCreatorDetails(FilterCreatorDetails const &lhs) : id_(lhs.id_), key_(lhs.key_), creator_(lhs.creator_)
{
}
bool operator<(FilterCreatorDetails const &lhs) const
{
return id_ < lhs.id_;
};
};

struct key{};
struct id{};

typedef mi::multi_index_container<
DecoderCreatorDetails,
mi::indexed_by<
mi::ordered_unique< mi::tag , mi::member >,
mi::ordered_non_unique< mi::tag , mi::member >
>
> decodermap_t;

typedef decodermap_t::index::type tex_by_name;


class DecoderRegistery
{
public:
DecoderCreator_t getCreator(std::string const &val)
{
tex_by_name::iterator it = imageDataDecoders.get().find(val);
if(it == texturefilters->get().end())
throw "This is bad practice!!"; //throw LoaderNotFoundException("Texture Loader not found for texture type : " + val);

return (*it).creator_;
}



A few scope changes and name changes and does it compile? Did it hell.

Some swearing and looking at the docs later we now have;

namespace mi = boost::multi_index;

struct DecoderCreatorDetails
{
GameTextureLoader3::FileTypes id_;
std::string ext_;
GameTextureLoader3::DecoderCreator_t creator_;

DecoderCreatorDetails(GameTextureLoader3::FileTypes id, std::string const &ext, GameTextureLoader3::DecoderCreator_t creator) : id_(id), ext_(ext), creator_(creator)
{
}
DecoderCreatorDetails(DecoderCreatorDetails const &lhs) : id_(lhs.id_), ext_(lhs.ext_), creator_(lhs.creator_)
{
}
bool operator<(DecoderCreatorDetails const &lhs) const
{
return id_ < lhs.id_;
};
};

struct extension{};
struct id{};

typedef mi::multi_index_container<
DecoderCreatorDetails,
mi::indexed_by<
mi::ordered_unique< mi::tag, mi::member >,
mi::ordered_non_unique< mi::tag, mi::member >
>
> decodermap_t;

class DecoderRegistry
{
public:
template
GameTextureLoader3::DecoderCreator_t getCreator(T const &val)
{
typedef typename decodermap_t::index::type tex_by_key;
tex_by_key &name_idx = imageDataDecoders.get();

tex_by_key::iterator it = name_idx.find(val);
if(it == name_idx.end())
throw "This is bad practice!!"; // throw LoaderNotFoundException("Texture Loader not found for texture of type : " + boost::lexical_cast(val));

return (*it).creator_;
}
// rest of class definition
}



This compiles perfectly.
Changes? Well, aside from templating it after I got it working I've moxed the typedef back into function scope and decomposed the lookup operation into two lines.

Even that if() statement was throwing an error, that works now and I haven't changed a thing about it; well as the operations go, techinically it's faster now as we don't perform two lookups.

Voodoo I tell you!
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now