Changeset 1052 for code/trunk/src/util
- Timestamp:
- Apr 14, 2008, 3:42:49 AM (17 years ago)
- Location:
- code/trunk/src/util
- Files:
-
- 16 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/util/CMakeLists.txt
r1051 r1052 8 8 Math.cc 9 9 String.cc 10 Clipboard.cc 10 11 SubString.cc 11 12 MultiTypePrimitive.cc -
code/trunk/src/util/Convert.h
r871 r1052 21 21 * Author: 22 22 * Benjamin Grauer 23 * Fabian 'x3n' Landau 23 24 * Co-authors: 24 * Fabian 'x3n' Landau25 * ... 25 26 */ 26 27 … … 42 43 43 44 44 // DEFAULT CLASS 45 template <typename FromType, typename ToType> 46 class Converter 47 { 48 public: 49 bool operator()(ToType* output, const FromType& input) const 50 { 51 return false; 52 } 53 }; 54 55 // PARTIAL SPECIALIZATION TO CONVERT TO STRINGS 56 template<typename FromType> 57 class Converter<FromType, std::string> 58 { 59 public: 60 bool operator()(std::string* output, const FromType& input) const 61 { 62 std::ostringstream oss; 63 if (oss << input) 64 { 65 (*output) = oss.str(); 45 ////////// 46 // MAIN // 47 ////////// 48 49 // Enum to declare the wanted conversion preference in case of equal type-levels 50 enum ConversionPreference 51 { 52 CP_PreferToType, 53 CP_PreferFromType, 54 }; 55 56 // Helper classes to determine the preferred partial template specialization 57 class _ToType_ {}; 58 class _FromType_ {}; 59 class _Explicit_ {}; 60 61 62 // The default convert functions 63 template <class FromType, class ToType, class Type> 64 struct ConverterSpecialized 65 { 66 enum { specialized = false }; 67 static bool convert(ToType* output, const FromType& input) 68 { return false; } 69 }; 70 71 72 // The default convert function if both types are the same 73 template <class BothTypes> 74 struct ConverterSpecialized<BothTypes, BothTypes, _Explicit_> 75 { 76 enum { specialized = true }; 77 static bool convert(BothTypes* output, const BothTypes& input) 78 { (*output) = input; return true; } 79 }; 80 81 82 // The possible levels 83 #define __low__ 0 // Everything that is or behaves like a primitive type (an can be converted with a typecast to every other low-level type) 84 #define __mid__ 1 // Everything that has overloaded << and >> operators to operate on a std::stream 85 #define __high__ 2 // Everything that doesn't fullfill the lowerlevel-requirements and therefore needs specialized conversions 86 87 // Defines the levels of all types: Default is __high__ so you don't have to define every high-level type 88 template <class T> struct ConverterLevel { enum { level = __high__ }; }; 89 template <> struct ConverterLevel<std::string> { enum { level = __mid__ }; }; 90 template <> struct ConverterLevel<orxonox::Radian> { enum { level = __mid__ }; }; 91 template <> struct ConverterLevel<orxonox::Degree> { enum { level = __mid__ }; }; 92 template <> struct ConverterLevel<int> { enum { level = __low__ }; }; 93 template <> struct ConverterLevel<unsigned int> { enum { level = __low__ }; }; 94 template <> struct ConverterLevel<char> { enum { level = __low__ }; }; 95 template <> struct ConverterLevel<unsigned char> { enum { level = __low__ }; }; 96 template <> struct ConverterLevel<short> { enum { level = __low__ }; }; 97 template <> struct ConverterLevel<unsigned short> { enum { level = __low__ }; }; 98 template <> struct ConverterLevel<long> { enum { level = __low__ }; }; 99 template <> struct ConverterLevel<unsigned long> { enum { level = __low__ }; }; 100 template <> struct ConverterLevel<float> { enum { level = __low__ }; }; 101 template <> struct ConverterLevel<double> { enum { level = __low__ }; }; 102 template <> struct ConverterLevel<long double> { enum { level = __low__ }; }; 103 template <> struct ConverterLevel<bool> { enum { level = __low__ }; }; 104 105 106 // Calculates the preference based on the levels of FromType and ToType 107 template <int from, int to> 108 struct ConverterPreference 109 { 110 enum 111 { 112 // The maximum of both levels: element of {0, 1, 2} 113 // max 0: Both types are primitives or have a similar behaviour 114 // max 1: At least one type is not a primitive, but both can be put on a std::stream 115 // max 2: There is at least one generic type that needs specialized conversions 116 max = (from > to) ? from : to, 117 118 // The difference between both levels limited to +-1: element of {-1, 0, 1} 119 // diff -1: The FromType has higher level than the ToType 120 // diff 0: Both types have the same level 121 // diff 1: The ToType has higher level than the FromType 122 diff = ((to - from) > 1) ? 1 : (((to - from) < -1) ? -1 : to - from) 123 }; 124 }; 125 126 127 // The default conversion: This usually does nothing 128 template <int max, class FromType, class ToType> 129 struct ConverterDefault 130 { 131 static bool convert(ToType* output, const FromType& input) 132 { 133 return false; 134 } 135 }; 136 // The default conversion for primitives: A typecast (defined over two partial specialized templates to exclude all non-primitive types and classes) template <int max, class FromType, class ToType> 137 template <class FromType, class ToType> 138 struct ConverterDefault<0, FromType, ToType> 139 { 140 static bool convert(ToType* output, const FromType& input) 141 { 142 (*output) = (ToType)input; 66 143 return true; 67 } 68 else 69 return false; 70 } 71 }; 72 73 // PARTIAL SPECIALIZATION TO CONVERT FROM STRING 74 template<typename ToType> 75 class Converter<std::string, ToType> 76 { 77 public: 78 bool operator()(ToType* output, const std::string& input) const 79 { 80 std::istringstream iss(input); 81 if (iss >> (*output)) 144 } 145 }; 146 147 148 // Converter: Converts input of FromType into output of ToType 149 template <int diff, int max, class FromType, class ToType, ConversionPreference pref> 150 struct Converter 151 { 152 static bool convert(ToType* output, const FromType& input) 153 { 154 return false; 155 } 156 }; 157 // Converter: level{FromType} > level{ToType} 158 template <int max, class FromType, class ToType, ConversionPreference pref> 159 struct Converter<-1, max, FromType, ToType, pref> 160 { static bool convert(ToType* output, const FromType& input) 161 { return (ConverterSpecialized<FromType, ToType, _Explicit_>::specialized) ? (ConverterSpecialized<FromType, ToType, _Explicit_>::convert(output, input)) : (ConverterSpecialized<FromType, ToType, _FromType_>::specialized) ? (ConverterSpecialized<FromType, ToType, _FromType_>::convert(output, input)) : (ConverterDefault<max, FromType, ToType>::convert(output, input)); } }; 162 // Converter: level{FromType} < level{ToType} 163 template <int max, class FromType, class ToType, ConversionPreference pref> 164 struct Converter<1, max, FromType, ToType, pref> 165 { static bool convert(ToType* output, const FromType& input) 166 { return (ConverterSpecialized<FromType, ToType, _Explicit_>::specialized) ? (ConverterSpecialized<FromType, ToType, _Explicit_>::convert(output, input)) : (ConverterSpecialized<FromType, ToType, _ToType_>::specialized) ? (ConverterSpecialized<FromType, ToType, _ToType_>::convert(output, input)) : (ConverterDefault<max, FromType, ToType>::convert(output, input)); } }; 167 // Converter: level{FromType} = level{ToType} 168 // CP_PreferToType 169 template <int max, class FromType, class ToType> 170 struct Converter<0, max, FromType, ToType, CP_PreferToType> 171 { static bool convert(ToType* output, const FromType& input) 172 { return (ConverterSpecialized<FromType, ToType, _Explicit_>::specialized) ? (ConverterSpecialized<FromType, ToType, _Explicit_>::convert(output, input)) : (ConverterSpecialized<FromType, ToType, _ToType_>::specialized) ? (ConverterSpecialized<FromType, ToType, _ToType_>::convert(output, input)) : (ConverterSpecialized<FromType, ToType, _FromType_>::specialized) ? (ConverterSpecialized<FromType, ToType, _FromType_>::convert(output, input)) : (ConverterDefault<max, FromType, ToType>::convert(output, input)); } }; 173 // CP_PreferFromType 174 template <int max, class FromType, class ToType> 175 struct Converter<0, max, FromType, ToType, CP_PreferFromType> 176 { static bool convert(ToType* output, const FromType& input) 177 { return (ConverterSpecialized<FromType, ToType, _Explicit_>::specialized) ? (ConverterSpecialized<FromType, ToType, _Explicit_>::convert(output, input)) : (ConverterSpecialized<FromType, ToType, _FromType_>::specialized) ? (ConverterSpecialized<FromType, ToType, _FromType_>::convert(output, input)) : (ConverterSpecialized<FromType, ToType, _ToType_>::specialized) ? (ConverterSpecialized<FromType, ToType, _ToType_>::convert(output, input)) : (ConverterDefault<max, FromType, ToType>::convert(output, input)); } }; 178 179 180 // Calls the Converter::convertValue function with the correct template type parameters calculated by ConverterPreference 181 template <class FromType, class ToType> 182 static bool convertValue(ToType* output, const FromType& input, ConversionPreference preference = CP_PreferToType) 183 { 184 return (preference == CP_PreferToType) ? 185 Converter<ConverterPreference<ConverterLevel<FromType>::level, ConverterLevel<ToType>::level>::diff, 186 ConverterPreference<ConverterLevel<FromType>::level, ConverterLevel<ToType>::level>::max, 187 FromType, 188 ToType, 189 CP_PreferToType>::convert(output, input) 190 : Converter<ConverterPreference<ConverterLevel<FromType>::level, ConverterLevel<ToType>::level>::diff, 191 ConverterPreference<ConverterLevel<FromType>::level, ConverterLevel<ToType>::level>::max, 192 FromType, 193 ToType, 194 CP_PreferFromType>::convert(output, input); 195 } 196 197 198 ////////////////////// 199 // HELPER FUNCTIONS // 200 ////////////////////// 201 202 // Helper function: Calls convertValue with and without default value and returns true if the conversion was successful 203 template<class FromType, class ToType> 204 static bool ConvertValue(ToType* output, const FromType& input, ConversionPreference preference = CP_PreferToType) 205 { 206 return convertValue(output, input, preference); 207 } 208 template<class FromType, class ToType> 209 static bool ConvertValue(ToType* output, const FromType& input, const ToType& fallback, ConversionPreference preference = CP_PreferToType) 210 { 211 if (convertValue(output, input, preference)) 82 212 return true; 83 else 84 return false; 85 } 86 }; 87 88 // FUNCTION SO WE DO NOT HAVE TO TELL THE COMPILER ABOUT THE TYPE 89 template<typename FromType, typename ToType> 90 static bool ConvertValue(ToType* output, const FromType& input) 91 { 92 Converter<FromType, ToType> converter; 93 return converter(output, input); 213 214 (*output) = fallback; 215 return false; 94 216 } 95 217 96 // THE SAME, BUT WITH DEFAULT VALUE 97 template<typename FromType, typename ToType> 98 static bool ConvertValue(ToType* output, const FromType& input, const ToType& fallback) 99 { 100 Converter<FromType, ToType> converter; 101 if (converter(output, input)) 102 return true; 103 104 (*output) = fallback; 105 return false; 218 // Helper function: Calls convertValue with and without default value and returns the converted value 219 template<class FromType, class ToType> 220 static ToType getConvertedValue(const FromType& input, ConversionPreference preference = CP_PreferToType) 221 { 222 ToType output = ToType(); 223 ConvertValue(&output, input, preference); 224 return output; 106 225 } 107 108 // THE SAME LIKE BEFORE, BUT NEEDS NO POINTER TO THE OUTPUT 109 template<typename FromType, typename ToType> 110 static ToType ConvertValueAndReturn(const FromType& input) 111 { 112 ToType output; 113 ConvertValue(&output, input); 114 return output; 226 template<class FromType, class ToType> 227 static ToType getConvertedValue(const FromType& input, const ToType& fallback, ConversionPreference preference = CP_PreferToType) 228 { 229 ToType output = fallback; 230 ConvertValue(&output, input, fallback, preference); 231 return output; 115 232 } 116 233 117 // THE SAME, BUT WITH DEFAULT VALUE 118 template<typename FromType, typename ToType> 119 static ToType ConvertValueAndReturn(const FromType& input, const FromType& fallback) 120 { 121 ToType output; 122 ConvertValue(&output, input, fallback); 123 return output; 124 } 125 126 ////////////////////////// 127 // MORE SPECIALISATIONS // 128 ////////////////////////// 129 130 // STRING TO STRING 131 template<> 132 class Converter<std::string, std::string> 133 { 134 public: 135 bool operator()(std::string* output, const std::string& input) const 136 { 137 (*output) = std::string(input); 138 return true; 139 } 140 }; 234 235 ///////////////////// 236 // SPECIALIZATIONS // 237 ///////////////////// 238 239 ///////////// 240 // SAMPLES // 241 ///////////// 242 /* 243 // convert everything to xyz 244 template <class FromType> 245 struct ConverterSpecialized<FromType, xyz, _ToType_> 246 { 247 enum { specialized = true }; 248 static bool convert(xyz* output, const FromType& input) 249 { return ...; } 250 }; 251 252 // convert xyz to everything 253 template <class ToType> 254 struct ConverterSpecialized<xyz, ToType, _FromType_> 255 { 256 enum { specialized = true }; 257 static bool convert(ToType* output, const xyz& input) 258 { return ...; } 259 }; 260 261 // convert abc to xyz 262 template <> 263 struct ConverterSpecialized<abc, xyz, _Explicit_> 264 { 265 enum { specialized = true }; 266 static bool convert(xyz* output, const abc& input) 267 { return ...; } 268 }; 269 */ 270 271 //////////// 272 // STRING // 273 //////////// 274 275 // convert to string 276 template <class FromType> 277 struct ConverterSpecialized<FromType, std::string, _ToType_> 278 { 279 enum { specialized = true }; 280 static bool convert(std::string* output, const FromType& input) 281 { 282 std::ostringstream oss; 283 if (oss << input) 284 { 285 (*output) = oss.str(); 286 return true; 287 } 288 else 289 return false; 290 } 291 }; 292 293 // convert from string 294 template <class ToType> 295 struct ConverterSpecialized<std::string, ToType, _FromType_> 296 { 297 enum { specialized = true }; 298 static bool convert(ToType* output, const std::string& input) 299 { 300 std::istringstream iss(input); 301 if (iss >> (*output)) 302 return true; 303 else 304 return false; 305 } 306 }; 307 141 308 142 309 //////////////// … … 144 311 //////////////// 145 312 146 // PARTIAL SPECIALIZATION TO CONVERT FROM MULTITYPEPRIMITIVE 147 template<typename ToType> 148 class Converter<MultiTypePrimitive, ToType> 149 { 150 public: 151 bool operator()(ToType* output, const MultiTypePrimitive& input) const 152 { 153 if (input.getType() == MT_int) 154 return ConvertValue(output, input.getInt()); 155 else if (input.getType() == MT_uint) 156 return ConvertValue(output, input.getUnsignedInt()); 157 else if (input.getType() == MT_char) 158 return ConvertValue(output, input.getChar()); 159 else if (input.getType() == MT_uchar) 160 return ConvertValue(output, input.getUnsignedChar()); 161 else if (input.getType() == MT_short) 162 return ConvertValue(output, input.getShort()); 163 else if (input.getType() == MT_ushort) 164 return ConvertValue(output, input.getUnsignedShort()); 165 else if (input.getType() == MT_long) 166 return ConvertValue(output, input.getLong()); 167 else if (input.getType() == MT_ulong) 168 return ConvertValue(output, input.getUnsignedLong()); 169 else if (input.getType() == MT_float) 170 return ConvertValue(output, input.getFloat()); 171 else if (input.getType() == MT_double) 172 return ConvertValue(output, input.getDouble()); 173 else if (input.getType() == MT_longdouble) 174 return ConvertValue(output, input.getLongDouble()); 175 else if (input.getType() == MT_bool) 176 return ConvertValue(output, input.getBool()); 177 else 178 return false; 179 } 180 }; 181 template<> 182 class Converter<MultiTypePrimitive, std::string> 183 { 184 public: 185 bool operator()(std::string* output, const MultiTypePrimitive& input) const 186 { 187 if (input.getType() == MT_int) 188 return ConvertValue(output, input.getInt()); 189 else if (input.getType() == MT_uint) 190 return ConvertValue(output, input.getUnsignedInt()); 191 else if (input.getType() == MT_char) 192 return ConvertValue(output, input.getChar()); 193 else if (input.getType() == MT_uchar) 194 return ConvertValue(output, input.getUnsignedChar()); 195 else if (input.getType() == MT_short) 196 return ConvertValue(output, input.getShort()); 197 else if (input.getType() == MT_ushort) 198 return ConvertValue(output, input.getUnsignedShort()); 199 else if (input.getType() == MT_long) 200 return ConvertValue(output, input.getLong()); 201 else if (input.getType() == MT_ulong) 202 return ConvertValue(output, input.getUnsignedLong()); 203 else if (input.getType() == MT_float) 204 return ConvertValue(output, input.getFloat()); 205 else if (input.getType() == MT_double) 206 return ConvertValue(output, input.getDouble()); 207 else if (input.getType() == MT_longdouble) 208 return ConvertValue(output, input.getLongDouble()); 209 else if (input.getType() == MT_bool) 210 return ConvertValue(output, input.getBool()); 211 else 212 return false; 213 } 214 }; 215 216 // PARTIAL SPECIALIZATION TO CONVERT FROM MULTITYPESTRING 217 template<typename ToType> 218 class Converter<MultiTypeString, ToType> 219 { 220 public: 221 bool operator()(ToType* output, const MultiTypeString& input) const 222 { 223 if (input.getType() == MT_constchar) 224 return ConvertValue(output, input.getConstChar()); 225 else if (input.getType() == MT_string) 226 return ConvertValue(output, input.getString()); 227 else 228 return ConvertValue(output, (MultiTypePrimitive)input); 229 } 230 }; 231 template<> 232 class Converter<MultiTypeString, std::string> 233 { 234 public: 235 bool operator()(std::string* output, const MultiTypeString& input) const 236 { 237 if (input.getType() == MT_constchar) 238 return ConvertValue(output, input.getConstChar()); 239 else if (input.getType() == MT_string) 240 return ConvertValue(output, input.getString()); 241 else 242 return ConvertValue(output, (MultiTypePrimitive)input); 243 } 244 }; 245 246 // PARTIAL SPECIALIZATION TO CONVERT FROM MULTITYPEMATH 247 template<typename ToType> 248 class Converter<MultiTypeMath, ToType> 249 { 250 public: 251 bool operator()(ToType* output, const MultiTypeMath& input) const 252 { 253 if (input.getType() == MT_vector2) 254 return ConvertValue(output, input.getVector2()); 255 else if (input.getType() == MT_vector3) 256 return ConvertValue(output, input.getVector3()); 257 else if (input.getType() == MT_quaternion) 258 return ConvertValue(output, input.getQuaternion()); 259 else if (input.getType() == MT_colourvalue) 260 return ConvertValue(output, input.getColourValue()); 261 else if (input.getType() == MT_radian) 262 return ConvertValue(output, input.getRadian()); 263 else if (input.getType() == MT_degree) 264 return ConvertValue(output, input.getDegree()); 265 else 266 return ConvertValue(output, (MultiTypeString)input); 267 } 268 }; 269 template<> 270 class Converter<MultiTypeMath, std::string> 271 { 272 public: 273 bool operator()(std::string* output, const MultiTypeMath& input) const 274 { 275 if (input.getType() == MT_vector2) 276 return ConvertValue(output, input.getVector2()); 277 else if (input.getType() == MT_vector3) 278 return ConvertValue(output, input.getVector3()); 279 else if (input.getType() == MT_quaternion) 280 return ConvertValue(output, input.getQuaternion()); 281 else if (input.getType() == MT_colourvalue) 282 return ConvertValue(output, input.getColourValue()); 283 else if (input.getType() == MT_radian) 284 return ConvertValue(output, input.getRadian()); 285 else if (input.getType() == MT_degree) 286 return ConvertValue(output, input.getDegree()); 287 else 288 return ConvertValue(output, (MultiTypeString)input); 313 // convert from MultiTypePrimitive 314 template <class ToType> 315 struct ConverterSpecialized<MultiTypePrimitive, ToType, _FromType_> 316 { 317 enum { specialized = true }; 318 static bool convert(ToType* output, const MultiTypePrimitive& input) 319 { 320 if (input.getType() == MT_void) 321 return ConvertValue(output, input.getVoid()); 322 else if (input.getType() == MT_int) 323 return ConvertValue(output, input.getInt()); 324 else if (input.getType() == MT_uint) 325 return ConvertValue(output, input.getUnsignedInt()); 326 else if (input.getType() == MT_char) 327 return ConvertValue(output, input.getChar()); 328 else if (input.getType() == MT_uchar) 329 return ConvertValue(output, input.getUnsignedChar()); 330 else if (input.getType() == MT_short) 331 return ConvertValue(output, input.getShort()); 332 else if (input.getType() == MT_ushort) 333 return ConvertValue(output, input.getUnsignedShort()); 334 else if (input.getType() == MT_long) 335 return ConvertValue(output, input.getLong()); 336 else if (input.getType() == MT_ulong) 337 return ConvertValue(output, input.getUnsignedLong()); 338 else if (input.getType() == MT_float) 339 return ConvertValue(output, input.getFloat()); 340 else if (input.getType() == MT_double) 341 return ConvertValue(output, input.getDouble()); 342 else if (input.getType() == MT_longdouble) 343 return ConvertValue(output, input.getLongDouble()); 344 else if (input.getType() == MT_bool) 345 return ConvertValue(output, input.getBool()); 346 else 347 return false; 348 } 349 }; 350 351 // convert from MultiTypeString 352 template <class ToType> 353 struct ConverterSpecialized<MultiTypeString, ToType, _FromType_> 354 { 355 enum { specialized = true }; 356 static bool convert(ToType* output, const MultiTypeString& input) 357 { 358 if (input.getType() == MT_constchar) 359 return ConvertValue(output, input.getConstChar()); 360 else if (input.getType() == MT_string) 361 return ConvertValue(output, input.getString()); 362 else 363 return ConvertValue(output, (MultiTypePrimitive)input); 364 } 365 }; 366 367 // convert from MultiTypeMath 368 template <class ToType> 369 struct ConverterSpecialized<MultiTypeMath, ToType, _FromType_> 370 { 371 enum { specialized = true }; 372 static bool convert(ToType* output, const MultiTypeMath& input) 373 { 374 if (input.getType() == MT_vector2) 375 return ConvertValue(output, input.getVector2()); 376 else if (input.getType() == MT_vector3) 377 return ConvertValue(output, input.getVector3()); 378 else if (input.getType() == MT_quaternion) 379 return ConvertValue(output, input.getQuaternion()); 380 else if (input.getType() == MT_colourvalue) 381 return ConvertValue(output, input.getColourValue()); 382 else if (input.getType() == MT_radian) 383 return ConvertValue(output, input.getRadian()); 384 else if (input.getType() == MT_degree) 385 return ConvertValue(output, input.getDegree()); 386 else 387 return ConvertValue(output, (MultiTypeString)input); 289 388 } 290 389 }; … … 297 396 // Vector2 to std::string 298 397 template <> 299 class Converter<orxonox::Vector2, std::string> 300 { 301 public: 302 bool operator()(std::string* output, const orxonox::Vector2& input) const 303 { 304 std::ostringstream ostream; 305 if (ostream << input.x << "," << input.y) 306 { 307 (*output) = ostream.str(); 308 return true; 309 } 310 311 return false; 398 struct ConverterSpecialized<orxonox::Vector2, std::string, _Explicit_> 399 { 400 enum { specialized = true }; 401 static bool convert(std::string* output, const orxonox::Vector2& input) 402 { 403 std::ostringstream ostream; 404 if (ostream << input.x << "," << input.y) 405 { 406 (*output) = ostream.str(); 407 return true; 408 } 409 return false; 312 410 } 313 411 }; … … 315 413 // Vector3 to std::string 316 414 template <> 317 class Converter<orxonox::Vector3, std::string> 318 { 319 public: 320 bool operator()(std::string* output, const orxonox::Vector3& input) const 321 { 322 std::ostringstream ostream; 323 if (ostream << input.x << "," << input.y << "," << input.z) 324 { 325 (*output) = ostream.str(); 326 return true; 327 } 328 329 return false; 415 struct ConverterSpecialized<orxonox::Vector3, std::string, _Explicit_> 416 { 417 enum { specialized = true }; 418 static bool convert(std::string* output, const orxonox::Vector3& input) 419 { 420 std::ostringstream ostream; 421 if (ostream << input.x << "," << input.y << "," << input.z) 422 { 423 (*output) = ostream.str(); 424 return true; 425 } 426 return false; 330 427 } 331 428 }; … … 333 430 // Vector4 to std::string 334 431 template <> 335 class Converter<orxonox::Vector4, std::string> 336 { 337 public: 338 bool operator()(std::string* output, const orxonox::Vector4& input) const 339 { 340 std::ostringstream ostream; 341 if (ostream << input.x << "," << input.y << "," << input.z << "," << input.w) 342 { 343 (*output) = ostream.str(); 344 return true; 345 } 346 347 return false; 432 struct ConverterSpecialized<orxonox::Vector4, std::string, _Explicit_> 433 { 434 enum { specialized = true }; 435 static bool convert(std::string* output, const orxonox::Vector4& input) 436 { 437 std::ostringstream ostream; 438 if (ostream << input.x << "," << input.y << "," << input.z << "," << input.w) 439 { 440 (*output) = ostream.str(); 441 return true; 442 } 443 return false; 348 444 } 349 445 }; … … 351 447 // Quaternion to std::string 352 448 template <> 353 class Converter<orxonox::Quaternion, std::string> 354 { 355 public: 356 bool operator()(std::string* output, const orxonox::Quaternion& input) const 357 { 358 std::ostringstream ostream; 359 if (ostream << input.w << "," << input.x << "," << input.y << "," << input.z) 360 { 361 (*output) = ostream.str(); 362 return true; 363 } 364 365 return false; 449 struct ConverterSpecialized<orxonox::Quaternion, std::string, _Explicit_> 450 { 451 enum { specialized = true }; 452 static bool convert(std::string* output, const orxonox::Quaternion& input) 453 { 454 std::ostringstream ostream; 455 if (ostream << input.w << "," << input.x << "," << input.y << "," << input.z) 456 { 457 (*output) = ostream.str(); 458 return true; 459 } 460 return false; 366 461 } 367 462 }; … … 369 464 // ColourValue to std::string 370 465 template <> 371 class Converter<orxonox::ColourValue, std::string> 372 { 373 public: 374 bool operator()(std::string* output, const orxonox::ColourValue& input) const 375 { 376 std::ostringstream ostream; 377 if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a) 378 { 379 (*output) = ostream.str(); 380 return true; 381 } 382 383 return false; 466 struct ConverterSpecialized<orxonox::ColourValue, std::string, _Explicit_> 467 { 468 enum { specialized = true }; 469 static bool convert(std::string* output, const orxonox::ColourValue& input) 470 { 471 std::ostringstream ostream; 472 if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a) 473 { 474 (*output) = ostream.str(); 475 return true; 476 } 477 return false; 384 478 } 385 479 }; … … 392 486 // std::string to Vector2 393 487 template <> 394 class Converter<std::string, orxonox::Vector2> 395 { 396 public: 397 bool operator()(orxonox::Vector2* output, const std::string& input) const 398 { 399 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 400 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 401 402 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', '"', '\0', '\0', '\0'); 403 404 if (tokens.size() >= 2) 405 { 406 if (!ConvertValue(&(output->x), tokens[0])) 407 return false; 408 if (!ConvertValue(&(output->y), tokens[1])) 409 return false; 410 411 return true; 412 } 413 414 return false; 488 struct ConverterSpecialized<std::string, orxonox::Vector2, _Explicit_> 489 { 490 enum { specialized = true }; 491 static bool convert(orxonox::Vector2* output, const std::string& input) 492 { 493 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 494 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 495 496 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 497 if (tokens.size() >= 2) 498 { 499 if (!ConvertValue(&(output->x), tokens[0])) 500 return false; 501 if (!ConvertValue(&(output->y), tokens[1])) 502 return false; 503 504 return true; 505 } 506 return false; 415 507 } 416 508 }; … … 418 510 // std::string to Vector3 419 511 template <> 420 class Converter<std::string, orxonox::Vector3> 421 { 422 public: 423 bool operator()(orxonox::Vector3* output, const std::string& input) const 424 { 425 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 426 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 427 428 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', '"', '\0', '\0', '\0'); 429 430 if (tokens.size() >= 3) 431 { 432 if (!ConvertValue(&(output->x), tokens[0])) 433 return false; 434 if (!ConvertValue(&(output->y), tokens[1])) 435 return false; 436 if (!ConvertValue(&(output->z), tokens[2])) 437 return false; 438 439 return true; 440 } 441 442 return false; 512 struct ConverterSpecialized<std::string, orxonox::Vector3, _Explicit_> 513 { 514 enum { specialized = true }; 515 static bool convert(orxonox::Vector3* output, const std::string& input) 516 { 517 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 518 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 519 520 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 521 if (tokens.size() >= 3) 522 { 523 if (!ConvertValue(&(output->x), tokens[0])) 524 return false; 525 if (!ConvertValue(&(output->y), tokens[1])) 526 return false; 527 if (!ConvertValue(&(output->z), tokens[2])) 528 return false; 529 530 return true; 531 } 532 return false; 443 533 } 444 534 }; … … 446 536 // std::string to Vector4 447 537 template <> 448 class Converter<std::string, orxonox::Vector4> 449 { 450 public: 451 bool operator()(orxonox::Vector4* output, const std::string& input) const 452 { 453 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 454 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 455 456 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', '"', '\0', '\0', '\0'); 457 458 if (tokens.size() >= 4) 459 { 460 if (!ConvertValue(&(output->x), tokens[0])) 461 return false; 462 if (!ConvertValue(&(output->y), tokens[1])) 463 return false; 464 if (!ConvertValue(&(output->z), tokens[2])) 465 return false; 466 if (!ConvertValue(&(output->w), tokens[3])) 467 return false; 468 469 return true; 470 } 471 472 return false; 538 struct ConverterSpecialized<std::string, orxonox::Vector4, _Explicit_> 539 { 540 enum { specialized = true }; 541 static bool convert(orxonox::Vector4* output, const std::string& input) 542 { 543 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 544 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 545 546 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 547 if (tokens.size() >= 4) 548 { 549 if (!ConvertValue(&(output->x), tokens[0])) 550 return false; 551 if (!ConvertValue(&(output->y), tokens[1])) 552 return false; 553 if (!ConvertValue(&(output->z), tokens[2])) 554 return false; 555 if (!ConvertValue(&(output->w), tokens[3])) 556 return false; 557 558 return true; 559 } 560 return false; 473 561 } 474 562 }; … … 476 564 // std::string to Quaternion 477 565 template <> 478 class Converter<std::string, orxonox::Quaternion> 479 { 480 public: 481 bool operator()(orxonox::Quaternion* output, const std::string& input) const 482 { 483 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 484 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 485 486 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', '"', '\0', '\0', '\0'); 487 488 if (tokens.size() >= 4) 489 { 490 if (!ConvertValue(&(output->w), tokens[0])) 491 return false; 492 if (!ConvertValue(&(output->x), tokens[1])) 493 return false; 494 if (!ConvertValue(&(output->y), tokens[2])) 495 return false; 496 if (!ConvertValue(&(output->z), tokens[3])) 497 return false; 498 499 return true; 500 } 501 502 return false; 566 struct ConverterSpecialized<std::string, orxonox::Quaternion, _Explicit_> 567 { 568 enum { specialized = true }; 569 static bool convert(orxonox::Quaternion* output, const std::string& input) 570 { 571 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 572 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 573 574 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 575 if (tokens.size() >= 4) 576 { 577 if (!ConvertValue(&(output->w), tokens[0])) 578 return false; 579 if (!ConvertValue(&(output->x), tokens[1])) 580 return false; 581 if (!ConvertValue(&(output->y), tokens[2])) 582 return false; 583 if (!ConvertValue(&(output->z), tokens[3])) 584 return false; 585 586 return true; 587 } 588 return false; 503 589 } 504 590 }; … … 506 592 // std::string to ColourValue 507 593 template <> 508 class Converter<std::string, orxonox::ColourValue> 509 { 510 public: 511 bool operator()(orxonox::ColourValue* output, const std::string& input) const 512 { 513 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 514 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 515 516 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', '"', '\0', '\0', '\0'); 517 518 if (tokens.size() >= 4) 519 { 520 if (!ConvertValue(&(output->r), tokens[0])) 521 return false; 522 if (!ConvertValue(&(output->g), tokens[1])) 523 return false; 524 if (!ConvertValue(&(output->b), tokens[2])) 525 return false; 526 if (!ConvertValue(&(output->a), tokens[3])) 527 return false; 528 529 return true; 530 } 531 532 return false; 594 struct ConverterSpecialized<std::string, orxonox::ColourValue, _Explicit_> 595 { 596 enum { specialized = true }; 597 static bool convert(orxonox::ColourValue* output, const std::string& input) 598 { 599 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 600 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 601 602 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 603 if (tokens.size() >= 4) 604 { 605 if (!ConvertValue(&(output->r), tokens[0])) 606 return false; 607 if (!ConvertValue(&(output->g), tokens[1])) 608 return false; 609 if (!ConvertValue(&(output->b), tokens[2])) 610 return false; 611 if (!ConvertValue(&(output->a), tokens[3])) 612 return false; 613 614 return true; 615 } 616 return false; 533 617 } 534 618 }; -
code/trunk/src/util/Math.h
r892 r1052 26 26 */ 27 27 28 #ifndef _ Math_H__29 #define _ Math_H__28 #ifndef _Util_Math_H__ 29 #define _Util_Math_H__ 30 30 31 31 #include <ostream> … … 146 146 } 147 147 148 #endif /* _Math_H__ */ 149 148 #endif /* _Util_Math_H__ */ -
code/trunk/src/util/MultiType.h
r871 r1052 35 35 { 36 36 MT_null, 37 MT_void, 37 38 MT_int, 38 39 MT_uint, … … 49 50 MT_constchar, 50 51 MT_string, 52 MT_xmlelement, 51 53 MT_vector2, 52 54 MT_vector3, … … 59 61 union _UtilExport MultiTypeValue 60 62 { 63 void* void_; 61 64 int int_; 62 65 unsigned int uint_; -
code/trunk/src/util/MultiTypeMath.cc
r871 r1052 88 88 } 89 89 90 MultiTypeMath::operator void*() const 91 { return (this->type_ == MT_void) ? this->value_.void_ : getConvertedValue<MultiTypeMath, void*>(*this, 0); } 90 92 MultiTypeMath::operator int() const 91 { return (this->type_ == MT_int) ? this->value_.int_ : ConvertValueAndReturn<MultiTypeMath, int>(*this); }93 { return (this->type_ == MT_int) ? this->value_.int_ : getConvertedValue<MultiTypeMath, int>(*this, 0); } 92 94 MultiTypeMath::operator unsigned int() const 93 { return (this->type_ == MT_uint) ? this->value_.uint_ : ConvertValueAndReturn<MultiTypeMath, unsigned int>(*this); }95 { return (this->type_ == MT_uint) ? this->value_.uint_ : getConvertedValue<MultiTypeMath, unsigned int>(*this, 0); } 94 96 MultiTypeMath::operator char() const 95 { return (this->type_ == MT_char) ? this->value_.char_ : ConvertValueAndReturn<MultiTypeMath, char>(*this); }97 { return (this->type_ == MT_char) ? this->value_.char_ : getConvertedValue<MultiTypeMath, char>(*this, 0); } 96 98 MultiTypeMath::operator unsigned char() const 97 { return (this->type_ == MT_uchar) ? this->value_.uchar_ : ConvertValueAndReturn<MultiTypeMath, unsigned char>(*this); }99 { return (this->type_ == MT_uchar) ? this->value_.uchar_ : getConvertedValue<MultiTypeMath, unsigned char>(*this, 0); } 98 100 MultiTypeMath::operator short() const 99 { return (this->type_ == MT_short) ? this->value_.short_ : ConvertValueAndReturn<MultiTypeMath, short>(*this); }101 { return (this->type_ == MT_short) ? this->value_.short_ : getConvertedValue<MultiTypeMath, short>(*this, 0); } 100 102 MultiTypeMath::operator unsigned short() const 101 { return (this->type_ == MT_ushort) ? this->value_.ushort_ : ConvertValueAndReturn<MultiTypeMath, unsigned short>(*this); }103 { return (this->type_ == MT_ushort) ? this->value_.ushort_ : getConvertedValue<MultiTypeMath, unsigned short>(*this, 0); } 102 104 MultiTypeMath::operator long() const 103 { return (this->type_ == MT_long) ? this->value_.long_ : ConvertValueAndReturn<MultiTypeMath, long>(*this); }105 { return (this->type_ == MT_long) ? this->value_.long_ : getConvertedValue<MultiTypeMath, long>(*this, 0); } 104 106 MultiTypeMath::operator unsigned long() const 105 { return (this->type_ == MT_ulong) ? this->value_.ulong_ : ConvertValueAndReturn<MultiTypeMath, unsigned long>(*this); }107 { return (this->type_ == MT_ulong) ? this->value_.ulong_ : getConvertedValue<MultiTypeMath, unsigned long>(*this, 0); } 106 108 MultiTypeMath::operator float() const 107 { return (this->type_ == MT_float) ? this->value_.float_ : ConvertValueAndReturn<MultiTypeMath, float>(*this); }109 { return (this->type_ == MT_float) ? this->value_.float_ : getConvertedValue<MultiTypeMath, float>(*this, 0); } 108 110 MultiTypeMath::operator double() const 109 { return (this->type_ == MT_double) ? this->value_.double_ : ConvertValueAndReturn<MultiTypeMath, double>(*this); }111 { return (this->type_ == MT_double) ? this->value_.double_ : getConvertedValue<MultiTypeMath, double>(*this, 0); } 110 112 MultiTypeMath::operator long double() const 111 { return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : ConvertValueAndReturn<MultiTypeMath, long double>(*this); }113 { return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : getConvertedValue<MultiTypeMath, long double>(*this, 0); } 112 114 MultiTypeMath::operator bool() const 113 { return (this->type_ == MT_bool) ? this->value_.bool_ : ConvertValueAndReturn<MultiTypeMath, bool>(*this); }115 { return (this->type_ == MT_bool) ? this->value_.bool_ : getConvertedValue<MultiTypeMath, bool>(*this, 0); } 114 116 MultiTypeMath::operator std::string() const 115 { return (this->type_ == MT_string) ? this->string_ : ConvertValueAndReturn<MultiTypeMath, std::string>(*this); }117 { return (this->type_ == MT_string) ? this->string_ : getConvertedValue<MultiTypeMath, std::string>(*this); } 116 118 MultiTypeMath::operator const char*() const 117 { return ((this->type_ == MT_constchar) ? this->string_ : ConvertValueAndReturn<MultiTypeMath, std::string>(*this)).c_str(); }119 { return ((this->type_ == MT_constchar) ? this->string_ : getConvertedValue<MultiTypeMath, std::string>(*this)).c_str(); } 118 120 MultiTypeMath::operator orxonox::Vector2() const 119 { return (this->type_ == MT_vector2) ? this->vector2_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Vector2>(*this); }121 { return (this->type_ == MT_vector2) ? this->vector2_ : getConvertedValue<MultiTypeMath, orxonox::Vector2>(*this); } 120 122 MultiTypeMath::operator orxonox::Vector3() const 121 { return (this->type_ == MT_vector3) ? this->vector3_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Vector3>(*this); }123 { return (this->type_ == MT_vector3) ? this->vector3_ : getConvertedValue<MultiTypeMath, orxonox::Vector3>(*this); } 122 124 MultiTypeMath::operator orxonox::Quaternion() const 123 { return (this->type_ == MT_quaternion) ? this->quaternion_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Quaternion>(*this); }125 { return (this->type_ == MT_quaternion) ? this->quaternion_ : getConvertedValue<MultiTypeMath, orxonox::Quaternion>(*this); } 124 126 MultiTypeMath::operator orxonox::ColourValue() const 125 { return (this->type_ == MT_colourvalue) ? this->colourvalue_ : ConvertValueAndReturn<MultiTypeMath, orxonox::ColourValue>(*this); }127 { return (this->type_ == MT_colourvalue) ? this->colourvalue_ : getConvertedValue<MultiTypeMath, orxonox::ColourValue>(*this); } 126 128 MultiTypeMath::operator orxonox::Radian() const 127 { return (this->type_ == MT_radian) ? this->radian_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Radian>(*this); }129 { return (this->type_ == MT_radian) ? this->radian_ : getConvertedValue<MultiTypeMath, orxonox::Radian>(*this); } 128 130 MultiTypeMath::operator orxonox::Degree() const 129 { return (this->type_ == MT_degree) ? this->degree_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Degree>(*this); }131 { return (this->type_ == MT_degree) ? this->degree_ : getConvertedValue<MultiTypeMath, orxonox::Degree>(*this); } 130 132 131 133 void MultiTypeMath::setValue(const MultiTypeMath& mtm) … … 138 140 this->radian_ = mtm.radian_; 139 141 this->degree_ = mtm.degree_; 142 } 143 144 std::string MultiTypeMath::getTypename() const 145 { 146 if (this->type_ == MT_vector2) 147 return "Vector2"; 148 else if (this->type_ == MT_vector3) 149 return "Vector3"; 150 else if (this->type_ == MT_colourvalue) 151 return "ColourValue"; 152 else if (this->type_ == MT_quaternion) 153 return "Quaternion"; 154 else if (this->type_ == MT_radian) 155 return "Radian"; 156 else if (this->type_ == MT_degree) 157 return "Degree"; 158 else 159 return MultiTypeString::getTypename(); 140 160 } 141 161 -
code/trunk/src/util/MultiTypeMath.h
r890 r1052 38 38 { 39 39 public: 40 MultiTypeMath(MultiType type = MT_null); 40 MultiTypeMath(MultiType type = MT_null); 41 inline MultiTypeMath(void* value) : MultiTypeString(value) {} 41 42 inline MultiTypeMath(int value) : MultiTypeString(value) {} 42 43 inline MultiTypeMath(unsigned int value) : MultiTypeString(value) {} … … 51 52 inline MultiTypeMath(long double value) : MultiTypeString(value) {} 52 53 inline MultiTypeMath(bool value) : MultiTypeString(value) {} 53 inline MultiTypeMath(const char* value) : MultiTypeString(value) {}54 inline MultiTypeMath(const std::string& value) : MultiTypeString(value) {}54 inline MultiTypeMath(const char* value) : MultiTypeString(value) {} 55 inline MultiTypeMath(const std::string& value) : MultiTypeString(value) {} 55 56 inline MultiTypeMath(const orxonox::Vector2& value) { this->setValue(value); } 56 57 inline MultiTypeMath(const orxonox::Vector3& value) { this->setValue(value); } … … 89 90 bool operator!=(const MultiTypeMath& mtm) const; 90 91 92 virtual operator void*() const; 91 93 virtual operator int() const; 92 94 virtual operator unsigned int() const; … … 141 143 inline void getValue(orxonox::Degree* variable) const { (*variable) = orxonox::Degree (this->degree_); } 142 144 145 virtual std::string getTypename() const; 146 143 147 virtual std::string toString() const; 144 148 virtual bool fromString(const std::string value); -
code/trunk/src/util/MultiTypePrimitive.cc
r871 r1052 34 34 this->type_ = type; 35 35 36 if (type == MT_int) 36 if (type == MT_void) 37 this->value_.void_ = 0; 38 else if (type == MT_int) 37 39 this->value_.int_ = 0; 38 40 else if (type == MT_uint) … … 59 61 this->value_.bool_ = false; 60 62 else 61 this->value_. int_ = 0;63 this->value_.void_ = 0; 62 64 } 63 65 … … 66 68 if (this->type_ == mtp.type_) 67 69 { 68 if (this->type_ == MT_int) 70 if (this->type_ == MT_void) 71 return (this->value_.void_ == mtp.value_.void_); 72 else if (this->type_ == MT_int) 69 73 return (this->value_.int_ == mtp.value_.int_); 70 74 else if (this->type_ == MT_uint) … … 99 103 if (this->type_ == mtp.type_) 100 104 { 101 if (this->type_ == MT_int) 105 if (this->type_ == MT_void) 106 return (this->value_.void_ != mtp.value_.void_); 107 else if (this->type_ == MT_int) 102 108 return (this->value_.int_ != mtp.value_.int_); 103 109 else if (this->type_ == MT_uint) … … 128 134 } 129 135 136 MultiTypePrimitive::operator void*() const 137 { return (this->type_ == MT_void) ? this->value_.void_ : getConvertedValue<MultiTypePrimitive, void*>(*this, 0); } 130 138 MultiTypePrimitive::operator int() const 131 { return (this->type_ == MT_int) ? this->value_.int_ : ConvertValueAndReturn<MultiTypePrimitive, int>(*this); }139 { return (this->type_ == MT_int) ? this->value_.int_ : getConvertedValue<MultiTypePrimitive, int>(*this, 0); } 132 140 MultiTypePrimitive::operator unsigned int() const 133 { return (this->type_ == MT_uint) ? this->value_.uint_ : ConvertValueAndReturn<MultiTypePrimitive, unsigned int>(*this); }141 { return (this->type_ == MT_uint) ? this->value_.uint_ : getConvertedValue<MultiTypePrimitive, unsigned int>(*this, 0); } 134 142 MultiTypePrimitive::operator char() const 135 { return (this->type_ == MT_char) ? this->value_.char_ : ConvertValueAndReturn<MultiTypePrimitive, char>(*this); }143 { return (this->type_ == MT_char) ? this->value_.char_ : getConvertedValue<MultiTypePrimitive, char>(*this, 0); } 136 144 MultiTypePrimitive::operator unsigned char() const 137 { return (this->type_ == MT_uchar) ? this->value_.uchar_ : ConvertValueAndReturn<MultiTypePrimitive, unsigned char>(*this); }145 { return (this->type_ == MT_uchar) ? this->value_.uchar_ : getConvertedValue<MultiTypePrimitive, unsigned char>(*this, 0); } 138 146 MultiTypePrimitive::operator short() const 139 { return (this->type_ == MT_short) ? this->value_.short_ : ConvertValueAndReturn<MultiTypePrimitive, short>(*this); }147 { return (this->type_ == MT_short) ? this->value_.short_ : getConvertedValue<MultiTypePrimitive, short>(*this, 0); } 140 148 MultiTypePrimitive::operator unsigned short() const 141 { return (this->type_ == MT_ushort) ? this->value_.ushort_ : ConvertValueAndReturn<MultiTypePrimitive, unsigned short>(*this); }149 { return (this->type_ == MT_ushort) ? this->value_.ushort_ : getConvertedValue<MultiTypePrimitive, unsigned short>(*this, 0); } 142 150 MultiTypePrimitive::operator long() const 143 { return (this->type_ == MT_long) ? this->value_.long_ : ConvertValueAndReturn<MultiTypePrimitive, long>(*this); }151 { return (this->type_ == MT_long) ? this->value_.long_ : getConvertedValue<MultiTypePrimitive, long>(*this, 0); } 144 152 MultiTypePrimitive::operator unsigned long() const 145 { return (this->type_ == MT_ulong) ? this->value_.ulong_ : ConvertValueAndReturn<MultiTypePrimitive, unsigned long>(*this); }153 { return (this->type_ == MT_ulong) ? this->value_.ulong_ : getConvertedValue<MultiTypePrimitive, unsigned long>(*this, 0); } 146 154 MultiTypePrimitive::operator float() const 147 { return (this->type_ == MT_float) ? this->value_.float_ : ConvertValueAndReturn<MultiTypePrimitive, float>(*this); }155 { return (this->type_ == MT_float) ? this->value_.float_ : getConvertedValue<MultiTypePrimitive, float>(*this, 0); } 148 156 MultiTypePrimitive::operator double() const 149 { return (this->type_ == MT_double) ? this->value_.double_ : ConvertValueAndReturn<MultiTypePrimitive, double>(*this); }157 { return (this->type_ == MT_double) ? this->value_.double_ : getConvertedValue<MultiTypePrimitive, double>(*this, 0); } 150 158 MultiTypePrimitive::operator long double() const 151 { return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : ConvertValueAndReturn<MultiTypePrimitive, long double>(*this); }159 { return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : getConvertedValue<MultiTypePrimitive, long double>(*this, 0); } 152 160 MultiTypePrimitive::operator bool() const 153 { return (this->type_ == MT_bool) ? this->value_.bool_ : ConvertValueAndReturn<MultiTypePrimitive, bool>(*this); }161 { return (this->type_ == MT_bool) ? this->value_.bool_ : getConvertedValue<MultiTypePrimitive, bool>(*this, 0); } 154 162 155 163 void MultiTypePrimitive::setValue(const MultiTypePrimitive& mtp) … … 159 167 } 160 168 169 std::string MultiTypePrimitive::getTypename() const 170 { 171 if (this->type_ == MT_void) 172 return "pointer"; 173 else if (this->type_ == MT_int) 174 return "int"; 175 else if (this->type_ == MT_uint) 176 return "unsigned int"; 177 else if (this->type_ == MT_char) 178 return "char"; 179 else if (this->type_ == MT_uchar) 180 return "unsigned char"; 181 else if (this->type_ == MT_short) 182 return "short"; 183 else if (this->type_ == MT_ushort) 184 return "unsigned short"; 185 else if (this->type_ == MT_long) 186 return "long"; 187 else if (this->type_ == MT_ulong) 188 return "unsigned long"; 189 else if (this->type_ == MT_float) 190 return "float"; 191 else if (this->type_ == MT_double) 192 return "double"; 193 else if (this->type_ == MT_longdouble) 194 return "long double"; 195 else if (this->type_ == MT_bool) 196 return "bool"; 197 else 198 return "unknown"; 199 } 200 161 201 std::string MultiTypePrimitive::toString() const 162 202 { 163 203 std::string output; 164 204 165 if (this->type_ == MT_int) 205 if (this->type_ == MT_void) 206 ConvertValue(&output, this->value_.void_); 207 else if (this->type_ == MT_int) 166 208 ConvertValue(&output, this->value_.int_); 167 209 else if (this->type_ == MT_uint) … … 193 235 bool MultiTypePrimitive::fromString(const std::string value) 194 236 { 195 if (this->type_ == MT_int) 237 if (this->type_ == MT_void) 238 return ConvertValue(&this->value_.void_, value, (void*)0); 239 else if (this->type_ == MT_int) 196 240 return ConvertValue(&this->value_.int_, value, (int)0); 197 241 else if (this->type_ == MT_uint) -
code/trunk/src/util/MultiTypePrimitive.h
r890 r1052 36 36 #include "MultiType.h" 37 37 38 namespace orxonox 39 { 40 class BaseObject; 41 } 38 42 class _UtilExport MultiTypePrimitive 39 43 { 40 44 public: 41 MultiTypePrimitive(MultiType type = MT_null); 45 MultiTypePrimitive(MultiType type = MT_null); 46 inline MultiTypePrimitive(void* value) { this->setValue(value); } 42 47 inline MultiTypePrimitive(int value) { this->setValue(value); } 43 48 inline MultiTypePrimitive(unsigned int value) { this->setValue(value); } … … 56 61 57 62 inline MultiTypePrimitive& operator=(MultiType value) { this->type_ = MT_null; return *this; } 63 inline MultiTypePrimitive& operator=(void* value) { this->setValue(value); return *this; } 58 64 inline MultiTypePrimitive& operator=(int value) { this->setValue(value); return *this; } 59 65 inline MultiTypePrimitive& operator=(unsigned int value) { this->setValue(value); return *this; } … … 70 76 inline MultiTypePrimitive& operator=(const MultiTypePrimitive& mtp) { this->setValue(mtp); return *this; } 71 77 78 inline bool operator==(void* value) const { return (this->value_.void_ == value); } 72 79 inline bool operator==(int value) const { return (this->value_.int_ == value); } 73 80 inline bool operator==(unsigned int value) const { return (this->value_.uint_ == value); } … … 84 91 bool operator==(const MultiTypePrimitive& mtp) const; 85 92 86 inline bool operator!=( int value) const { return (this->value_.int_!= value); }93 inline bool operator!=(void* value) const { return (this->value_.void_ != value); } 87 94 inline bool operator!=(unsigned int value) const { return (this->value_.uint_ != value); } 88 95 inline bool operator!=(char value) const { return (this->value_.char_ != value); } … … 98 105 bool operator!=(const MultiTypePrimitive& mtp) const; 99 106 107 template <class T> 108 operator T*() const 109 { return ((T*)this->value_.void_); } 110 virtual operator void*() const; 100 111 virtual operator int() const; 101 112 virtual operator unsigned int() const; … … 111 122 virtual operator bool() const; 112 123 124 inline void setValue(void* value) { this->type_ = MT_void; this->value_.void_ = value; } 113 125 inline void setValue(int value) { this->type_ = MT_int; this->value_.int_ = value; } 114 126 inline void setValue(unsigned int value) { this->type_ = MT_uint; this->value_.uint_ = value; } … … 125 137 void setValue(const MultiTypePrimitive& mtp); 126 138 139 inline void* getVoid() const { return this->value_.void_; } 127 140 inline int getInt() const { return this->value_.int_; } 128 141 inline unsigned int getUnsignedInt() const { return this->value_.uint_; } … … 151 164 inline bool& getBool() { return this->value_.bool_; } 152 165 166 inline void getValue(void* variable) const { variable = this->value_.void_; } 153 167 inline void getValue(int* variable) const { (*variable) = this->value_.int_; } 154 168 inline void getValue(unsigned int* variable) const { (*variable) = this->value_.uint_; } … … 167 181 inline bool isA(MultiType type) const { return (this->type_ == type); } 168 182 183 virtual std::string getTypename() const; 184 169 185 virtual std::string toString() const; 170 186 virtual bool fromString(const std::string value); -
code/trunk/src/util/MultiTypeString.cc
r871 r1052 32 32 MultiTypeString::MultiTypeString(MultiType type) : MultiTypePrimitive(type) 33 33 { 34 if (type == MT_constchar) 35 this->string_ = std::string(""); 36 else if (type == MT_string) 37 this->string_ = std::string(""); 34 // Nothing to do for string and xml-element 38 35 } 39 36 … … 64 61 } 65 62 63 MultiTypeString::operator void*() const 64 { return (this->type_ == MT_void) ? this->value_.void_ : getConvertedValue<MultiTypeString, void*>(*this, 0); } 66 65 MultiTypeString::operator int() const 67 { return (this->type_ == MT_int) ? this->value_.int_ : ConvertValueAndReturn<MultiTypeString, int>(*this); }66 { return (this->type_ == MT_int) ? this->value_.int_ : getConvertedValue<MultiTypeString, int>(*this, 0); } 68 67 MultiTypeString::operator unsigned int() const 69 { return (this->type_ == MT_uint) ? this->value_.uint_ : ConvertValueAndReturn<MultiTypeString, unsigned int>(*this); }68 { return (this->type_ == MT_uint) ? this->value_.uint_ : getConvertedValue<MultiTypeString, unsigned int>(*this, 0); } 70 69 MultiTypeString::operator char() const 71 { return (this->type_ == MT_char) ? this->value_.char_ : ConvertValueAndReturn<MultiTypeString, char>(*this); }70 { return (this->type_ == MT_char) ? this->value_.char_ : getConvertedValue<MultiTypeString, char>(*this, 0); } 72 71 MultiTypeString::operator unsigned char() const 73 { return (this->type_ == MT_uchar) ? this->value_.uchar_ : ConvertValueAndReturn<MultiTypeString, unsigned char>(*this); }72 { return (this->type_ == MT_uchar) ? this->value_.uchar_ : getConvertedValue<MultiTypeString, unsigned char>(*this, 0); } 74 73 MultiTypeString::operator short() const 75 { return (this->type_ == MT_short) ? this->value_.short_ : ConvertValueAndReturn<MultiTypeString, short>(*this); }74 { return (this->type_ == MT_short) ? this->value_.short_ : getConvertedValue<MultiTypeString, short>(*this, 0); } 76 75 MultiTypeString::operator unsigned short() const 77 { return (this->type_ == MT_ushort) ? this->value_.ushort_ : ConvertValueAndReturn<MultiTypeString, unsigned short>(*this); }76 { return (this->type_ == MT_ushort) ? this->value_.ushort_ : getConvertedValue<MultiTypeString, unsigned short>(*this, 0); } 78 77 MultiTypeString::operator long() const 79 { return (this->type_ == MT_long) ? this->value_.long_ : ConvertValueAndReturn<MultiTypeString, long>(*this); }78 { return (this->type_ == MT_long) ? this->value_.long_ : getConvertedValue<MultiTypeString, long>(*this, 0); } 80 79 MultiTypeString::operator unsigned long() const 81 { return (this->type_ == MT_ulong) ? this->value_.ulong_ : ConvertValueAndReturn<MultiTypeString, unsigned long>(*this); }80 { return (this->type_ == MT_ulong) ? this->value_.ulong_ : getConvertedValue<MultiTypeString, unsigned long>(*this, 0); } 82 81 MultiTypeString::operator float() const 83 { return (this->type_ == MT_float) ? this->value_.float_ : ConvertValueAndReturn<MultiTypeString, float>(*this); }82 { return (this->type_ == MT_float) ? this->value_.float_ : getConvertedValue<MultiTypeString, float>(*this, 0); } 84 83 MultiTypeString::operator double() const 85 { return (this->type_ == MT_double) ? this->value_.double_ : ConvertValueAndReturn<MultiTypeString, double>(*this); }84 { return (this->type_ == MT_double) ? this->value_.double_ : getConvertedValue<MultiTypeString, double>(*this, 0); } 86 85 MultiTypeString::operator long double() const 87 { return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : ConvertValueAndReturn<MultiTypeString, long double>(*this); }86 { return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : getConvertedValue<MultiTypeString, long double>(*this, 0); } 88 87 MultiTypeString::operator bool() const 89 { return (this->type_ == MT_bool) ? this->value_.bool_ : ConvertValueAndReturn<MultiTypeString, bool>(*this); }88 { return (this->type_ == MT_bool) ? this->value_.bool_ : getConvertedValue<MultiTypeString, bool>(*this, 0); } 90 89 MultiTypeString::operator std::string() const 91 { return (this->type_ == MT_string) ? this->string_ : ConvertValueAndReturn<MultiTypeString, std::string>(*this); }90 { return (this->type_ == MT_string) ? this->string_ : getConvertedValue<MultiTypeString, std::string>(*this); } 92 91 MultiTypeString::operator const char*() const 93 { return ((this->type_ == MT_constchar) ? this->string_ : ConvertValueAndReturn<MultiTypeString, std::string>(*this)).c_str(); }92 { return ((this->type_ == MT_constchar) ? this->string_ : getConvertedValue<MultiTypeString, std::string>(*this)).c_str(); } 94 93 95 94 void MultiTypeString::setValue(const MultiTypeString& mts) … … 99 98 } 100 99 100 std::string MultiTypeString::getTypename() const 101 { 102 if (this->type_ == MT_constchar) 103 return "string"; 104 else if (this->type_ == MT_string) 105 return "string"; 106 else 107 return MultiTypePrimitive::getTypename(); 108 } 109 101 110 std::string MultiTypeString::toString() const 102 111 { 112 std::string output; 113 103 114 if (this->type_ == MT_constchar) 104 115 return this->string_; … … 107 118 else 108 119 return MultiTypePrimitive::toString(); 120 121 return output; 109 122 } 110 123 -
code/trunk/src/util/MultiTypeString.h
r890 r1052 39 39 { 40 40 public: 41 MultiTypeString(MultiType type = MT_null); 41 MultiTypeString(MultiType type = MT_null); 42 inline MultiTypeString(void* value) : MultiTypePrimitive(value) {} 42 43 inline MultiTypeString(int value) : MultiTypePrimitive(value) {} 43 44 inline MultiTypeString(unsigned int value) : MultiTypePrimitive(value) {} … … 52 53 inline MultiTypeString(long double value) : MultiTypePrimitive(value) {} 53 54 inline MultiTypeString(bool value) : MultiTypePrimitive(value) {} 54 inline MultiTypeString(const char* value) { this->setValue(value); }55 inline MultiTypeString(const std::string& value) { this->setValue(value); }56 inline MultiTypeString(const MultiTypeString& mts) { this->setValue(mts); }55 inline MultiTypeString(const char* value) { this->setValue(value); } 56 inline MultiTypeString(const std::string& value) { this->setValue(value); } 57 inline MultiTypeString(const MultiTypeString& mts) { this->setValue(mts); } 57 58 virtual inline ~MultiTypeString() {} 58 59 59 60 using MultiTypePrimitive::operator=; 60 inline MultiTypeString& operator=(const char* value) { this->setValue(value); return *this; }61 inline MultiTypeString& operator=(const std::string& value) { this->setValue(value); return *this; }62 inline MultiTypeString& operator=(const MultiTypeString& mts) { this->setValue(mts); return *this; }61 inline MultiTypeString& operator=(const char* value) { this->setValue(value); return *this; } 62 inline MultiTypeString& operator=(const std::string& value) { this->setValue(value); return *this; } 63 inline MultiTypeString& operator=(const MultiTypeString& mts) { this->setValue(mts); return *this; } 63 64 64 65 using MultiTypePrimitive::operator==; 65 inline bool operator==(const char* value) const { return (this->string_== std::string(value)); }66 inline bool operator==(const std::string& value) const { return (this->string_== value); }66 inline bool operator==(const char* value) const { return (this->string_ == std::string(value)); } 67 inline bool operator==(const std::string& value) const { return (this->string_ == value); } 67 68 bool operator==(const MultiTypeString& mts) const; 68 69 69 70 using MultiTypePrimitive::operator!=; 70 inline bool operator!=(const char* value) const { return (this->string_!= std::string(value)); }71 inline bool operator!=(const std::string& value) const { return (this->string_!= value); }71 inline bool operator!=(const char* value) const { return (this->string_ != std::string(value)); } 72 inline bool operator!=(const std::string& value) const { return (this->string_ != value); } 72 73 bool operator!=(const MultiTypeString& mts) const; 73 74 74 virtual operator int() const; 75 virtual operator unsigned int() const; 76 virtual operator char() const; 77 virtual operator unsigned char() const; 78 virtual operator short() const; 79 virtual operator unsigned short() const; 80 virtual operator long() const; 81 virtual operator unsigned long() const; 82 virtual operator float () const; 83 virtual operator double () const; 84 virtual operator long double() const; 85 virtual operator bool() const; 86 virtual operator std::string() const; 87 virtual operator const char*() const; 75 virtual operator void*() const; 76 virtual operator int() const; 77 virtual operator unsigned int() const; 78 virtual operator char() const; 79 virtual operator unsigned char() const; 80 virtual operator short() const; 81 virtual operator unsigned short() const; 82 virtual operator long() const; 83 virtual operator unsigned long() const; 84 virtual operator float () const; 85 virtual operator double () const; 86 virtual operator long double() const; 87 virtual operator bool() const; 88 virtual operator std::string() const; 89 virtual operator const char*() const; 88 90 89 91 using MultiTypePrimitive::setValue; 90 inline void setValue(const char* value) { this->type_ = MT_string; this->string_= std::string(value); }91 inline void setValue(const std::string& value) { this->type_ = MT_string; this->string_= value; }92 inline void setValue(const char* value) { this->type_ = MT_string; this->string_ = std::string(value); } 93 inline void setValue(const std::string& value) { this->type_ = MT_string; this->string_ = value; } 92 94 void setValue(const MultiTypeString& mts); 93 95 94 inline const std::string getString()const { return this->string_; }95 inline const char* getConstChar() const { return this->string_.c_str(); }96 inline std::string getString() const { return this->string_; } 97 inline const char* getConstChar() const { return this->string_.c_str(); } 96 98 97 inline const std::string& getString(){ return this->string_; }98 inline const char* getConstChar() { return this->string_.c_str(); }99 inline std::string& getString() { return this->string_; } 100 inline const char* getConstChar() { return this->string_.c_str(); } 99 101 100 102 using MultiTypePrimitive::getValue; 101 inline void getValue(std::string* variable) const { (*variable) = this->string_; } 102 inline void getValue(const char** variable) const { (*variable) = this->string_.c_str(); } 103 inline void getValue(std::string* variable) const { (*variable) = this->string_; } 104 inline void getValue(const char** variable) const { (*variable) = this->string_.c_str(); } 105 106 virtual std::string getTypename() const; 103 107 104 108 virtual std::string toString() const; … … 106 110 107 111 protected: 108 std::string string_;112 std::string string_; 109 113 }; 110 114 -
code/trunk/src/util/String.cc
r871 r1052 26 26 */ 27 27 28 #include <cctype> 29 #include <iostream> 28 30 #include "String.h" 29 31 … … 54 56 55 57 /** 58 @brief Returns a copy of a string without trailing whitespaces. 59 @param str The string 60 @return The modified copy 61 */ 62 std::string removeTrailingWhitespaces(const std::string& str) 63 { 64 unsigned int pos1 = 0; 65 unsigned int pos2 = str.size() - 1; 66 for (; pos1 < str.size() && (str[pos1] == ' ' || str[pos1] == '\t' || str[pos1] == '\n'); pos1++); 67 for (; pos2 >= 0 && (str[pos2] == ' ' || str[pos2] == '\t' || str[pos2] == '\n'); pos2--); 68 return str.substr(pos1, pos2 - pos1 + 1); 69 } 70 71 /** 72 @brief Returns the position of the next quote in the string, starting with start. 73 @param str The string 74 @param start The startposition 75 @return The position of the next quote (std::string::npos if there is no next quote) 76 */ 77 unsigned int getNextQuote(const std::string& str, unsigned int start) 78 { 79 unsigned int quote = start - 1; 80 81 while ((quote = str.find('\"', quote + 1)) != std::string::npos) 82 { 83 unsigned int backslash = quote; 84 unsigned int numbackslashes = 0; 85 for (; backslash > 0; backslash--, numbackslashes++) 86 if (str[backslash - 1] != '\\') 87 break; 88 89 if (numbackslashes % 2 == 0) 90 break; 91 } 92 93 return quote; 94 } 95 96 /** 97 @brief Returns true if pos is between two quotes. 98 @param str The string 99 @param pos The position to check 100 @return True if pos is between two quotes 101 */ 102 bool isBetweenQuotes(const std::string& str, unsigned int pos) 103 { 104 if (pos == std::string::npos) 105 return false; 106 107 unsigned int quotecount = 0; 108 unsigned int quote = 0; 109 while ((quote = getNextQuote(str, quote)) < pos) 110 { 111 quotecount++; 112 } 113 114 if (quote == std::string::npos) 115 return false; 116 117 return ((quotecount % 2) == 1); 118 } 119 120 /** 121 @brief Returns true if the string contains something like '..."between quotes"...' 122 @param The string 123 @return True if there is something between quotes 124 */ 125 bool hasStringBetweenQuotes(const std::string& str) 126 { 127 unsigned int pos1 = getNextQuote(str, 0); 128 unsigned int pos2 = getNextQuote(str, pos1 + 1); 129 return (pos1 != std::string::npos && pos2 != std::string::npos && pos2 > pos1 + 1); 130 } 131 132 /** 133 @brief If the string contains something like '..."between quotes"...' then 'between quotes' gets returned (without quotes). 134 @param The string 135 @param The string between the quotes 136 */ 137 std::string getStringBetweenQuotes(const std::string& str) 138 { 139 unsigned int pos1 = getNextQuote(str, 0); 140 unsigned int pos2 = getNextQuote(str, pos1 + 1); 141 if (pos1 != std::string::npos && pos2 != std::string::npos) 142 return str.substr(pos1, pos2 - pos1 + 1); 143 else 144 return ""; 145 } 146 147 /** 148 @brief Removes enclosing quotes if available. 149 @brief str The string to strip 150 @return The string with removed quotes 151 */ 152 std::string stripEnclosingQuotes(const std::string& str) 153 { 154 unsigned int start = std::string::npos; 155 unsigned int end = 0; 156 157 for (unsigned int pos = 0; (pos < str.size()) && (pos < std::string::npos); pos++) 158 { 159 if (str[pos] == '"') 160 { 161 start = pos; 162 break; 163 } 164 165 if ((str[pos] != ' ') && (str[pos] != '\t') && (str[pos] != '\n')) 166 return str; 167 } 168 169 for (unsigned int pos = str.size() - 1; pos < std::string::npos; pos--) 170 { 171 if (str[pos] == '"') 172 { 173 end = pos; 174 break; 175 } 176 177 if ((str[pos] != ' ') && (str[pos] != '\t') && (str[pos] != '\n')) 178 return str; 179 } 180 181 if ((start != std::string::npos) && (end != 0)) 182 return str.substr(start + 1, end - start - 1); 183 else 184 return str; 185 } 186 187 /** 56 188 @brief Determines if a string in is a comment. 57 189 @param str The string to check … … 70 202 // 3) ;comment in unreal tournament config-file style 71 203 // 4) //comment in code style 72 if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';' || (teststring[0] == '/' && teststring[0] == '/')) 73 return true; 204 if (teststring.size() >= 2) 205 { 206 if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';' || (teststring[0] == '/' && teststring[1] == '/')) 207 return true; 208 } 209 else if (teststring.size() == 1) 210 { 211 if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';') 212 return true; 213 } 74 214 75 215 return false; … … 83 223 bool isEmpty(const std::string& str) 84 224 { 85 return getStripped(str) == ""; 225 std::string temp = getStripped(str); 226 return ((temp == "") || (temp.size() == 0)); 86 227 } 87 228 … … 109 250 } 110 251 252 std::string addSlashes(const std::string& str) 253 { 254 std::string output = str; 255 256 for (unsigned int pos = 0; (pos = output.find('\\', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\\\"); } 257 for (unsigned int pos = 0; (pos = output.find('\n', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\n"); } 258 for (unsigned int pos = 0; (pos = output.find('\t', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\t"); } 259 for (unsigned int pos = 0; (pos = output.find('\v', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\v"); } 260 for (unsigned int pos = 0; (pos = output.find('\b', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\b"); } 261 for (unsigned int pos = 0; (pos = output.find('\r', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\r"); } 262 for (unsigned int pos = 0; (pos = output.find('\f', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\f"); } 263 for (unsigned int pos = 0; (pos = output.find('\a', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\a"); } 264 for (unsigned int pos = 0; (pos = output.find('"', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\\""); } 265 for (unsigned int pos = 0; (pos = output.find('\0', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\0"); } 266 267 return output; 268 } 269 270 std::string removeSlashes(const std::string& str) 271 { 272 if (str.size() == 0) 273 return str; 274 275 std::string output = ""; 276 for (unsigned int pos = 0; pos < str.size() - 1; ) 277 { 278 if (str[pos] == '\\') 279 { 280 if (str[pos + 1] == '\\') { output += '\\'; pos += 2; continue; } 281 else if (str[pos + 1] == 'n') { output += '\n'; pos += 2; continue; } 282 else if (str[pos + 1] == 't') { output += '\t'; pos += 2; continue; } 283 else if (str[pos + 1] == 'v') { output += '\v'; pos += 2; continue; } 284 else if (str[pos + 1] == 'b') { output += '\b'; pos += 2; continue; } 285 else if (str[pos + 1] == 'r') { output += '\r'; pos += 2; continue; } 286 else if (str[pos + 1] == 'f') { output += '\f'; pos += 2; continue; } 287 else if (str[pos + 1] == 'a') { output += '\a'; pos += 2; continue; } 288 else if (str[pos + 1] == '"') { output += '"'; pos += 2; continue; } 289 else if (str[pos + 1] == '0') { output += '\0'; pos += 2; continue; } 290 } 291 output += str[pos]; 292 pos++; 293 if (pos == str.size() - 1) 294 output += str[pos]; 295 } 296 297 return output; 298 } 299 111 300 /** 112 301 @brief Replaces each char between A and Z with its lowercase equivalent. … … 115 304 void lowercase(std::string* str) 116 305 { 117 static unsigned const char difference_between_A_and_a = 'A' - 'a'; 118 119 for (std::string::iterator it = (*str).begin(); it != (*str).end(); ++it) 120 if ((*it) >= 'A' && (*it) <= 'Z') 121 (*it) -= difference_between_A_and_a; 306 for (unsigned int i = 0; i < str->size(); ++i) 307 { 308 (*str)[i] = tolower((*str)[i]); 309 } 122 310 } 123 311 … … 140 328 void uppercase(std::string* str) 141 329 { 142 static unsigned const char difference_between_A_and_a = 'A' - 'a'; 143 144 for (std::string::iterator it = (*str).begin(); it != (*str).end(); ++it) 145 if ((*it) >= 'a' && (*it) <= 'z') 146 (*it) += difference_between_A_and_a; 330 for (unsigned int i = 0; i < str->size(); ++i) 331 { 332 (*str)[i] = toupper((*str)[i]); 333 } 147 334 } 148 335 … … 160 347 161 348 /** 162 *@brief compares two strings without ignoring the case163 *@param s1 first string164 *@param s2 second string165 349 @brief compares two strings without ignoring the case 350 @param s1 first string 351 @param s2 second string 352 */ 166 353 int nocaseCmp(const std::string& s1, const std::string& s2) 167 354 { … … 188 375 189 376 /** 190 *@brief compares two strings without ignoring the case191 *@param s1 first string192 *@param s2 second string193 *@param len how far from the beginning to start.194 377 @brief compares two strings without ignoring the case 378 @param s1 first string 379 @param s2 second string 380 @param len how far from the beginning to start. 381 */ 195 382 int nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len) 196 383 { … … 212 399 return 0; 213 400 } 401 402 /** 403 @brief Returns true if the string contains a comment, introduced by #, %, ; or //. 404 @param str The string 405 @return True if the string contains a comment 406 */ 407 bool hasComment(const std::string& str) 408 { 409 return (getCommentPosition(str) != std::string::npos); 410 } 411 412 /** 413 @brief If the string contains a comment, the comment gets returned (including the comment symbol), an empty string otherwise. 414 @param str The string 415 @return The comment 416 */ 417 std::string getComment(const std::string& str) 418 { 419 return str.substr(getCommentPosition(str)); 420 } 421 422 /** 423 @brief If the string contains a comment, the position of the comment-symbol gets returned, std::string::npos otherwise. 424 @param str The string 425 @return The position 426 */ 427 unsigned int getCommentPosition(const std::string& str) 428 { 429 return getNextCommentPosition(str, 0); 430 } 431 432 /** 433 @brief Returns the position of the next comment-symbol, starting with start. 434 @param str The string 435 @param start The startposition 436 @return The position 437 */ 438 unsigned int getNextCommentPosition(const std::string& str, unsigned int start) 439 { 440 for (unsigned int i = start; i < str.size(); i++) 441 if (isComment(str.substr(i))) 442 return i; 443 444 return std::string::npos; 445 } -
code/trunk/src/util/String.h
r871 r1052 26 26 */ 27 27 28 #ifndef _ String_H__29 #define _ String_H__28 #ifndef _Util_String_H__ 29 #define _Util_String_H__ 30 30 31 31 #include <string> … … 34 34 #include "UtilPrereqs.h" 35 35 36 _UtilExport void strip(std::string* str);37 _UtilExport std::string getStripped(const std::string& str);36 _UtilExport void strip(std::string* str); 37 _UtilExport std::string getStripped(const std::string& str); 38 38 39 _UtilExport bool isEmpty(const std::string& str); 40 _UtilExport bool isComment(const std::string& str); 41 _UtilExport bool isNumeric(const std::string& str); 39 _UtilExport std::string removeTrailingWhitespaces(const std::string& str); 42 40 43 _UtilExport void lowercase(std::string* str);44 _UtilExport std::string getLowercase(const std::string& str);41 _UtilExport unsigned int getNextQuote(const std::string& str, unsigned int start); 42 _UtilExport bool isBetweenQuotes(const std::string& str, unsigned int pos); 45 43 46 _UtilExport void uppercase(std::string*str);47 _UtilExport std::string getUppercase(const std::string& str);44 _UtilExport bool hasStringBetweenQuotes(const std::string& str); 45 _UtilExport std::string getStringBetweenQuotes(const std::string& str); 48 46 49 _UtilExport int nocaseCmp(const std::string& s1, const std::string& s2); 50 _UtilExport int nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len); 47 _UtilExport std::string stripEnclosingQuotes(const std::string& str); 48 49 _UtilExport bool isEmpty(const std::string& str); 50 _UtilExport bool isComment(const std::string& str); 51 _UtilExport bool isNumeric(const std::string& str); 52 53 _UtilExport std::string addSlashes(const std::string& str); 54 _UtilExport std::string removeSlashes(const std::string& str); 55 56 _UtilExport void lowercase(std::string* str); 57 _UtilExport std::string getLowercase(const std::string& str); 58 59 _UtilExport void uppercase(std::string* str); 60 _UtilExport std::string getUppercase(const std::string& str); 61 62 _UtilExport int nocaseCmp(const std::string& s1, const std::string& s2); 63 _UtilExport int nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len); 64 65 _UtilExport bool hasComment(const std::string& str); 66 _UtilExport std::string getComment(const std::string& str); 67 _UtilExport unsigned int getCommentPosition(const std::string& str); 68 _UtilExport unsigned int getNextCommentPosition(const std::string& str, unsigned int start = 0); 51 69 52 70 //! The Convert class has some static member functions to convert strings to values and values to strings. … … 144 162 }; 145 163 146 #endif /* _ String_H__ */164 #endif /* _Util_String_H__ */ -
code/trunk/src/util/SubString.cc
r871 r1052 66 66 SubString::SubString(const std::string& string, 67 67 const std::string& delimiters, const std::string& delimiterNeighbours, bool emptyEntries, 68 char escapeChar, char safemode_char, char openparenthesis_char, char closeparenthesis_char, char comment_char) 69 { 70 SubString::splitLine(this->strings, string, delimiters, delimiterNeighbours, emptyEntries, escapeChar, safemode_char, openparenthesis_char, closeparenthesis_char, comment_char); 68 char escapeChar, bool removeExcapeChar, char safemode_char, bool removeSafemodeChar, 69 char openparenthesis_char, char closeparenthesis_char, bool removeParenthesisChars, char comment_char) 70 { 71 SubString::splitLine(this->strings, this->bInSafemode, string, delimiters, delimiterNeighbours, emptyEntries, escapeChar, removeExcapeChar, safemode_char, removeSafemodeChar, openparenthesis_char, closeparenthesis_char, removeParenthesisChars, comment_char); 71 72 } 72 73 … … 79 80 { 80 81 for (unsigned int i = subSetBegin; i < subString.size(); i++) 82 { 81 83 this->strings.push_back(subString[i]); 84 this->bInSafemode.push_back(subString.isInSafemode(i)); 85 } 82 86 } 83 87 … … 91 95 SubString::SubString(const SubString& subString, unsigned int subSetBegin, unsigned int subSetEnd) 92 96 { 93 for (unsigned int i = subSetBegin; i < subString.size() || i < subSetEnd; i++) 97 for (unsigned int i = subSetBegin; i < subString.size() && i < subSetEnd; i++) 98 { 94 99 this->strings.push_back(subString[i]); 100 this->bInSafemode.push_back(subString.isInSafemode(i)); 101 } 95 102 } 96 103 … … 103 110 { 104 111 for(unsigned int i = 0; i < argc; ++i) 112 { 105 113 this->strings.push_back(std::string(argv[i])); 114 this->bInSafemode.push_back(false); 115 } 106 116 } 107 117 … … 129 139 { 130 140 this->strings = subString.strings; 141 this->bInSafemode = subString.bInSafemode; 131 142 return *this; 132 143 } … … 140 151 bool SubString::operator==(const SubString& subString) const 141 152 { 142 return ( this->strings == subString.strings);153 return ((this->strings == subString.strings) && (this->bInSafemode == subString.bInSafemode)); 143 154 } 144 155 … … 165 176 166 177 for (unsigned int i = 0; i < length; i++) 167 if ( this->strings[i] != subString.strings[i])178 if ((this->strings[i] != subString.strings[i]) || (this->bInSafemode[i] != subString.bInSafemode[i])) 168 179 return false; 169 180 return true; … … 190 201 { 191 202 for (unsigned int i = 0; i < subString.size(); i++) 203 { 192 204 this->strings.push_back(subString[i]); 205 this->bInSafemode.push_back(subString.isInSafemode(i)); 206 } 193 207 return *this; 194 208 } … … 203 217 { 204 218 this->strings.clear(); 219 this->bInSafemode.clear(); 205 220 char split[2]; 206 221 split[0] = splitter; 207 222 split[1] = '\0'; 208 SubString::splitLine(this->strings, string, split);223 SubString::splitLine(this->strings, this->bInSafemode, string, split); 209 224 return strings.size(); 210 225 } … … 223 238 unsigned int SubString::split(const std::string& string, 224 239 const std::string& delimiters, const std::string& delimiterNeighbours, bool emptyEntries, 225 char escapeChar, char safemode_char, char openparenthesis_char, char closeparenthesis_char, char comment_char) 240 char escapeChar, bool removeExcapeChar, char safemode_char, bool removeSafemodeChar, 241 char openparenthesis_char, char closeparenthesis_char, bool removeParenthesisChars, char comment_char) 226 242 { 227 243 this->strings.clear(); 228 SubString::splitLine(this->strings, string, delimiters, delimiterNeighbours, emptyEntries, escapeChar, safemode_char, openparenthesis_char, closeparenthesis_char, comment_char); 244 this->bInSafemode.clear(); 245 SubString::splitLine(this->strings, this->bInSafemode, string, delimiters, delimiterNeighbours, emptyEntries, escapeChar, removeExcapeChar, safemode_char, removeSafemodeChar, openparenthesis_char, closeparenthesis_char, removeParenthesisChars, comment_char); 229 246 return this->strings.size(); 230 247 } … … 292 309 * @param escape_char: Escape carater (escapes splitters) 293 310 * @param safemode_char: the beginning of the safemode is marked with this 311 * @param removeSafemodeChar removes the safemode_char from the beginning and the ending of a token 312 * @param openparenthesis_char the beginning of a safemode is marked with this 313 * @param closeparenthesis_char the ending of a safemode is marked with this 314 * @param removeParenthesisChars removes the parenthesis from the beginning and the ending of a token 294 315 * @param comment_char: the beginning of a comment is marked with this: (until the end of a Line) 295 316 * @param start_state: the Initial state on how to parse the String. 296 * @return sSPLIT_LINE_STATE the parser was in when returning317 * @return SPLIT_LINE_STATE the parser was in when returning 297 318 * 298 319 * This is the Actual Splitting Algorithm from Clemens Wacha … … 302 323 SubString::SPLIT_LINE_STATE 303 324 SubString::splitLine(std::vector<std::string>& ret, 325 std::vector<bool>& bInSafemode, 304 326 const std::string& line, 305 327 const std::string& delimiters, … … 307 329 bool emptyEntries, 308 330 char escape_char, 331 bool removeExcapeChar, 309 332 char safemode_char, 333 bool removeSafemodeChar, 310 334 char openparenthesis_char, 311 335 char closeparenthesis_char, 336 bool removeParenthesisChars, 312 337 char comment_char, 313 338 SPLIT_LINE_STATE start_state) … … 318 343 319 344 std::string token; 345 bool inSafemode = false; 320 346 321 347 if(start_state != SL_NORMAL && ret.size() > 0) … … 323 349 token = ret[ret.size()-1]; 324 350 ret.pop_back(); 351 } 352 if(start_state != SL_NORMAL && bInSafemode.size() > 0) 353 { 354 inSafemode = bInSafemode[bInSafemode.size()-1]; 355 bInSafemode.pop_back(); 325 356 } 326 357 … … 333 364 { 334 365 state = SL_ESCAPE; 366 if (!removeExcapeChar) 367 token += line[i]; 335 368 } 336 369 else if(line[i] == safemode_char) 337 370 { 338 371 state = SL_SAFEMODE; 372 inSafemode = true; 373 if (!removeSafemodeChar) 374 token += line[i]; 339 375 } 340 376 else if(line[i] == openparenthesis_char) 341 377 { 342 378 state = SL_PARENTHESES; 379 inSafemode = true; 380 if (!removeParenthesisChars) 381 token += line[i]; 343 382 } 344 383 else if(line[i] == comment_char) … … 351 390 ret.push_back(token); 352 391 token.clear(); 392 bInSafemode.push_back(inSafemode); 393 inSafemode = false; 353 394 } 354 395 token += line[i]; // EAT … … 365 406 ret.push_back(token); 366 407 token.clear(); 408 bInSafemode.push_back(inSafemode); 409 inSafemode = false; 367 410 } 368 411 state = SL_NORMAL; … … 386 429 break; 387 430 case SL_ESCAPE: 388 if(line[i] == 'n') token += '\n'; 389 else if(line[i] == 't') token += '\t'; 390 else if(line[i] == 'v') token += '\v'; 391 else if(line[i] == 'b') token += '\b'; 392 else if(line[i] == 'r') token += '\r'; 393 else if(line[i] == 'f') token += '\f'; 394 else if(line[i] == 'a') token += '\a'; 395 else if(line[i] == '?') token += '\?'; 396 else token += line[i]; // EAT 431 if (!removeSafemodeChar) 432 token += line[i]; 433 else 434 { 435 if(line[i] == 'n') token += '\n'; 436 else if(line[i] == 't') token += '\t'; 437 else if(line[i] == 'v') token += '\v'; 438 else if(line[i] == 'b') token += '\b'; 439 else if(line[i] == 'r') token += '\r'; 440 else if(line[i] == 'f') token += '\f'; 441 else if(line[i] == 'a') token += '\a'; 442 else if(line[i] == '?') token += '\?'; 443 else token += line[i]; // EAT 444 } 397 445 state = SL_NORMAL; 398 446 break; … … 401 449 { 402 450 state = SL_NORMAL; 451 if (!removeSafemodeChar) 452 token += line[i]; 403 453 } 404 454 else if(line[i] == escape_char) … … 429 479 { 430 480 state = SL_NORMAL; 481 if (!removeParenthesisChars) 482 token += line[i]; 431 483 } 432 484 else if(line[i] == escape_char) … … 461 513 ret.push_back(token); 462 514 token.clear(); 515 bInSafemode.push_back(inSafemode); 516 inSafemode = false; 463 517 } 464 518 state = SL_NORMAL; … … 484 538 ret.push_back(token); 485 539 token.clear(); 540 bInSafemode.push_back(inSafemode); 541 inSafemode = false; 486 542 } 487 543 return(state); -
code/trunk/src/util/SubString.h
r871 r1052 88 88 SubString(const std::string& string, 89 89 const std::string& delimiters, const std::string& delimiterNeighbours = "", bool emptyEntries=false, 90 char escapeChar ='\\', char safemode_char = '"', char openparenthesis_char = '(', char closeparenthesis_char = ')', char comment_char = '\0'); 90 char escapeChar ='\\', bool removeExcapeChar = true, char safemode_char = '"', bool removeSafemodeChar = true, 91 char openparenthesis_char = '(', char closeparenthesis_char = ')', bool removeParenthesisChars = true, char comment_char = '\0'); 91 92 SubString(unsigned int argc, const char** argv); 92 93 /** @brief create a Substring as a copy of another one. @param subString the SubString to copy. */ … … 111 112 unsigned int split(const std::string& string, 112 113 const std::string& delimiters, const std::string& delimiterNeighbours = "", bool emptyEntries = false, 113 char escapeChar ='\\', char safemode_char = '"', char openparenthesis_char = '(', char closeparenthesis_char = ')', char comment_char = '\0'); 114 char escapeChar ='\\', bool removeExcapeChar = true, char safemode_char = '"', bool removeSafemodeChar = true, 115 char openparenthesis_char = '(', char closeparenthesis_char = ')', bool removeParenthesisChars = true, char comment_char = '\0'); 114 116 std::string join(const std::string& delimiter = " ") const; 115 117 //////////////////////////////////////// … … 120 122 121 123 // retrieve Information from within 122 /** @ returns true if the SubString is empty */124 /** @brief Returns true if the SubString is empty */ 123 125 inline bool empty() const { return this->strings.empty(); }; 124 /** @ returns the count of Strings stored in this substring */126 /** @brief Returns the count of Strings stored in this substring */ 125 127 inline unsigned int size() const { return this->strings.size(); }; 126 /** @ param i the i'th String @returns the i'th string from the subset of Strings*/128 /** @brief Returns the i'th string from the subset of Strings @param i the i'th String */ 127 129 inline const std::string& operator[](unsigned int i) const { return this->strings[i]; }; 128 /** @ param i the i'th String @returns the i'th string from the subset of Strings*/130 /** @brief Returns the i'th string from the subset of Strings @param i the i'th String */ 129 131 inline const std::string& getString(unsigned int i) const { return (*this)[i]; }; 130 /** @returns the front of the StringList. */ 132 /** @brief Returns true if the token is in safemode. @param i the i'th token */ 133 inline bool isInSafemode(unsigned int i) const { return this->bInSafemode[i]; } 134 /** @brief Returns the front of the StringList. */ 131 135 inline const std::string& front() const { return this->strings.front(); }; 132 /** @ returns the back of the StringList. */136 /** @brief Returns the back of the StringList. */ 133 137 inline const std::string& back() const { return this->strings.back(); }; 134 138 /** @brief removes the back of the strings list. */ 135 inline void pop_back() { this->strings.pop_back(); };139 inline void pop_back() { this->strings.pop_back(); this->bInSafemode.pop_back(); }; 136 140 137 141 // the almighty algorithm. 138 142 static SPLIT_LINE_STATE splitLine(std::vector<std::string>& ret, 143 std::vector<bool>& bInSafemode, 139 144 const std::string& line, 140 145 const std::string& delimiters = SubString::WhiteSpaces, … … 142 147 bool emptyEntries = false, 143 148 char escape_char = '\\', 149 bool removeExcapeChar = true, 144 150 char safemode_char = '"', 151 bool removeSafemodeChar = true, 145 152 char openparenthesis_char = '(', 146 153 char closeparenthesis_char = ')', 154 bool removeParenthesisChars = true, 147 155 char comment_char = '\0', 148 156 SPLIT_LINE_STATE start_state = SL_NORMAL); … … 157 165 private: 158 166 std::vector<std::string> strings; //!< strings produced from a single string splitted in multiple strings 167 std::vector<bool> bInSafemode; 159 168 }; 160 169 -
code/trunk/src/util/UtilPrereqs.h
r1024 r1052 61 61 class ArgReader; 62 62 class Convert; 63 template <typename FromType, typename ToType>64 class Converter;65 63 class MultiTypePrimitive; 66 64 class MultiTypeString; -
code/trunk/src/util/tinyxml/ticpp.h
r871 r1052 973 973 Stream output operator. 974 974 */ 975 friend std::ostream& operator <<( std::ostream& out, Node& base )975 friend std::ostream& operator <<( std::ostream& out, const Node& base ) 976 976 { 977 977 out << *base.GetTiXmlPointer();
Note: See TracChangeset
for help on using the changeset viewer.