propertyParsing.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 _PROPERTYPARSING_H_
  23. #define _PROPERTYPARSING_H_
  24. class ColorI;
  25. class LinearColorF;
  26. class Point2I;
  27. class Point2F;
  28. class Point3F;
  29. class Point4F;
  30. class Point3I;
  31. class Point4I;
  32. class RectI;
  33. class RectF;
  34. class Box3I;
  35. class Box3F;
  36. #ifndef USE_TEMPLATE_MATRIX
  37. class MatrixF;
  38. #else
  39. template<typename DATA_TYPE, U32 rows, U32 cols> class Matrix;
  40. typedef Matrix<F32, 4, 4> MatrixF;
  41. #endif
  42. class AngAxisF;
  43. class QuatF;
  44. class String;
  45. class FileName;
  46. class SimObject;
  47. class SimObjectType;
  48. template<class T> class Vector;
  49. //-----------------------------------------------------------------------------
  50. // String scan/print methods
  51. //
  52. // Macros in propertyImplementation.h point the ConcretePropertyDefinition scan/print delegates to these functions.
  53. //
  54. namespace PropertyInfo
  55. {
  56. // String
  57. bool default_scan(const String &data, String & result);
  58. bool default_print(String & result, const String & data);
  59. // Bool
  60. bool default_scan(const String &data, bool & result);
  61. bool default_print(String & result, const bool & data);
  62. // S32/F32/U32
  63. bool default_scan(const String &data, F32 & result);
  64. bool default_print(String & result, const F32 & data);
  65. bool default_scan(const String &data, U32 & result);
  66. bool default_print(String & result, const U32 & data);
  67. bool default_scan(const String &data, S32 & result);
  68. bool default_print(String & result, const S32 & data);
  69. // Vector<S32/F32/U32>
  70. bool default_scan(const String &data, Vector<F32> & result);
  71. bool default_print(String & result, const Vector<F32> & data);
  72. bool default_scan(const String &data, Vector<U32> & result);
  73. bool default_print(String & result, const Vector<U32> & data);
  74. bool default_scan(const String &data, Vector<S32> & result);
  75. bool default_print(String & result, const Vector<S32> & data);
  76. // Math Points
  77. bool default_scan(const String &data, Point2F & result);
  78. bool default_print(String & result, const Point2F & data);
  79. bool default_scan(const String &data, Point2I & result);
  80. bool default_print(String & result, const Point2I & data);
  81. bool default_scan(const String &data, Point3F & result);
  82. bool default_print(String & result, const Point3F & data);
  83. bool default_scan(const String &data, Point3I & result);
  84. bool default_print(String & result, const Point3I & data);
  85. bool default_scan(const String &data, Point4F & result);
  86. bool default_print(String & result, const Point4F & data);
  87. bool default_scan(const String &data, Point4I & result);
  88. bool default_print(String & result, const Point4I & data);
  89. // Math Boxs & Rectangles
  90. bool default_scan(const String &data, RectI & result);
  91. bool default_print(String & result, const RectI & data);
  92. bool default_scan(const String &data, RectF & result);
  93. bool default_print(String & result, const RectF & data);
  94. bool default_scan(const String &data, Box3I & result);
  95. bool default_print(String & result, const Box3I & data);
  96. bool default_scan(const String &data, Box3F & result);
  97. bool default_print(String & result, const Box3F & data);
  98. //-----------------------------------------------------------------------------
  99. bool default_scan( const String &data, AngAxisF & result );
  100. bool default_print( String & result, const AngAxisF & data );
  101. bool default_scan( const String &data, QuatF & result );
  102. bool default_print( String & result, const QuatF & data );
  103. bool default_scan( const String &data, MatrixF & result );
  104. bool default_print( String & result, const MatrixF & data );
  105. // Colors
  106. bool default_scan(const String &data, LinearColorF & result);
  107. bool default_print(String & result, const LinearColorF & data);
  108. bool default_scan(const String &data, ColorI & result);
  109. bool default_print(String & result, const ColorI & data);
  110. // filename handler
  111. bool default_scan(const String &data, FileName & result);
  112. bool default_print(String & result, const FileName & data);
  113. // SimObjectType
  114. bool default_scan(const String &data, SimObjectType & result);
  115. bool default_print(String & result, SimObjectType const & data);
  116. // SimObject
  117. bool default_scan(const String &data, SimObject * & result);
  118. bool default_print(String & result, SimObject * const & data);
  119. template<class T>
  120. inline bool typed_simobject_scan(const String &data, T * & result)
  121. {
  122. SimObject * obj;
  123. result = default_scan(data,obj)? dynamic_cast<T*>(obj) : NULL;
  124. return result;
  125. }
  126. template<class T>
  127. inline bool typed_simobject_print(String & result, T * const & data)
  128. {
  129. return default_print(result,data);
  130. }
  131. bool hex_scan(const String & string, U32 & hex);
  132. bool hex_print(String & string, const U32 & hex);
  133. bool hex_scan(const String & string, S32 & hex);
  134. bool hex_print(String & string, const S32 & hex);
  135. bool hex_scan(const String & string, U16 & hex);
  136. bool hex_print(String & string, const U16 & hex);
  137. bool hex_scan(const String & string, S16 & hex);
  138. bool hex_print(String & string, const S16 & hex);
  139. bool hex_scan(const String & string, U8 & hex);
  140. bool hex_print(String & string, const U8 & hex);
  141. bool hex_scan(const String & string, S8 & hex);
  142. bool hex_print(String & string, const S8 & hex);
  143. bool default_print(String & result, SimObjectType * const & data);
  144. template<typename T, size_t count>
  145. char* FormatPropertyBuffer(const void* dataPtr, char* buffer, U32 bufSize)
  146. {
  147. const T* values = reinterpret_cast<const T*>(dataPtr);
  148. char* ptr = buffer;
  149. for (size_t i = 0; i < count; ++i)
  150. {
  151. S32 written = 0;
  152. if constexpr (std::is_same_v<T, int> || std::is_same_v<T, S32>)
  153. written = dSprintf(ptr, bufSize - (ptr - buffer), "%d", values[i]);
  154. else if constexpr (std::is_same_v<T, float> || std::is_same_v<T, F32>)
  155. written = dSprintf(ptr, bufSize - (ptr - buffer), "%g", values[i]);
  156. else
  157. AssertFatal(sizeof(T) == 0, "Unsupported type in FormatProperty");
  158. ptr += written;
  159. if (i < count - 1)
  160. *ptr++ = ' ';
  161. }
  162. return ptr; // return end of written string for chaining
  163. }
  164. template<typename T, size_t count>
  165. const char* FormatProperty(const void* dataPtr)
  166. {
  167. static const U32 bufSize = 256;
  168. char* buffer = Con::getReturnBuffer(bufSize);
  169. FormatPropertyBuffer<T,count>(dataPtr, buffer, bufSize);
  170. return buffer;
  171. }
  172. template<typename T, size_t count>
  173. inline bool ParseProperty(char* str, T(&out)[count])
  174. {
  175. size_t index = 0;
  176. char* tok = dStrtok(str, " \t");
  177. while (tok && index < count)
  178. {
  179. if constexpr (std::is_same_v<T, int> || std::is_same_v<T, S32>)
  180. {
  181. out[index++] = mRound(dAtof(tok));
  182. }
  183. else if constexpr (std::is_same_v<T, float> || std::is_same_v<T, F32>)
  184. {
  185. out[index++] = dAtof(tok);
  186. }
  187. else
  188. {
  189. AssertFatal(sizeof(T) == 0, "Unsupported type in PropertyInfo::ParseProperty");
  190. }
  191. tok = dStrtok(nullptr, " \t");
  192. }
  193. return index == count;
  194. }
  195. }
  196. // Default Scan/print definition
  197. #define DEFINE_PROPERTY_DEFAULT_PRINT(dataType) bool PropertyInfo::default_print(String & resultString, dataType const & dataTyped)
  198. #define DEFINE_PROPERTY_DEFAULT_SCAN(dataType) bool PropertyInfo::default_scan(const String &dataString, dataType & resultTyped)
  199. #endif // _PROPERTYPARSING_H_