2013-12-04 16:18:22 +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.
|
|
|
|
****************************************************************************/
|
2013-11-29 22:48:47 +08:00
|
|
|
|
|
|
|
#include "CCValue.h"
|
2013-12-04 16:18:22 +08:00
|
|
|
#include <sstream>
|
2013-11-29 22:48:47 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
Value::Value()
|
2013-12-04 17:46:57 +08:00
|
|
|
: _vectorData(nullptr)
|
|
|
|
, _mapData(nullptr)
|
|
|
|
, _intKeyMapData(nullptr)
|
2013-12-04 16:18:22 +08:00
|
|
|
, _type(Type::NONE)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Value(int v)
|
2013-12-04 17:46:57 +08:00
|
|
|
: _vectorData(nullptr)
|
|
|
|
, _mapData(nullptr)
|
|
|
|
, _intKeyMapData(nullptr)
|
2013-12-04 16:18:22 +08:00
|
|
|
, _type(Type::INTEGER)
|
|
|
|
{
|
|
|
|
_baseData.intVal = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Value(float v)
|
2013-12-04 17:46:57 +08:00
|
|
|
: _vectorData(nullptr)
|
|
|
|
, _mapData(nullptr)
|
|
|
|
, _intKeyMapData(nullptr)
|
2013-12-04 16:18:22 +08:00
|
|
|
, _type(Type::FLOAT)
|
|
|
|
{
|
|
|
|
_baseData.floatVal = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Value(double v)
|
2013-12-04 17:46:57 +08:00
|
|
|
: _vectorData(nullptr)
|
|
|
|
, _mapData(nullptr)
|
|
|
|
, _intKeyMapData(nullptr)
|
2013-12-04 16:18:22 +08:00
|
|
|
, _type(Type::DOUBLE)
|
|
|
|
{
|
|
|
|
_baseData.doubleVal = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Value(bool v)
|
2013-12-04 17:46:57 +08:00
|
|
|
: _vectorData(nullptr)
|
|
|
|
, _mapData(nullptr)
|
|
|
|
, _intKeyMapData(nullptr)
|
2013-12-04 16:18:22 +08:00
|
|
|
, _type(Type::BOOLEAN)
|
|
|
|
{
|
|
|
|
_baseData.boolVal = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Value(const char* v)
|
2013-12-04 17:46:57 +08:00
|
|
|
: _vectorData(nullptr)
|
|
|
|
, _mapData(nullptr)
|
|
|
|
, _intKeyMapData(nullptr)
|
2013-12-04 16:18:22 +08:00
|
|
|
, _type(Type::STRING)
|
|
|
|
{
|
|
|
|
_strData = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Value(const std::string& v)
|
2013-12-04 17:46:57 +08:00
|
|
|
: _vectorData(nullptr)
|
|
|
|
, _mapData(nullptr)
|
|
|
|
, _intKeyMapData(nullptr)
|
2013-12-04 16:18:22 +08:00
|
|
|
, _type(Type::STRING)
|
|
|
|
{
|
|
|
|
_strData = v;
|
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
Value::Value(const ValueVector& v)
|
|
|
|
: _vectorData(new ValueVector())
|
|
|
|
, _mapData(nullptr)
|
|
|
|
, _intKeyMapData(nullptr)
|
|
|
|
, _type(Type::VECTOR)
|
2013-12-04 16:18:22 +08:00
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
*_vectorData = v;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
Value::Value(ValueVector&& v)
|
|
|
|
: _vectorData(new ValueVector())
|
|
|
|
, _mapData(nullptr)
|
|
|
|
, _intKeyMapData(nullptr)
|
|
|
|
, _type(Type::VECTOR)
|
2013-12-04 16:37:48 +08:00
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
*_vectorData = std::move(v);
|
2013-12-04 16:37:48 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
Value::Value(const ValueMap& v)
|
|
|
|
: _vectorData(nullptr)
|
|
|
|
, _mapData(new ValueMap())
|
|
|
|
, _intKeyMapData(nullptr)
|
|
|
|
, _type(Type::MAP)
|
2013-12-04 16:18:22 +08:00
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
*_mapData = v;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
Value::Value(ValueMap&& v)
|
|
|
|
: _vectorData(nullptr)
|
|
|
|
, _mapData(new ValueMap())
|
|
|
|
, _intKeyMapData(nullptr)
|
|
|
|
, _type(Type::MAP)
|
2013-12-04 16:18:22 +08:00
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
*_mapData = std::move(v);
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
Value::Value(const IntValueMap& v)
|
|
|
|
: _vectorData(nullptr)
|
|
|
|
, _mapData(nullptr)
|
|
|
|
, _intKeyMapData(new IntValueMap())
|
|
|
|
, _type(Type::INT_KEY_MAP)
|
2013-12-04 16:18:22 +08:00
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
*_intKeyMapData = v;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
Value::Value(IntValueMap&& v)
|
|
|
|
: _vectorData(nullptr)
|
|
|
|
, _mapData(nullptr)
|
|
|
|
, _intKeyMapData(new IntValueMap())
|
|
|
|
, _type(Type::INT_KEY_MAP)
|
2013-12-04 16:37:48 +08:00
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
*_intKeyMapData = std::move(v);
|
2013-12-04 16:37:48 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
Value::Value(const Value& other)
|
2013-12-04 17:46:57 +08:00
|
|
|
: _vectorData(nullptr)
|
|
|
|
, _mapData(nullptr)
|
|
|
|
, _intKeyMapData(nullptr)
|
2013-12-04 16:18:22 +08:00
|
|
|
{
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Value(Value&& other)
|
2013-12-04 17:46:57 +08:00
|
|
|
: _vectorData(nullptr)
|
|
|
|
, _mapData(nullptr)
|
|
|
|
, _intKeyMapData(nullptr)
|
2013-12-04 16:18:22 +08:00
|
|
|
{
|
|
|
|
*this = std::move(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::~Value()
|
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
CC_SAFE_DELETE(_vectorData);
|
|
|
|
CC_SAFE_DELETE(_mapData);
|
|
|
|
CC_SAFE_DELETE(_intKeyMapData);
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (const Value& other)
|
|
|
|
{
|
|
|
|
switch (other._type) {
|
|
|
|
case Type::INTEGER:
|
|
|
|
_baseData.intVal = other._baseData.intVal;
|
|
|
|
break;
|
|
|
|
case Type::FLOAT:
|
|
|
|
_baseData.floatVal = other._baseData.floatVal;
|
|
|
|
break;
|
|
|
|
case Type::DOUBLE:
|
|
|
|
_baseData.doubleVal = other._baseData.doubleVal;
|
|
|
|
break;
|
|
|
|
case Type::BOOLEAN:
|
|
|
|
_baseData.boolVal = other._baseData.boolVal;
|
|
|
|
break;
|
|
|
|
case Type::STRING:
|
|
|
|
_strData = other._strData;
|
|
|
|
break;
|
2013-12-04 17:46:57 +08:00
|
|
|
case Type::VECTOR:
|
|
|
|
if (_vectorData == nullptr)
|
|
|
|
_vectorData = new ValueVector();
|
|
|
|
*_vectorData = *other._vectorData;
|
2013-12-04 16:18:22 +08:00
|
|
|
break;
|
2013-12-04 17:46:57 +08:00
|
|
|
case Type::MAP:
|
|
|
|
if (_mapData == nullptr)
|
|
|
|
_mapData = new ValueMap();
|
|
|
|
*_mapData = *other._mapData;
|
2013-12-04 16:18:22 +08:00
|
|
|
break;
|
2013-12-04 17:46:57 +08:00
|
|
|
case Type::INT_KEY_MAP:
|
|
|
|
if (_intKeyMapData == nullptr)
|
|
|
|
_intKeyMapData = new IntValueMap();
|
|
|
|
*_intKeyMapData = *other._intKeyMapData;
|
2013-12-04 16:18:22 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_type = other._type;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (Value&& other)
|
|
|
|
{
|
|
|
|
switch (other._type) {
|
|
|
|
case Type::INTEGER:
|
|
|
|
_baseData.intVal = other._baseData.intVal;
|
|
|
|
break;
|
|
|
|
case Type::FLOAT:
|
|
|
|
_baseData.floatVal = other._baseData.floatVal;
|
|
|
|
break;
|
|
|
|
case Type::DOUBLE:
|
|
|
|
_baseData.doubleVal = other._baseData.doubleVal;
|
|
|
|
break;
|
|
|
|
case Type::BOOLEAN:
|
|
|
|
_baseData.boolVal = other._baseData.boolVal;
|
|
|
|
break;
|
|
|
|
case Type::STRING:
|
|
|
|
_strData = other._strData;
|
|
|
|
break;
|
2013-12-04 17:46:57 +08:00
|
|
|
case Type::VECTOR:
|
|
|
|
CC_SAFE_DELETE(_vectorData);
|
|
|
|
_vectorData = other._vectorData;
|
2013-12-04 16:18:22 +08:00
|
|
|
break;
|
2013-12-04 17:46:57 +08:00
|
|
|
case Type::MAP:
|
|
|
|
CC_SAFE_DELETE(_mapData);
|
|
|
|
_mapData = other._mapData;
|
2013-12-04 16:18:22 +08:00
|
|
|
break;
|
2013-12-04 17:46:57 +08:00
|
|
|
case Type::INT_KEY_MAP:
|
|
|
|
CC_SAFE_DELETE(_intKeyMapData);
|
|
|
|
_intKeyMapData = other._intKeyMapData;
|
2013-12-04 16:18:22 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_type = other._type;
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
other._vectorData = nullptr;
|
|
|
|
other._mapData = nullptr;
|
|
|
|
other._intKeyMapData = nullptr;
|
2013-12-04 16:18:22 +08:00
|
|
|
other._type = Type::NONE;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Value::asInt() const
|
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
CCASSERT(_type != Type::VECTOR && _type != Type::MAP, "");
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::INTEGER)
|
|
|
|
{
|
|
|
|
return _baseData.intVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::STRING)
|
|
|
|
{
|
|
|
|
return atoi(_strData.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::FLOAT)
|
|
|
|
{
|
|
|
|
return _baseData.floatVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::DOUBLE)
|
|
|
|
{
|
|
|
|
return _baseData.doubleVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::BOOLEAN)
|
|
|
|
{
|
|
|
|
return _baseData.boolVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Value::asFloat() const
|
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
CCASSERT(_type != Type::VECTOR && _type != Type::MAP, "");
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::FLOAT)
|
|
|
|
{
|
|
|
|
return _baseData.floatVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::STRING)
|
|
|
|
{
|
|
|
|
return atof(_strData.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::INTEGER)
|
|
|
|
{
|
|
|
|
return _baseData.intVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::DOUBLE)
|
|
|
|
{
|
|
|
|
return _baseData.doubleVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::BOOLEAN)
|
|
|
|
{
|
|
|
|
return _baseData.boolVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
double Value::asDouble() const
|
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
CCASSERT(_type != Type::VECTOR && _type != Type::MAP, "");
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::DOUBLE)
|
|
|
|
{
|
|
|
|
return _baseData.doubleVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::STRING)
|
|
|
|
{
|
|
|
|
return (float)atof(_strData.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::INTEGER)
|
|
|
|
{
|
|
|
|
return _baseData.intVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::FLOAT)
|
|
|
|
{
|
|
|
|
return _baseData.floatVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::BOOLEAN)
|
|
|
|
{
|
|
|
|
return _baseData.boolVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::asBool() const
|
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
CCASSERT(_type != Type::VECTOR && _type != Type::MAP, "");
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::BOOLEAN)
|
|
|
|
{
|
|
|
|
return _baseData.boolVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::STRING)
|
|
|
|
{
|
|
|
|
return (_strData == "0" || _strData == "false") ? false : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::INTEGER)
|
|
|
|
{
|
|
|
|
return _baseData.intVal == 0 ? false : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::FLOAT)
|
|
|
|
{
|
|
|
|
return _baseData.floatVal == 0.0f ? false : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::DOUBLE)
|
|
|
|
{
|
|
|
|
return _baseData.doubleVal == 0.0 ? false : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Value::asString() const
|
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
CCASSERT(_type != Type::VECTOR && _type != Type::MAP, "");
|
2013-12-04 16:18:22 +08:00
|
|
|
|
|
|
|
if (_type == Type::STRING)
|
|
|
|
{
|
|
|
|
return _strData;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream ret;
|
|
|
|
|
|
|
|
switch (_type) {
|
|
|
|
case Type::INTEGER:
|
|
|
|
ret << _baseData.intVal;
|
|
|
|
break;
|
|
|
|
case Type::FLOAT:
|
|
|
|
ret << _baseData.floatVal;
|
|
|
|
break;
|
|
|
|
case Type::DOUBLE:
|
|
|
|
ret << _baseData.doubleVal;
|
|
|
|
break;
|
|
|
|
case Type::BOOLEAN:
|
|
|
|
ret << _baseData.boolVal;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret.str();
|
|
|
|
}
|
2013-11-29 22:48:47 +08:00
|
|
|
|
|
|
|
NS_CC_END
|