Boost logo

Boost :

From: Lee Brown (lee_at_[hidden])
Date: 2002-02-16 00:37:30


On Thursday 14 February 2002 09:35, you wrote:
> > On Wed, 13 Feb 2002, Karl Nelson wrote:

Do you think something like this would be useful?
It allows to declare a set/get pair as a property that
may be set a run time. No casting is needed.

Would it be better to have a property inherit from
a base "class property".

Sorry if it seems confusing.
Double sorry if there are errors, this is a
gedundesperiment or whatever they're called.

template
<
   class C, // the class with the property
   class Type, // the type of the property
>
struct strstream_stringify_property {

   template
   <
      Type (C::*Get)() // the method to get property
>
   static string get_property(const C& p) const
   {
      stristream ret;
      ret << p.*Get();

      return ret;
   }

   template
   <
      void (C::*Set)(Type) // the method to set the property
>
   static void set_property(C& p, const string& value)
   {
      strostream v(value);
      Type t;
      v >> t;
      p.*Set(t);
   }
};

template
<
   class C // C is the class that has the properties
   class Stringify = strstream_stringify_property<C>, // this stringifies
get and set values
>
class property {
public:
   typedef C class_type;

   string get_name() { return name_; }
   void set_name(string name) { name_ = name; }

   string get(C& c) { return get_func_(c); }
   void set(C& c, string v) { set_func_(c, v); }

   template
   <
      class T
>
   property(T (C::*Get)(), void (C::*Set(T)), const string& name) :
      get_func_(Stringify<C>::get_property<T, Get>),
      set_func_(Stringify<C>::set_property<T, Set>),
      name_ (name) { C::add_property(this) }

private:
   typedef static string (Stringify<C>::*get_func_type)(const C&) const;
   typedef static void (Stringify<C>::*set_func_type)(const C& c, string v);

   get_func_type get_func_;
   set_func_type set_func_;
   string name_;

};

template
<
   class C // the class that has the properties
>
class propertied {
   static void add_property(property<C>* p);
   static list<string> list_property_names();
   static list<string> list_property_values();

   static string get_property_value(const string& name);
   static void set_property_value(const string& name, const string& value);
   static void set_property_name(const string& prev_name, const string&
new_name);

   static list<property<C> > list_properties();
   static property<C> get_property(const string& name);
   static void set_property(const string& name, property<C> p);

private:
   static list<property<C> > properties;
};

#define PROPERTY(c, n) static property<c> property_##n(&c::get_##n,
&c::set_##n, #n)


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