uvar.hh

Go to the documentation of this file.
00001 
00002 
00003 // This file is part of UObject Component Architecture
00004 // Copyright (c) 2007 Gostai S.A.S.
00005 //
00006 // Permission to use, copy, modify, and redistribute this software for
00007 // non-commercial use is hereby granted.
00008 //
00009 // This software is provided "as is" without warranty of any kind,
00010 // either expressed or implied, including but not limited to the
00011 // implied warranties of fitness for a particular purpose.
00012 //
00013 // For more information, comments, bug reports: http://www.urbiforge.com
00014 
00015 #ifndef URBI_UVAR_HH
00016 # define URBI_UVAR_HH
00017 
00018 # include <string>
00019 
00021 # define PRIVATE(vartype, varname)              \
00022   private:                                      \
00023     vartype varname;                            \
00024   public:                                       \
00025     vartype get_ ##  varname ()                 \
00026     {                                           \
00027       return varname;                           \
00028     }                                           \
00029     void set_ ##  varname (vartype& v)          \
00030     {                                           \
00031       varname = v;                              \
00032     }                                           \
00033   private:
00034 
00035 namespace urbi
00036 {
00037 
00039   class UProp
00040   {
00041   public:
00042 
00043     void operator =(const UValue &v);
00044     void operator =(const double v);
00045     void operator =(const std::string & v);
00046 
00047     operator double();
00048     operator std::string();
00049     operator UValue();
00050 
00051     UProp(UVar &owner, UProperty name)
00052       :owner(owner), name(name)
00053     {}
00054 
00055   private:
00056     UVar & owner;
00057     UProperty name;
00058 
00059     //disable copy ctor and equal operator
00060     UProp & operator =(const UProp &b);
00061     UProp(const UProp &b);
00062   };
00063 
00064 
00065 
00066   //Helper macro to initialize UProps in UVar constructors
00067 #   define VAR_PROP_INIT                        \
00068   rangemin(*this, PROP_RANGEMIN),               \
00069   rangemax(*this, PROP_RANGEMAX),               \
00070   speedmin(*this, PROP_SPEEDMIN),               \
00071   speedmax(*this, PROP_SPEEDMAX),               \
00072   delta(*this, PROP_DELTA),                     \
00073   blend(*this, PROP_BLEND)
00074 
00075 
00080   class UVar
00081   {
00082   public:
00083     UVar()
00084       : owned (false),
00085         VAR_PROP_INIT,
00086         vardata (0), name ("noname")
00087     {}
00088     UVar(UVar&)
00089       : owned (false),
00090         VAR_PROP_INIT,
00091         vardata (0), name ("")
00092     {
00095     }
00096     UVar(const std::string&);
00097     UVar(const std::string&, const std::string&);
00098     UVar(UObject&, const std::string&);
00099     ~UVar();
00100 
00101     void init(const std::string&, const std::string&);
00102     void setOwned();
00103     void syncValue ();
00104 
00105     void operator = (ufloat);
00106     void operator = (const std::string&);
00107     void operator = (const UBinary &);  
00108     void operator = (const UImage &i);  
00109     void operator = (const USound &s);  
00110     void operator = (const UList &l);
00111     void operator = (const UValue &v);
00112     operator int ();
00113     operator bool ()  {return (int)(*this);}
00114     operator UBinary ();   
00115 
00116     operator UBinary *();
00120     operator UImage ();
00124     operator USound();
00125     operator ufloat ();
00126     operator std::string ();
00127     operator UList();
00128 
00131     void requestValue();
00132 
00133     //kernel operators
00134     ufloat& in();
00135     ufloat& out();
00136 
00137     bool owned; //< is the variable owned by the module?
00138 
00139     //Property accessors
00140 
00141     UProp rangemin;
00142     UProp rangemax;
00143     UProp speedmin;
00144     UProp speedmax;
00145     UProp delta;
00146     UProp blend;
00147 
00148     UValue getProp(UProperty prop);
00149     void setProp(UProperty prop, const UValue &v);
00150     void setProp(UProperty prop, double v);
00151     void setProp(UProperty prop, const char * v);
00152     void setProp(UProperty prop, const std::string& v)
00153     {
00154       setProp(prop, v.c_str());
00155     }
00156 
00157     // internal
00158     void __update(UValue&);
00159 
00160   private:
00162     UValue& val()
00163     {
00164       return value;
00165     }
00166 
00168     UVardata  *vardata;
00169     void __init();
00170 
00172     PRIVATE(std::string, name)
00174     PRIVATE(UValue, value)
00175 
00176     // Check that the invariant of this class are verified.
00177     bool invariant () const;
00178   };
00179 
00180   inline void UProp::operator =(const UValue& v)  {owner.setProp(name, v);}
00181   inline void UProp::operator =(const double v)  {owner.setProp(name, v);}
00182   inline void UProp::operator =(const std::string& v){owner.setProp(name, v);}
00183   inline UProp::operator double()  {return (double)owner.getProp(name);}
00184   inline UProp::operator std::string()  {return owner.getProp(name);}
00185   inline UProp::operator UValue()  {return owner.getProp(name);}
00186 
00187 } // end namespace urbi
00188 
00189 # undef PRIVATE
00190 
00191 #endif // ! URBI_UVAR_HH

Generated on Tue Apr 10 17:45:45 2007 for URBISDK by  doxygen 1.5.1