Reducing code repetition in C++
===

I am using x-macros to reduce the amount of repetition and code duplication while implementing a Lua interface for the game [Bitfighter][1]. The following code works fine:

      //                            Fn name     Valid param profiles  Profile count                           
    #  define TELEPORTER_LUA_METHOD_TABLE \
          TELEPORTER_LUA_METHOD_ITEM(addDest,    ARRAYDEF({{ PT,  END }}), 1 ) \
          TELEPORTER_LUA_METHOD_ITEM(delDest,    ARRAYDEF({{ INT, END }}), 1 ) \
          TELEPORTER_LUA_METHOD_ITEM(clearDests, ARRAYDEF({{      END }}), 1 ) \
   
   
    // BLOCK A Start
    const luaL_reg Teleporter::luaMethods[] =
    {
    #  define TELEPORTER_LUA_METHOD_ITEM(name, b, c) { #name, luaW_doMethod<Teleporter, &Teleporter::name > },
          TELEPORTER_LUA_METHOD_TABLE
    #  undef TELEPORTER_LUA_METHOD_ITEM
       { NULL, NULL }
    };
    // BLOCK A End

      /* Generates the following:
      const luaL_reg Teleporter::luaMethods[] =
      {
           { "addDest", luaW_doMethod<Teleporter, &Teleporter::addDest > }
           { "delDest", luaW_doMethod<Teleporter, &Teleporter::delDest > }
           { "clearDests", luaW_doMethod<Teleporter, &Teleporter::clearDests > }
           { NULL, NULL }
      };
      */

   
    // BLOCK B Start
    const LuaFunctionProfile Teleporter::functionArgs[] =
    {
    #  define TELEPORTER_LUA_METHOD_ITEM(name, profiles, profileCount) { #name, profiles, profileCount },
          TELEPORTER_LUA_METHOD_TABLE
    #  undef TELEPORTER_LUA_METHOD_ITEM
       { NULL, { }, 0 }
    };
    // BLOCK B End
    
    #undef TELEPORTER_LUA_METHOD_TABLE

      /* Generates the following:
      const LuaFunctionProfile Teleporter::functionArgs[] =
      {
         { "addDest",    {{ PT,  END }}, 1 }
         { "delDest",    {{ INT, END }}, 1 }
         { "clearDests", {{      END }}, 1 }
         { NULL, { }, 0 }
      };
      */

    #undef TELEPORTER_LUA_METHOD_TABLE
       
So far, so good.

Except that I do essentially the same thing in dozens of classes.  What I'd REALLY like to do is define the method table in each class (which can be called anything), then define two macros that can be called like this:

    GENERATE_LUA_METHODS(Teleporter, TELEPORTER_LUA_METHOD_TABLE)
    GENERATE_FUNCTION_PROFILE(Teleporter, TELEPORTER_LUA_METHOD_TABLE)

to avoid all the repeated code above in blocks A & B.  The obvious way is to use a nested macro, but that is, unfortunately, illegal.

Is there a better way?
   


  [1]: http://bitfighter.org