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