
----- Original Message -----
From: Mathias Gaunard <mathias.gaunard@ens-lyon.org> To: boost@lists.boost.org Cc: Sent: Friday, February 10, 2012 5:19 PM Subject: Re: [boost] PCL - Portable C++ Library
On 02/10/2012 09:53 PM, paul Fultz wrote:
And everything needed to be defined together in one master macro, to make it easier to index the meta-data classes. It would've been nice to write it like this:
class SomeClass { PROPERTY((int) id, get get_id) PROPERTY((string) name, get get_name, set set_name) ... };
But I could never finde a good way to do that.
I can think of a way, but it would involve a hard limit on the number of properties and a switch for each access.
How would you use a switch? My design works by creating a metadata class, which would look something like this: class SomeClass { public: const int metadata_count = 2; //Partial-specialize this class for each property template<class Self, int N> struct metadata {}; template<class Self> struct metadata<Self, 0> { Self& self; metadata(Self& self) : self(self) {} int get() const { ... } void set(int x) { ... } const char * name() { return "id"; } }; template<class Self> struct metadata<Self, 1> { Self& self; metadata(Self& self) : self(self) {} string get() const { ... } void set(string x) { ... } const char * name() { return "name"; } }; ... }; Then I use the for_each from the MPL library to iterator over metadata classes. Then the user can access the properties using the visitor pattern like this: struct property_visitor { void operator(T x) { cout << x.name() << "," << x.get(); } }; SomeClass x; visit_each(x, property_visitor()); Now, the metadata_count is known because its all in one macro. To seperate it to each macro would require using the line number or something like that as the index. Which would require me to iterate over all integers to find valid metadata. How would you approach it using a switch and hard-limit? Sounds interesting. Perhaps that switch could be collapsed into compile-time "switch" of some sort. -paul