[mpl] Newbie question - working with sets
I've been trying to use MPL to create a system on an embedded platform
(where memory is at a premium and dynamic allocations are expensive)
that allows for multiple components or properties to be combined into a
single object at both compile and run-time.
The basic idea is, say I have the following properties for a 3D model
(each its own independent class; there are more than the examples I list
here):
ModelBase
Configuration
JointAnimation
TextureAnimation
LightingConfig
I know that a box doesn't need JointAnimation, so when I instantiate a
Model for it I'd like it to not have that "property".
(I could easily accomplish all of this using a std::map and a dynamic
allocation for each property, but again, it's prohibitive on the
platform and I'd rather use metaprogramming if I can.)
So what I'd like to do, in theory, is something like:
// Declare a generic class for a property-based object that templates
off of the set of properties it can contain
template<class PropertiesSet>
class ObjectWithProperties
{
public:
// Create an instance of ObjectWithProperties, using some subset of
PropertiesSet
template<class PropertiesSubset>
static ObjectWithProperties *create();
// Attempt to retrieve a Property, return null on failure
template<class PropertyType>
PropertyType *getProperty();
private:
SomeStaticListManager< size<PropertiesSet>::value >
which_properties_are_used;
void *data_buffer_for_properties;
};
// Define the set of properties that a model can consist of
typedef set
on Fri Sep 07 2007, Dan Posluns
The Create() method would iterate over the subset, tally up the sizeof() for each class, and then perform a single allocation for data_buffer_for_properties and proceed to use placement new to instantiate each class in the subset. (The destructor would then clean up all of that ugliness.)
This sounds like a good job for Boost.Fusion
The which_properties_are_used member would be an efficient list manager that can be used to lookup the existence of properties, indexing the types in the set using the order<> operator.
Unfortunately, I haven't been able to get very far with set<>. I tried the following:
typedef fold< PropertiesSubset, int_<0>, plus< _1, sizeof_<_2> > >::type TotalSize;
cout << TotalSize::value;
... and it gives me nothing like what I'm looking for.
Huh... That won't give you anything useful anyway, because it doesn't account for the alignment requirement of the different types. I suggest using Fusion to build a tuple of the properties you're interested in. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com The Astoria Seminar ==> http://www.astoriaseminar.com
participants (2)
-
Dan Posluns
-
David Abrahams