(newbie) design question (is there an MPL solution?)
I'm just now treading into the (rather intimidating) waters of template metaprogramming, so I'm not even sure if it will fit what I'm trying to accomplish. I need to create a kind of registry to keep track of instances of variables of different types (actually, several of these registries, representing different system states); variables of a type would be identified by an unique (integer) id. In other words, the variables within a particular registry represent the state of the system at a given point in time, so although each registry would have variables of the same types and ids, the values of the variables in different registries may be different. If the variables were all of the same type, a simple map would work. What I would like is a way of asking this registry for a variable of a specified type and id, and get back the appropriate instance (of the appropriate type). I.e., something like the following would be ideal: MyType mt = registry.get<MyType>(my_id); Is there anything in the MPL (or elsewhere in Boost) that might help me accomplish what I'm looking to do? If not, does anyone have any design suggestions to accomplish this in another way? Any help would be appreciated! Thanks, Tim
"Timothy Perrigo"
I'm just now treading into the (rather intimidating) waters of template metaprogramming, so I'm not even sure if it will fit what I'm trying to accomplish. I need to create a kind of registry to keep track of instances of variables of different types (actually, several of these registries, representing different system states); variables of a type would be identified by an unique (integer) id. In other words, the variables within a particular registry represent the state of the system at a given point in time, so although each registry would have variables of the same types and ids, the values of the variables in different registries may be different. If the variables were all of the same type, a simple map would work. What I would like is a way of asking this registry for a variable of a specified type and id, and get back the appropriate instance (of the appropriate type). I.e., something like the following would be ideal:
MyType mt = registry.get<MyType>(my_id);
Is there anything in the MPL (or elsewhere in Boost) that might help me accomplish what I'm looking to do? If not, does anyone have any design suggestions to accomplish this in another way? Any help would be appreciated!
Thanks, Tim
I do not see too much difficulties implementing something like that:
class static_any_base {
public:
virtual ~static_any_base() {}
};
template<typename T>
struct static_any_t {
static_any_t( T const& v ) : m_value( v ) {}
T m_value;
};
class MyRegistry {
public:
typedef IdType int;
template<typename T>
IdType
add( T const& t )
{
m_storage.push_back( boost::shared_ptr
Timothy Perrigo wrote: ...
I need to create a kind of registry to keep track of instances of variables of different types (actually, several of these registries, representing different system states); variables of a type would be identified by an unique (integer) id. In other words, the variables within a particular registry represent the state of the system at a given point in time, so although each registry would have variables of the same types and ids, the values of the variables in different registries may be different. If the variables were all of the same type, a simple map would work. What I would like is a way of asking this registry for a variable of a specified type and id, and get back the appropriate instance (of the appropriate type). I.e., something like the following would be ideal:
MyType mt = registry.get<MyType>(my_id);
why not create a templated registry? surely that would solve your problem if each registry could only hold a particular type then instead of typing MyType mt = registry.get<MyType>(my_id); you could type: MyType mt = registry<MyType>.get(my_id); but if you want to have lots of registries of type MyType why not have a static factory function within each registry class, lets say we want a particular registry based upon a integer id, then you could write: MyType mt = registry<MyType>::GetInstance(5).get(my_id); jonathan oulds
Timothy Perrigo
I'm just now treading into the (rather intimidating) waters of template metaprogramming, so I'm not even sure if it will fit what I'm trying to accomplish. I need to create a kind of registry to keep track of instances of variables of different types (actually, several of these registries, representing different system states); variables of a type would be identified by an unique (integer) id. In other words, the variables within a particular registry represent the state of the system at a given point in time, so although each registry would have variables of the same types and ids, the values of the variables in different registries may be different. If the variables were all of the same type, a simple map would work. What I would like is a way of asking this registry for a variable of a specified type and id, and get back the appropriate instance (of the appropriate type). I.e., something like the following would be ideal:
MyType mt = registry.get<MyType>(my_id);
Is there anything in the MPL (or elsewhere in Boost) that might help me accomplish what I'm looking to do? If not, does anyone have any design suggestions to accomplish this in another way? Any help would be appreciated!
Need more info. Does each id map to just one type, or might there be different types using the same ID in different registries of type R? Is the id a runtime value, or is it known at compile-time? -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
On Aug 10, 2004, at 10:04 AM, David Abrahams wrote:
Timothy Perrigo
writes: I'm just now treading into the (rather intimidating) waters of template metaprogramming, so I'm not even sure if it will fit what I'm trying to accomplish. I need to create a kind of registry to keep track of instances of variables of different types (actually, several of these registries, representing different system states); variables of a type would be identified by an unique (integer) id. In other words, the variables within a particular registry represent the state of the system at a given point in time, so although each registry would have variables of the same types and ids, the values of the variables in different registries may be different. If the variables were all of the same type, a simple map would work. What I would like is a way of asking this registry for a variable of a specified type and id, and get back the appropriate instance (of the appropriate type). I.e., something like the following would be ideal:
MyType mt = registry.get<MyType>(my_id);
Is there anything in the MPL (or elsewhere in Boost) that might help me accomplish what I'm looking to do? If not, does anyone have any design suggestions to accomplish this in another way? Any help would be appreciated!
Need more info.
Does each id map to just one type, or might there be different types using the same ID in different registries of type R?
Is the id a runtime value, or is it known at compile-time?
-- Dave Abrahams Boost Consulting http://www.boost-consulting.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Thanks for the reply...I'll try to give as much background information as I can without clouding the issue. We're working on a constraint processing system. Particular problems will be modeled with variables with values within certain domains and subject to certain constraints. These constraints, when propagated, will narrow the domains of the variables, but some form of searching will be required to bind all the variables to values and find a feasible solution. The idea behind the registry I mentioned is for it to serve as a snapshot of the current state of the system (at a specific point in a search, for example). The "same" variables will exist in different registries, but may have different domains since their sets of possible values may have been reduced due to decisions made during the search. So to answer your first question, a particular id will always correspond to a variable of a given type, though its value at that point in time (i.e, in that registry) may differ from the same variable in a different registry. Right now, the id is a runtime value; whenever a new constrained variable is created, it is given a new id. Does this help at all? Any suggestions, pointers, critiques, etc would be greatly appreciated! PS -- Gennadiy Rozental's earlier reply does get me past my earlier stumbling block, though we're still investigating whether this approach will allow us to do everything we'd like, so I'd definitely be interested in hearing any other ideas. Thanks! Tim
Timothy Perrigo
Dave wrote:
Need more info.
Does each id map to just one type, or might there be different types using the same ID in different registries of type R?
Is the id a runtime value, or is it known at compile-time?
Thanks for the reply...I'll try to give as much background information as I can without clouding the issue. We're working on a constraint processing system. Particular problems will be modeled with variables with values within certain domains and subject to certain constraints. These constraints, when propagated, will narrow the domains of the variables, but some form of searching will be required to bind all the variables to values and find a feasible solution. The idea behind the registry I mentioned is for it to serve as a snapshot of the current state of the system (at a specific point in a search, for example). The "same" variables will exist in different registries, but may have different domains since their sets of possible values may have been reduced due to decisions made during the search.
Actually all that background info clouds the issue.
So to answer your first question, a particular id will always correspond to a variable of a given type, though its value at that point in time (i.e, in that registry) may differ from the same variable in a different registry.
OK.
Right now, the id is a runtime value; whenever a new constrained variable is created, it is given a new id.
But does it *need* to be a runtime value? If so, something like Gennadiy's solution is the only choice. If not, you can do something much more efficient with MPL. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
On Aug 10, 2004, at 4:12 PM, David Abrahams wrote:
Timothy Perrigo
writes: Dave wrote:
Need more info.
Does each id map to just one type, or might there be different types using the same ID in different registries of type R?
Is the id a runtime value, or is it known at compile-time?
Thanks for the reply...I'll try to give as much background information as I can without clouding the issue. We're working on a constraint processing system. Particular problems will be modeled with variables with values within certain domains and subject to certain constraints. These constraints, when propagated, will narrow the domains of the variables, but some form of searching will be required to bind all the variables to values and find a feasible solution. The idea behind the registry I mentioned is for it to serve as a snapshot of the current state of the system (at a specific point in a search, for example). The "same" variables will exist in different registries, but may have different domains since their sets of possible values may have been reduced due to decisions made during the search.
Actually all that background info clouds the issue. Sorry...I was afraid of that!
So to answer your first question, a particular id will always correspond to a variable of a given type, though its value at that point in time (i.e, in that registry) may differ from the same variable in a different registry.
OK.
Right now, the id is a runtime value; whenever a new constrained variable is created, it is given a new id.
But does it *need* to be a runtime value? If so, something like Gennadiy's solution is the only choice. If not, you can do something much more efficient with MPL.
It may be possible to design things so that the ids of the variables are known at compile time...If so, do you have any thoughts on how we could accomplish this using MPL? I'm excited about learning more about metaprogramming, so I'd welcome any suggestions or advice. Thanks again, Tim
Timothy Perrigo
But does it *need* to be a runtime value? If so, something like Gennadiy's solution is the only choice. If not, you can do something much more efficient with MPL.
It may be possible to design things so that the ids of the variables are known at compile time...If so, do you have any thoughts on how we could accomplish this using MPL? I'm excited about learning more about metaprogramming, so I'd welcome any suggestions or advice.
Actually if the value is known at compile-time, just use the Boost.Tuples library (to be replaced eventually by Boost.Fusion, which is built on MPL) directly. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
participants (4)
-
David Abrahams
-
Gennadiy Rozental
-
Jonathan Oulds
-
Timothy Perrigo