[Orxonox-commit 5534] r10197 - in code/trunk: src/libraries/util src/orxonox/gamestates test/util

landauf at orxonox.net landauf at orxonox.net
Sat Jan 17 18:43:36 CET 2015


Author: landauf
Date: 2015-01-17 18:43:35 +0100 (Sat, 17 Jan 2015)
New Revision: 10197

Modified:
   code/trunk/src/libraries/util/MultiType.h
   code/trunk/src/orxonox/gamestates/GSServer.cc
   code/trunk/test/util/MultiTypeTest.cc
Log:
avoid implicit conversion in MultiType.get<T>()

Modified: code/trunk/src/libraries/util/MultiType.h
===================================================================
--- code/trunk/src/libraries/util/MultiType.h	2015-01-13 21:55:12 UTC (rev 10196)
+++ code/trunk/src/libraries/util/MultiType.h	2015-01-17 17:43:35 UTC (rev 10197)
@@ -347,37 +347,15 @@
             /// Checks if the MT contains no value.
             inline bool null() const { return !this->value_; }
 
-            inline operator char()                 const { return (this->value_ ? this->value_->get<char>()                 : 0); }
-            inline operator unsigned char()        const { return (this->value_ ? this->value_->get<unsigned char>()        : 0); }
-            inline operator short()                const { return (this->value_ ? this->value_->get<short>()                : 0); }
-            inline operator unsigned short()       const { return (this->value_ ? this->value_->get<unsigned short>()       : 0); }
-            inline operator int()                  const { return (this->value_ ? this->value_->get<int>()                  : 0); }
-            inline operator unsigned int()         const { return (this->value_ ? this->value_->get<unsigned int>()         : 0); }
-            inline operator long()                 const { return (this->value_ ? this->value_->get<long>()                 : 0); }
-            inline operator unsigned long()        const { return (this->value_ ? this->value_->get<unsigned long>()        : 0); }
-            inline operator long long()            const { return (this->value_ ? this->value_->get<long long>()            : 0); }
-            inline operator unsigned long long()   const { return (this->value_ ? this->value_->get<unsigned long long>()   : 0); }
-            inline operator float()                const { return (this->value_ ? this->value_->get<float>()                : 0); }
-            inline operator double()               const { return (this->value_ ? this->value_->get<double>()               : 0); }
-            inline operator long double()          const { return (this->value_ ? this->value_->get<long double>()          : 0); }
-            inline operator bool()                 const { return (this->value_ ? this->value_->get<bool>()                 : 0); }
-            inline operator void*()                const { return (this->value_ ? this->value_->get<void*>()                : 0); }
-            inline operator std::string()          const { return (this->value_ ? this->value_->get<std::string>()          : NilValue<std::string>()); }
-            inline operator orxonox::Vector2()     const { return (this->value_ ? this->value_->get<orxonox::Vector2>()     : NilValue<orxonox::Vector2>()); }
-            inline operator orxonox::Vector3()     const { return (this->value_ ? this->value_->get<orxonox::Vector3>()     : NilValue<orxonox::Vector3>()); }
-            inline operator orxonox::Vector4()     const { return (this->value_ ? this->value_->get<orxonox::Vector4>()     : NilValue<orxonox::Vector4>()); }
-            inline operator orxonox::ColourValue() const { return (this->value_ ? this->value_->get<orxonox::ColourValue>() : NilValue<orxonox::ColourValue>()); }
-            inline operator orxonox::Quaternion()  const { return (this->value_ ? this->value_->get<orxonox::Quaternion>()  : NilValue<orxonox::Quaternion>()); }
-            inline operator orxonox::Radian()      const { return (this->value_ ? this->value_->get<orxonox::Radian>()      : NilValue<orxonox::Radian>()); }
-            inline operator orxonox::Degree()      const { return (this->value_ ? this->value_->get<orxonox::Degree>()      : NilValue<orxonox::Degree>()); }
-            /// Returns the current value, converted to a T* pointer.
-            template <class T> operator T*() const { return (static_cast<T*>(this->operator void*())); }
+            /// Conversion operator for all types
+            template <class T> operator T()  const { return this->get<T>(); }
 
             /// Assigns the value to the given pointer. The value gets converted if the types don't match.
             template <typename T> inline bool getValue(T* value) const { if (this->value_) { return this->value_->getValue(value); } return false; }
 
-            /// Returns the current value, converted to the requested type.
-            template <typename T> inline T get() const { return *this; }
+            /// Returns the current value, converted to the requested type. This base implementation works only for pointers. All other supported types are
+            /// implemented in  specialized functions at the bottom of this file.
+            template <typename T> inline T get() const { return static_cast<T>(this->get<void*>()); }
 
 
             ///////////////////////////////
@@ -493,6 +471,30 @@
     template <> inline bool MultiType::isType<void>() const { return this->null(); }
     template <> inline bool MultiType::convert<void>() { this->reset(); return true; }
 
