Changeset 2014
- Timestamp:
- Oct 26, 2008, 12:35:58 AM (16 years ago)
- Location:
- code/branches/core3
- Files:
-
- 1 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core3/src/orxonox/Orxonox.cc
r1778 r2014 231 231 Factory::createClassHierarchy(); 232 232 233 std::string nmbr; 234 char res('a'); 235 //const char* nmbr; 236 //const char* str; 237 convertValue(&nmbr, res); 238 //const unsigned int blah = 4; 239 //convertValue(nmbr, blah); 240 //convertValue(&str, 4.0f); 241 242 //using ::operator<<; 243 using std::string; 244 int a = 3; 245 FooBar asdf; 246 std::string asdf2; 247 std::ostringstream oss; 248 char blah(' '); 249 oss << blah; 250 std::cout << asdf2; 251 ConvertValue(&asdf2, asdf); 252 //if (conversionTests::OStreamOperator<FooBar>::exists) 253 // int asdfasdfasdf = 7; 254 //COUT(3) << asdf; 255 256 TestConv(1, int, (3), float, 3.0); 257 TestConv(2, int, (3), string, "3"); 258 TestConv(3, string, ("3.0"), float, 3.0f); 259 TestConv(4, char, ('a'), string, "a"); 260 TestConv(5, string, ("df"), char, 'd'); 261 TestConv(6, Vector2, (3,4), string, "3,4"); 262 TestConv(7, const char*, ("4.3"), float, 4.3f); 263 TestConv(8, const char*, ("4,3"), Vector2, Vector2(4,3)); 264 //TestConv(9, const char*, ("4.4"), Radian, Radian(4.4)); 265 TestConv(100, int, (3), const char*, "3"); 266 TestConv(101, Vector3, (1, 2, 3), float, 3.0); 267 268 std::ostringstream out; 233 #include "convert_test.cc" 234 235 // some special string conversions 236 const char* myCString = "84.0"; 237 COUT(0) << conversion_cast<std::string>(myCString) << std::endl; 238 COUT(0) << conversion_cast<float>(myCString) << std::endl; 239 COUT(0) << conversion_cast<Vector2>(myCString) << std::endl; 240 COUT(0) << conversion_cast<std::string>(conversion_cast<Radian>(myCString)) << std::endl; 241 COUT(0) << std::endl; 242 243 std::string myString = "3,4,1"; 244 COUT(0) << conversion_cast<Vector3>(myString) << std::endl; 245 246 247 //std::string nmbr; 248 //char res('a'); 249 ////const char* nmbr; 250 ////const char* str; 251 //convertValue(&nmbr, res); 252 ////const unsigned int blah = 4; 253 ////convertValue(nmbr, blah); 254 ////convertValue(&str, 4.0f); 255 256 ////using ::operator<<; 257 //using std::string; 258 //int a = 3; 259 //FooBar asdf; 260 //std::string asdf2; 261 //std::ostringstream oss; 262 //char blah(' '); 263 //oss << blah; 264 //std::cout << asdf2; 265 //ConvertValue(&asdf2, asdf); 266 ////if (conversionTests::OStreamOperator<FooBar>::exists) 267 //// int asdfasdfasdf = 7; 268 ////COUT(3) << asdf; 269 270 //TestConv(1, int, (3), float, 3.0); 271 //TestConv(2, int, (3), string, "3"); 272 //TestConv(3, string, ("3.0"), float, 3.0f); 273 //TestConv(4, char, ('a'), string, "a"); 274 //TestConv(5, string, ("df"), char, 'd'); 275 //TestConv(6, Vector2, (3,4), string, "3,4"); 276 //TestConv(7, const char*, ("4.3"), float, 4.3f); 277 //TestConv(8, const char*, ("4,3"), Vector2, Vector2(4,3)); 278 ////TestConv(9, const char*, ("4.4"), Radian, Radian(4.4)); 279 //TestConv(100, int, (3), const char*, "3"); 280 //TestConv(101, Vector3, (1, 2, 3), float, 3.0); 281 282 //std::ostringstream out; 269 283 270 284 std::string mode; -
code/branches/core3/src/util/Convert.h
r1779 r2014 23 23 * Benjamin Grauer 24 24 * Fabian 'x3n' Landau 25 * Reto Grieder (direct conversion tests)25 * Reto Grieder 26 26 * Co-authors: 27 27 * ... … … 45 45 #include "Debug.h" 46 46 47 // G ccgenerates warnings when implicitely casting from float to int for instance.48 // This is however exactly what convertValue does, so we need to suppress the warnings.49 // They only occur inwhen using the ImplicitConversion template.47 // GCC generates warnings when implicitely casting from float to int for instance. 48 // This is however exactly what convertValue does, so we need to suppress these warnings. 49 // They only occur when using the ImplicitConversion template. 50 50 #if ORXONOX_COMPILER == ORXONOX_COMPILER_GNUC 51 51 # pragma GCC system_header 52 52 #endif 53 53 54 54 55 /////////////////////////////////////////////// 55 56 // Static detection for conversion functions // 56 57 /////////////////////////////////////////////// 58 57 59 /* The idea to use the sizeof() operator on return functions to determine function existance 58 is described in 'Moder C++ design' by Alexandrescu (2001). */60 is described in 'Modern C++ design' by Alexandrescu (2001). */ 59 61 60 62 // disable warnings about possible loss of data … … 64 66 #endif 65 67 66 namespace conversionTests 67 { 68 // A struct that is guaranteed to be larger than any return type of our conversion functions. 69 // So we simply add all the sizes of the return types plus a little bit more. 70 struct VeryBigStruct 71 { 72 char intSize[sizeof(int)]; 73 char addingMore[4096]; // just to be sure ;) 74 }; 75 76 template <class FromType, class ToType> 77 class ImplicitConversion 78 { 79 private: 80 ImplicitConversion(); ImplicitConversion(const ImplicitConversion&); ~ImplicitConversion(); 81 // Gets chosen only if there is an implicit conversion from FromType to ToType. 82 static int test(ToType); 83 // Accepts any argument. Why do we not use a template? The reason is that with templates, 84 // the function above is only taken iff it is an exact type match. But since we want to 85 // check for implicit conversion, we have to use the ellipsis. 86 static VeryBigStruct test(...); 87 static FromType object; // helper object to handle private c'tor and d'tor 88 public: 89 // test(object) only has 'VerySmallStruct' return type iff the compiler doesn't choose test(...) 90 enum { exists = !(sizeof(test(object)) == sizeof(VeryBigStruct)) }; 91 }; 92 } 68 template <class FromType, class ToType> 69 class ImplicitConversion 70 { 71 private: 72 ImplicitConversion(); ImplicitConversion(const ImplicitConversion&); ~ImplicitConversion(); 73 // Gets chosen only if there is an implicit conversion from FromType to ToType. 74 static char test(ToType); 75 // Accepts any argument. Why do we not use a template? The reason is that with templates, 76 // the function above is only taken iff it is an exact type match. But since we want to 77 // check for implicit conversion, we have to use the ellipsis. 78 static long long test(...); 79 static FromType object; // helper object to handle private c'tor and d'tor 80 public: 81 // test(object) only has 'long long' return type iff the compiler doesn't choose test(...) 82 enum { exists = (sizeof(test(object)) == sizeof(char)) }; 83 }; 93 84 94 85 #if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC … … 102 93 /* 103 94 There is a distinct priority when choosing the right conversion function: 104 Overwrites: 105 1. (Partial) template specialisation of ConverterExplicit::convert 106 2. Global functions convertValue(ToType* output, const FromType input) 95 Overwrite: 96 1. (Partial) template specialisation of ConverterExplicit::convert() 107 97 Fallbacks: 108 3. Any possible implicit conversion. This includes FooBar --> int if FooBar defines operator float(). 109 4. Global or member operators for stringstream when converting from or to std::string (or FROM const char*) 110 5. Function that simply displays "Could not convert value" with information obtained from typeid(). 111 112 A note: There has to be an exact type match (or according to the rules of template spec.) except for 3. 113 114 There are obviously a lot of ways to specifiy a user defined conversion. What should I use? 115 When using any non-template function based conversion (implicit conversion, convertValue, << or >>) 116 then you should consider that this function has to be defined prior to including this file. 117 If you do not whish do do that, you will have to spcecialsize the ConverterExplicit template. 118 There is a not so obvious advantage of the other way (non-template): You could declare each conversion function 119 in the Prereqs file of the corresponding library. Then you can use the conversion everywhere. 120 This is not possible with template specialisations even when defining them in this file (You would create 121 a circular dependency when using a class from Core for instance, because complete template specialisations 122 get compiled anyway (there is no template parameter)). 98 2. Any possible implicit conversion. This includes 'FooBar' --> 'int' if FooBar defines operator float(). 99 3. Global or member operators for stringstream when converting from or to std::string (or FROM const char*) 100 4. (Partial) template specialisation of ConverterFallback::convert() 101 5. Function that simply displays "Could not convert value" with type information obtained from typeid(). 102 103 Notes: 104 There has to be an exact type match when using template specialisations. 105 Template specialisations can be defined after including this file. Any implicit cast function or iostream 106 operator has to be declared BEFORE this file gets parsed. 107 108 Defining your own functions: 109 There are obviously 4 ways to specifiy a user defined conversion. What should I use? 110 111 Usually, ConverterFallback fits quite well. You won't have to deal with the conversion from 112 'MyClass' to 'MyClass' by using another explicit template specialisation to avoid ambiguities. 113 114 However if you want to overwrite an implicit conversion or an iostream operator, you really need to 115 make use of ConverterExplicit. 123 116 */ 124 117 125 118 namespace 126 119 { 127 // little template that maps to ints to entire types (Alexandrescu 2001)120 //! Little template that maps integers to entire types (Alexandrescu 2001) 128 121 template <int I> 129 122 struct Int2Type { }; … … 141 134 static bool convert(ToType* output, const FromType& input) 142 135 { 143 COUT(2) << "Could not convert value of type " << typeid(FromType).name()144 << " to type " << typeid(ToType).name() << std::endl;136 //COUT(2) << "Could not convert value of type " << typeid(FromType).name() 137 // << " to type " << typeid(ToType).name() << std::endl; 145 138 return false; 146 139 } … … 183 176 } 184 177 185 // template that evaluates whether OStringStream is possible for conversions to std::string178 // template that evaluates whether we can convert to std::string via ostringstream 186 179 template <class FromType> 187 180 struct ConverterStringStream<std::string, FromType> … … 190 183 { 191 184 using namespace fallbackTemplates; 185 // this operator call only chooses fallbackTemplates::operator<< if there's no other fitting function 192 186 std::ostringstream oss; 193 187 if (oss << input) … … 216 210 } 217 211 218 // template that evaluates whether IStringStream is possible for conversions from std::string212 // template that evaluates whether we can convert from std::string via ostringstream 219 213 template <class ToType> 220 214 struct ConverterStringStream<ToType, std::string> … … 224 218 using namespace fallbackTemplates; 225 219 std::istringstream iss(input); 220 // this operator call only chooses fallbackTemplates::operator>> if there's no other fitting function 226 221 if (iss >> (*output)) 227 222 { … … 238 233 /////////////////// 239 234 240 // static cast nopossible, try stringstream conversion next235 // implicit cast not possible, try stringstream conversion next 241 236 template <class ToType, class FromType> 242 237 inline bool convertImplicitely(ToType* output, const FromType& input, ::Int2Type<false>) … … 254 249 255 250 256 /////////////////////// 257 // Explicit Fallback //258 /////////////////////// 251 //////////////////////////////// 252 // ConverterExplicit Fallback // 253 //////////////////////////////// 259 254 260 255 // Default template if no specialisation is available … … 264 259 static bool convert(ToType* output, const FromType& input) 265 260 { 266 // try implict conversion by probing first because we use '...' instead of a template 267 const bool probe = conversionTests::ImplicitConversion<FromType, ToType>::exists; 261 // Try implict cast and probe first. If a simple cast is not possible, it will not compile 262 // We therefore have to out source it into another template function 263 const bool probe = ImplicitConversion<FromType, ToType>::exists; 268 264 return convertImplicitely(output, input, ::Int2Type<probe>()); 269 265 } … … 278 274 @brief 279 275 Converts any value to any other as long as there exists a conversion. 280 Otherwise, the conversion will generate a runtime warning .276 Otherwise, the conversion will generate a runtime warning and return false. 281 277 For information about the different conversion methods (user defined too), see the section 282 278 'Actual conversion sequence' in this file above. 283 @note284 This function is only a fallback if there is no appropriate 'convertValue' function.285 279 */ 286 280 template <class ToType, class FromType> … … 299 293 // Calls convertValue and returns true if the conversion was successful. 300 294 // Otherwise the fallback is used. 295 /** 296 @brief 297 Converts any value to any other as long as there exists a conversion. 298 Otherwise, the conversion will generate a runtime warning and return false. 299 For information about the different conversion methods (user defined too), see the section 300 'Actual conversion sequence' in this file above. 301 If the conversion doesn't succeed, 'fallback' is written to '*output'. 302 @param fallback 303 A default value that gets written to '*output' if there is no conversion. 304 */ 305 template<class FromType, class ToType> 306 inline bool convertValue(ToType* output, const FromType& input, const ToType& fallback) 307 { 308 if (convertValue(output, input)) 309 return true; 310 else 311 { 312 (*output) = fallback; 313 return false; 314 } 315 } 316 317 // for compatibility reason. (capital 'c' in ConvertValue) 301 318 template<class FromType, class ToType> 302 319 inline bool ConvertValue(ToType* output, const FromType& input, const ToType& fallback) 303 320 { 304 if (convertValue(output, input)) 305 return true; 306 307 (*output) = fallback; 308 return false; 321 return convertValue(output, input, fallback); 309 322 } 310 323 … … 314 327 { 315 328 ToType output; 316 ConvertValue(&output, input);329 convertValue(&output, input); 317 330 return output; 318 331 } … … 323 336 { 324 337 ToType output; 325 ConvertValue(&output, input, fallback);338 convertValue(&output, input, fallback); 326 339 return output; 327 340 } … … 333 346 { 334 347 ToType output; 335 ConvertValue(&output, input); 336 return output; 337 } 338 339 // Like conversion_cast above, but uses a fallback on failure. 340 template<class ToType, class FromType> 341 inline ToType conversion_cast(const FromType& input, const ToType& fallback) 342 { 343 ToType output; 344 ConvertValue(&output, input, fallback); 348 convertValue(&output, input); 345 349 return output; 346 350 } … … 351 355 //////////////////////////////// 352 356 353 // delegate conversion from const char* to std::string357 // Delegate conversion from const char* to std::string 354 358 template <class ToType> 355 359 struct ConverterExplicit<ToType, const char*> -
code/branches/core3/src/util/MathConvert.h
r1779 r2014 37 37 38 38 #include "UtilPrereqs.h" 39 #include "Math.h" 39 40 #include "Convert.h" 40 #include "Math.h"41 41 42 42 … … 153 153 // From Radian 154 154 template <class ToType> 155 inline bool fallbackConversion(ToType* output, const orxonox::Radian input) 156 { 157 return convertValue<ToType, Ogre::Real>(output, input.valueRadians()); 158 } 155 struct ConverterFallback<ToType, orxonox::Radian> 156 { 157 static bool convert(ToType* output, const orxonox::Radian& input) 158 { 159 return convertValue<ToType, Ogre::Real>(output, input.valueRadians()); 160 } 161 }; 159 162 160 163 // From Degree 161 164 template <class ToType> 162 inline bool fallbackConversion(ToType* output, const orxonox::Degree input) 163 { 164 return convertValue<ToType, Ogre::Real>(output, input.valueDegrees()); 165 } 165 struct ConverterFallback<ToType, orxonox::Degree> 166 { 167 static bool convert(ToType* output, const orxonox::Degree& input) 168 { 169 return convertValue<ToType, Ogre::Real>(output, input.valueDegrees()); 170 } 171 }; 166 172 167 173 // To Radian 168 174 template <class FromType> 169 inline bool fallbackConversion(orxonox::Radian* output, const FromType input) 170 { 171 float temp; 172 if (convertValue(&temp, input)) 173 { 174 *output = temp; 175 return true; 176 } 177 else 178 return false; 179 } 175 struct ConverterFallback<orxonox::Radian, FromType> 176 { 177 static bool convert(orxonox::Radian* output, const FromType& input) 178 { 179 float temp; 180 if (convertValue(&temp, input)) 181 { 182 *output = temp; 183 return true; 184 } 185 else 186 return false; 187 } 188 }; 180 189 181 190 // To Degree 182 191 template <class FromType> 183 inline bool fallbackConversion(orxonox::Degree* output, const FromType input) 184 { 185 float temp; 186 if (convertValue(&temp, input)) 187 { 188 *output = temp; 189 return true; 190 } 191 else 192 return false; 193 } 192 struct ConverterFallback<orxonox::Degree, FromType> 193 { 194 static bool convert(orxonox::Degree* output, const FromType& input) 195 { 196 float temp; 197 if (convertValue(&temp, input)) 198 { 199 *output = temp; 200 return true; 201 } 202 else 203 return false; 204 } 205 }; 194 206 195 207 #endif /* _MathConvert_H__ */ -
code/branches/core3/visual_studio/audio_properties.vsprops
r1572 r2014 13 13 <Tool 14 14 Name="VCLinkerTool" 15 AdditionalDependencies=" libvorbisfile-1.2.0$(CSS).lib alut$(CS).lib OpenAL32$(CS).lib"15 AdditionalDependencies="vorbisfile$(CSS).lib alut$(CS).lib OpenAL32$(CS).lib" 16 16 /> 17 17 </VisualStudioPropertySheet> -
code/branches/core3/visual_studio/base_properties.vsprops
r1658 r2014 8 8 <Tool 9 9 Name="VCCLCompilerTool" 10 AdditionalIncludeDirectories=""$(RootDir)";"$(RootDir)src";"$(RootDir)src\orxonox";"$(RootDir)src\ tolua";"$(RootDir)src\ois";"$(LibDir)ogre-1.4.9\OgreMain\include";"$(LibDir)boost-1.35.0";"$(LibDir)enet-1.2\include";"$(LibDir)libogg-1.1.3\include";"$(LibDir)libvorbis-1.2.0\include";"$(LibDir)lua-5.1.3\src";"$(LibDir)openal-1.1\include";"$(LibDir)openal-1.1\alut\include";"$(LibDir)tcl-8.5.\generic";"$(LibDir)zlib-1.2.3""10 AdditionalIncludeDirectories=""$(RootDir)";"$(RootDir)src";"$(RootDir)src\orxonox";"$(RootDir)src\ceguilua-0.6.1";"$(LibDir)ogre-1.4.9\include";"$(LibDir)boost-1.35.0\include";"$(LibDir)cegui-0.6.1\include";"$(LibDir)enet-1.2\include";"$(LibDir)libogg-1.1.3\include";"$(LibDir)libvorbis-1.2.0\include";"$(LibDir)openal-1.1\include";"$(LibDir)freealut-1.1.0\include";"$(LibDir)tcl-8.5.2\include";"$(LibDir)zlib-1.2.3\include";"$(LibDir)lua-5.1.3\include"" 11 11 PreprocessorDefinitions="WIN32;__WIN32__;_WIN32;BOOST_ALL_DYN_LINK;OIS_DYNAMIC_LIB; ZLIB_WINAPI" 12 12 WarningLevel="3" … … 22 22 Name="VCLinkerTool" 23 23 OutputFile="$(OutDir)$(ProjectName)$(CS).dll" 24 AdditionalLibraryDirectories=" $(DependencyDir)lib"24 AdditionalLibraryDirectories=""$(LibDir)boost-1.35.0\lib\$(ConfigurationName)";"$(LibDir)cegui-0.6.1\lib\$(ConfigurationName)";"$(LibDir)enet-1.2\lib\$(ConfigurationName)";"$(LibDir)freealut-1.1.0\lib\$(ConfigurationName)";"$(LibDir)libogg-1.1.3\lib\$(ConfigurationName)";"$(LibDir)libvorbis-1.2.0\lib\$(ConfigurationName)";"$(LibDir)ogre-1.4.9\lib\$(ConfigurationName)";"$(LibDir)openal-1.1\lib\$(ConfigurationName)";"$(LibDir)tcl-8.5.2\lib\$(ConfigurationName)";"$(LibDir)zlib-1.2.3\lib\$(ConfigurationName)";"$(LibDir)lua-5.1.3\lib\$(ConfigurationName)"" 25 25 SubSystem="1" 26 26 ImportLibrary="$(LibOutDir)$(TargetName).lib" … … 45 45 <UserMacro 46 46 Name="LibDir" 47 Value="$(RootDir)..\lib_ src\"47 Value="$(RootDir)..\lib_dist\vc8\dependencies\$(SolutionName)\" 48 48 /> 49 49 <UserMacro -
code/branches/core3/visual_studio/core_properties.vsprops
r1729 r2014 13 13 <Tool 14 14 Name="VCLinkerTool" 15 AdditionalDependencies="OgreMain$(CSS).lib lua -5.1.3$(CS).lib tcl85t.lib"15 AdditionalDependencies="OgreMain$(CSS).lib lua$(CS).lib tcl85t.lib" 16 16 /> 17 17 </VisualStudioPropertySheet> -
code/branches/core3/visual_studio/orxonox_properties.vsprops
r1729 r2014 15 15 <Tool 16 16 Name="VCLinkerTool" 17 AdditionalDependencies="OgreMain$(CSS).lib tcl85t.lib enet -1.2$(CS).lib zlib-1.2.3$(CS).lib"17 AdditionalDependencies="OgreMain$(CSS).lib tcl85t.lib enet$(CS).lib zlib$(CS).lib" 18 18 OutputFile="$(OutDir)$(ProjectName)$(CS).exe" 19 19 IgnoreDefaultLibraryNames="LIBCMT;LIBCMTD"
Note: See TracChangeset
for help on using the changeset viewer.