Generic.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "Generic.h"
  24. #include "Types.h"
  25. namespace crown
  26. {
  27. //-----------------------------------------------------------------------------
  28. Generic::Generic() : m_type(GT_UNDEFINED)
  29. {
  30. }
  31. //-----------------------------------------------------------------------------
  32. Generic::Generic(const Generic& other)
  33. {
  34. *this = other;
  35. }
  36. //-----------------------------------------------------------------------------
  37. Generic::~Generic()
  38. {
  39. }
  40. //-----------------------------------------------------------------------------
  41. Generic::Generic(int32_t value) : m_type(GT_INT)
  42. {
  43. m_data.int32_t_value = value;
  44. }
  45. //-----------------------------------------------------------------------------
  46. Generic::Generic(uint32_t value) : m_type(GT_UINT)
  47. {
  48. m_data.uint32_t_value = value;
  49. }
  50. //-----------------------------------------------------------------------------
  51. Generic::Generic(uint16_t value) : m_type(GT_USHORT)
  52. {
  53. m_data.uint16_t_value = value;
  54. }
  55. //-----------------------------------------------------------------------------
  56. Generic::Generic(float value) : m_type(GT_FLOAT)
  57. {
  58. m_data.float_value = value;
  59. }
  60. //-----------------------------------------------------------------------------
  61. Generic::Generic(bool value) : m_type(GT_BOOL)
  62. {
  63. m_data.bool_value = value;
  64. }
  65. //-----------------------------------------------------------------------------
  66. bool Generic::as_int32_t(int32_t& out) const
  67. {
  68. if (m_type == GT_INT)
  69. {
  70. out = m_data.int32_t_value;
  71. return true;
  72. }
  73. if (m_type == GT_UINT)
  74. {
  75. out = (int32_t)m_data.uint32_t_value;
  76. return true;
  77. }
  78. if (m_type == GT_USHORT)
  79. {
  80. out = (int32_t)m_data.uint16_t_value;
  81. return true;
  82. }
  83. if (m_type == GT_FLOAT)
  84. {
  85. out = (int32_t)m_data.float_value;
  86. return true;
  87. }
  88. if (m_type == GT_BOOL)
  89. {
  90. out = m_data.bool_value ? 1 : 0;
  91. return true;
  92. }
  93. return false;
  94. }
  95. //-----------------------------------------------------------------------------
  96. int32_t Generic::as_int32_t() const
  97. {
  98. int32_t value = 0;
  99. as_int32_t(value);
  100. return value;
  101. }
  102. //-----------------------------------------------------------------------------
  103. bool Generic::as_uint32_t(uint32_t& out) const
  104. {
  105. if (m_type == GT_INT)
  106. {
  107. out = (uint32_t)m_data.int32_t_value;
  108. return true;
  109. }
  110. if (m_type == GT_UINT)
  111. {
  112. out = m_data.uint32_t_value;
  113. return true;
  114. }
  115. if (m_type == GT_USHORT)
  116. {
  117. out = (uint32_t)m_data.uint16_t_value;
  118. return true;
  119. }
  120. if (m_type == GT_FLOAT)
  121. {
  122. out = (uint32_t)m_data.float_value;
  123. return true;
  124. }
  125. if (m_type == GT_BOOL)
  126. {
  127. out = m_data.bool_value?1:0;
  128. return true;
  129. }
  130. return false;
  131. }
  132. //-----------------------------------------------------------------------------
  133. uint32_t Generic::as_uint32_t() const
  134. {
  135. uint32_t value = 0;
  136. as_uint32_t(value);
  137. return value;
  138. }
  139. //-----------------------------------------------------------------------------
  140. bool Generic::as_uint16_t(uint16_t& out) const
  141. {
  142. if (m_type == GT_INT)
  143. {
  144. out = (uint16_t)m_data.int32_t_value;
  145. return true;
  146. }
  147. if (m_type == GT_UINT)
  148. {
  149. out = (uint16_t)m_data.uint32_t_value;
  150. return true;
  151. }
  152. if (m_type == GT_USHORT)
  153. {
  154. out = m_data.uint16_t_value;
  155. return true;
  156. }
  157. if (m_type == GT_FLOAT)
  158. {
  159. out = (uint16_t)m_data.float_value;
  160. return true;
  161. }
  162. if (m_type == GT_BOOL)
  163. {
  164. out = m_data.bool_value?1:0;
  165. return true;
  166. }
  167. return false;
  168. }
  169. //-----------------------------------------------------------------------------
  170. uint16_t Generic::as_uint16_t() const
  171. {
  172. uint16_t value = 0;
  173. as_uint16_t(value);
  174. return value;
  175. }
  176. //-----------------------------------------------------------------------------
  177. bool Generic::as_float(float& out) const
  178. {
  179. if (m_type == GT_INT)
  180. {
  181. out = (float)m_data.int32_t_value;
  182. return true;
  183. }
  184. if (m_type == GT_UINT)
  185. {
  186. out = (float)m_data.uint32_t_value;
  187. return true;
  188. }
  189. if (m_type == GT_USHORT)
  190. {
  191. out = (float)m_data.uint16_t_value;
  192. return true;
  193. }
  194. if (m_type == GT_FLOAT)
  195. {
  196. out = m_data.float_value;
  197. return true;
  198. }
  199. if (m_type == GT_BOOL)
  200. {
  201. out = m_data.bool_value?1.0f:0.0f;
  202. return true;
  203. }
  204. return false;
  205. }
  206. //-----------------------------------------------------------------------------
  207. float Generic::as_float() const
  208. {
  209. float value = 0.0f;
  210. as_float(value);
  211. return value;
  212. }
  213. //-----------------------------------------------------------------------------
  214. bool Generic::as_bool(bool& out) const
  215. {
  216. if (m_type == GT_INT)
  217. {
  218. out = m_data.int32_t_value==0?false:true;
  219. return true;
  220. }
  221. if (m_type == GT_UINT)
  222. {
  223. out = m_data.uint32_t_value==0?false:true;
  224. return true;
  225. }
  226. if (m_type == GT_USHORT)
  227. {
  228. out = m_data.uint16_t_value==0?false:true;
  229. return true;
  230. }
  231. if (m_type == GT_FLOAT)
  232. {
  233. //Does it make sense?
  234. out = m_data.float_value==0.0f?false:true;
  235. return true;
  236. }
  237. if (m_type == GT_BOOL)
  238. {
  239. out = m_data.bool_value;
  240. return true;
  241. }
  242. return false;
  243. }
  244. //-----------------------------------------------------------------------------
  245. bool Generic::as_bool() const
  246. {
  247. bool value = false;
  248. as_bool(value);
  249. return value;
  250. }
  251. //-----------------------------------------------------------------------------
  252. bool Generic::operator==(const Generic& other)
  253. {
  254. if (m_type != other.m_type)
  255. {
  256. return false;
  257. }
  258. switch (m_type)
  259. {
  260. case GT_INT:
  261. return m_data.int32_t_value == other.m_data.int32_t_value;
  262. break;
  263. case GT_UINT:
  264. return m_data.uint32_t_value == other.m_data.uint32_t_value;
  265. break;
  266. case GT_USHORT:
  267. return m_data.uint16_t_value == other.m_data.uint16_t_value;
  268. break;
  269. case GT_FLOAT:
  270. return m_data.float_value == other.m_data.float_value;
  271. break;
  272. case GT_BOOL:
  273. return m_data.bool_value == other.m_data.bool_value;
  274. break;
  275. case GT_UNDEFINED:
  276. return true;
  277. break;
  278. }
  279. return false;
  280. }
  281. //-----------------------------------------------------------------------------
  282. const Generic& Generic::operator=(const Generic& other)
  283. {
  284. m_type = other.m_type;
  285. switch (m_type)
  286. {
  287. case GT_INT:
  288. m_data.int32_t_value = other.m_data.int32_t_value;
  289. break;
  290. case GT_UINT:
  291. m_data.uint32_t_value = other.m_data.uint32_t_value;
  292. break;
  293. case GT_USHORT:
  294. m_data.uint16_t_value = other.m_data.uint16_t_value;
  295. break;
  296. case GT_FLOAT:
  297. m_data.float_value = other.m_data.float_value;
  298. break;
  299. case GT_BOOL:
  300. m_data.bool_value = other.m_data.bool_value;
  301. break;
  302. case GT_UNDEFINED:
  303. //Copy nothing
  304. break;
  305. }
  306. return other;
  307. }
  308. } // namespace crown