Changeset 1779
- Timestamp:
- Sep 14, 2008, 11:31:28 PM (16 years ago)
- Location:
- code/branches/core3/src/util
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core3/src/util/Convert.h
r1778 r1779 135 135 /////////////////// 136 136 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 137 // Default template. No conversion available at all. 138 template <class ToType, class FromType> 139 struct ConverterFallback 155 140 { 156 141 static bool convert(ToType* output, const FromType& input) 157 142 { 158 return fallbackConversion(output, input); 159 } 160 }; 143 COUT(2) << "Could not convert value of type " << typeid(FromType).name() 144 << " to type " << typeid(ToType).name() << std::endl; 145 return false; 146 } 147 }; 148 149 150 /////////////////////// 151 // ConverterFallback // 152 /////////////////////// 161 153 162 154 // Default template for stringstream … … 166 158 static bool convert(ToType* output, const FromType& input) 167 159 { 168 return fallbackConversion(output, input);160 return ConverterFallback<ToType, FromType>::convert(output, input); 169 161 } 170 162 }; … … 181 173 { 182 174 std::string temp; 183 if (ConverterFallback Template<std::string, FromType>::convert(&temp, input))175 if (ConverterFallback<std::string, FromType>::convert(&temp, input)) 184 176 { 185 177 std::operator <<(outstream, temp); … … 219 211 inline bool operator >>(std::istream& instream, ToType& output) 220 212 { 221 return ConverterFallback Template<ToType, std::string>213 return ConverterFallback<ToType, std::string> 222 214 ::convert(&output, static_cast<std::istringstream&>(instream).str()); 223 215 } … … 246 238 /////////////////// 247 239 240 // static cast no possible, try stringstream conversion next 241 template <class ToType, class FromType> 242 inline bool convertImplicitely(ToType* output, const FromType& input, ::Int2Type<false>) 243 { 244 return ConverterStringStream<ToType, FromType>::convert(output, input); 245 } 246 248 247 // We can cast implicitely 249 248 template <class ToType, class FromType> … … 252 251 (*output) = static_cast<ToType>(input); 253 252 return true; 254 }255 256 // static cast no possible, try stringstream conversion next257 template <class ToType, class FromType>258 inline bool convertImplicitely(ToType* output, const FromType& input, ::Int2Type<false>)259 {260 return ConverterStringStream<ToType, FromType>::convert(output, input);261 253 } 262 254 … … 266 258 /////////////////////// 267 259 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 260 // Default template if no specialisation is available 261 template <class ToType, class FromType> 262 struct ConverterExplicit 280 263 { 281 264 static bool convert(ToType* output, const FromType& input) 282 265 { 283 return explicitConversion(output, input); 266 // try implict conversion by probing first because we use '...' instead of a template 267 const bool probe = conversionTests::ImplicitConversion<FromType, ToType>::exists; 268 return convertImplicitely(output, input, ::Int2Type<probe>()); 284 269 } 285 270 }; … … 302 287 inline bool convertValue(ToType* output, const FromType& input) 303 288 { 304 return ConverterExplicit Template<ToType, FromType>::convert(output, input);289 return ConverterExplicit<ToType, FromType>::convert(output, input); 305 290 } 306 291 … … 368 353 // delegate conversion from const char* to std::string 369 354 template <class ToType> 370 inline bool explicitConversion(ToType* output, const char* input) 371 { 372 return convertValue<ToType, std::string>(output, input); 373 } 355 struct ConverterExplicit<ToType, const char*> 356 { 357 static bool convert(ToType* output, const char* input) 358 { 359 return convertValue<ToType, std::string>(output, input); 360 } 361 }; 374 362 375 363 // These conversions would exhibit ambiguous << or >> operators when using stringstream 376 inline bool explicitConversion(std::string* output, const char input) 377 { 378 *output = std::string(1, input); 379 return true; 380 } 381 inline bool explicitConversion(std::string* output, const unsigned char input) 382 { 383 *output = std::string(1, input); 384 return true; 385 } 386 inline bool explicitConversion(char* output, const std::string input) 387 { 388 if (input != "") 389 *output = input[0]; 390 else 391 *output = '\0'; 392 return true; 393 } 394 inline bool explicitConversion(unsigned char* output, const std::string input) 395 { 396 if (input != "") 397 *output = input[0]; 398 else 399 *output = '\0'; 400 return true; 401 } 364 template <> 365 struct ConverterExplicit<std::string, char> 366 { 367 static bool convert(std::string* output, const char input) 368 { 369 *output = std::string(1, input); 370 return true; 371 } 372 }; 373 template <> 374 struct ConverterExplicit<std::string, unsigned char> 375 { 376 static bool convert(std::string* output, const unsigned char input) 377 { 378 *output = std::string(1, input); 379 return true; 380 } 381 }; 382 template <> 383 struct ConverterExplicit<char, std::string> 384 { 385 static bool convert(char* output, const std::string input) 386 { 387 if (input != "") 388 *output = input[0]; 389 else 390 *output = '\0'; 391 return true; 392 } 393 }; 394 template <> 395 struct ConverterExplicit<unsigned char, std::string> 396 { 397 static bool convert(unsigned char* output, const std::string input) 398 { 399 if (input != "") 400 *output = input[0]; 401 else 402 *output = '\0'; 403 return true; 404 } 405 }; 402 406 403 407 #endif /* _Convert_H__ */ -
code/branches/core3/src/util/Math.cc
r1778 r1779 130 130 131 131 // std::string to Vector2 132 bool fallbackConversion(orxonox::Vector2* output, const std::string& input) 133 { 134 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 135 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 136 137 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 132 bool ConverterFallback<orxonox::Vector2, std::string>::convert(orxonox::Vector2* output, const std::string& input) 133 { 134 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 135 if ((opening_parenthesis = input.find('(')) == std::string::npos) 136 opening_parenthesis = 0; 137 else 138 opening_parenthesis++; 139 140 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), 141 ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 138 142 if (tokens.size() >= 2) 139 143 { … … 149 153 150 154 // std::string to Vector3 151 bool fallbackConversion(orxonox::Vector3* output, const std::string& input) 152 { 153 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 154 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 155 156 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 155 bool ConverterFallback<orxonox::Vector3, std::string>::convert(orxonox::Vector3* output, const std::string& input) 156 { 157 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 158 if ((opening_parenthesis = input.find('(')) == std::string::npos) 159 opening_parenthesis = 0; 160 else 161 opening_parenthesis++; 162 163 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), 164 ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 157 165 if (tokens.size() >= 3) 158 166 { … … 170 178 171 179 // std::string to Vector4 172 bool fallbackConversion(orxonox::Vector4* output, const std::string& input) 180 bool ConverterFallback<orxonox::Vector4, std::string>::convert(orxonox::Vector4* output, const std::string& input) 181 { 182 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 183 if ((opening_parenthesis = input.find('(')) == std::string::npos) 184 opening_parenthesis = 0; 185 else 186 opening_parenthesis++; 187 188 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), 189 ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 190 if (tokens.size() >= 4) 191 { 192 if (!ConvertValue(&(output->x), tokens[0])) 193 return false; 194 if (!ConvertValue(&(output->y), tokens[1])) 195 return false; 196 if (!ConvertValue(&(output->z), tokens[2])) 197 return false; 198 if (!ConvertValue(&(output->w), tokens[3])) 199 return false; 200 201 return true; 202 } 203 return false; 204 } 205 206 // std::string to Quaternion 207 bool ConverterFallback<orxonox::Quaternion, std::string>::convert(orxonox::Quaternion* output, const std::string& input) 173 208 { 174 209 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); … … 178 213 if (tokens.size() >= 4) 179 214 { 180 if (!ConvertValue(&(output-> x), tokens[0]))181 return false; 182 if (!ConvertValue(&(output-> y), tokens[1]))183 return false; 184 if (!ConvertValue(&(output-> z), tokens[2]))185 return false; 186 if (!ConvertValue(&(output-> w), tokens[3]))187 return false; 188 189 return true; 190 } 191 return false; 192 } 193 194 // std::string to Quaternion195 bool fallbackConversion(orxonox::Quaternion* output, const std::string& input)215 if (!ConvertValue(&(output->w), tokens[0])) 216 return false; 217 if (!ConvertValue(&(output->x), tokens[1])) 218 return false; 219 if (!ConvertValue(&(output->y), tokens[2])) 220 return false; 221 if (!ConvertValue(&(output->z), tokens[3])) 222 return false; 223 224 return true; 225 } 226 return false; 227 } 228 229 // std::string to ColourValue 230 bool ConverterFallback<orxonox::ColourValue, std::string>::convert(orxonox::ColourValue* output, const std::string& input) 196 231 { 197 232 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); … … 201 236 if (tokens.size() >= 4) 202 237 { 203 if (!ConvertValue(&(output->w), tokens[0]))204 return false;205 if (!ConvertValue(&(output->x), tokens[1]))206 return false;207 if (!ConvertValue(&(output->y), tokens[2]))208 return false;209 if (!ConvertValue(&(output->z), tokens[3]))210 return false;211 212 return true;213 }214 return false;215 }216 217 // std::string to ColourValue218 bool fallbackConversion(orxonox::ColourValue* output, const std::string& input)219 {220 unsigned int opening_parenthesis, closing_parenthesis = input.find(')');221 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; }222 223 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');224 if (tokens.size() >= 4)225 {226 238 if (!ConvertValue(&(output->r), tokens[0])) 227 239 return false; -
code/branches/core3/src/util/MathConvert.h
r1778 r1779 46 46 47 47 // Vector2 to std::string 48 inline bool explicitConversion(std::string* output, const orxonox::Vector2& input) 48 template <> 49 struct ConverterExplicit<std::string, orxonox::Vector2> 49 50 { 50 std::ostringstream ostream; 51 if (ostream << input.x << "," << input.y) 51 static bool convert(std::string* output, const orxonox::Vector2& input) 52 52 { 53 (*output) = ostream.str(); 54 return true; 53 std::ostringstream ostream; 54 if (ostream << input.x << "," << input.y) 55 { 56 (*output) = ostream.str(); 57 return true; 58 } 59 return false; 55 60 } 56 return false; 57 } 61 }; 58 62 59 63 // Vector3 to std::string 60 inline bool explicitConversion(std::string* output, const orxonox::Vector3& input) 64 template <> 65 struct ConverterExplicit<std::string, orxonox::Vector3> 61 66 { 62 std::ostringstream ostream; 63 if (ostream << input.x << "," << input.y << "," << input.z) 67 static bool convert(std::string* output, const orxonox::Vector3& input) 64 68 { 65 (*output) = ostream.str(); 66 return true; 69 std::ostringstream ostream; 70 if (ostream << input.x << "," << input.y << "," << input.z) 71 { 72 (*output) = ostream.str(); 73 return true; 74 } 75 return false; 67 76 } 68 return false; 69 } 77 }; 70 78 71 79 // Vector4 to std::string 72 inline bool explicitConversion(std::string* output, const orxonox::Vector4& input) 80 template <> 81 struct ConverterExplicit<std::string, orxonox::Vector4> 73 82 { 74 std::ostringstream ostream; 75 if (ostream << input.x << "," << input.y << "," << input.z << "," << input.w) 83 static bool convert(std::string* output, const orxonox::Vector4& input) 76 84 { 77 (*output) = ostream.str(); 78 return true; 85 std::ostringstream ostream; 86 if (ostream << input.x << "," << input.y << "," << input.z << "," << input.w) 87 { 88 (*output) = ostream.str(); 89 return true; 90 } 91 return false; 79 92 } 80 return false; 81 } 93 }; 82 94 83 95 // Quaternion to std::string 84 inline bool explicitConversion(std::string* output, const orxonox::Quaternion& input) 96 template <> 97 struct ConverterExplicit<std::string, orxonox::Quaternion> 85 98 { 86 std::ostringstream ostream; 87 if (ostream << input.w << "," << input.x << "," << input.y << "," << input.z) 99 static bool convert(std::string* output, const orxonox::Quaternion& input) 88 100 { 89 (*output) = ostream.str(); 90 return true; 101 std::ostringstream ostream; 102 if (ostream << input.w << "," << input.x << "," << input.y << "," << input.z) 103 { 104 (*output) = ostream.str(); 105 return true; 106 } 107 return false; 91 108 } 92 return false; 93 } 109 }; 94 110 95 111 // ColourValue to std::string 96 inline bool explicitConversion(std::string* output, const orxonox::ColourValue& input) 112 template <> 113 struct ConverterExplicit<std::string, orxonox::ColourValue> 97 114 { 98 std::ostringstream ostream; 99 if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a) 115 static bool convert(std::string* output, const orxonox::ColourValue& input) 100 116 { 101 (*output) = ostream.str(); 102 return true; 117 std::ostringstream ostream; 118 if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a) 119 { 120 (*output) = ostream.str(); 121 return true; 122 } 123 return false; 103 124 } 104 return false; 105 } 125 }; 106 126 107 127 … … 111 131 112 132 // std::string to Vector2 113 _UtilExport bool fallbackConversion(orxonox::Vector2* output, const std::string& input); 133 template <> struct _UtilExport ConverterFallback<orxonox::Vector2, std::string> 134 { static bool convert(orxonox::Vector2* output, const std::string& input); }; 114 135 // std::string to Vector3 115 _UtilExport bool fallbackConversion(orxonox::Vector3* output, const std::string& input); 136 template <> struct _UtilExport ConverterFallback<orxonox::Vector3, std::string> 137 { static bool convert(orxonox::Vector3* output, const std::string& input); }; 116 138 // std::string to Vector4 117 _UtilExport bool fallbackConversion(orxonox::Vector4* output, const std::string& input); 139 template <> struct _UtilExport ConverterFallback<orxonox::Vector4, std::string> 140 { static bool convert(orxonox::Vector4* output, const std::string& input); }; 118 141 // std::string to Quaternion 119 _UtilExport bool fallbackConversion(orxonox::Quaternion* output, const std::string& input); 142 template <> struct _UtilExport ConverterFallback<orxonox::Quaternion, std::string> 143 { static bool convert(orxonox::Quaternion* output, const std::string& input); }; 120 144 // std::string to ColourValue 121 _UtilExport bool fallbackConversion(orxonox::ColourValue* output, const std::string& input); 145 template <> struct _UtilExport ConverterFallback<orxonox::ColourValue, std::string> 146 { static bool convert(orxonox::ColourValue* output, const std::string& input); }; 122 147 123 148
Note: See TracChangeset
for help on using the changeset viewer.