Utility.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _UTILITY_H_
  23. #define _UTILITY_H_
  24. #ifndef _SIMBASE_H_
  25. #include "sim/simBase.h"
  26. #endif
  27. #ifndef _PLATFORM_H_
  28. #include "platform/platform.h"
  29. #endif
  30. #ifndef _PLATFORMGL_H_
  31. #include "platform/platformGL.h"
  32. #endif
  33. #ifndef _MMATH_H_
  34. #include "math/mMath.h"
  35. #endif
  36. #include <algorithm>
  37. #include <cctype>
  38. #include <locale>
  39. //-----------------------------------------------------------------------------
  40. // Miscellaneous Defines.
  41. //-----------------------------------------------------------------------------
  42. #define MASK_ALL (U32_MAX)
  43. #define MASK_BITCOUNT (32)
  44. #define DEBUG_MODE_COUNT (8)
  45. #define MAX_LAYERS_SUPPORTED (32)
  46. #define CANNOT_RENDER_PROXY_NAME "CannotRenderProxy"
  47. #define b2_pi2 (b2_pi * 2.0f)
  48. //-----------------------------------------------------------------------------
  49. class SceneObject;
  50. struct Vector2;
  51. typedef Vector<SceneObject*> typeSceneObjectVector;
  52. typedef const Vector<SceneObject*>& typeSceneObjectVectorConstRef;
  53. ///-----------------------------------------------------------------------------
  54. DefineConsoleType( Typeb2AABB )
  55. //-----------------------------------------------------------------------------
  56. namespace Utility
  57. {
  58. //-----------------------------------------------------------------------------
  59. #define STATIC_VOID_CAST_TO( pointerType, castToType, obj ) static_cast<castToType*>( reinterpret_cast<pointerType*>(obj) )
  60. #define DYNAMIC_VOID_CAST_TO( pointerType, castToType, obj ) dynamic_cast<castToType*>( reinterpret_cast<pointerType*>(obj) )
  61. //-----------------------------------------------------------------------------
  62. /// String helpers.
  63. const char* mGetFirstNonWhitespace( const char* inString );
  64. Vector2 mGetStringElementVector( const char* inString, const U32 index = 0 );
  65. VectorF mGetStringElementVector3D( const char* inString, const U32 index = 0 );
  66. const char* mGetStringElement( const char* inString, const U32 index, const bool copyBuffer = true );
  67. U32 mGetStringElementCount( const char *string );
  68. U32 mConvertStringToMask( const char* string );
  69. const char* mConvertMaskToString( const U32 mask );
  70. //------------------------------------------------------------------------------
  71. // Modern C++17 string trim functions
  72. // trim from start (in place)
  73. static inline void ltrim(std::string& s) {
  74. s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
  75. return !std::isspace(ch);
  76. }));
  77. }
  78. // trim from end (in place)
  79. static inline void rtrim(std::string& s) {
  80. s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
  81. return !std::isspace(ch);
  82. }).base(), s.end());
  83. }
  84. // trim from both ends (in place)
  85. static inline void trim(std::string& s) {
  86. ltrim(s);
  87. rtrim(s);
  88. }
  89. // trim from start (copying)
  90. static inline std::string ltrim_copy(std::string s) {
  91. ltrim(s);
  92. return s;
  93. }
  94. // trim from end (copying)
  95. static inline std::string rtrim_copy(std::string s) {
  96. rtrim(s);
  97. return s;
  98. }
  99. // trim from both ends (copying)
  100. static inline std::string trim_copy(std::string s) {
  101. trim(s);
  102. return s;
  103. }
  104. } // Namespace Utility.
  105. #endif // _UTILITY_H_