Urbi SDK Remote for C++  2.7.5
uvar.hxx
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007, 2008, 2009, 2010, Gostai S.A.S.
00003  *
00004  * This software is provided "as is" without warranty of any kind,
00005  * either expressed or implied, including but not limited to the
00006  * implied warranties of fitness for a particular purpose.
00007  *
00008  * See the LICENSE file for more information.
00009  */
00010 
00012 #ifndef URBI_UVAR_HXX
00013 # define URBI_UVAR_HXX
00014 
00015 # include <stdexcept>
00016 # include <libport/cassert>
00017 # include <urbi/uvar.hh>
00018 
00019 namespace urbi
00020 {
00021   inline
00022   UVar::operator bool() const
00023   {
00024     return static_cast<int>(*this) != 0;
00025   }
00026 
00027   inline
00028   UVar::UVar()
00029     : owned(false)
00030     , VAR_PROP_INIT
00031     , impl_(0)
00032     , vardata(0)
00033     , name("noname")
00034     , temp(false)
00035     , local(false)
00036   {
00037   }
00038 
00039   inline
00040   void
00041   UVar::init(const std::string& objname, const std::string& varname,
00042              impl::UContextImpl* ctx)
00043   {
00044     init(objname + '.' + varname, ctx);
00045   }
00046 
00047 
00048   inline
00049   void
00050   UVar::setOwned()
00051   {
00052     check();
00053     impl_->setOwned();
00054   }
00055 
00056   inline
00057   UDataType
00058   UVar::type() const
00059   {
00060     check();
00061     return impl_->type();
00062   }
00063 
00064   inline
00065   void
00066   UVar::syncValue()
00067   {
00068     check();
00069     impl_->sync();
00070   }
00071 
00072   inline const UValue&
00073   UVar::val() const
00074   {
00075     check();
00076     return impl_->get();
00077   }
00078 
00079   inline
00080   void
00081   UVar::keepSynchronized()
00082   {
00083     check();
00084     impl_->keepSynchronized();
00085   }
00086 
00087   inline
00088   UVar&
00089   UVar::operator=(const UValue& v)
00090   {
00091     check();
00092     impl_->set(v);
00093     return *this;
00094   }
00095 
00096 # define SET(Type)                         \
00097   inline UVar& UVar::operator=(Type tv)    \
00098   {                                        \
00099     /*no need to copy, impl will do it*/   \
00100     check();                              \
00101     UValue v(tv, false);                   \
00102     impl_->set(v);                         \
00103     return *this;                          \
00104   }
00105 
00106   SET(const UBinary&)
00107   SET(const UImage&)
00108   SET(const UList&)
00109   SET(const UDictionary&)
00110   SET(const USound&)
00111   SET(const std::string&)
00112   SET(ufloat)
00113 # undef SET
00114 
00115   template<typename T>
00116   UVar& UVar::operator = (const T& v)
00117   {
00118     check();
00119     UValue val;
00120     val, v;
00121     impl_->set(val);
00122     return *this;
00123   }
00124 
00125 # define GET(Type)                    \
00126   inline UVar::operator Type() const  \
00127   {                                   \
00128     check();                         \
00129     return impl_->get();              \
00130   }
00131 
00132   GET(UImage)
00133   GET(UList)
00134   GET(UDictionary)
00135   GET(USound)
00136   GET(const UBinary&)
00137   GET(int)
00138   GET(std::string)
00139   GET(ufloat)
00140 # undef GET
00141 
00142   inline UVar::operator UBinary*() const
00143   {
00144     return new UBinary(static_cast<const UBinary&>(*this));
00145   }
00146 
00147   inline void UVar::setProp(UProperty prop, const UValue &v)
00148   {
00149     check();
00150     impl_->setProp(prop, v);
00151   }
00152 
00153   inline void UVar::setProp(UProperty prop, const char* v)
00154   {
00155     check();
00156     UValue tv(v);
00157     impl_->setProp(prop, tv);
00158   }
00159 
00160   inline void UVar::setProp(UProperty prop, ufloat v)
00161   {
00162     check();
00163     UValue tv(v);
00164     impl_->setProp(prop, tv);
00165   }
00166 
00167   inline bool
00168   UVar::setBypass(bool enable)
00169   {
00170     check();
00171     return impl_->setBypass(enable);
00172   }
00173 
00174   inline UValue
00175   UVar::getProp(UProperty prop)
00176   {
00177     check();
00178     return impl_->getProp(prop);
00179   }
00180 
00181   inline void
00182   UVar::unnotify()
00183   {
00184     check();
00185     impl_->unnotify();
00186   }
00187 
00188   inline void
00189   UVar::check() const
00190   {
00191     if (!impl_)
00192       throw std::runtime_error("invalid use of unbound UVar");
00193   }
00194 
00195   inline void
00196   UVar::useRTP(bool state)
00197   {
00198     rtp = state?RTP_YES:RTP_NO;
00199     impl_->useRTP(state);
00200   }
00201 
00202   template<typename T>
00203   T UVar::as() const
00204   {
00205     return uvalue_cast<T>(const_cast<UValue&>(val()));
00206   }
00207 
00208   template<typename T>
00209   T UVar::as(T*) const
00210   {
00211      return uvalue_cast<T>(const_cast<UValue&>(val()));
00212   }
00213 
00214   template<typename T>
00215   T& UVar::fill(T& v) const
00216   {
00217     v = uvalue_cast<T>(const_cast<UValue&>(val()));
00218     return v;
00219   }
00220 
00221   template<typename T>
00222   bool
00223   UVar::operator == (const T& v) const
00224   {
00225     return uvalue_cast<T>(const_cast<UValue&>(val())) == v;
00226   }
00227 } // end namespace urbi
00228 
00229 #endif // ! URBI_UVAR_HXX