Search code examples
c++preprocessorcodec

confusing define directive in c++ project


I am trying to figure out what this bit of code I encountered does but can't quite seem to understand how it works.

#define CODEC_BIND(classname, type) codec_creator cc_##classname##_##type ((type), new classname());

The codec_creator is a class that assigns a specific codec to an unordered map in the constructor as seen below.

extern std::unordered_map <unsigned short, std::shared_ptr<codec>> g_creator_map;
class codec_creator
{
public:
    codec_creator(unsigned short pt, codec* c)
    {
        g_creator_map[pt] = std::shared_ptr<codec>(c);
    }
};

As I see it, CODEC_BIND is used to add different types of codecs to the unordered map by calling the constructor in codec_creator for different codec classes. I am unsure though if this is the only thing it does though. Does it have any other purpose ?


Solution

  • The macro expands into a variable declaration (when used properly), probably used to create global variables.

    The variable type is codec_creator, the name is based off of the specific classname (an unsigned short number) and type values passed to the macro using the ## preprocessor operator. The variable's constructor call will pass the classname value and a newly constructed object of the type. This constructor will then store this class object pointer in the g_creator_map map. The variable constructed by the macro will have minimal size, because the class has no member variables.