Generic.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #pragma once
  24. #include "Types.h"
  25. namespace crown
  26. {
  27. class Generic
  28. {
  29. public:
  30. enum Type
  31. {
  32. GT_UNDEFINED = 0,
  33. // Basic types
  34. GT_INT = 1,
  35. GT_UINT = 2,
  36. GT_USHORT = 3,
  37. GT_FLOAT = 4,
  38. GT_BOOL = 5
  39. };
  40. Generic();
  41. Generic(const Generic& other);
  42. ~Generic();
  43. explicit Generic(int32_t value);
  44. explicit Generic(uint32_t value);
  45. explicit Generic(uint16_t value);
  46. explicit Generic(float value);
  47. explicit Generic(bool value);
  48. explicit Generic(const char* value);
  49. inline Type get_type() const { return m_type; }
  50. bool as_int32_t(int32_t& out) const;
  51. int32_t as_int32_t() const;
  52. bool as_uint32_t(uint32_t& out) const;
  53. uint32_t as_uint32_t() const;
  54. bool as_uint16_t(uint16_t& out) const;
  55. uint16_t as_uint16_t() const;
  56. bool as_float(float& out) const;
  57. float as_float() const;
  58. bool as_bool(bool& out) const;
  59. bool as_bool() const;
  60. template <typename T>
  61. bool as_type(T* out) const;
  62. template <typename T>
  63. T* as_type() const;
  64. const Generic& operator=(const Generic& other);
  65. bool operator==(const Generic& other);
  66. private:
  67. union Data
  68. {
  69. int32_t int32_t_value;
  70. uint32_t uint32_t_value;
  71. uint16_t uint16_t_value;
  72. float float_value;
  73. bool bool_value;
  74. };
  75. Type m_type;
  76. Data m_data;
  77. };
  78. template <typename T>
  79. bool Generic::as_type(T* out) const
  80. {
  81. // if (mType == GT_Object)
  82. // {
  83. // *out = dynamic_cas_t<T>(mData.object_value);
  84. // return *out != NULL || mData.object_value == NULL;
  85. // }
  86. return false;
  87. }
  88. template <typename T>
  89. T* Generic::as_type() const
  90. {
  91. // if (mType == GT_Object)
  92. // {
  93. // return dynamic_cast<T*>(mData.object_value);
  94. // }
  95. return NULL;
  96. }
  97. } //namespace crown