tags:

views:

149

answers:

6

Hello,

I would ideally like to be able to add (very repetitive) C/C++ code to my actual code, but at compile time, code which would come from say, the stdout of a python script, the same way one does with macros.

For example, let's say I want to have functions that depend on the public attributes of a given class, being able to just write the following in my C++ code would be a blessing:

generate_boring_functions(FooBarClass,"FooBarClass.cpp")

Is that feasible using conventional means? Or must I hack with Makefiles and temporary source files?

Thanks.

+2  A: 

A makefile (or equivalent) is a "conventional" means!

Oli Charlesworth
A: 

You could try the Boost Preprocessor Library. It's just an extension of the regular preprocessor, but if you're creative, you can achieve nearly anything in it.

DeadMG
+3  A: 

You do most likely need to tweak the Makefile a bit. It would be easy to write a (Python) script that reads each of your source files as an additional preprocessing step, replacing instances of generate_boring_functions (or any other script-macro) with the correct code, potentially just by invoking generate_boring_functions.py with the right arguments, and bypassing the need for temporary files by sending the source to the compiler over standard input.

Damn, now I want to make something like this.

Edit: A rule like this, stuck in a makefile, could be used to handle the extra build step. This is untested and added only for some shot at completeness.

%.o : %.cpp
    python macros.py $< | g++ -x cpp -c - -o $@
Jon Purdy
How would I send the source to the compiler over stdin(not using temp files)?
Manux
Oy, http://stackoverflow.com/questions/2083874/compile-string-of-c-code
Manux
@Manux: This approach would only work with one source file at a time...
Oli Charlesworth
Indeed, but I can have python parse all files, then to concatenate all the necessary code in the right order(headers first etc...) easily, then just call something like "python generate_all_code.py | gcc -x cpp -o out -", as if I compiled one big c++ file... bad idea?
Manux
@Manux, directly piping to gcc is likely to put you in a world of hurt when you need to debug your python output. I suggest you save to a temp file and then cat the temp file to gcc.
JSBangs
+3  A: 

If a makefile isn't conventional enough for you, you could get by with cleverly-written macros.

class FooBarClass
{
    DEFINE_BORING_METHODS( FooBarClass )

    /* interesting functions begin here */
}

I very frequently see this done to implement the boilerplate parts of COM classes.

But if you want something that's neither make nor macro, then I don't know what you could possibly mean.

JSBangs
+1 macros to simplify boilerplate code.
Greg Domjan
A: 

Did you have a look at PythoidC ? It can be used to generate C code.

Onkar Deshpande
A: 

I've never used this particular technology, but it sounds as though you're looking for something like Ned Batchelder's Cog tool.

Python scripts are embedded into a C++ source file such that when run through the cog tool additional C++ code is generated for the C++ compiler to consume. So your build process would consist of an extra step to have cog produce the actual C++ source file before the C++ compiler is invoked.

Michael Burr