CCValue.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __cocos2d_libs__CCValue__
  22. #define __cocos2d_libs__CCValue__
  23. #include "platform/CCPlatformMacros.h"
  24. #include "base/ccMacros.h"
  25. #include <string>
  26. #include <vector>
  27. #include <unordered_map>
  28. /**
  29. * @addtogroup base
  30. * @{
  31. */
  32. NS_CC_BEGIN
  33. class Value;
  34. typedef std::vector<Value> ValueVector;
  35. typedef std::unordered_map<std::string, Value> ValueMap;
  36. typedef std::unordered_map<int, Value> ValueMapIntKey;
  37. CC_DLL extern const ValueVector ValueVectorNull;
  38. CC_DLL extern const ValueMap ValueMapNull;
  39. CC_DLL extern const ValueMapIntKey ValueMapIntKeyNull;
  40. /*
  41. * This class is provide as a wrapper of basic types, such as int and bool.
  42. */
  43. class CC_DLL Value
  44. {
  45. public:
  46. /** A predefined Value that has not value. */
  47. static const Value Null;
  48. /** Default constructor. */
  49. Value();
  50. /** Create a Value by an unsigned char value. */
  51. explicit Value(unsigned char v);
  52. /** Create a Value by an integer value. */
  53. explicit Value(int v);
  54. /** Create a Value by an unsigned value. */
  55. explicit Value(unsigned int v);
  56. /** Create a Value by a float value. */
  57. explicit Value(float v);
  58. /** Create a Value by a double value. */
  59. explicit Value(double v);
  60. /** Create a Value by a bool value. */
  61. explicit Value(bool v);
  62. /** Create a Value by a char pointer. It will copy the chars internally. */
  63. explicit Value(const char* v);
  64. /** Create a Value by a string. */
  65. explicit Value(const std::string& v);
  66. /** Create a Value by a ValueVector object. */
  67. explicit Value(const ValueVector& v);
  68. /** Create a Value by a ValueVector object. It will use std::move internally. */
  69. explicit Value(ValueVector&& v);
  70. /** Create a Value by a ValueMap object. */
  71. explicit Value(const ValueMap& v);
  72. /** Create a Value by a ValueMap object. It will use std::move internally. */
  73. explicit Value(ValueMap&& v);
  74. /** Create a Value by a ValueMapIntKey object. */
  75. explicit Value(const ValueMapIntKey& v);
  76. /** Create a Value by a ValueMapIntKey object. It will use std::move internally. */
  77. explicit Value(ValueMapIntKey&& v);
  78. /** Create a Value by another Value object. */
  79. Value(const Value& other);
  80. /** Create a Value by a Value object. It will use std::move internally. */
  81. Value(Value&& other);
  82. /** Destructor. */
  83. ~Value();
  84. /** Assignment operator, assign from Value to Value. */
  85. Value& operator= (const Value& other);
  86. /** Assignment operator, assign from Value to Value. It will use std::move internally. */
  87. Value& operator= (Value&& other);
  88. /** Assignment operator, assign from unsigned char to Value. */
  89. Value& operator= (unsigned char v);
  90. /** Assignment operator, assign from integer to Value. */
  91. Value& operator= (int v);
  92. /** Assignment operator, assign from integer to Value. */
  93. Value& operator= (unsigned int v);
  94. /** Assignment operator, assign from float to Value. */
  95. Value& operator= (float v);
  96. /** Assignment operator, assign from double to Value. */
  97. Value& operator= (double v);
  98. /** Assignment operator, assign from bool to Value. */
  99. Value& operator= (bool v);
  100. /** Assignment operator, assign from char* to Value. */
  101. Value& operator= (const char* v);
  102. /** Assignment operator, assign from string to Value. */
  103. Value& operator= (const std::string& v);
  104. /** Assignment operator, assign from ValueVector to Value. */
  105. Value& operator= (const ValueVector& v);
  106. /** Assignment operator, assign from ValueVector to Value. */
  107. Value& operator= (ValueVector&& v);
  108. /** Assignment operator, assign from ValueMap to Value. */
  109. Value& operator= (const ValueMap& v);
  110. /** Assignment operator, assign from ValueMap to Value. It will use std::move internally. */
  111. Value& operator= (ValueMap&& v);
  112. /** Assignment operator, assign from ValueMapIntKey to Value. */
  113. Value& operator= (const ValueMapIntKey& v);
  114. /** Assignment operator, assign from ValueMapIntKey to Value. It will use std::move internally. */
  115. Value& operator= (ValueMapIntKey&& v);
  116. /** != operator overloading */
  117. bool operator!= (const Value& v);
  118. /** != operator overloading */
  119. bool operator!= (const Value& v) const;
  120. /** == operator overloading */
  121. bool operator== (const Value& v);
  122. /** == operator overloading */
  123. bool operator== (const Value& v) const;
  124. /** Gets as a byte value. Will convert to unsigned char if possible, or will trigger assert error. */
  125. unsigned char asByte() const;
  126. /** Gets as an integer value. Will convert to integer if possible, or will trigger assert error. */
  127. int asInt() const;
  128. /** Gets as an unsigned value. Will convert to unsigned if possible, or will trigger assert error. */
  129. unsigned int asUnsignedInt() const;
  130. /** Gets as a float value. Will convert to float if possible, or will trigger assert error. */
  131. float asFloat() const;
  132. /** Gets as a double value. Will convert to double if possible, or will trigger assert error. */
  133. double asDouble() const;
  134. /** Gets as a bool value. Will convert to bool if possible, or will trigger assert error. */
  135. bool asBool() const;
  136. /** Gets as a string value. Will convert to string if possible, or will trigger assert error. */
  137. std::string asString() const;
  138. /** Gets as a ValueVector reference. Will convert to ValueVector if possible, or will trigger assert error. */
  139. ValueVector& asValueVector();
  140. /** Gets as a const ValueVector reference. Will convert to ValueVector if possible, or will trigger assert error. */
  141. const ValueVector& asValueVector() const;
  142. /** Gets as a ValueMap reference. Will convert to ValueMap if possible, or will trigger assert error. */
  143. ValueMap& asValueMap();
  144. /** Gets as a const ValueMap reference. Will convert to ValueMap if possible, or will trigger assert error. */
  145. const ValueMap& asValueMap() const;
  146. /** Gets as a ValueMapIntKey reference. Will convert to ValueMapIntKey if possible, or will trigger assert error. */
  147. ValueMapIntKey& asIntKeyMap();
  148. /** Gets as a const ValueMapIntKey reference. Will convert to ValueMapIntKey if possible, or will trigger assert error. */
  149. const ValueMapIntKey& asIntKeyMap() const;
  150. /**
  151. * Checks if the Value is null.
  152. * @return True if the Value is null, false if not.
  153. */
  154. bool isNull() const { return _type == Type::NONE; }
  155. /** Value type wrapped by Value. */
  156. enum class Type
  157. {
  158. /// no value is wrapped, an empty Value
  159. NONE = 0,
  160. /// wrap byte
  161. BYTE,
  162. /// wrap integer
  163. INTEGER,
  164. /// wrap unsigned
  165. UNSIGNED,
  166. /// wrap float
  167. FLOAT,
  168. /// wrap double
  169. DOUBLE,
  170. /// wrap bool
  171. BOOLEAN,
  172. /// wrap string
  173. STRING,
  174. /// wrap vector
  175. VECTOR,
  176. /// wrap ValueMap
  177. MAP,
  178. /// wrap ValueMapIntKey
  179. INT_KEY_MAP
  180. };
  181. /** Gets the value type. */
  182. Type getType() const { return _type; }
  183. /** Gets the description of the class. */
  184. std::string getDescription() const;
  185. private:
  186. void clear();
  187. void reset(Type type);
  188. union
  189. {
  190. unsigned char byteVal;
  191. int intVal;
  192. unsigned int unsignedVal;
  193. float floatVal;
  194. double doubleVal;
  195. bool boolVal;
  196. std::string* strVal;
  197. ValueVector* vectorVal;
  198. ValueMap* mapVal;
  199. ValueMapIntKey* intKeyMapVal;
  200. }_field;
  201. Type _type;
  202. };
  203. /** @} */
  204. NS_CC_END
  205. #endif /* defined(__cocos2d_libs__CCValue__) */