Search code examples
c++code-generation

How to generate repeated code in Visual Studio for C++ using macros like #define in different files


I need to write code in one file, like:

void set_property_1();
void set_property_2();
void set_property_3();
...
void set_property_100();

And in another file with get_.

void get_property_1();
void get_property_2();
void get_property_3();
...
void get_property_100();

To make it just a bit faster i made a macro like

#define SET(num) void set_property_##num();
#define GET(num) void get_property_##num();
SET(1)
SET(2)
 ...
SET(10)

GET(1)
GET(2)
 ... 
GET(10)

It help just a bit. The question is, how to generate code like this in more common way? like

//pseudocode
//in third file
#define LIST = {1,2,3,4,5,6,7,8,9,10}

//In first file;
MACRO_FOR(AUTO L: LIST)SET(L)

//In second file;
MACRO_FOR(AUTO L: LIST)GET(L)

I`m using VisualStudio so may be there is some extension do that? Or some libraries? All what i find searchnig codegeneration c++ is information about low lvl stuff. Thanks for information.


Solution

  • As result one function which do what i want. Generate all combinations without permutations from any number of sets of names to make unique not repetitive macros :D. I`v place it here. May be will helpfull for someone. The repo.

    Usage in Visual Studio 19.

    1. Build .exe from source.
    2. Add Enviroment Varible to .exe file in Windows settings, i call it MACRO_GENERATOR.
    3. In Visual Studio -> project Properties->Build Events-> Pre-Build Event add call %MACRO_GENERATOR% $(ProjectDir) and then all files in Project directory will be executed.
    4. Add word #GENERATE at first string and use //#define[] and //#for like this:
    #GENERATE
    #define ADD_FUNC(NAME,PROPERTY) void f_##NAME##_##PROPERTY(){};
    #define ADD_FUNC2(NAME,PROPERTY,WEIGHT) void f_##NAME##_##PROPERTY##_##WEIGHT();
    #define ADD_FUNC3(NAME,PROPERTY,WEIGHT,KLMN) void f_##NAME##_##PROPERTY##_##WEIGHT##_##KLMN();
    
    //#define[] NAME one,two
    //#define[] COLOR red,green,blue
    //#define[] WEIGHT low,big,very_nice
    //#define[] KLMN K,L,M,N
    
    //#for ADD_FUNC(NAME, COLOR)
    //#for ADD_FUNC2(NAME,COLOR,WEIGHT)
    //#for ADD_FUNC3(NAME,COLOR,WEIGHT,KLMN)
    
    #include <iostream>
    
    int main()
    {
        std::cout << "Hello World!\n";
    }
    
    1. Press build and files in the project dir will be replaced with new with generated macros.
    #define ADD_FUNC(NAME,PROPERTY) void f_##NAME##_##PROPERTY(){};
    #define ADD_FUNC2(NAME,PROPERTY,WEIGHT) void f_##NAME##_##PROPERTY##_##WEIGHT();
    #define ADD_FUNC3(NAME,PROPERTY,WEIGHT,KLMN) void f_##NAME##_##PROPERTY##_##WEIGHT##_##KLMN();
    
    //NAME one,two
    //COLOR red,green,blue
    //WEIGHT low,big,very_nice
    //KLMN K,L,M,N
    
    // ADD_FUNC(NAME, COLOR) START GENERATION
     ADD_FUNC(one,red)
     ADD_FUNC(one,green)
     ADD_FUNC(one,blue)
     ADD_FUNC(two,red)
     ADD_FUNC(two,green)
     ADD_FUNC(two,blue)
    // ADD_FUNC(NAME, COLOR) END GENERATION
    
    // ADD_FUNC2(NAME,COLOR,WEIGHT) START GENERATION
     ADD_FUNC2(one,red,low)
     ADD_FUNC2(one,red,big)
     ADD_FUNC2(one,red,very_nice)
     ADD_FUNC2(one,green,low)
     ADD_FUNC2(one,green,big)
     ADD_FUNC2(one,green,very_nice)
     ADD_FUNC2(one,blue,low)
     ADD_FUNC2(one,blue,big)
     ADD_FUNC2(one,blue,very_nice)
     ADD_FUNC2(two,red,low)
     ADD_FUNC2(two,red,big)
     ADD_FUNC2(two,red,very_nice)
     ADD_FUNC2(two,green,low)
     ADD_FUNC2(two,green,big)
     ADD_FUNC2(two,green,very_nice)
     ADD_FUNC2(two,blue,low)
     ADD_FUNC2(two,blue,big)
     ADD_FUNC2(two,blue,very_nice)
    // ADD_FUNC2(NAME,COLOR,WEIGHT) END GENERATION
    
    // ADD_FUNC3(NAME,COLOR,WEIGHT,KLMN) START GENERATION
     ADD_FUNC3(one,red,low,K)
     ADD_FUNC3(one,red,low,L)
     ADD_FUNC3(one,red,low,M)
     ADD_FUNC3(one,red,low,N)
     ADD_FUNC3(one,red,big,K)
     ADD_FUNC3(one,red,big,L)
     ADD_FUNC3(one,red,big,M)
     ADD_FUNC3(one,red,big,N)
     ADD_FUNC3(one,red,very_nice,K)
     ADD_FUNC3(one,red,very_nice,L)
     ADD_FUNC3(one,red,very_nice,M)
     ADD_FUNC3(one,red,very_nice,N)
     ADD_FUNC3(one,green,low,K)
     ADD_FUNC3(one,green,low,L)
     ADD_FUNC3(one,green,low,M)
     ADD_FUNC3(one,green,low,N)
     ADD_FUNC3(one,green,big,K)
     ADD_FUNC3(one,green,big,L)
     ADD_FUNC3(one,green,big,M)
     ADD_FUNC3(one,green,big,N)
     ADD_FUNC3(one,green,very_nice,K)
     ADD_FUNC3(one,green,very_nice,L)
     ADD_FUNC3(one,green,very_nice,M)
     ADD_FUNC3(one,green,very_nice,N)
     ADD_FUNC3(one,blue,low,K)
     ADD_FUNC3(one,blue,low,L)
     ADD_FUNC3(one,blue,low,M)
     ADD_FUNC3(one,blue,low,N)
     ADD_FUNC3(one,blue,big,K)
     ADD_FUNC3(one,blue,big,L)
     ADD_FUNC3(one,blue,big,M)
     ADD_FUNC3(one,blue,big,N)
     ADD_FUNC3(one,blue,very_nice,K)
     ADD_FUNC3(one,blue,very_nice,L)
     ADD_FUNC3(one,blue,very_nice,M)
     ADD_FUNC3(one,blue,very_nice,N)
     ADD_FUNC3(two,red,low,K)
     ADD_FUNC3(two,red,low,L)
     ADD_FUNC3(two,red,low,M)
     ADD_FUNC3(two,red,low,N)
     ADD_FUNC3(two,red,big,K)
     ADD_FUNC3(two,red,big,L)
     ADD_FUNC3(two,red,big,M)
     ADD_FUNC3(two,red,big,N)
     ADD_FUNC3(two,red,very_nice,K)
     ADD_FUNC3(two,red,very_nice,L)
     ADD_FUNC3(two,red,very_nice,M)
     ADD_FUNC3(two,red,very_nice,N)
     ADD_FUNC3(two,green,low,K)
     ADD_FUNC3(two,green,low,L)
     ADD_FUNC3(two,green,low,M)
     ADD_FUNC3(two,green,low,N)
     ADD_FUNC3(two,green,big,K)
     ADD_FUNC3(two,green,big,L)
     ADD_FUNC3(two,green,big,M)
     ADD_FUNC3(two,green,big,N)
     ADD_FUNC3(two,green,very_nice,K)
     ADD_FUNC3(two,green,very_nice,L)
     ADD_FUNC3(two,green,very_nice,M)
     ADD_FUNC3(two,green,very_nice,N)
     ADD_FUNC3(two,blue,low,K)
     ADD_FUNC3(two,blue,low,L)
     ADD_FUNC3(two,blue,low,M)
     ADD_FUNC3(two,blue,low,N)
     ADD_FUNC3(two,blue,big,K)
     ADD_FUNC3(two,blue,big,L)
     ADD_FUNC3(two,blue,big,M)
     ADD_FUNC3(two,blue,big,N)
     ADD_FUNC3(two,blue,very_nice,K)
     ADD_FUNC3(two,blue,very_nice,L)
     ADD_FUNC3(two,blue,very_nice,M)
     ADD_FUNC3(two,blue,very_nice,N)
    // ADD_FUNC3(NAME,COLOR,WEIGHT,KLMN) END GENERATION
    
    #include <iostream>
    
    int main()
    {
        std::cout << "Hello World!\n";
    }
    
    
    1. Be carefull! :) Only few checks added. The text of generation commands must be really closer to example.