2013-11-29 22:48:47 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __cocos2d_libs__CCValue__
|
|
|
|
#define __cocos2d_libs__CCValue__
|
|
|
|
|
|
|
|
#include "CCPlatformMacros.h"
|
|
|
|
#include "ccMacros.h"
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class Value;
|
2013-12-04 16:18:22 +08:00
|
|
|
|
2013-12-02 15:03:13 +08:00
|
|
|
typedef std::vector<Value> ValueArray;
|
|
|
|
typedef std::unordered_map<std::string, Value> ValueDict;
|
2013-12-03 14:47:35 +08:00
|
|
|
typedef std::unordered_map<int, Value> IntValueDict;
|
2013-11-29 22:48:47 +08:00
|
|
|
|
|
|
|
class Value
|
|
|
|
{
|
|
|
|
public:
|
2013-12-04 16:18:22 +08:00
|
|
|
Value();
|
|
|
|
explicit Value(int v);
|
|
|
|
explicit Value(float v);
|
|
|
|
explicit Value(double v);
|
|
|
|
explicit Value(bool v);
|
|
|
|
explicit Value(const char* v);
|
|
|
|
explicit Value(const std::string& v);
|
|
|
|
explicit Value(const ValueArray& v);
|
|
|
|
explicit Value(const ValueDict& v);
|
|
|
|
explicit Value(ValueDict&& v);
|
|
|
|
explicit Value(const IntValueDict& v);
|
|
|
|
Value(const Value& other);
|
|
|
|
Value(Value&& other);
|
|
|
|
~Value();
|
|
|
|
|
|
|
|
Value& operator= (const Value& other);
|
|
|
|
Value& operator= (Value&& other);
|
|
|
|
|
|
|
|
int asInt() const;
|
|
|
|
float asFloat() const;
|
|
|
|
double asDouble() const;
|
|
|
|
bool asBool() const;
|
|
|
|
std::string asString() const;
|
|
|
|
|
|
|
|
inline ValueArray& asArray() { return *_arrData; }
|
|
|
|
inline const ValueArray& asArray() const { return *_arrData; }
|
|
|
|
|
|
|
|
inline ValueDict& asDict() { return *_dictData; }
|
|
|
|
inline const ValueDict& asDict() const { return *_dictData; }
|
|
|
|
|
|
|
|
inline IntValueDict& asIntKeyDict() { return *_intKeyDictData; }
|
|
|
|
inline const IntValueDict& asIntKeyDict() const { return *_intKeyDictData; }
|
2013-12-04 15:34:05 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
inline bool isNull() const { return _type == Type::NONE; }
|
2013-12-03 14:47:35 +08:00
|
|
|
|
2013-11-29 22:48:47 +08:00
|
|
|
enum class Type
|
|
|
|
{
|
|
|
|
NONE,
|
|
|
|
INTEGER,
|
|
|
|
FLOAT,
|
|
|
|
DOUBLE,
|
|
|
|
BOOLEAN,
|
|
|
|
STRING,
|
|
|
|
ARRAY,
|
2013-12-03 14:47:35 +08:00
|
|
|
DICTIONARY,
|
|
|
|
INT_KEY_DICT
|
2013-11-29 22:48:47 +08:00
|
|
|
};
|
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
inline Type getType() const { return _type; };
|
2013-11-29 22:48:47 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
union
|
|
|
|
{
|
|
|
|
int intVal;
|
|
|
|
float floatVal;
|
|
|
|
double doubleVal;
|
|
|
|
bool boolVal;
|
|
|
|
}_baseData;
|
|
|
|
|
|
|
|
std::string _strData;
|
2013-12-04 10:33:27 +08:00
|
|
|
ValueArray* _arrData;
|
|
|
|
ValueDict* _dictData;
|
|
|
|
IntValueDict* _intKeyDictData;
|
2013-11-29 22:48:47 +08:00
|
|
|
|
|
|
|
Type _type;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
|
2013-11-29 22:48:47 +08:00
|
|
|
#endif /* defined(__cocos2d_libs__CCValue__) */
|