Re: [boost] interest in properties library

Hi,
I am thinking about submitting a library proposal and would like to query wether there is any interest in such a library and chance of success of such a proposal.
The basic idea is to implement the possibility to decorate a class with named properties, such that given a pointer to an instance
accessed by name rather than using set/get methods. Also the type of the instance should not need to be known at compile time, other than
derives from a suitable base class describing the property mechanisms.
There is a rough implementation existing and used in a product environment. Setting and getting is done using appropriate boost functions. Type resolution is done using RTTI (especially dynamic cast). Complex inheritance hierarchies are supported through virtual inheritance from the
such that several classes along the hierarchy can contribute
On Fri, Dec 19, 2008 at 5:50 AM, Daniel Oberhoff < daniel.oberhoff_at_[hidden]> wrote: properties can be that it base class, property slots.
CRTP and static initialization is used to register properties. Convenient setter/getter function generators are provided as is a mechanism to access nested properties using dot notation.
I'm not sure I follow exactly what you mean, could you give a small example of the interface and usage? It sounds like you are referring to something along the lines of C# properties though I don't immediately see what you are using dynamic casting for or what exactly this provides as a benefit over get/set methods. Thanks.
sorry, forgot the examples. so, to enable properties for a class: class Foo : public HasProperties<VariantTypes, foo> { protected: typedef HasProperties<VariantTypes, foo> PBase; typedef Foo This; static void init_properties() { PBase::add_ivar_property_rw( "a", &This::a ); } private: int a; }; usage: HasPropertiesBase<VariantTypes> * foo = new Foo; foo->set_property("a", 1); int a = get<int>( foo->get_property( "a" ) ); Here VariantTyped is a mpl::sequence Furthermore it still works when a class has several such properties activated classes along its inheritance ancestors, and all properties can be acessed unless there is a name conflict, in which case classes further down override properties from its ancestors, though this could be handled differently. What I still want to do is enable simpler syntax and maybe signals triggered by property and other goodies, but that is actually rather simple to do, using bind and/or lambda. Best Daniel

Daniel Oberhoff wrote:
HasPropertiesBase<VariantTypes> * foo = new Foo; foo->set_property("a", 1); int a = get<int>( foo->get_property( "a" ) );
And I suppose if it's not an int, it throws? For proprieties, it might be better to provide lexical conversion to the target type, so that it always succeeds (modulo allocation errors). Anyway, I'm not too sure this is very useful. It's just a std::map<std::string, some_variant_type>. Does that really deserve its own class?

On Sat, Dec 20, 2008 at 5:29 AM, Daniel Oberhoff <daniel.oberhoff@fit.fraunhofer.de> 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

Hello, Daniel. Saturday, December 20, 2008 at 4:29:53 PM you wrote: DO> What I still want to do is enable simpler syntax and maybe signals DO> triggered by property and other goodies, but that is actually rather DO> simple to do, using bind and/or lambda. Some time ago I showed another way to define properties for the class. I think what this way quite simpler. Take a look: Some class which describes game item: class Item { public: Item() : properties__(this) {;} // that is all you need to // initialize properties for the class instance. virtual ~Item() {;} PROPERTIES(Item, // Static information about item PROPERTY(SItemInfo const*, ItemInfo) // Quantity of the stored items (with default // initializer PROPERTY_I(size_t, Quantity, 0) // Flag which indicates what item is on the base PROPERTY_I(bool, IsOnTheBase, false) ); }; Usage of this class in the code: boost::shared_ptr<Item> i(new Item()); i->Quantity = quantity; Class 'Item' contains simple properties whose directly mapped to the data members. Another class, which contains "getter" property: class Module { public: PROPERTIES(Module, // Common information about module RO_PROPERTY(SModuleInfo, ModuleInfo, GetModuleInfo) ); virtual const SModuleInfo& GetModuleInfo() const {return m_ModuleInfo;} }; This property can be accessed in the same way: SModuleInfo mi = module.ModuleInfo; or in the 'function-style' way: int size = module.ModuleInfo().ModuleSize; Also you can define n-arity indexer properties: class Base : public IBase { public: PROPERTIES(Base, RO_INDEX_PROPERTY(const IModule*, Map, 2, (TPARAM, TPARAM), GetModule) ); }; Usage is: Base& b = ...; IModule* m = b.Map[1][2]; , virtual properties: class IModule { public: PROPERTIES(IModule, // definition of the abstract property ABSTRACT_RO_PROPERTY(SModuleInfo, ModuleInfo) ); }; -- class Module : public IModule { public: PROPERTIES(Module, // Implementation of the abstract property IMPLEMENT_PROPERTY(IModule, RO_PROPERTY(SModuleInfo, ModuleInfo, GetModuleInfo)) ); enumerate properties in runtime, simple serialize/deserialize properties and so on. Best Regards, Sergey mailto:flex_ferrum@artberg.ru
participants (4)
-
Daniel Oberhoff
-
Emil Dotchevski
-
Mathias Gaunard
-
Sergey Sadovnikov