#define macro

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #define macro

    Hi.
    Code:
    #define DEFINE_COMMAND(name, description, refRequired, numParams, paramInfo) \
    	kCommandInfo_name = { \
    	#name, \
    	"", \
    	0, \
    	#description, \
    	refRequired, \
    	numParams, \
    	paramInfo, \
    	HANDLER(Cmd_name_Execute), \
    	Cmd_Default_Parse, \
    	NULL, \
    	0 \
    	};
    I want whatever string is passed as the name parameter to be substituted in two longer identifiers, i.e. if I pass Horticulture for name, I should get back kCommandInfo_Ho rticulture and Cmd_Horticultur e_Execute.
    Seems the preprocessor requires space before and after a token, of course.
    The stringizing and token-pasting operators don't appear to help here.
    Is there a way to get the preprocessor to insert a passed token into a longer identifier?
    Thanks.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You will probably need more macros so you can take advantage of the token pasting operator a bit like

    [code=c]
    #define CONCAT(a,b) a ## b
    #define CONCAT3(a,b,c) CONCAT(CONCAT(a ,b),c)
    [/code]

    and then call these from your original #defined macro

    P.S. No guarantees these work without modification as my daughter lunch is a higher priority task than running these through a compiler for you :D

    Comment

    • scruggsy
      New Member
      • Mar 2007
      • 147

      #3
      Originally posted by Banfa
      P.S. No guarantees these work without modification as my daughter lunch is a higher priority task than running these through a compiler for you :D
      :D
      Thanks for the tip. I'll fool around with it some more.

      Comment

      • scruggsy
        New Member
        • Mar 2007
        • 147

        #4
        Got it. Thanks again.
        While on the subject, is it possible to view the code generated by a macro or must I assume that if it compiles the macro expanded correctly (which isn't always the case)?

        Working macro:
        Code:
        #define DEFINE_COMMAND(name, description, refRequired, numParams, paramInfo) \
        	(kCommandInfo_ ## name) = { \
        	#name, \
        	"", \
        	0, \
        	#description, \
        	refRequired, \
        	numParams, \
        	paramInfo, \
        	HANDLER(Cmd_ ## name ## _Execute), \
        	Cmd_Default_Parse, \
        	NULL, \
        	0 \
        	};[
        
        CommandInfo DEFINE_COMMAND(AnimPathIncludes, 
        			   returns true if the actor is playing an anim containing the substring,
        			   1,
        			   1,
        			   kParams_OneString);
        Correctly gives:
        Code:
        CommandInfo kCommandInfo_AnimPathIncludes =
        {
        	"AnimPathIncludes",
        	"",
        	0,
        	"returns true if the actor is playing an anim containing the substring",
        	1,
        	1,
        	kParams_OneString,
        	HANDLER(Cmd_AnimPathIncludes_Execute),
        	Cmd_Default_Parse,
        	NULL,
        	0
        };

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Many compilers (but not all) have a switch to output preprocessed files. This is the file after all preprocessing has happened (i.e. ALL # statements so #defines and macro expension, #includes etc).

          Check your compiler documentation for a switch like this for your compiler (note in MSVC the switch for the compiler exists but I do not think it is available through the IDE you have to add it manually).

          A preprocessed file will be long, probably with large blank portions (corresponding to a #if statement that is false, include a header with preprocessor inclusion protection twice and you will get the header followed by the number of blank lines equal to the length of the header).

          A search and replace of 2 consecutive blank lines to 1 can help increase readability of it.

          The preprocessed file is compilable and this can aid debugging since you step through the actual code as opposed to the preprocessor definitions.


          I have worked with automatic code generators where the generated code contain about 10 levels of nested function like macros with none of the code looking like recognisable C or C functions due to the level of use of macros. When bugs existed in the code the only way to trace them was to compile to preprocessed source. Of course once you worked out where the bug in the preprocessed code was you had to try and work out which bit of which macro contained the error. Then of course contact the code generators maker and try and get the to change it.

          Comment

          • scruggsy
            New Member
            • Mar 2007
            • 147

            #6
            Wonderful. Thanks again.

            Comment

            Working...