Jump to content
  • Advertisement
Sign in to follow this  

Using Macros in C++ to Manipulate Names...

This topic is 4946 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a question that doesn't have too high of an urgen level, merely something that would help me, and satisfy my burning curiosity. I've recently discovered how to make some simple macros, and I have a question now: Is there some way I can manipulate a variable name in a macro, without having that code compiled? The reason I want this is because I have a simple game engine, with object registration. When you register an object, you supply it's creation function (a function that returns a pointer to an new instance of itself), it's parent index (that has already been registered, default=-1), and it's name as a string. Example of registration: (using polymorphism... Base class: instance)
class CTimmy : public instance { ... };
instance* NEW_Timmy() { return new CTimmy; }
OBJ_ID Timmy=object_register(&NEW_Timmy,"Timmy");
As you can see, this is a rather complicated process that the user (if any) might not want to go through for each class. What I was thinking is having something like this:
//Using same definition of "CTimmy"
#define RFUNC(type) instance* NEW_[...]() { return new type; }
#define REG(type,parent) OBJ_ID [...]=object_register(&NEW_[...],"[...]",parent)
//And finally:
And supposedly, these macros would give me the same result. Does anyone have any ideas as to how to do this? NOTE: The "[...]" is supposedly the supplied class name without the first character.

Share this post

Link to post
Share on other sites
I believe the following is what you're looking for:

// RFUNC macro
#define RFUNC(Name) Instance* NEW_##Name() { return new Name; }

// REG macro
#define REG(Name, Parent) OBJ_ID Name##ObjID = object_register(&NEW_##Name, #Name, Parent)

I lied, this won't give you the results you're looking for (I can't get the Name to passed correctly, it passes ##Name##).

Edit 2:
Okay, I fixed it, it works fine now.

Edit 3:
I misread your post. I don't believe that you can modify the input for a macro (someone else may be able to correct me).

Share this post

Link to post
Share on other sites
Meh, don't strain yourself on this, if this doesn't work, I can always use the alternative, which is actually what I'm considering.
Thanks for your help.

NOTE: What does the '##' macro operator do? I know the '#' operator returns a string of that argument...

EDIT: I was able to refine it to something this simple:
OBJECT_REGISTER(Timmy, CTimmy, NEW_Timmy, -1);

EDIT: Wait a minute... Your code does work! Thanks! Rating++

Share this post

Link to post
Share on other sites
This works fine, other than it doesn't remove the C from CTimmy. The ## is the Token-Pasting Operator:

The double-number-sign or “token-pasting” operator (##), which is sometimes called the “merging” operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token and therefore cannot be the first or last token in the macro definition.

If a formal parameter in a macro definition is preceded or followed by the token-pasting operator, the formal parameter is immediately replaced by the unexpanded actual argument. Macro expansion is not performed on the argument prior to replacement.

Then, each occurrence of the token-pasting operator in token-string is removed, and the tokens preceding and following it are concatenated. The resulting token must be a valid token. If it is, the token is scanned for possible replacement if it represents a macro name. The identifier represents the name by which the concatenated tokens will be known in the program before replacement. Each token represents a token defined elsewhere, either within the program or on the compiler command line. White space preceding or following the operator is optional.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!