uvar.cc

Go to the documentation of this file.
00001 
00021 #include "urbi/uobject.hh"
00022 
00023 #include "urbi/uexternal.hh"
00024 #include "urbi/usyncclient.hh"
00025 
00026 namespace urbi
00027 {
00028   class UVardata
00029   {
00030   public:
00031     UVardata()
00032     {
00033     }
00034     ~UVardata()
00035     {
00036     }
00037   };
00038 
00040   void
00041   UVar::__init()
00042   {
00043     varmap[name].push_back(this);
00044     vardata = 0; // unused. For internal softdevices only
00045     this->owned = false;
00046     assert (dummyUObject);
00047 
00048     createUCallback(dummyUObject->__name,
00049                     "var",
00050                     dummyUObject, &UObject::voidfun, name, monitormap);
00051   }
00052 
00054   ufloat&
00055   UVar::out()
00056   {
00057     return value.val;
00058   }
00059 
00061   ufloat&
00062   UVar::in()
00063   {
00064     return value.val;
00065   }
00066 
00067 
00068   void
00069   UVar::setProp(UProperty p, const UValue& v)
00070   {
00071     URBI(()) << name << "->" << urbi::name(p) << "=" << v << ";";
00072   }
00073 
00074   void
00075   UVar::setProp(UProperty p, const char* v)
00076   {
00077     URBI(()) << name << "->" << urbi::name(p) << "=" << v << ";";
00078   }
00079 
00080   void
00081   UVar::setProp(UProperty p, double v)
00082   {
00083     // FIXME: This is not the right way to do it.  Generalize
00084     // conversions between enums and strings.
00085     int i = static_cast<int>(v);
00086     if (p == PROP_BLEND && is_blendtype(i))
00087       URBI(()) << name << "->"<< urbi::name(p) << "="
00088                << urbi::name(static_cast<UBlendType>(i)) << ";";
00089     else
00090       URBI(()) << name << "->"<< urbi::name(p) << "="
00091                << v << ";";
00092   }
00093 
00094   UValue
00095   UVar::getProp(UProperty p)
00096   {
00097     UMessage* m=
00098       ((USyncClient&)URBI(())).syncGet("%s->%s",
00099                                        name.c_str(), urbi::name (p));
00100     UValue v = *m->value;
00101     delete m;
00102     return v;
00103   }
00104 
00105   /*
00106     UBlendType
00107     UVar::blend()
00108     {
00109     echo("Properties not implemented in remote mode yet.\n");
00110     return UNORMAL;
00111     }
00112   */
00113 
00115   UVar::~UVar()
00116   {
00117     UVarTable::iterator varmapfind = varmap.find(name);
00118 
00119     if (varmapfind != varmap.end())
00120       {
00121         for (std::list<UVar*>::iterator it = varmapfind->second.begin();
00122              it != varmapfind->second.end();)
00123           if (*it == this)
00124             it=varmapfind->second.erase(it);
00125           else
00126             ++it;
00127 
00128         if (varmapfind->second.empty())
00129           varmap.erase(varmapfind);
00130       }
00131   }
00132 
00134   void
00135   UVar::operator = (ufloat n)
00136   {
00137     URBI(()) << name << "=" << n << ";";
00138   }
00139 
00141   void
00142   UVar::operator= (const std::string& s)
00143   {
00144     URBI(()) << name << "=\"" << s << "\";";
00145   }
00146 
00148   void
00149   UVar::operator = (const UBinary& b)
00150   {
00151     getDefaultClient()->sendBin(b.common.data, b.common.size,
00152                                 "%s=BIN %d %s;",
00153                                 name.c_str(), b.common.size,
00154                                 b.getMessage().c_str());
00155   }
00156 
00157   void
00158   UVar::operator= (const UImage& i)
00159   {
00160     //we don't use UBinary Image ctor because it copies data
00161     UBinary b;
00162     b.type = BINARY_IMAGE;
00163     b.image = i;
00164     (*this) = b;
00165     b.common.data = 0; //required, dtor frees data
00166   }
00167 
00168   void
00169   UVar::operator= (const USound& i)
00170   {
00171     //we don't use UBinary Image ctor because it copies data
00172     UBinary b;
00173     b.type = BINARY_SOUND;
00174     b.sound = i;
00175     (*this) = b;
00176     b.common.data = 0; //required, dtor frees data
00177   }
00178 
00179   void
00180   UVar::operator= (const UList& l)
00181   {
00182     URBI(()) << name << "=";
00183     UValue v;
00184     v.type = DATA_LIST;
00185     v.list = &const_cast<UList&>(l);
00186     URBI(()) << v << ";";
00187     v.type = DATA_VOID;
00188     v.list = 0;
00189   }
00190 
00191 
00192   UVar::operator int ()
00193   {
00194     return (int) value;
00195   };
00196 
00197   UVar::operator ufloat ()
00198   {
00199     return (ufloat) value;
00200   };
00201 
00202 
00203   UVar::operator std::string ()
00204   {
00205     return (std::string) value;
00206   };
00207 
00208 
00209   UVar::operator UBinary()
00210   {
00211     return value;
00212   };
00213 
00214   UVar::operator UBinary*()
00215   {
00216     return new UBinary(value.operator UBinary());
00217   };
00218 
00219   UVar::operator UImage()
00220   {
00221     return (UImage) value;
00222   };
00223 
00224   UVar::operator USound()
00225   {
00226     return (USound) value;
00227   };
00228 
00229   UVar::operator UList()
00230   {
00231     return (UList) value;
00232   };
00233 
00235   void
00236   UVar::__update(UValue& v)
00237   {
00238 # ifdef LIBURBIDEBUG
00239     std::cout << "  Variable " << name << " updated to : ";
00240 
00241     switch (v.type)
00242       {
00243       case DATA_DOUBLE:
00244         std::cout << (double)v << std::endl;
00245         break;
00246       case DATA_STRING:
00247         std::cout << (std::string)v << std::endl;
00248         break;
00249       case DATA_BINARY:
00250       case DATA_LIST:
00251       case DATA_OBJECT:
00252       case DATA_VOID:
00253         break;
00254       }
00255 # endif
00256     value = v;
00257   }
00258 
00260   void
00261   UVar::setOwned()
00262   {
00263     owned = true;
00264   }
00265 
00266   void
00267   UVar::requestValue()
00268   {
00269     //build a getvalue message  that will be parsed and returned by the server
00270     URBI(()) << externalModuleTag << ':'
00271              <<'[' << UEM_ASSIGNVALUE << ","
00272              << '"' << name << '"' << ',' << name << "];";
00273   }
00274 
00275   void
00276   UVar::syncValue ()
00277   {
00278     USyncClient&        client = (USyncClient&) URBI(());
00279     UMessage*           m;
00280     char                tag[32];
00281 
00282     client.makeUniqueTag(tag);
00283     client.send ("if (isdef (%s) && !isvoid (%s)) { %s:%s } else { %s:1/0 };",
00284                  name.c_str (), name.c_str (), tag, name.c_str (), tag);
00285     m = client.waitForTag(tag);
00286     if (m->type == MESSAGE_DATA)
00287       __update (*m->value);
00288   }
00289 
00290 } //namespace urbi

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