value.C

00001 /*
00002  * This file is part of the "Archon" framework.
00003  * (http://files3d.sourceforge.net)
00004  *
00005  * Copyright © 2002 by Kristian Spangsege and Brian Kristiansen.
00006  *
00007  * Permission to use, copy, modify, and distribute this software and
00008  * its documentation under the terms of the GNU General Public License is
00009  * hereby granted. No representations are made about the suitability of
00010  * this software for any purpose. It is provided "as is" without express
00011  * or implied warranty. See the GNU General Public License
00012  * (http://www.gnu.org/copyleft/gpl.html) for more details.
00013  *
00014  * The characters in this file are ISO8859-1 encoded.
00015  *
00016  * The documentation in this file is in "Doxygen" style
00017  * (http://www.doxygen.org).
00018  */
00019 
00020 #include <archon/x3d/proxy/value.H>
00021 #include <archon/x3d/proxy/node.H>
00022 
00023 namespace Archon
00024 {
00025   namespace X3D
00026   {
00027     namespace Proxy
00028     {
00029       template<typename T> struct CorbaVal {};
00030 
00031       template<> struct CorbaVal<bool>
00032       {
00033         typedef CORBA::Boolean Type;
00034         typedef x3d::sai::booleanarray Array;
00035         typedef x3d::sai::booleanarray_var ArrayVar;
00036         static bool im(Porter *, Type v) { return bool(v); }
00037         static Type ex(Porter *, bool v) { return Type(v); }
00038         static bool fromAny(const CORBA::Any &a, Type &v)
00039         { return a >>= CORBA::Any::to_boolean(v); }
00040         static void toAny(CORBA::Any &a, Type v)
00041         { a <<= CORBA::Any::from_boolean(v); }
00042       };
00043 
00044       template<> struct CorbaVal<int>
00045       {
00046         typedef CORBA::Long Type;
00047         typedef x3d::sai::longarray Array;
00048         typedef x3d::sai::longarray_var ArrayVar;
00049         typedef x3d::sai::longdoublearray DoubleArray;
00050         typedef x3d::sai::longdoublearray_var DoubleArrayVar;
00051         static int im(Porter *, Type v) { return int(v); }
00052         static Type ex(Porter *, int v) { return Type(v); }
00053         static bool fromAny(const CORBA::Any &a, Type &v) { return a >>= v; }
00054         static void toAny(CORBA::Any &a, Type v) { a <<= v; }
00055       };
00056 
00057       template<> struct CorbaVal<long>
00058       {
00059         typedef CORBA::Long Type;
00060         typedef x3d::sai::longarray Array;
00061         typedef x3d::sai::longarray_var ArrayVar;
00062         typedef x3d::sai::longdoublearray DoubleArray;
00063         typedef x3d::sai::longdoublearray_var DoubleArrayVar;
00064         static long im(Porter *, Type v) { return long(v); }
00065         static Type ex(Porter *, long v) { return Type(v); }
00066         static bool fromAny(const CORBA::Any &a, Type &v) { return a >>= v; }
00067         static void toAny(CORBA::Any &a, Type v) { a <<= v; }
00068       };
00069 
00070       template<> struct CorbaVal<float>
00071       {
00072         typedef CORBA::Float Type;
00073         typedef x3d::sai::floatarray Array;
00074         typedef x3d::sai::floatarray_var ArrayVar;
00075         typedef x3d::sai::floatdoublearray DoubleArray;
00076         typedef x3d::sai::floatdoublearray_var DoubleArrayVar;
00077         static float im(Porter *, Type v) { return float(v); }
00078         static Type ex(Porter *, float v) { return Type(v); }
00079         static bool fromAny(const CORBA::Any &a, Type &v) { return a >>= v; }
00080         static void toAny(CORBA::Any &a, Type v) { a <<= v; }
00081       };
00082 
00083       template<> struct CorbaVal<double>
00084       {
00085         typedef CORBA::Double Type;
00086         typedef x3d::sai::doublearray Array;
00087         typedef x3d::sai::doublearray_var ArrayVar;
00088         typedef x3d::sai::doubledoublearray DoubleArray;
00089         typedef x3d::sai::doubledoublearray_var DoubleArrayVar;
00090         static double im(Porter *, Type v) { return double(v); }
00091         static Type ex(Porter *, double v) { return Type(v); }
00092         static bool fromAny(const CORBA::Any &a, Type &v) { return a >>= v; }
00093         static void toAny(CORBA::Any &a, Type v) { a <<= v; }
00094       };
00095 
00096       template<> struct CorbaVal<string>
00097       {
00098         typedef const char *Type;
00099         typedef x3d::sai::stringarray Array;
00100         typedef x3d::sai::stringarray_var ArrayVar;
00101         static string im(Porter *, Type v) { return string(v); }
00102         static Type ex(Porter *, string v)
00103         { return CORBA::string_dup(v.c_str()); }
00104         static bool fromAny(const CORBA::Any &a, Type &v) { return a >>= v; }
00105         static void toAny(CORBA::Any &a, Type v) { a <<= v; }
00106       };
00107 
00108       template<> struct CorbaVal<Ref<NodeBase> >
00109       {
00110         typedef CORBA::ULong Type;
00111         typedef x3d::sai::ulongarray Array;
00112         typedef x3d::sai::ulongarray_var ArrayVar;
00113         static Ref<NodeBase> im(Porter *p, Type v)
00114         { return p->id2node(static_cast<unsigned long>(v)); }
00115         static Type ex(Porter *p, Ref<NodeBase> v)
00116         { return Type(p->node2id(v)); }
00117         static bool fromAny(const CORBA::Any &a, Type &v) { return a >>= v; }
00118         static void toAny(CORBA::Any &a, Type v) { a <<= v; }
00119       };
00120 
00121 
00122 
00123       template<typename F>
00124       void Porter::imScalar(const x3d::sai::Value &s, void *t)
00125       {
00126         typedef typename F::Type T;
00127         typedef CorbaVal<T> C;
00128         typename C::Type w;
00129         if(!C::fromAny(s.val, w)) ARCHON_THROW(TypeMismatchException);
00130         T &v = *reinterpret_cast<T *>(t);
00131         v = C::im(this, w);
00132       }
00133 
00134       template<typename F>
00135       void Porter::imTuple(const x3d::sai::Value &s, void *t)
00136       {
00137         typedef typename F::Type T;
00138         typedef typename F::ElemType E;
00139         typedef CorbaVal<E> C;
00140         const typename C::Array *w;
00141         if(!(s.val >>= w)) ARCHON_THROW(TypeMismatchException);
00142         if(w->length() != F::size) ARCHON_THROW(TypeMismatchException);
00143         T &v = *reinterpret_cast<T *>(t);
00144         for(unsigned i=0; i<F::size; ++i) v[i] = C::im(this, (*w)[i]);
00145       }
00146 
00147       template<typename F>
00148       void Porter::imScalarSeq(const x3d::sai::Value &s, void *t)
00149       {
00150         typedef typename F::Type T;
00151         typedef typename F::Single::Type E;
00152         typedef CorbaVal<E> C;
00153         const typename C::Array *w;
00154         if(!(s.val >>= w)) ARCHON_THROW(TypeMismatchException);
00155         T &v = *reinterpret_cast<T *>(t);
00156         v.resize(w->length());
00157         for(unsigned i=0; i<w->length(); ++i) v[i] = C::im(this, (*w)[i]);
00158       }
00159 
00160       template<typename F>
00161       void Porter::imTupleSeq(const x3d::sai::Value &s, void *t)
00162       {
00163         typedef typename F::Type T;
00164         typedef typename F::Single S;
00165         typedef typename S::ElemType E;
00166         typedef CorbaVal<E> C;
00167         const typename C::DoubleArray *w;
00168         if(!(s.val >>= w)) ARCHON_THROW(TypeMismatchException);
00169         T &v = *reinterpret_cast<T *>(t);
00170         v.resize(w->length());
00171         for(unsigned i=0; i<w->length(); ++i)
00172         {
00173           typename S::Type &u = v[i];
00174           const typename C::Array &t = (*w)[i];
00175           if(t.length() != S::size) ARCHON_THROW(TypeMismatchException);
00176           for(unsigned j=0; j<S::size; ++j) u[j] = C::im(this, t[j]);
00177         }
00178       }
00179 
00183       void Porter::imSFImage(const x3d::sai::Value &s, void *t)
00184       {
00185         ARCHON_THROW1(InternalException, "imSFImage not yet implemented");
00186       }
00187 
00191       void Porter::imMFImage(const x3d::sai::Value &s, void *t)
00192       {
00193         ARCHON_THROW1(InternalException, "imMFImage not yet implemented");
00194       }
00195 
00196       void Porter::imSFRotation(const x3d::sai::Value &s, void *t)
00197       {
00198         typedef SFRotation F;
00199         typedef F::Type T;
00200         typedef F::ElemType E;
00201         typedef CorbaVal<E> C;
00202         const C::Array *w;
00203         if(!(s.val >>= w)) ARCHON_THROW(TypeMismatchException);
00204         if(w->length() != F::size) ARCHON_THROW(TypeMismatchException);
00205         T &v = *reinterpret_cast<T *>(t);
00206         v.axis[0] = C::im(this, (*w)[0]);
00207         v.axis[1] = C::im(this, (*w)[1]);
00208         v.axis[2] = C::im(this, (*w)[2]);
00209         v.angle = C::im(this, (*w)[3]);
00210       }
00211 
00212       void Porter::imMFRotation(const x3d::sai::Value &s, void *t)
00213       {
00214         typedef MFRotation F;
00215         typedef F::Type T;
00216         typedef F::Single S;
00217         typedef S::ElemType E;
00218         typedef CorbaVal<E> C;
00219         const C::DoubleArray *w;
00220         if(!(s.val >>= w)) ARCHON_THROW(TypeMismatchException);
00221         T &v = *reinterpret_cast<T *>(t);
00222         v.resize(w->length());
00223         for(unsigned i=0; i<w->length(); ++i)
00224         {
00225           S::Type &u = v[i];
00226           const C::Array &t = (*w)[i];
00227           if(t.length() != S::size) ARCHON_THROW(TypeMismatchException);
00228           u.axis[0] = C::im(this, t[0]);
00229           u.axis[1] = C::im(this, t[1]);
00230           u.axis[2] = C::im(this, t[2]);
00231           u.angle = C::im(this, t[3]);
00232         }
00233       }
00234 
00235       void Porter::imSFTime(const x3d::sai::Value &s, void *t)
00236       {
00237         typedef SFTime F;
00238         typedef F::Type T;
00239         typedef F::ElemType E;
00240         typedef CorbaVal<E> C;
00241         const C::Array *w;
00242         if(!(s.val >>= w)) ARCHON_THROW(TypeMismatchException);
00243         if(w->length() != F::size) ARCHON_THROW(TypeMismatchException);
00244         T &v = *reinterpret_cast<T *>(t);
00245         v.set(C::im(this, (*w)[0]), C::im(this, (*w)[1]));
00246       }
00247 
00248       void Porter::imMFTime(const x3d::sai::Value &s, void *t)
00249       {
00250         typedef MFTime F;
00251         typedef F::Type T;
00252         typedef F::Single S;
00253         typedef S::ElemType E;
00254         typedef CorbaVal<E> C;
00255         const C::DoubleArray *w;
00256         if(!(s.val >>= w)) ARCHON_THROW(TypeMismatchException);
00257         T &v = *reinterpret_cast<T *>(t);
00258         v.resize(w->length());
00259         for(unsigned i=0; i<w->length(); ++i)
00260         {
00261           const C::Array &t = (*w)[i];
00262           if(t.length() != S::size) ARCHON_THROW(TypeMismatchException);
00263           v[i].set(C::im(this, t[0]), C::im(this, t[1]));
00264         }
00265       }
00266 
00267 
00268 
00269 
00270       template<typename F>
00271       void Porter::exScalar(const void *s, x3d::sai::Value &t)
00272       {
00273         typedef typename F::Type T;
00274         typedef CorbaVal<T> C;
00275         const T &v = *reinterpret_cast<const T *>(s);
00276         C::toAny(t.val, C::ex(this, v));
00277       }
00278 
00279       template<typename F>
00280       void Porter::exTuple(const void *s, x3d::sai::Value &t)
00281       {
00282         typedef typename F::Type T;
00283         typedef typename F::ElemType E;
00284         typedef CorbaVal<E> C;
00285         const T &v = *reinterpret_cast<const T *>(s);
00286         typename C::ArrayVar w(new typename C::Array(F::size));
00287         w->length(F::size);
00288         for(unsigned i=0; i<F::size; ++i) w[i] = C::ex(this, v[i]);
00289         t.val <<= w._retn();
00290       }
00291 
00292       template<typename F>
00293       void Porter::exScalarSeq(const void *s, x3d::sai::Value &t)
00294       {
00295         typedef typename F::Type T;
00296         typedef typename F::Single::Type E;
00297         typedef CorbaVal<E> C;
00298         const T &v = *reinterpret_cast<const T *>(s);
00299         typename C::ArrayVar w(new typename C::Array(v.size()));
00300         w->length(v.size());
00301         for(unsigned i=0; i<v.size(); ++i) w[i] = C::ex(this, v[i]);
00302         t.val <<= w._retn();
00303       }
00304 
00305       template<typename F>
00306       void Porter::exTupleSeq(const void *s, x3d::sai::Value &t)
00307       {
00308         typedef typename F::Type T;
00309         typedef typename F::Single S;
00310         typedef typename S::ElemType E;
00311         typedef CorbaVal<E> C;
00312         const T &v = *reinterpret_cast<const T *>(s);
00313         typename C::DoubleArrayVar
00314           w(new typename C::DoubleArray(v.size()));
00315         w->length(v.size());
00316         for(unsigned i=0; i<v.size(); ++i)
00317         {
00318           typename C::Array &u = w[i];
00319           u.length(S::size);
00320           const typename S::Type &x = v[i];
00321           for(unsigned j=0; j<S::size; ++j) u[j] = C::ex(this, x[j]);
00322         }
00323         t.val <<= w._retn();
00324       }
00325 
00329       void Porter::exSFImage(const void *s, x3d::sai::Value &t)
00330       {
00331         ARCHON_THROW1(InternalException, "exSFImage not yet implemented");
00332       }
00333 
00337       void Porter::exMFImage(const void *s, x3d::sai::Value &t)
00338       {
00339         ARCHON_THROW1(InternalException, "exMFImage not yet implemented");
00340       }
00341 
00342       void Porter::exSFRotation(const void *s, x3d::sai::Value &t)
00343       {
00344         typedef SFRotation F;
00345         typedef F::Type T;
00346         typedef F::ElemType E;
00347         typedef CorbaVal<E> C;
00348         const T &v = *reinterpret_cast<const T *>(s);
00349         C::ArrayVar w(new C::Array(F::size));
00350         w->length(F::size);
00351         w[0] = C::ex(this, v.axis[0]);
00352         w[1] = C::ex(this, v.axis[1]);
00353         w[2] = C::ex(this, v.axis[2]);
00354         w[3] = C::ex(this, v.angle);
00355         t.val <<= w._retn();
00356       }
00357 
00358       void Porter::exMFRotation(const void *s, x3d::sai::Value &t)
00359       {
00360         typedef MFRotation F;
00361         typedef F::Type T;
00362         typedef F::Single S;
00363         typedef S::ElemType E;
00364         typedef CorbaVal<E> C;
00365         const T &v = *reinterpret_cast<const T *>(s);
00366         C::DoubleArrayVar w(new C::DoubleArray(v.size()));
00367         w->length(v.size());
00368         for(unsigned i=0; i<v.size(); ++i)
00369         {
00370           C::Array &u = w[i];
00371           u.length(S::size);
00372           const S::Type &x = v[i];
00373           u[0] = C::ex(this, x.axis[0]);
00374           u[1] = C::ex(this, x.axis[1]);
00375           u[2] = C::ex(this, x.axis[2]);
00376           u[3] = C::ex(this, x.angle);
00377         }
00378         t.val <<= w._retn();
00379       }
00380 
00381       void Porter::exSFTime(const void *s, x3d::sai::Value &t)
00382       {
00383         typedef SFTime F;
00384         typedef F::Type T;
00385         typedef F::ElemType E;
00386         typedef CorbaVal<E> C;
00387         const T &v = *reinterpret_cast<const T *>(s);
00388         C::ArrayVar w(new C::Array(F::size));
00389         w->length(F::size);
00390         long sec;
00391         long nsec;
00392         v.get(sec, nsec);
00393         w[0] = C::ex(this, sec);
00394         w[1] = C::ex(this, nsec);
00395         t.val <<= w._retn();
00396       }
00397 
00398       void Porter::exMFTime(const void *s, x3d::sai::Value &t)
00399       {
00400         typedef MFTime F;
00401         typedef F::Type T;
00402         typedef F::Single S;
00403         typedef S::ElemType E;
00404         typedef CorbaVal<E> C;
00405         const T &v = *reinterpret_cast<const T *>(s);
00406         C::DoubleArrayVar w(new C::DoubleArray(v.size()));
00407         w->length(v.size());
00408         for(unsigned i=0; i<v.size(); ++i)
00409         {
00410           C::Array &u = w[i];
00411           u.length(S::size);
00412           long sec;
00413           long nsec;
00414           v[i].get(sec, nsec);
00415           u[0] = C::ex(this, sec);
00416           u[1] = C::ex(this, nsec);
00417         }
00418         t.val <<= w._retn();
00419       }
00420 
00421       Porter::Porter()
00422       {
00423         importers.resize(fieldTypeIndexRoof);
00424         exporters.resize(fieldTypeIndexRoof);
00425 
00426         exporters[SFBool::index] = &Porter::exScalar<SFBool>;
00427         importers[SFBool::index] = &Porter::imScalar<SFBool>;
00428         exporters[MFBool::index] = &Porter::exScalarSeq<MFBool>;
00429         importers[MFBool::index] = &Porter::imScalarSeq<MFBool>;
00430 
00431         exporters[SFColor::index] = &Porter::exTuple<SFColor>;
00432         importers[SFColor::index] = &Porter::imTuple<SFColor>;
00433         exporters[MFColor::index] = &Porter::exTupleSeq<MFColor>;
00434         importers[MFColor::index] = &Porter::imTupleSeq<MFColor>;
00435 
00436         exporters[SFColorRGBA::index] = &Porter::exTuple<SFColorRGBA>;
00437         importers[SFColorRGBA::index] = &Porter::imTuple<SFColorRGBA>;
00438         exporters[MFColorRGBA::index] = &Porter::exTupleSeq<MFColorRGBA>;
00439         importers[MFColorRGBA::index] = &Porter::imTupleSeq<MFColorRGBA>;
00440 
00441         exporters[SFDouble::index] = &Porter::exScalar<SFDouble>;
00442         importers[SFDouble::index] = &Porter::imScalar<SFDouble>;
00443         exporters[MFDouble::index] = &Porter::exScalarSeq<MFDouble>;
00444         importers[MFDouble::index] = &Porter::imScalarSeq<MFDouble>;
00445 
00446         exporters[SFFloat::index] = &Porter::exScalar<SFFloat>;
00447         importers[SFFloat::index] = &Porter::imScalar<SFFloat>;
00448         exporters[MFFloat::index] = &Porter::exScalarSeq<MFFloat>;
00449         importers[MFFloat::index] = &Porter::imScalarSeq<MFFloat>;
00450 
00451         exporters[SFImage::index] = &Porter::exSFImage;
00452         importers[SFImage::index] = &Porter::imSFImage;
00453         exporters[MFImage::index] = &Porter::exMFImage;
00454         importers[MFImage::index] = &Porter::imMFImage;
00455 
00456         exporters[SFInt32::index] = &Porter::exScalar<SFInt32>;
00457         importers[SFInt32::index] = &Porter::imScalar<SFInt32>;
00458         exporters[MFInt32::index] = &Porter::exScalarSeq<MFInt32>;
00459         importers[MFInt32::index] = &Porter::imScalarSeq<MFInt32>;
00460 
00461         exporters[SFNode<NodeBase>::index] =
00462           &Porter::exScalar<SFNode<NodeBase> >;
00463         importers[SFNode<NodeBase>::index] =
00464           &Porter::imScalar<SFNode<NodeBase> >;
00465         exporters[MFNode<NodeBase>::index] =
00466           &Porter::exScalarSeq<MFNode<NodeBase> >;
00467         importers[MFNode<NodeBase>::index] =
00468           &Porter::imScalarSeq<MFNode<NodeBase> >;
00469 
00470         exporters[SFRotation::index] = &Porter::exSFRotation;
00471         importers[SFRotation::index] = &Porter::imSFRotation;
00472         exporters[MFRotation::index] = &Porter::exMFRotation;
00473         importers[MFRotation::index] = &Porter::imMFRotation;
00474 
00475         exporters[SFString::index] = &Porter::exScalar<SFString>;
00476         importers[SFString::index] = &Porter::imScalar<SFString>;
00477         exporters[MFString::index] = &Porter::exScalarSeq<MFString>;
00478         importers[MFString::index] = &Porter::imScalarSeq<MFString>;
00479 
00480         exporters[SFTime::index] = &Porter::exSFTime;
00481         importers[SFTime::index] = &Porter::imSFTime;
00482         exporters[MFTime::index] = &Porter::exMFTime;
00483         importers[MFTime::index] = &Porter::imMFTime;
00484 
00485         exporters[SFVec2d::index] = &Porter::exTuple<SFVec2d>;
00486         importers[SFVec2d::index] = &Porter::imTuple<SFVec2d>;
00487         exporters[MFVec2d::index] = &Porter::exTupleSeq<MFVec2d>;
00488         importers[MFVec2d::index] = &Porter::imTupleSeq<MFVec2d>;
00489 
00490         exporters[SFVec2f::index] = &Porter::exTuple<SFVec2f>;
00491         importers[SFVec2f::index] = &Porter::imTuple<SFVec2f>;
00492         exporters[MFVec2f::index] = &Porter::exTupleSeq<MFVec2f>;
00493         importers[MFVec2f::index] = &Porter::imTupleSeq<MFVec2f>;
00494 
00495         exporters[SFVec3d::index] = &Porter::exTuple<SFVec3d>;
00496         importers[SFVec3d::index] = &Porter::imTuple<SFVec3d>;
00497         exporters[MFVec3d::index] = &Porter::exTupleSeq<MFVec3d>;
00498         importers[MFVec3d::index] = &Porter::imTupleSeq<MFVec3d>;
00499 
00500         exporters[SFVec3f::index] = &Porter::exTuple<SFVec3f>;
00501         importers[SFVec3f::index] = &Porter::imTuple<SFVec3f>;
00502         exporters[MFVec3f::index] = &Porter::exTupleSeq<MFVec3f>;
00503         importers[MFVec3f::index] = &Porter::imTupleSeq<MFVec3f>;
00504       }
00505     }
00506   }
00507 }

Generated on Sun Jul 30 22:55:48 2006 for Archon by  doxygen 1.4.4