Boost logo

Boost :

Subject: Re: [boost] interest in properties library
From: Daniel Oberhoff (daniel.oberhoff_at_[hidden])
Date: 2008-12-21 17:31:08


> 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.
>
Interesting approach. If you need non-intrusiveness. Though if
intrusiveness is not a problem I think my approach is better, because
a) you bind the properties explicitly to the class and thus have much
less setup work to do and b) it automatically manages many properties
along the inheritance graph.
>
> 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);
>
Well, I use boost::variant, the other option is boost::any. The
variant makes it possible to inspect the type at runtime and automate
conversions, while the boost:.any would give you type safety. I admit
your approach would be more efficient, but I designed this with
scriptability in mind, which is also why I use strings as keys. Also
it looks like your approach does not allow addressing properties at
run-time.

Daniel


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk