2013-11-29 22:48:47 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies
|
2013-11-29 22:48:47 +08:00
|
|
|
|
|
|
|
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__
|
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "base/CCPlatformMacros.h"
|
|
|
|
#include "2d/ccMacros.h"
|
2013-11-29 22:48:47 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class Value;
|
2013-12-04 16:18:22 +08:00
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
typedef std::vector<Value> ValueVector;
|
|
|
|
typedef std::unordered_map<std::string, Value> ValueMap;
|
2013-12-26 22:07:20 +08:00
|
|
|
typedef std::unordered_map<int, Value> ValueMapIntKey;
|
2013-11-29 22:48:47 +08:00
|
|
|
|
|
|
|
class Value
|
|
|
|
{
|
|
|
|
public:
|
2013-12-11 14:59:18 +08:00
|
|
|
static const Value Null;
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
Value();
|
2013-12-09 16:38:27 +08:00
|
|
|
explicit Value(unsigned char v);
|
2013-12-04 16:18:22 +08:00
|
|
|
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);
|
2013-12-04 16:37:48 +08:00
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
explicit Value(const ValueVector& v);
|
|
|
|
explicit Value(ValueVector&& v);
|
2013-12-04 16:37:48 +08:00
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
explicit Value(const ValueMap& v);
|
|
|
|
explicit Value(ValueMap&& v);
|
2013-12-04 16:37:48 +08:00
|
|
|
|
2013-12-26 22:07:20 +08:00
|
|
|
explicit Value(const ValueMapIntKey& v);
|
|
|
|
explicit Value(ValueMapIntKey&& v);
|
2013-12-04 16:37:48 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
Value(const Value& other);
|
|
|
|
Value(Value&& other);
|
|
|
|
~Value();
|
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
// assignment operator
|
2013-12-04 16:18:22 +08:00
|
|
|
Value& operator= (const Value& other);
|
|
|
|
Value& operator= (Value&& other);
|
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
Value& operator= (unsigned char v);
|
|
|
|
Value& operator= (int v);
|
|
|
|
Value& operator= (float v);
|
|
|
|
Value& operator= (double v);
|
|
|
|
Value& operator= (bool v);
|
|
|
|
Value& operator= (const char* v);
|
|
|
|
Value& operator= (const std::string& v);
|
|
|
|
|
|
|
|
Value& operator= (const ValueVector& v);
|
|
|
|
Value& operator= (ValueVector&& v);
|
|
|
|
|
|
|
|
Value& operator= (const ValueMap& v);
|
|
|
|
Value& operator= (ValueMap&& v);
|
|
|
|
|
2013-12-26 22:07:20 +08:00
|
|
|
Value& operator= (const ValueMapIntKey& v);
|
|
|
|
Value& operator= (ValueMapIntKey&& v);
|
2013-12-09 16:38:27 +08:00
|
|
|
|
|
|
|
unsigned char asByte() const;
|
2013-12-04 16:18:22 +08:00
|
|
|
int asInt() const;
|
|
|
|
float asFloat() const;
|
|
|
|
double asDouble() const;
|
|
|
|
bool asBool() const;
|
|
|
|
std::string asString() const;
|
|
|
|
|
2014-01-06 19:55:59 +08:00
|
|
|
ValueVector& asValueVector();
|
|
|
|
const ValueVector& asValueVector() const;
|
2013-12-04 16:18:22 +08:00
|
|
|
|
2014-01-06 19:55:59 +08:00
|
|
|
ValueMap& asValueMap();
|
|
|
|
const ValueMap& asValueMap() const;
|
2013-12-04 16:18:22 +08:00
|
|
|
|
2014-01-06 19:55:59 +08:00
|
|
|
ValueMapIntKey& asIntKeyMap();
|
|
|
|
const ValueMapIntKey& asIntKeyMap() const;
|
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,
|
2013-12-09 16:38:27 +08:00
|
|
|
BYTE,
|
2013-11-29 22:48:47 +08:00
|
|
|
INTEGER,
|
|
|
|
FLOAT,
|
|
|
|
DOUBLE,
|
|
|
|
BOOLEAN,
|
|
|
|
STRING,
|
2013-12-04 17:46:57 +08:00
|
|
|
VECTOR,
|
|
|
|
MAP,
|
|
|
|
INT_KEY_MAP
|
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
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
std::string getDescription();
|
2013-12-09 16:38:27 +08:00
|
|
|
|
2013-11-29 22:48:47 +08:00
|
|
|
private:
|
2013-12-11 14:39:21 +08:00
|
|
|
void clear();
|
|
|
|
|
2013-11-29 22:48:47 +08:00
|
|
|
union
|
|
|
|
{
|
2013-12-09 16:38:27 +08:00
|
|
|
unsigned char byteVal;
|
2013-11-29 22:48:47 +08:00
|
|
|
int intVal;
|
|
|
|
float floatVal;
|
|
|
|
double doubleVal;
|
|
|
|
bool boolVal;
|
|
|
|
}_baseData;
|
|
|
|
|
|
|
|
std::string _strData;
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueVector* _vectorData;
|
|
|
|
ValueMap* _mapData;
|
2013-12-26 22:07:20 +08:00
|
|
|
ValueMapIntKey* _intKeyMapData;
|
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__) */
|