(How) is it possible to make a class in C++ which will have
information about class member variables (type and name) available at
runtime? The minimum requirement is to be able to iterate through
member variables at runtime and call callback for each.
Class should meet following requirements:
0) it should be possible to at least sequentially iterate through all
variables (from first to last), and call overloaded function for each
one of them. Anything else is not required.
1) use of templates is allowed
2) use of macros is allowed
3) member variables with runtime info cannot be added or removed at
runtime.
And following restrictions:
0) all member variables with runtime info, should be defined at
compile time.
1) All member variables with runtime info, should be easily accessible
as normal member variables, via ".". No "->" overloading, and no calls
like "getIntProperty(const char* variableName)" are allowed.
2) All member variables should be declared only once. Using macros, or
templates, if necessary, but only once. Manually adding variables to
some kind of list within constructor is forbidden. However, it is
allowed to hide this operation (adding variable to some kind of list)
within macros. And it is allowed to use additional macros like
"BEGIN_PUBLIC_VARS", "END_PUBLIC_VARS"
3) Writing custom code preprocessor is not allowed.
Here is what I would like to be able to do:
---
struct Test{
BEGIN_VARIABLES
DECLARE_VAR(int, i);
DECLARE_VAR(unsigned int, j);
DECLARE_VAR(char, c);
END_VARIABLES
float z;//this one won't be visible at runtime.
};
struct Callback{
void process(int i){
printf("int: %d\n", i);
}
void process(unsigned int i){
printf("unsigned int: %d\n", i);
}
void process(char c){
printf("char: %c\n", i);
}
};
....
int main(int argc, char** argv){
Test test;
test.i = 1;
test.j = 2;
test.c = 'a';
Callback callback;
test.process(callback);
return 0;
}
/*
this should generate output:
int 1
unsigned int 2
char a
*/
---
How can this be done in C++?
I've tried to do this with either macros or templates, but hit
following problems:
1) With macros, it is not possible to write #define directive that
will produce several other #define directives.
2) With templates, it is not possible to give variable name as a
template parameter (or I didn't find the way).
Here is what I could create with templates:
---
#include <stdio.h>
struct Base{
void basePrint(int i){
printf("int\n");
}
void basePrint(unsigned int){
printf("unsigned int\n");
}
void basePrint(short i){
printf("short\n");
}
void basePrint(unsigned short i){
printf("unsigned short\n");
}
void basePrint(char c){
printf("char\n");
}
void basePrint(unsigned char c){
printf("unsigned char\n");
}
void print(){
}
};
template <class C, typename T> struct AddField: public C{
void print(){
C::print();
T i = 0;
basePrint(i);
}
};
int main(int argc, char** argv){
AddField<AddField<AddField<AddField<
AddField<AddField<Base, int>,
unsigned int>, char>, char>, long>, long> t;
t.print();
return 0;
}
---
It is very close to what I would like to get, but I cannot add named
fields this way, and this construction is ugly, not to mention other
problems.
This is not a homework or something, I just would like how something
like this could be possibly done in pure C++ without additional tools.
Regards, Victor
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
[]
Standard macros don't give you enough flexibility to solve this in an
elegant way. Code generator seems to be the right way to do so.
http://groups.google.com/group/comp.lang.c++/msg/55a4b3749771959c
--
Max