- Timestamp:
- Sep 14, 2008, 3:40:48 PM (16 years ago)
- Location:
- code/branches/core3/src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core3/src/orxonox/Orxonox.cc
r1777 r1778 86 86 assert(res == var##nr##2) 87 87 88 template <>89 struct ConverterExplicit<orxonox::Radian, const char*>90 {91 static bool convert(orxonox::Radian* output, const char* input)92 {93 //std::string asdf("asfd");94 //std::cout << asdf;95 float temp;96 convertValue(&temp, input);97 *output = temp;98 }99 };88 //template <> 89 //struct ConverterExplicit<orxonox::Radian, const char*> 90 //{ 91 // static bool convert(orxonox::Radian* output, const char* input) 92 // { 93 // //std::string asdf("asfd"); 94 // //std::cout << asdf; 95 // float temp; 96 // convertValue(&temp, input); 97 // *output = temp; 98 // } 99 //}; 100 100 101 101 class FooBar { }; -
code/branches/core3/src/util/ArgReader.cc
r1777 r1778 65 65 #include "Convert.h" 66 66 67 template<>68 struct ConverterExplicit<const char*, FooBar>69 {70 static bool convert(const char** output, const FooBar input)71 {72 return true;73 }74 };67 //template<> 68 //struct ConverterExplicit<const char*, FooBar> 69 //{ 70 // static bool convert(const char** output, const FooBar input) 71 // { 72 // return true; 73 // } 74 //}; 75 75 76 76 #include "MultiType.h" … … 90 90 //val2 = val1; 91 91 //convertValue(&val2, val1); 92 // explicitConversion(&FooBar(), val1);92 //convertValue(&FooBar(), val1); 93 93 94 94 //using namespace1::fooBar1; … … 96 96 //int val1; 97 97 //char val2; 98 // explicitConversion(&val1, val2);98 //convertValue(&val1, val2); 99 99 100 100 //std::istringstream asdf; -
code/branches/core3/src/util/Convert.h
r1777 r1778 58 58 is described in 'Moder C++ design' by Alexandrescu (2001). */ 59 59 60 namespace conversionTests61 {62 // A struct that is guaranteed to be larger than any return type of our conversion functions.63 // So we simply add all the sizes of the return types plus a little bit more.64 struct VeryBigStruct65 {66 char intSize[sizeof(int)];67 char issSize[sizeof(std::istringstream)];68 char ossSize[sizeof(std::ostringstream)];69 char boolSize[sizeof(bool)];70 char addingMore[4096]; // just to be sure ;)71 };72 }73 74 namespace conversion_another_namespace75 {76 // We want to keep the templates for the convert functions out of global namespace so that there77 // are no ambiguities. These templates are never used anyway, they only serve to detect whether78 // there is a global funciton for a specific conversion or not.79 // Why the seperate namespace? --> see 'using' statement at the end of conversionTests::80 //template <class AnyToType, class AnyFromType>81 //conversionTests::VeryBigStruct explicitConversion(AnyToType* output, const AnyFromType input);82 //template <class Any, int Dummy>83 //conversionTests::VeryBigStruct operator<<(std::ostream& outstream, const Any& anything);84 //template <class Any, int Dummy>85 //conversionTests::VeryBigStruct operator>>(std::istream& instream, const Any& anything);86 }87 88 namespace conversionTests89 {90 60 // disable warnings about possible loss of data 91 61 #if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC … … 93 63 # pragma warning(disable:4244) 94 64 #endif 65 66 namespace conversionTests 67 { 68 // A struct that is guaranteed to be larger than any return type of our conversion functions. 69 // So we simply add all the sizes of the return types plus a little bit more. 70 struct VeryBigStruct 71 { 72 char intSize[sizeof(int)]; 73 char addingMore[4096]; // just to be sure ;) 74 }; 95 75 96 76 template <class FromType, class ToType> … … 110 90 enum { exists = !(sizeof(test(object)) == sizeof(VeryBigStruct)) }; 111 91 }; 92 } 112 93 113 94 #if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC 114 95 # pragma warning(pop) 115 96 #endif 116 117 //template <class FromType, class ToType>118 //class ExplicitConversion119 //{120 //private:121 // ExplicitConversion(); ExplicitConversion(const ExplicitConversion&); ~ExplicitConversion();122 // static FromType objectFromType; // helper object to handle private c'tor and d'tor123 // static ToType objectToType; // helper object to handle private c'tor and d'tor124 //public:125 // enum { exists = !(sizeof(explicitConversion(&objectToType, objectFromType)) == sizeof(VeryBigStruct)) };126 //};127 128 //template <class Type>129 //class IStreamOperator130 //{131 // IStreamOperator(); IStreamOperator(const IStreamOperator&); ~IStreamOperator();132 // static std::istream istream_; // helper object to perform the '>>' operation133 // static Type object; // helper object to handle private c'tor and d'tor134 //public:135 // enum { exists = !(sizeof(istream_ >> object) == sizeof(VeryBigStruct)) };136 //};137 138 //template <class Type>139 //class OStreamOperator140 //{141 // OStreamOperator(); OStreamOperator(const OStreamOperator&); ~OStreamOperator();142 // static std::ostream ostream_; // helper object to perform the '<<' operation143 // static Type object; // helper object to handle private c'tor and d'tor144 //public:145 // enum { exists = !(sizeof(ostream_ << object) == sizeof(VeryBigStruct)) };146 //};147 148 // Somehow msvc and gcc don't like it when using function arguments that have a type149 // in a third namespace. The global namespace functions then get overridden by the150 // templates above. So we simply put the generic ones in a another namespace.151 // Note: DON'T place this statement before a class template. That would trigger152 // a heavy bug (namespace leakage) in msvc 2005.153 //using namespace conversion_another_namespace;154 }155 97 156 98 … … 162 104 Overwrites: 163 105 1. (Partial) template specialisation of ConverterExplicit::convert 164 2. Global functions explicitConversion(ToType* output, const FromType input)106 2. Global functions convertValue(ToType* output, const FromType input) 165 107 Fallbacks: 166 108 3. Any possible implicit conversion. This includes FooBar --> int if FooBar defines operator float(). … … 171 113 172 114 There are obviously a lot of ways to specifiy a user defined conversion. What should I use? 173 When using any non-template function based conversion (implicit conversion, explicitConversion, << or >>)115 When using any non-template function based conversion (implicit conversion, convertValue, << or >>) 174 116 then you should consider that this function has to be defined prior to including this file. 175 117 If you do not whish do do that, you will have to spcecialsize the ConverterExplicit template. … … 193 135 /////////////////// 194 136 195 namespace conversion 196 { 197 // Default template for stringtream, no Conversion possible 198 template <class ToType, class FromType> 199 struct ConverterSS 200 { 201 static bool convert(ToType* output, FromType input) 202 { 203 COUT(2) << "Could not convert value of type " << typeid(FromType).name() 204 << " to type " << typeid(ToType).name() << std::endl; 205 return false; 206 } 207 }; 208 } 137 // Default template for fallbackConvert, no conversion possible 138 template <class ToType, class FromType> 139 inline bool fallbackConversion(ToType* output, FromType input) 140 { 141 COUT(2) << "Could not convert value of type " << typeid(FromType).name() 142 << " to type " << typeid(ToType).name() << std::endl; 143 return false; 144 } 145 146 147 ///////////////////// 148 // fallbackConvert // 149 ///////////////////// 150 151 // Class template used when << or >> was not available with string conversions 152 // It is required to call not yet declared fallbackConvert functions. 153 template <class ToType, class FromType> 154 struct ConverterFallbackTemplate 155 { 156 static bool convert(ToType* output, const FromType& input) 157 { 158 return fallbackConversion(output, input); 159 } 160 }; 161 162 // Default template for stringstream 163 template <class ToType, class FromType> 164 struct ConverterStringStream 165 { 166 static bool convert(ToType* output, const FromType& input) 167 { 168 return fallbackConversion(output, input); 169 } 170 }; 209 171 210 172 … … 215 177 namespace fallbackTemplates 216 178 { 217 template <class Any>218 bool operator <<(std::ostream& outstream, const Any& anything)219 {220 COUT(2) << "Could not convert value of type " << typeid(Any).name()221 << " to std::string" << std::endl;222 return false;223 }224 }225 226 namespace conversion227 {228 //using namespace fallbackTemplates;229 // template that evaluates whether OStringStream is possible for conversions to std::string230 179 template <class FromType> 231 struct ConverterSS<std::string, FromType>232 { 233 // probe for '<<' ostream operator234 static bool convert(std::string* output, const FromType& input)180 inline bool operator <<(std::ostream& outstream, const FromType& input) 181 { 182 std::string temp; 183 if (ConverterFallbackTemplate<std::string, FromType>::convert(&temp, input)) 235 184 { 236 std::ostringstream oss; 237 if (oss << input) 238 { 239 (*output) = oss.str(); 240 return true; 241 } 242 else 243 return false; 185 std::operator <<(outstream, temp); 186 return true; 244 187 } 245 }; 246 247 } 188 else 189 return false; 190 } 191 } 192 193 // template that evaluates whether OStringStream is possible for conversions to std::string 194 template <class FromType> 195 struct ConverterStringStream<std::string, FromType> 196 { 197 static bool convert(std::string* output, const FromType& input) 198 { 199 using namespace fallbackTemplates; 200 std::ostringstream oss; 201 if (oss << input) 202 { 203 (*output) = oss.str(); 204 return true; 205 } 206 else 207 return false; 208 } 209 }; 248 210 249 211 … … 254 216 namespace fallbackTemplates 255 217 { 256 template <class Any>257 bool operator >>(std::istream& instream, const Any& anything)258 {259 COUT(2) << "Could not convert std::string value to type " << typeid(ToType).name() << std::endl;260 return false;261 }262 }263 264 namespace conversion265 {266 // template that evaluates whether IStringStream is possible for conversions from std::string267 218 template <class ToType> 268 struct ConverterSS<ToType, std::string> 269 { 270 static bool convert(ToType* output, const std::string& input) 219 inline bool operator >>(std::istream& instream, ToType& output) 220 { 221 return ConverterFallbackTemplate<ToType, std::string> 222 ::convert(&output, static_cast<std::istringstream&>(instream).str()); 223 } 224 } 225 226 // template that evaluates whether IStringStream is possible for conversions from std::string 227 template <class ToType> 228 struct ConverterStringStream<ToType, std::string> 229 { 230 static bool convert(ToType* output, const std::string& input) 231 { 232 using namespace fallbackTemplates; 233 std::istringstream iss(input); 234 if (iss >> (*output)) 271 235 { 272 std::istringstream iss(input); 273 if (iss >> (*output)) 274 { 275 return true; 276 } 277 else 278 return false; 236 return true; 279 237 } 280 };281 282 using namespace fallbackTemplates;283 } 238 else 239 return false; 240 } 241 }; 284 242 285 243 … … 295 253 return true; 296 254 } 255 297 256 // static cast no possible, try stringstream conversion next 298 257 template <class ToType, class FromType> 299 258 inline bool convertImplicitely(ToType* output, const FromType& input, ::Int2Type<false>) 300 259 { 301 return conversion::ConverterSS<ToType, FromType>::convert(output, input); 302 } 303 304 305 ///////////////////////// 306 // Explicit conversion // 307 ///////////////////////// 308 309 namespace fallbackTemplates 310 { 311 // We want to keep the templates for the convert functions out of global namespace so that there 312 // are no ambiguities. These templates are never used anyway, they only serve to detect whether 313 // there is a global funciton for a specific conversion or not. 314 // Note: Don't put these functions in a separate namespace and add a 'using' directive here. 315 // For MS sake, just don't! 316 template <class ToType, class FromType> 317 bool explicitConversion(ToType* output, const FromType input) 318 { 319 // try implict conversion by probing first because we use '...' instead of a template 320 const bool probe = conversionTests::ImplicitConversion<FromType, ToType>::exists; 321 //using namespace conversion; 322 return convertImplicitely(output, input, ::Int2Type<probe>()); 323 } 324 } 325 326 327 ///////////////////// 328 // Local Overwrite // 329 ///////////////////// 330 331 // Template that is used when no explicit template specialisation is available. 332 // Try explicitConversion() function next. 333 template <class ToType, class FromType> 334 struct ConverterExplicit 260 return ConverterStringStream<ToType, FromType>::convert(output, input); 261 } 262 263 264 /////////////////////// 265 // Explicit Fallback // 266 /////////////////////// 267 268 template <class ToType, class FromType> 269 inline bool explicitConversion(ToType* output, const FromType& input) 270 { 271 // try implict conversion by probing first because we use '...' instead of a template 272 const bool probe = conversionTests::ImplicitConversion<FromType, ToType>::exists; 273 return convertImplicitely(output, input, ::Int2Type<probe>()); 274 } 275 276 277 // Indirect calls over a class template so we can call functions not yet declared. 278 template <class ToType, class FromType> 279 struct ConverterExplicitTemplate 335 280 { 336 281 static bool convert(ToType* output, const FromType& input) 337 282 { 338 using namespace fallbackTemplates;339 283 return explicitConversion(output, input); 340 284 } … … 348 292 /** 349 293 @brief 350 Converts any value to any other as long as there exi ts a conversion.294 Converts any value to any other as long as there exists a conversion. 351 295 Otherwise, the conversion will generate a runtime warning. 352 296 For information about the different conversion methods (user defined too), see the section 353 297 'Actual conversion sequence' in this file above. 298 @note 299 This function is only a fallback if there is no appropriate 'convertValue' function. 354 300 */ 355 301 template <class ToType, class FromType> 356 302 inline bool convertValue(ToType* output, const FromType& input) 357 303 { 358 // check whether we can convert one type to the other explicitely via explicit template specialisations 359 return ConverterExplicit<ToType, FromType>::convert(output, input); 304 return ConverterExplicitTemplate<ToType, FromType>::convert(output, input); 360 305 } 361 306 … … 425 370 inline bool explicitConversion(ToType* output, const char* input) 426 371 { 427 return ConverterExplicit<ToType, std::string>::convert(output, input);372 return convertValue<ToType, std::string>(output, input); 428 373 } 429 374 -
code/branches/core3/src/util/Math.cc
r1777 r1778 125 125 } 126 126 127 ////////////////////////// 128 // Conversion functions // 129 ////////////////////////// 130 127 131 // std::string to Vector2 128 bool explicitConversion(orxonox::Vector2* output, const std::string& input)132 bool fallbackConversion(orxonox::Vector2* output, const std::string& input) 129 133 { 130 134 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); … … 145 149 146 150 // std::string to Vector3 147 bool explicitConversion(orxonox::Vector3* output, const std::string& input)151 bool fallbackConversion(orxonox::Vector3* output, const std::string& input) 148 152 { 149 153 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); … … 166 170 167 171 // std::string to Vector4 168 bool explicitConversion(orxonox::Vector4* output, const std::string& input)172 bool fallbackConversion(orxonox::Vector4* output, const std::string& input) 169 173 { 170 174 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); … … 189 193 190 194 // std::string to Quaternion 191 bool explicitConversion(orxonox::Quaternion* output, const std::string& input)195 bool fallbackConversion(orxonox::Quaternion* output, const std::string& input) 192 196 { 193 197 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); … … 212 216 213 217 // std::string to ColourValue 214 bool explicitConversion(orxonox::ColourValue* output, const std::string& input)218 bool fallbackConversion(orxonox::ColourValue* output, const std::string& input) 215 219 { 216 220 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); -
code/branches/core3/src/util/MathConvert.h
r1777 r1778 27 27 */ 28 28 29 /* !29 /** 30 30 @file 31 @brief Actual conversion functions. 31 @brief 32 Math conversion functions. Definitions are in Math.cc 32 33 */ 33 34 … … 38 39 #include "Convert.h" 39 40 #include "Math.h" 40 41 // Note: explicitConversion function is used whenever possible prior to42 // template specialisations to allow for manual calls and overwrites.43 41 44 42 … … 113 111 114 112 // std::string to Vector2 115 _UtilExport bool explicitConversion(orxonox::Vector2* output, const std::string& input);113 _UtilExport bool fallbackConversion(orxonox::Vector2* output, const std::string& input); 116 114 // std::string to Vector3 117 _UtilExport bool explicitConversion(orxonox::Vector3* output, const std::string& input);115 _UtilExport bool fallbackConversion(orxonox::Vector3* output, const std::string& input); 118 116 // std::string to Vector4 119 _UtilExport bool explicitConversion(orxonox::Vector4* output, const std::string& input);117 _UtilExport bool fallbackConversion(orxonox::Vector4* output, const std::string& input); 120 118 // std::string to Quaternion 121 _UtilExport bool explicitConversion(orxonox::Quaternion* output, const std::string& input);119 _UtilExport bool fallbackConversion(orxonox::Quaternion* output, const std::string& input); 122 120 // std::string to ColourValue 123 _UtilExport bool explicitConversion(orxonox::ColourValue* output, const std::string& input);121 _UtilExport bool fallbackConversion(orxonox::ColourValue* output, const std::string& input); 124 122 125 123 … … 130 128 // From Radian 131 129 template <class ToType> 132 inline bool explicitConversion(ToType* output, const orxonox::Radian input)130 inline bool fallbackConversion(ToType* output, const orxonox::Radian input) 133 131 { 134 132 return convertValue<ToType, Ogre::Real>(output, input.valueRadians()); … … 137 135 // From Degree 138 136 template <class ToType> 139 inline bool explicitConversion(ToType* output, const orxonox::Degree input)137 inline bool fallbackConversion(ToType* output, const orxonox::Degree input) 140 138 { 141 139 return convertValue<ToType, Ogre::Real>(output, input.valueDegrees()); … … 144 142 // To Radian 145 143 template <class FromType> 146 inline bool explicitConversion(orxonox::Radian* output, const FromType input)144 inline bool fallbackConversion(orxonox::Radian* output, const FromType input) 147 145 { 148 146 float temp; … … 158 156 // To Degree 159 157 template <class FromType> 160 inline bool explicitConversion(orxonox::Degree* output, const FromType input)158 inline bool fallbackConversion(orxonox::Degree* output, const FromType input) 161 159 { 162 160 float temp; … … 169 167 return false; 170 168 } 171 // Radian to Radian172 inline bool explicitConversion(orxonox::Radian* output, const orxonox::Radian input)173 { *output = input; return true; }174 // Degree to Degree175 inline bool explicitConversion(orxonox::Degree* output, const orxonox::Degree input)176 { *output = input; return true; }177 // Radian to Degree178 inline bool explicitConversion(orxonox::Degree* output, const orxonox::Radian input)179 { *output = input; return true; }180 // Degree to Radian181 inline bool explicitConversion(orxonox::Radian* output, const orxonox::Degree input)182 { *output = input; return true; }183 184 //template <class ToType>185 //struct ConverterExplicit<ToType, Ogre::Radian>186 //{187 // static bool convert(ToType* output, const Ogre::Radian input)188 // {189 // return ConverterExplicit<ToType, Ogre::Real>::convert(output, input.valueRadians());190 // }191 //};192 193 //// Conversion from Ogre::Radian194 //template <class ToType>195 //struct ConverterExplicit<ToType, const Ogre::Radian>196 //{197 // static bool convert(ToType* output, const Ogre::Radian input)198 // {199 // return ConverterExplicit<ToType, Ogre::Real>::convert(output, input.getValueRadian());200 // }201 //};202 203 //template <class ToType>204 //inline bool explicitConversion(ToType* output, const Ogre::Radian& input)205 //{206 // return ConvertValue(output, ;207 //}208 209 //// Conversion from Ogre::Degrees210 //inline bool explicitConversion(std::string* output, const orxonox::ColourValue& input)211 //{212 // std::ostringstream ostream;213 // if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a)214 // {215 // (*output) = ostream.str();216 // return true;217 // }218 // return false;219 //}220 //221 //// Conversion to Ogre::Radian222 //inline bool explicitConversion(std::string* output, const orxonox::ColourValue& input)223 //{224 // std::ostringstream ostream;225 // if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a)226 // {227 // (*output) = ostream.str();228 // return true;229 // }230 // return false;231 //}232 //233 //// Conversion to Ogre::Degrees234 //inline bool explicitConversion(std::string* output, const orxonox::ColourValue& input)235 //{236 // std::ostringstream ostream;237 // if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a)238 // {239 // (*output) = ostream.str();240 // return true;241 // }242 // return false;243 //}244 169 245 170 #endif /* _MathConvert_H__ */
Note: See TracChangeset
for help on using the changeset viewer.