2013-12-04 16:18:22 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies
|
2013-12-04 16:18:22 +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.
|
|
|
|
****************************************************************************/
|
2013-11-29 22:48:47 +08:00
|
|
|
|
|
|
|
#include "CCValue.h"
|
2013-12-04 16:18:22 +08:00
|
|
|
#include <sstream>
|
2014-03-24 12:03:25 +08:00
|
|
|
#include <iomanip>
|
2013-11-29 22:48:47 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-12-11 14:59:18 +08:00
|
|
|
const Value Value::Null;
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
Value::Value()
|
2013-12-07 14:26:17 +08:00
|
|
|
: _vectorData(new ValueVector())
|
|
|
|
, _mapData(new ValueMap())
|
2013-12-26 22:07:20 +08:00
|
|
|
, _intKeyMapData(new ValueMapIntKey())
|
2013-12-04 16:18:22 +08:00
|
|
|
, _type(Type::NONE)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
Value::Value(unsigned char v)
|
|
|
|
: _vectorData(nullptr)
|
|
|
|
, _mapData(nullptr)
|
|
|
|
, _intKeyMapData(nullptr)
|
|
|
|
, _type(Type::BYTE)
|
|
|
|
{
|
|
|
|
_baseData.byteVal = v;
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
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-26 22:07:20 +08:00
|
|
|
Value::Value(const ValueMapIntKey& v)
|
2013-12-04 17:46:57 +08:00
|
|
|
: _vectorData(nullptr)
|
|
|
|
, _mapData(nullptr)
|
2013-12-26 22:07:20 +08:00
|
|
|
, _intKeyMapData(new ValueMapIntKey())
|
2013-12-04 17:46:57 +08:00
|
|
|
, _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-26 22:07:20 +08:00
|
|
|
Value::Value(ValueMapIntKey&& v)
|
2013-12-04 17:46:57 +08:00
|
|
|
: _vectorData(nullptr)
|
|
|
|
, _mapData(nullptr)
|
2013-12-26 22:07:20 +08:00
|
|
|
, _intKeyMapData(new ValueMapIntKey())
|
2013-12-04 17:46:57 +08:00
|
|
|
, _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-09 16:38:27 +08:00
|
|
|
clear();
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (const Value& other)
|
|
|
|
{
|
2014-01-02 17:54:52 +08:00
|
|
|
if (this != &other) {
|
|
|
|
switch (other._type) {
|
|
|
|
case Type::BYTE:
|
|
|
|
_baseData.byteVal = other._baseData.byteVal;
|
|
|
|
break;
|
|
|
|
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;
|
|
|
|
case Type::VECTOR:
|
|
|
|
if (_vectorData == nullptr)
|
|
|
|
_vectorData = new ValueVector();
|
|
|
|
*_vectorData = *other._vectorData;
|
|
|
|
break;
|
|
|
|
case Type::MAP:
|
|
|
|
if (_mapData == nullptr)
|
|
|
|
_mapData = new ValueMap();
|
|
|
|
*_mapData = *other._mapData;
|
|
|
|
break;
|
|
|
|
case Type::INT_KEY_MAP:
|
|
|
|
if (_intKeyMapData == nullptr)
|
|
|
|
_intKeyMapData = new ValueMapIntKey();
|
|
|
|
*_intKeyMapData = *other._intKeyMapData;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_type = other._type;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (Value&& other)
|
|
|
|
{
|
2014-01-02 17:54:52 +08:00
|
|
|
if (this != &other) {
|
|
|
|
switch (other._type) {
|
|
|
|
case Type::BYTE:
|
|
|
|
_baseData.byteVal = other._baseData.byteVal;
|
|
|
|
break;
|
|
|
|
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;
|
|
|
|
case Type::VECTOR:
|
|
|
|
CC_SAFE_DELETE(_vectorData);
|
|
|
|
_vectorData = other._vectorData;
|
|
|
|
break;
|
|
|
|
case Type::MAP:
|
|
|
|
CC_SAFE_DELETE(_mapData);
|
|
|
|
_mapData = other._mapData;
|
|
|
|
break;
|
|
|
|
case Type::INT_KEY_MAP:
|
|
|
|
CC_SAFE_DELETE(_intKeyMapData);
|
|
|
|
_intKeyMapData = other._intKeyMapData;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_type = other._type;
|
|
|
|
|
|
|
|
other._vectorData = nullptr;
|
|
|
|
other._mapData = nullptr;
|
|
|
|
other._intKeyMapData = nullptr;
|
|
|
|
other._type = Type::NONE;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
Value& Value::operator= (unsigned char v)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
_type = Type::BYTE;
|
|
|
|
_baseData.byteVal = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (int v)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
_type = Type::INTEGER;
|
|
|
|
_baseData.intVal = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (float v)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
_type = Type::FLOAT;
|
|
|
|
_baseData.floatVal = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (double v)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
_type = Type::DOUBLE;
|
|
|
|
_baseData.doubleVal = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (bool v)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
_type = Type::BOOLEAN;
|
|
|
|
_baseData.boolVal = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (const char* v)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
_type = Type::STRING;
|
|
|
|
_strData = v ? v : "";
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (const std::string& v)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
_type = Type::STRING;
|
|
|
|
_strData = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (const ValueVector& v)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
_type = Type::VECTOR;
|
|
|
|
_vectorData = new ValueVector();
|
|
|
|
*_vectorData = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (ValueVector&& v)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
_type = Type::VECTOR;
|
|
|
|
_vectorData = new ValueVector();
|
|
|
|
*_vectorData = std::move(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (const ValueMap& v)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
_type = Type::MAP;
|
|
|
|
_mapData = new ValueMap();
|
|
|
|
*_mapData = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (ValueMap&& v)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
_type = Type::MAP;
|
|
|
|
_mapData = new ValueMap();
|
|
|
|
*_mapData = std::move(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-12-26 22:07:20 +08:00
|
|
|
Value& Value::operator= (const ValueMapIntKey& v)
|
2013-12-09 16:38:27 +08:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
_type = Type::INT_KEY_MAP;
|
2013-12-26 22:07:20 +08:00
|
|
|
_intKeyMapData = new ValueMapIntKey();
|
2013-12-09 16:38:27 +08:00
|
|
|
*_intKeyMapData = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-12-26 22:07:20 +08:00
|
|
|
Value& Value::operator= (ValueMapIntKey&& v)
|
2013-12-09 16:38:27 +08:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
_type = Type::INT_KEY_MAP;
|
2013-12-26 22:07:20 +08:00
|
|
|
_intKeyMapData = new ValueMapIntKey();
|
2013-12-09 16:38:27 +08:00
|
|
|
*_intKeyMapData = std::move(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
unsigned char Value::asByte() const
|
|
|
|
{
|
|
|
|
CCASSERT(_type != Type::VECTOR && _type != Type::MAP, "");
|
|
|
|
|
|
|
|
if (_type == Type::BYTE)
|
|
|
|
{
|
|
|
|
return _baseData.byteVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::INTEGER)
|
|
|
|
{
|
|
|
|
return static_cast<unsigned char>(_baseData.intVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::STRING)
|
|
|
|
{
|
|
|
|
return static_cast<unsigned char>(atoi(_strData.c_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::FLOAT)
|
|
|
|
{
|
|
|
|
return static_cast<unsigned char>(_baseData.floatVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::DOUBLE)
|
|
|
|
{
|
|
|
|
return static_cast<unsigned char>(_baseData.doubleVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::BOOLEAN)
|
|
|
|
{
|
|
|
|
return _baseData.boolVal ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
if (_type == Type::BYTE)
|
|
|
|
{
|
|
|
|
return _baseData.byteVal;
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::STRING)
|
|
|
|
{
|
|
|
|
return atoi(_strData.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::FLOAT)
|
|
|
|
{
|
2013-12-09 16:38:27 +08:00
|
|
|
return static_cast<int>(_baseData.floatVal);
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::DOUBLE)
|
|
|
|
{
|
2013-12-09 16:38:27 +08:00
|
|
|
return static_cast<int>(_baseData.doubleVal);
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::BOOLEAN)
|
|
|
|
{
|
2013-12-09 16:38:27 +08:00
|
|
|
return _baseData.boolVal ? 1 : 0;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
if (_type == Type::BYTE)
|
|
|
|
{
|
|
|
|
return static_cast<float>(_baseData.byteVal);
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::STRING)
|
|
|
|
{
|
|
|
|
return atof(_strData.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::INTEGER)
|
|
|
|
{
|
2013-12-09 16:38:27 +08:00
|
|
|
return static_cast<float>(_baseData.intVal);
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::DOUBLE)
|
|
|
|
{
|
2013-12-09 16:38:27 +08:00
|
|
|
return static_cast<float>(_baseData.doubleVal);
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::BOOLEAN)
|
|
|
|
{
|
2013-12-09 16:38:27 +08:00
|
|
|
return _baseData.boolVal ? 1.0f : 0.0f;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
if (_type == Type::BYTE)
|
|
|
|
{
|
|
|
|
return static_cast<double>(_baseData.byteVal);
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::STRING)
|
|
|
|
{
|
2013-12-09 16:38:27 +08:00
|
|
|
return static_cast<double>(atof(_strData.c_str()));
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::INTEGER)
|
|
|
|
{
|
2013-12-09 16:38:27 +08:00
|
|
|
return static_cast<double>(_baseData.intVal);
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::FLOAT)
|
|
|
|
{
|
2013-12-09 16:38:27 +08:00
|
|
|
return static_cast<double>(_baseData.floatVal);
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::BOOLEAN)
|
|
|
|
{
|
2013-12-09 16:38:27 +08:00
|
|
|
return _baseData.boolVal ? 1.0 : 0.0;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
if (_type == Type::BYTE)
|
|
|
|
{
|
|
|
|
return _baseData.byteVal == 0 ? false : true;
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
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;
|
2014-03-24 12:03:25 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
|
|
|
|
switch (_type) {
|
2013-12-09 16:38:27 +08:00
|
|
|
case Type::BYTE:
|
|
|
|
ret << _baseData.byteVal;
|
|
|
|
break;
|
2013-12-04 16:18:22 +08:00
|
|
|
case Type::INTEGER:
|
|
|
|
ret << _baseData.intVal;
|
|
|
|
break;
|
|
|
|
case Type::FLOAT:
|
2014-03-24 12:03:25 +08:00
|
|
|
ret << std::fixed << std::setprecision( 7 )<< _baseData.floatVal;
|
2013-12-04 16:18:22 +08:00
|
|
|
break;
|
|
|
|
case Type::DOUBLE:
|
2014-03-24 12:03:25 +08:00
|
|
|
ret << std::fixed << std::setprecision( 16 ) << _baseData.doubleVal;
|
2013-12-04 16:18:22 +08:00
|
|
|
break;
|
|
|
|
case Type::BOOLEAN:
|
2013-12-11 14:39:21 +08:00
|
|
|
ret << (_baseData.boolVal ? "true" : "false");
|
2013-12-04 16:18:22 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret.str();
|
|
|
|
}
|
2013-11-29 22:48:47 +08:00
|
|
|
|
2014-01-06 19:55:59 +08:00
|
|
|
ValueVector& Value::asValueVector()
|
|
|
|
{
|
|
|
|
if (nullptr == _vectorData)
|
|
|
|
_vectorData = new ValueVector();
|
|
|
|
return *_vectorData;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ValueVector& Value::asValueVector() const
|
|
|
|
{
|
|
|
|
static const ValueVector EMPTY_VALUEVECTOR;
|
|
|
|
if (nullptr == _vectorData)
|
|
|
|
return EMPTY_VALUEVECTOR;
|
|
|
|
return *_vectorData;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueMap& Value::asValueMap()
|
|
|
|
{
|
|
|
|
if (nullptr == _mapData)
|
|
|
|
_mapData = new ValueMap();
|
|
|
|
return *_mapData;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ValueMap& Value::asValueMap() const
|
|
|
|
{
|
|
|
|
static const ValueMap EMPTY_VALUEMAP;
|
|
|
|
if (nullptr == _mapData)
|
|
|
|
return EMPTY_VALUEMAP;
|
|
|
|
return *_mapData;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueMapIntKey& Value::asIntKeyMap()
|
|
|
|
{
|
|
|
|
if (nullptr == _intKeyMapData)
|
|
|
|
_intKeyMapData = new ValueMapIntKey();
|
|
|
|
return *_intKeyMapData;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ValueMapIntKey& Value::asIntKeyMap() const
|
|
|
|
{
|
|
|
|
static const ValueMapIntKey EMPTY_VALUEMAP_INT_KEY;
|
|
|
|
if (nullptr == _intKeyMapData)
|
|
|
|
return EMPTY_VALUEMAP_INT_KEY;
|
|
|
|
return *_intKeyMapData;
|
|
|
|
}
|
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
static std::string getTabs(int depth)
|
|
|
|
{
|
|
|
|
std::string tabWidth;
|
|
|
|
|
|
|
|
for (int i = 0; i < depth; ++i)
|
|
|
|
{
|
|
|
|
tabWidth += "\t";
|
|
|
|
}
|
|
|
|
|
|
|
|
return tabWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string visit(const Value& v, int depth);
|
|
|
|
|
|
|
|
static std::string visitVector(const ValueVector& v, int depth)
|
|
|
|
{
|
|
|
|
std::stringstream ret;
|
|
|
|
|
|
|
|
if (depth > 0)
|
|
|
|
ret << "\n";
|
|
|
|
|
|
|
|
ret << getTabs(depth) << "[\n";
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (const auto& child : v)
|
|
|
|
{
|
|
|
|
ret << getTabs(depth+1) << i << ": " << visit(child, depth + 1);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret << getTabs(depth) << "]\n";
|
|
|
|
|
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
2013-12-18 17:01:57 +08:00
|
|
|
template <class T>
|
|
|
|
static std::string visitMap(const T& v, int depth)
|
2013-12-11 14:39:21 +08:00
|
|
|
{
|
|
|
|
std::stringstream ret;
|
|
|
|
|
|
|
|
if (depth > 0)
|
|
|
|
ret << "\n";
|
|
|
|
|
|
|
|
ret << getTabs(depth) << "{\n";
|
|
|
|
|
|
|
|
for (auto iter = v.begin(); iter != v.end(); ++iter)
|
|
|
|
{
|
|
|
|
ret << getTabs(depth + 1) << iter->first << ": ";
|
|
|
|
ret << visit(iter->second, depth + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret << getTabs(depth) << "}\n";
|
|
|
|
|
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string visit(const Value& v, int depth)
|
|
|
|
{
|
|
|
|
std::stringstream ret;
|
|
|
|
|
|
|
|
switch (v.getType())
|
|
|
|
{
|
2013-12-18 17:01:57 +08:00
|
|
|
case Value::Type::NONE:
|
2013-12-11 14:39:21 +08:00
|
|
|
case Value::Type::BYTE:
|
|
|
|
case Value::Type::INTEGER:
|
|
|
|
case Value::Type::FLOAT:
|
|
|
|
case Value::Type::DOUBLE:
|
|
|
|
case Value::Type::BOOLEAN:
|
|
|
|
case Value::Type::STRING:
|
|
|
|
ret << v.asString() << "\n";
|
|
|
|
break;
|
|
|
|
case Value::Type::VECTOR:
|
|
|
|
ret << visitVector(v.asValueVector(), depth);
|
|
|
|
break;
|
|
|
|
case Value::Type::MAP:
|
|
|
|
ret << visitMap(v.asValueMap(), depth);
|
|
|
|
break;
|
|
|
|
case Value::Type::INT_KEY_MAP:
|
2013-12-18 17:01:57 +08:00
|
|
|
ret << visitMap(v.asIntKeyMap(), depth);
|
2013-12-11 14:39:21 +08:00
|
|
|
break;
|
|
|
|
default:
|
2013-12-18 17:01:57 +08:00
|
|
|
CCASSERT(false, "Invalid type!");
|
2013-12-11 14:39:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Value::getDescription()
|
|
|
|
{
|
|
|
|
std::string ret("\n");
|
|
|
|
ret += visit(*this, 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
void Value::clear()
|
|
|
|
{
|
|
|
|
_type = Type::NONE;
|
|
|
|
_baseData.doubleVal = 0.0;
|
|
|
|
_strData.clear();
|
|
|
|
CC_SAFE_DELETE(_vectorData);
|
|
|
|
CC_SAFE_DELETE(_mapData);
|
|
|
|
CC_SAFE_DELETE(_intKeyMapData);
|
|
|
|
}
|
|
|
|
|
2013-11-29 22:48:47 +08:00
|
|
|
NS_CC_END
|