+    template <> inline char                 MultiType::get() const { return (this->value_ ? this->value_->get<char>()                 : 0); }
+    template <> inline unsigned char        MultiType::get() const { return (this->value_ ? this->value_->get<unsigned char>()        : 0); }
+    template <> inline short                MultiType::get() const { return (this->value_ ? this->value_->get<short>()                : 0); }
+    template <> inline unsigned short       MultiType::get() const { return (this->value_ ? this->value_->get<unsigned short>()       : 0); }
+    template <> inline int                  MultiType::get() const { return (this->value_ ? this->value_->get<int>()                  : 0); }
+    template <> inline unsigned int         MultiType::get() const { return (this->value_ ? this->value_->get<unsigned int>()         : 0); }
+    template <> inline long                 MultiType::get() const { return (this->value_ ? this->value_->get<long>()                 : 0); }
+    template <> inline unsigned long        MultiType::get() const { return (this->value_ ? this->value_->get<unsigned long>()        : 0); }
+    template <> inline long long            MultiType::get() const { return (this->value_ ? this->value_->get<long long>()            : 0); }
+    template <> inline unsigned long long   MultiType::get() const { return (this->value_ ? this->value_->get<unsigned long long>()   : 0); }
+    template <> inline float                MultiType::get() const { return (this->value_ ? this->value_->get<float>()                : 0); }
+    template <> inline double               MultiType::get() const { return (this->value_ ? this->value_->get<double>()               : 0); }
+    template <> inline long double          MultiType::get() const { return (this->value_ ? this->value_->get<long double>()          : 0); }
+    template <> inline bool                 MultiType::get() const { return (this->value_ ? this->value_->get<bool>()                 : 0); }
+    template <> inline void*                MultiType::get() const { return (this->value_ ? this->value_->get<void*>()                : 0); }
+    template <> inline std::string          MultiType::get() const { return (this->value_ ? this->value_->get<std::string>()          : NilValue<std::string>()); }
+    template <> inline orxonox::Vector2     MultiType::get() const { return (this->value_ ? this->value_->get<orxonox::Vector2>()     : NilValue<orxonox::Vector2>()); }
+    template <> inline orxonox::Vector3     MultiType::get() const { return (this->value_ ? this->value_->get<orxonox::Vector3>()     : NilValue<orxonox::Vector3>()); }
+    template <> inline orxonox::Vector4     MultiType::get() const { return (this->value_ ? this->value_->get<orxonox::Vector4>()     : NilValue<orxonox::Vector4>()); }
+    template <> inline orxonox::ColourValue MultiType::get() const { return (this->value_ ? this->value_->get<orxonox::ColourValue>() : NilValue<orxonox::ColourValue>()); }
+    template <> inline orxonox::Quaternion  MultiType::get() const { return (this->value_ ? this->value_->get<orxonox::Quaternion>()  : NilValue<orxonox::Quaternion>()); }
+    template <> inline orxonox::Radian      MultiType::get() const { return (this->value_ ? this->value_->get<orxonox::Radian>()      : NilValue<orxonox::Radian>()); }
+    template <> inline orxonox::Degree      MultiType::get() const { return (this->value_ ? this->value_->get<orxonox::Degree>()      : NilValue<orxonox::Degree>()); }
+
     template <> _UtilExport void MultiType::createNewValueContainer(const char& value);
     template <> _UtilExport void MultiType::createNewValueContainer(const unsigned char& value);
     template <> _UtilExport void MultiType::createNewValueContainer(const short& value);

Modified: code/trunk/src/orxonox/gamestates/GSServer.cc
===================================================================
--- code/trunk/src/orxonox/gamestates/GSServer.cc	2015-01-13 21:55:12 UTC (rev 10196)
+++ code/trunk/src/orxonox/gamestates/GSServer.cc	2015-01-17 17:43:35 UTC (rev 10197)
@@ -57,7 +57,7 @@
 
         GameMode::setIsServer(true);
 
-        this->server_ = new Server(CommandLineParser::getValue("port"));
+        this->server_ = new Server(CommandLineParser::getValue("port").get<int>());
         orxout(user_status) << "Loading scene in server mode" << endl;
 
         server_->open();

Modified: code/trunk/test/util/MultiTypeTest.cc
===================================================================
--- code/trunk/test/util/MultiTypeTest.cc	2015-01-13 21:55:12 UTC (rev 10196)
+++ code/trunk/test/util/MultiTypeTest.cc	2015-01-17 17:43:35 UTC (rev 10197)
@@ -59,6 +59,7 @@
     TEST(MultiType, ValueLongDouble)        { long double value = 0.123456789123456789;         MultiType mt(value);    EXPECT_TRUE(mt.isType<long double>());          EXPECT_EQ(0.123456789123456789, mt.get<long double>()); }
     TEST(MultiType, ValueBool)              { bool value = true;                                MultiType mt(value);    EXPECT_TRUE(mt.isType<bool>());                 EXPECT_EQ(true, mt.get<bool>()); }
     TEST(MultiType, ValueVoidpointer)       { int* pointer = new int; void* value = pointer;    MultiType mt(value);    EXPECT_TRUE(mt.isType<void*>());                EXPECT_EQ(value, mt.get<void*>()); delete pointer; }
+    TEST(MultiType, ValueSomepointer)       { int* value = new int;                             MultiType mt(value);    EXPECT_TRUE(mt.isType<void*>());                EXPECT_EQ(value, mt.get<int*>()); delete value; }
     TEST(MultiType, ValueString)            { std::string value = "Hello World";                MultiType mt(value);    EXPECT_TRUE(mt.isType<std::string>());          EXPECT_EQ("Hello World", mt.get<std::string>()); }
     TEST(MultiType, ValueVector2)           { Vector2 value = Vector2(11, 22);                  MultiType mt(value);    EXPECT_TRUE(mt.isType<Vector2>());              EXPECT_EQ(Vector2(11, 22), mt.get<Vector2>()); }
     TEST(MultiType, ValueVector3)           { Vector3 value = Vector3(11, 22, 33);              MultiType mt(value);    EXPECT_TRUE(mt.isType<Vector3>());              EXPECT_EQ(Vector3(11, 22, 33), mt.get<Vector3>()); }




More information about the Orxonox-commit mailing list