Macro question

I am tired of typing, Is it possible to write a macro to automatically generate these code for me?

e.g.
#define GenerateInputOutputCode(idx1, field1, idx2, field2) \
virtual void serialize() const \
{ \
outValue(idx1, field1) ; \
outValue(idx2, field2) ; \
} \
\
virtual void deserialize() \
{ \
inValue(idx1, field1) ; \
inValue(idx2, field2) ; \
}


I would like to be able to accept arbitrary number of fields.
No. The C preprocessor just isn't designed to do that. You could generate the code with a program in a different language. That's what I did.
http://onslaught-vn.svn.sourceforge.net/viewvc/onslaught-vn/trunk/src/binder.py?revision=189&view=markup
http://onslaught-vn.svn.sourceforge.net/viewvc/onslaught-vn/trunk/src/Binder.h?revision=153&view=markup
OK, Thank you. That's what I was afraid of. I thought someone might know sth I don't ...
ATL macros create arrays that are then used in one or more functions. Since your data is heterogeneous, maybe the exact same approach might not be possible, so I leave you with an approximation:

1
2
3
4
5
6
7
8
9
10
11
12
#define BEGIN_INPUTOUTPUT() virtual void serialize() const {
#define END_INPUTOUTPUT() }
//And this one would be repeated once per variable
#define INPUTOUTPUT_ENTRY(type, name) outValue(type, name)


//And you would use them like this:
BEGIN_INPUTOUTPUT()
    INPUTOUTPUT_ENTRY(int, field1);
    INPUTOUTPUT_ENTRY(std::string, field2);
    INPUTOUTPUT_ENTRY(std::vector<double>, field3);
END_INPUTOUTPUT()
The trouble of that approach is, the user still have to specify the idx twice, one in the serialize and one in the deserialize, that's exactly what I wan to avoid ...
True, but imagine this: If you can somehow create an array of the different objects (maybe by using a polymorphic base), then you can have the macros declare an array and then the functions can refer to this array.

If this doesn't suit your needs, then go with helios' approach.
Topic archived. No new replies allowed.