tags:

views:

112

answers:

4

I have a case where I'm using a macro to generate simple subclasses. I've currently got it defined as:

#define REGISTER( TYPE, NAME )                     \
    struct NAME ## Class : public ParentClass      \
    {                                              \
        NAME ## Class () : ParentClass ( # NAME ); \
    }

I really want the Name to contain a period, but then the macro gets expanded to this and doesn't work:

struct Simple.ClassClass : public ParentClass
{
    Simple.ClassClass () : ParentClass ("Simple.Class");
};

Is there anyway to use a preprocessor definition to get rid of the period in the name? Then I could keep the period in the string (as desired) and have the class name use a different (legal) character.

The class name doesn't matter (as long as it's unique). The only thing that matters is the readable string.

I have access to Boost.Preprocessor, but I couldn't understand if any of it would be useful for this.

Thanks.

EDIT: The reason behind this is I'm implementing the Industrial Strength Pluggable Factories as a template and a macro for easy generation of the maker class. There may be a lot of maker IDs though so I want the ability to "namespace" them by using a dot. I'm doing it right now with underscores already, I was just curious if I could get it working with a dot instead because I find it more visually pleasing. The only important part is that since another person besides me who is writing code would not see the code the macro generates, the macro can't do any insidious stuff behind the scenes. So whatever string gets put in as the name must be the key used to retrieve the item.

I relooked at the problem this morning and I think I can just avoid macros all together. Thanks anyway.

+1  A: 

Why don't you instead create a small standalone program that generates the classe declarations in a .h file? That way you don't need to fiddle with macros and have much more control.

Anders K.
don't give the poster Ideas. that's almost never a good idea.write code manually and avoid errorprone code.
nebukadnezzar
@nebu i wish "write code manually" and "avoid errorprone code" was mutually exclusive :-)
Anders K.
+2  A: 

I don't think you can mainipulate the parameter NAME att all, beside concatenate it with another string. So the obivous solution is no to use a string that contains dots.
REGISTER(whateverType, SomethingwithoutDot)

I know it doesn't answer your question but it seems to me that you should take a look on the problem that you try to solve with this macro and maybe find another way solving that problem instead. I would think will it be more productive

Gorgen
+1 for "try doing it another way".
Jonathan Sternberg
A: 

Not your answer, but a workaround. If you are happy with class name SimpleClass instead of SimpleClassClass, then this should work:

#define REGISTER( TYPE, NAME )                     \
struct NAME ## Class : public ParentClass      \
{                                              \
    NAME ## Class () : ParentClass ( # NAME ".Class" ){} \
}
Donotalo
A: 

Probably not what you are looking for, but why not just add an extra parameter to the macro?

#define REGISTER( TYPE, PREFIX, NAME )               \ 
    struct PREFIX ## Class : public ParentClass      \ 
    {                                                \ 
        PREFIX ## Class () : ParentClass ( # NAME ); \ 
    } 

REGISTER(SomeType, Simple, Simple.Class);
Remy Lebeau - TeamB