|
Boost : |
Subject: Re: [boost] interest in properties library
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2008-12-20 14:20:19
On Sat, Dec 20, 2008 at 5:29 AM, Daniel Oberhoff
<daniel.oberhoff_at_[hidden]> wrote:
> HasPropertiesBase<VariantTypes> * foo = new Foo;
> foo->set_property("a", 1);
> int a = get<int>( foo->get_property( "a" ) );
If this is your typical use case, I would implement properties
non-intrusively in a separate property_database:
property_database pd;
boost::shared_ptr<Foo> foo(new Foo);
pd.set<int>(foo,"a");
The property_database class is essentially a
std::map<boost::weak_ptr<void>,std::map<std::string,boost::any>>, and
you'd dynamic_cast<void *> the pointers passed to set and get.
Another thought, you can also use tag types to make the properties
compile-time type-safe. See the similar system used in
http://www.boost.org/doc/libs/1_37_0/libs/exception/doc/boost-exception.html.
In that case the syntax could be:
typedef property<struct tag_a,int> prop_a;
pd.set(prop_a(42),foo);
int p=pd.get<prop_a>(foo);
Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk