2013-12-04 16:18:22 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
http://www.cocos2d-x.org
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
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:
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
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.
|
2014-05-01 12:12:39 +08:00
|
|
|
****************************************************************************/
|
2013-11-29 22:48:47 +08:00
|
|
|
|
2014-04-27 01:35:57 +08:00
|
|
|
#include "base/CCValue.h"
|
2013-12-04 16:18:22 +08:00
|
|
|
#include <sstream>
|
2014-03-24 12:03:25 +08:00
|
|
|
#include <iomanip>
|
2014-07-14 20:45:24 +08:00
|
|
|
#include "base/ccUtils.h"
|
2013-11-29 22:48:47 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-10-29 18:47:02 +08:00
|
|
|
CC_DLL const ValueVector ValueVectorNull;
|
|
|
|
CC_DLL const ValueMap ValueMapNull;
|
|
|
|
CC_DLL const ValueMapIntKey ValueMapIntKeyNull;
|
2014-05-13 17:03:48 +08:00
|
|
|
|
2013-12-11 14:59:18 +08:00
|
|
|
const Value Value::Null;
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
Value::Value()
|
2014-05-01 12:12:39 +08:00
|
|
|
: _type(Type::NONE)
|
2013-12-04 16:18:22 +08:00
|
|
|
{
|
2014-05-12 18:45:36 +08:00
|
|
|
memset(&_field, 0, sizeof(_field));
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
Value::Value(unsigned char v)
|
2014-05-01 12:12:39 +08:00
|
|
|
: _type(Type::BYTE)
|
2013-12-09 16:38:27 +08:00
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.byteVal = v;
|
2013-12-09 16:38:27 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
Value::Value(int v)
|
2014-05-01 12:12:39 +08:00
|
|
|
: _type(Type::INTEGER)
|
2013-12-04 16:18:22 +08:00
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.intVal = v;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
2016-04-22 13:49:20 +08:00
|
|
|
Value::Value(unsigned int v)
|
|
|
|
: _type(Type::UNSIGNED)
|
|
|
|
{
|
|
|
|
_field.unsignedVal = v;
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
Value::Value(float v)
|
2014-05-01 12:12:39 +08:00
|
|
|
: _type(Type::FLOAT)
|
2013-12-04 16:18:22 +08:00
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.floatVal = v;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Value::Value(double v)
|
2014-05-01 12:12:39 +08:00
|
|
|
: _type(Type::DOUBLE)
|
2013-12-04 16:18:22 +08:00
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.doubleVal = v;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Value::Value(bool v)
|
2014-05-01 12:12:39 +08:00
|
|
|
: _type(Type::BOOLEAN)
|
2013-12-04 16:18:22 +08:00
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.boolVal = v;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Value::Value(const char* v)
|
2014-05-01 12:12:39 +08:00
|
|
|
: _type(Type::STRING)
|
2013-12-04 16:18:22 +08:00
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
_field.strVal = new (std::nothrow) std::string();
|
2014-05-01 12:12:39 +08:00
|
|
|
if (v)
|
|
|
|
{
|
|
|
|
*_field.strVal = v;
|
|
|
|
}
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Value::Value(const std::string& v)
|
2014-05-01 12:12:39 +08:00
|
|
|
: _type(Type::STRING)
|
2013-12-04 16:18:22 +08:00
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
_field.strVal = new (std::nothrow) std::string();
|
2014-05-01 12:12:39 +08:00
|
|
|
*_field.strVal = v;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
Value::Value(const ValueVector& v)
|
2014-05-01 12:12:39 +08:00
|
|
|
: _type(Type::VECTOR)
|
2013-12-04 16:18:22 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
_field.vectorVal = new (std::nothrow) ValueVector();
|
2014-05-01 12:12:39 +08:00
|
|
|
*_field.vectorVal = v;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
Value::Value(ValueVector&& v)
|
2014-05-01 12:12:39 +08:00
|
|
|
: _type(Type::VECTOR)
|
2013-12-04 16:37:48 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
_field.vectorVal = new (std::nothrow) ValueVector();
|
2014-05-01 12:12:39 +08:00
|
|
|
*_field.vectorVal = std::move(v);
|
2013-12-04 16:37:48 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
Value::Value(const ValueMap& v)
|
2014-05-01 12:12:39 +08:00
|
|
|
: _type(Type::MAP)
|
2013-12-04 16:18:22 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
_field.mapVal = new (std::nothrow) ValueMap();
|
2014-05-01 12:12:39 +08:00
|
|
|
*_field.mapVal = v;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
Value::Value(ValueMap&& v)
|
2014-05-01 12:12:39 +08:00
|
|
|
: _type(Type::MAP)
|
2013-12-04 16:18:22 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
_field.mapVal = new (std::nothrow) ValueMap();
|
2014-05-01 12:12:39 +08:00
|
|
|
*_field.mapVal = std::move(v);
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
2013-12-26 22:07:20 +08:00
|
|
|
Value::Value(const ValueMapIntKey& v)
|
2014-05-01 12:12:39 +08:00
|
|
|
: _type(Type::INT_KEY_MAP)
|
2013-12-04 16:18:22 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
_field.intKeyMapVal = new (std::nothrow) ValueMapIntKey();
|
2014-05-01 12:12:39 +08:00
|
|
|
*_field.intKeyMapVal = v;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
2013-12-26 22:07:20 +08:00
|
|
|
Value::Value(ValueMapIntKey&& v)
|
2014-05-01 12:12:39 +08:00
|
|
|
: _type(Type::INT_KEY_MAP)
|
2013-12-04 16:37:48 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
_field.intKeyMapVal = new (std::nothrow) ValueMapIntKey();
|
2014-05-01 12:12:39 +08:00
|
|
|
*_field.intKeyMapVal = std::move(v);
|
2013-12-04 16:37:48 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
Value::Value(const Value& other)
|
2014-05-12 23:32:51 +08:00
|
|
|
: _type(Type::NONE)
|
2013-12-04 16:18:22 +08:00
|
|
|
{
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Value(Value&& other)
|
2014-05-12 23:32:51 +08:00
|
|
|
: _type(Type::NONE)
|
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) {
|
2014-05-01 12:12:39 +08:00
|
|
|
reset(other._type);
|
|
|
|
|
2014-01-02 17:54:52 +08:00
|
|
|
switch (other._type) {
|
|
|
|
case Type::BYTE:
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.byteVal = other._field.byteVal;
|
2014-01-02 17:54:52 +08:00
|
|
|
break;
|
|
|
|
case Type::INTEGER:
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.intVal = other._field.intVal;
|
2014-01-02 17:54:52 +08:00
|
|
|
break;
|
2016-04-22 13:49:20 +08:00
|
|
|
case Type::UNSIGNED:
|
|
|
|
_field.unsignedVal = other._field.unsignedVal;
|
|
|
|
break;
|
2014-01-02 17:54:52 +08:00
|
|
|
case Type::FLOAT:
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.floatVal = other._field.floatVal;
|
2014-01-02 17:54:52 +08:00
|
|
|
break;
|
|
|
|
case Type::DOUBLE:
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.doubleVal = other._field.doubleVal;
|
2014-01-02 17:54:52 +08:00
|
|
|
break;
|
|
|
|
case Type::BOOLEAN:
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.boolVal = other._field.boolVal;
|
2014-01-02 17:54:52 +08:00
|
|
|
break;
|
|
|
|
case Type::STRING:
|
2014-05-12 18:45:36 +08:00
|
|
|
if (_field.strVal == nullptr)
|
|
|
|
{
|
|
|
|
_field.strVal = new std::string();
|
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
*_field.strVal = *other._field.strVal;
|
2014-01-02 17:54:52 +08:00
|
|
|
break;
|
|
|
|
case Type::VECTOR:
|
2014-05-12 18:45:36 +08:00
|
|
|
if (_field.vectorVal == nullptr)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
_field.vectorVal = new (std::nothrow) ValueVector();
|
2014-05-12 18:45:36 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
*_field.vectorVal = *other._field.vectorVal;
|
2014-01-02 17:54:52 +08:00
|
|
|
break;
|
|
|
|
case Type::MAP:
|
2014-05-12 18:45:36 +08:00
|
|
|
if (_field.mapVal == nullptr)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
_field.mapVal = new (std::nothrow) ValueMap();
|
2014-05-12 18:45:36 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
*_field.mapVal = *other._field.mapVal;
|
2014-01-02 17:54:52 +08:00
|
|
|
break;
|
|
|
|
case Type::INT_KEY_MAP:
|
2014-05-12 18:45:36 +08:00
|
|
|
if (_field.intKeyMapVal == nullptr)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
_field.intKeyMapVal = new (std::nothrow) ValueMapIntKey();
|
2014-05-12 18:45:36 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
*_field.intKeyMapVal = *other._field.intKeyMapVal;
|
2014-01-02 17:54:52 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (Value&& other)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
if (this != &other)
|
|
|
|
{
|
2014-05-12 23:32:51 +08:00
|
|
|
clear();
|
2014-05-01 12:12:39 +08:00
|
|
|
switch (other._type)
|
|
|
|
{
|
2014-01-02 17:54:52 +08:00
|
|
|
case Type::BYTE:
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.byteVal = other._field.byteVal;
|
2014-01-02 17:54:52 +08:00
|
|
|
break;
|
|
|
|
case Type::INTEGER:
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.intVal = other._field.intVal;
|
2014-01-02 17:54:52 +08:00
|
|
|
break;
|
2016-04-22 13:49:20 +08:00
|
|
|
case Type::UNSIGNED:
|
|
|
|
_field.unsignedVal = other._field.unsignedVal;
|
|
|
|
break;
|
2014-01-02 17:54:52 +08:00
|
|
|
case Type::FLOAT:
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.floatVal = other._field.floatVal;
|
2014-01-02 17:54:52 +08:00
|
|
|
break;
|
|
|
|
case Type::DOUBLE:
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.doubleVal = other._field.doubleVal;
|
2014-01-02 17:54:52 +08:00
|
|
|
break;
|
|
|
|
case Type::BOOLEAN:
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.boolVal = other._field.boolVal;
|
2014-01-02 17:54:52 +08:00
|
|
|
break;
|
|
|
|
case Type::STRING:
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.strVal = other._field.strVal;
|
2014-01-02 17:54:52 +08:00
|
|
|
break;
|
|
|
|
case Type::VECTOR:
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.vectorVal = other._field.vectorVal;
|
2014-01-02 17:54:52 +08:00
|
|
|
break;
|
|
|
|
case Type::MAP:
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.mapVal = other._field.mapVal;
|
2014-01-02 17:54:52 +08:00
|
|
|
break;
|
|
|
|
case Type::INT_KEY_MAP:
|
2014-05-01 12:12:39 +08:00
|
|
|
_field.intKeyMapVal = other._field.intKeyMapVal;
|
2014-01-02 17:54:52 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_type = other._type;
|
2014-05-01 12:12:39 +08:00
|
|
|
|
|
|
|
memset(&other._field, 0, sizeof(other._field));
|
2014-01-02 17:54:52 +08:00
|
|
|
other._type = Type::NONE;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
Value& Value::operator= (unsigned char v)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
reset(Type::BYTE);
|
|
|
|
_field.byteVal = v;
|
2013-12-09 16:38:27 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (int v)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
reset(Type::INTEGER);
|
|
|
|
_field.intVal = v;
|
2013-12-09 16:38:27 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-04-22 13:49:20 +08:00
|
|
|
Value& Value::operator= (unsigned int v)
|
|
|
|
{
|
|
|
|
reset(Type::UNSIGNED);
|
|
|
|
_field.unsignedVal = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
Value& Value::operator= (float v)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
reset(Type::FLOAT);
|
|
|
|
_field.floatVal = v;
|
2013-12-09 16:38:27 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (double v)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
reset(Type::DOUBLE);
|
|
|
|
_field.doubleVal = v;
|
2013-12-09 16:38:27 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (bool v)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
reset(Type::BOOLEAN);
|
|
|
|
_field.boolVal = v;
|
2013-12-09 16:38:27 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (const char* v)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
reset(Type::STRING);
|
|
|
|
*_field.strVal = v ? v : "";
|
2013-12-09 16:38:27 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (const std::string& v)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
reset(Type::STRING);
|
|
|
|
*_field.strVal = v;
|
2013-12-09 16:38:27 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (const ValueVector& v)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
reset(Type::VECTOR);
|
|
|
|
*_field.vectorVal = v;
|
2013-12-09 16:38:27 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (ValueVector&& v)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
reset(Type::VECTOR);
|
|
|
|
*_field.vectorVal = std::move(v);
|
2013-12-09 16:38:27 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (const ValueMap& v)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
reset(Type::MAP);
|
|
|
|
*_field.mapVal = v;
|
2013-12-09 16:38:27 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator= (ValueMap&& v)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
reset(Type::MAP);
|
|
|
|
*_field.mapVal = std::move(v);
|
2013-12-09 16:38:27 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-12-26 22:07:20 +08:00
|
|
|
Value& Value::operator= (const ValueMapIntKey& v)
|
2013-12-09 16:38:27 +08:00
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
reset(Type::INT_KEY_MAP);
|
|
|
|
*_field.intKeyMapVal = v;
|
2013-12-09 16:38:27 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-12-26 22:07:20 +08:00
|
|
|
Value& Value::operator= (ValueMapIntKey&& v)
|
2013-12-09 16:38:27 +08:00
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
reset(Type::INT_KEY_MAP);
|
|
|
|
*_field.intKeyMapVal = std::move(v);
|
2013-12-09 16:38:27 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-06-26 01:49:54 +08:00
|
|
|
bool Value::operator!= (const Value& v)
|
|
|
|
{
|
|
|
|
return !(*this == v);
|
|
|
|
}
|
|
|
|
bool Value::operator!= (const Value& v) const
|
|
|
|
{
|
|
|
|
return !(*this == v);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::operator== (const Value& v)
|
|
|
|
{
|
|
|
|
const auto &t = *this;
|
|
|
|
return t == v;
|
|
|
|
}
|
|
|
|
bool Value::operator== (const Value& v) const
|
|
|
|
{
|
|
|
|
if (this == &v) return true;
|
|
|
|
if (v._type != this->_type) return false;
|
|
|
|
if (this->isNull()) return true;
|
|
|
|
switch (_type)
|
|
|
|
{
|
2016-04-22 13:49:20 +08:00
|
|
|
case Type::BYTE: return v._field.byteVal == this->_field.byteVal;
|
|
|
|
case Type::INTEGER: return v._field.intVal == this->_field.intVal;
|
|
|
|
case Type::UNSIGNED:return v._field.unsignedVal == this->_field.unsignedVal;
|
|
|
|
case Type::BOOLEAN: return v._field.boolVal == this->_field.boolVal;
|
|
|
|
case Type::STRING: return *v._field.strVal == *this->_field.strVal;
|
|
|
|
case Type::FLOAT: return fabs(v._field.floatVal - this->_field.floatVal) <= FLT_EPSILON;
|
|
|
|
case Type::DOUBLE: return fabs(v._field.doubleVal - this->_field.doubleVal) <= FLT_EPSILON;
|
|
|
|
case Type::VECTOR:
|
2014-06-26 01:49:54 +08:00
|
|
|
{
|
2016-04-22 13:49:20 +08:00
|
|
|
const auto &v1 = *(this->_field.vectorVal);
|
|
|
|
const auto &v2 = *(v._field.vectorVal);
|
|
|
|
const auto size = v1.size();
|
|
|
|
if (size == v2.size())
|
2014-06-26 01:49:54 +08:00
|
|
|
{
|
2016-04-22 13:49:20 +08:00
|
|
|
for (size_t i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
if (v1[i] != v2[i]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
2014-06-26 01:49:54 +08:00
|
|
|
}
|
2016-04-22 13:49:20 +08:00
|
|
|
return false;
|
2014-06-26 01:49:54 +08:00
|
|
|
}
|
2016-04-22 13:49:20 +08:00
|
|
|
case Type::MAP:
|
2014-06-26 01:49:54 +08:00
|
|
|
{
|
2016-04-22 13:49:20 +08:00
|
|
|
const auto &map1 = *(this->_field.mapVal);
|
|
|
|
const auto &map2 = *(v._field.mapVal);
|
|
|
|
for (const auto &kvp : map1)
|
2014-06-26 01:49:54 +08:00
|
|
|
{
|
2016-04-22 13:49:20 +08:00
|
|
|
auto it = map2.find(kvp.first);
|
|
|
|
if (it == map2.end() || it->second != kvp.second)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-26 01:49:54 +08:00
|
|
|
}
|
2016-04-22 13:49:20 +08:00
|
|
|
return true;
|
2014-06-26 01:49:54 +08:00
|
|
|
}
|
2016-04-22 13:49:20 +08:00
|
|
|
case Type::INT_KEY_MAP:
|
2014-06-26 01:49:54 +08:00
|
|
|
{
|
2016-04-22 13:49:20 +08:00
|
|
|
const auto &map1 = *(this->_field.intKeyMapVal);
|
|
|
|
const auto &map2 = *(v._field.intKeyMapVal);
|
|
|
|
for (const auto &kvp : map1)
|
2014-06-26 01:49:54 +08:00
|
|
|
{
|
2016-04-22 13:49:20 +08:00
|
|
|
auto it = map2.find(kvp.first);
|
|
|
|
if (it == map2.end() || it->second != kvp.second)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-26 01:49:54 +08:00
|
|
|
}
|
2016-04-22 13:49:20 +08:00
|
|
|
return true;
|
2014-06-26 01:49:54 +08:00
|
|
|
}
|
2016-04-22 13:49:20 +08:00
|
|
|
default:
|
|
|
|
break;
|
2014-06-26 01:49:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-01 12:12:39 +08:00
|
|
|
/// Convert value to a specified type
|
2013-12-09 16:38:27 +08:00
|
|
|
unsigned char Value::asByte() const
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
|
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
if (_type == Type::BYTE)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return _field.byteVal;
|
2013-12-09 16:38:27 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
if (_type == Type::INTEGER)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return static_cast<unsigned char>(_field.intVal);
|
2013-12-09 16:38:27 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2016-04-22 13:49:20 +08:00
|
|
|
if (_type == Type::UNSIGNED)
|
|
|
|
{
|
|
|
|
return static_cast<unsigned char>(_field.unsignedVal);
|
|
|
|
}
|
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
if (_type == Type::STRING)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return static_cast<unsigned char>(atoi(_field.strVal->c_str()));
|
2013-12-09 16:38:27 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
if (_type == Type::FLOAT)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return static_cast<unsigned char>(_field.floatVal);
|
2013-12-09 16:38:27 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
if (_type == Type::DOUBLE)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return static_cast<unsigned char>(_field.doubleVal);
|
2013-12-09 16:38:27 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
if (_type == Type::BOOLEAN)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return _field.boolVal ? 1 : 0;
|
2013-12-09 16:38:27 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
int Value::asInt() const
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::INTEGER)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return _field.intVal;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2016-04-22 13:49:20 +08:00
|
|
|
if (_type == Type::UNSIGNED)
|
|
|
|
{
|
|
|
|
CCASSERT(_field.unsignedVal < INT_MAX, "Can only convert values < INT_MAX");
|
|
|
|
return (int)_field.unsignedVal;
|
|
|
|
}
|
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
if (_type == Type::BYTE)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return _field.byteVal;
|
2013-12-09 16:38:27 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::STRING)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return atoi(_field.strVal->c_str());
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::FLOAT)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return static_cast<int>(_field.floatVal);
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::DOUBLE)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return static_cast<int>(_field.doubleVal);
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::BOOLEAN)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return _field.boolVal ? 1 : 0;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-22 13:49:20 +08:00
|
|
|
|
|
|
|
unsigned int Value::asUnsignedInt() const
|
|
|
|
{
|
|
|
|
CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
|
|
|
|
if (_type == Type::UNSIGNED)
|
|
|
|
{
|
|
|
|
return _field.unsignedVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::INTEGER)
|
|
|
|
{
|
|
|
|
CCASSERT(_field.intVal >= 0, "Only values >= 0 can be converted to unsigned");
|
|
|
|
return static_cast<unsigned int>(_field.intVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::BYTE)
|
|
|
|
{
|
|
|
|
return static_cast<unsigned int>(_field.byteVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::STRING)
|
|
|
|
{
|
|
|
|
// NOTE: strtoul is required (need to augment on unsupported platforms)
|
|
|
|
return static_cast<unsigned int>(strtoul(_field.strVal->c_str(), nullptr, 10));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::FLOAT)
|
|
|
|
{
|
|
|
|
return static_cast<unsigned int>(_field.floatVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::DOUBLE)
|
|
|
|
{
|
|
|
|
return static_cast<unsigned int>(_field.doubleVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_type == Type::BOOLEAN)
|
|
|
|
{
|
|
|
|
return _field.boolVal ? 1u : 0u;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0u;
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
float Value::asFloat() const
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::FLOAT)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return _field.floatVal;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
if (_type == Type::BYTE)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return static_cast<float>(_field.byteVal);
|
2013-12-09 16:38:27 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::STRING)
|
|
|
|
{
|
2014-07-14 20:45:24 +08:00
|
|
|
return utils::atof(_field.strVal->c_str());
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::INTEGER)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return static_cast<float>(_field.intVal);
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2016-04-22 13:49:20 +08:00
|
|
|
if (_type == Type::UNSIGNED)
|
|
|
|
{
|
|
|
|
return static_cast<float>(_field.unsignedVal);
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::DOUBLE)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return static_cast<float>(_field.doubleVal);
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::BOOLEAN)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return _field.boolVal ? 1.0f : 0.0f;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
double Value::asDouble() const
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::DOUBLE)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return _field.doubleVal;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
if (_type == Type::BYTE)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return static_cast<double>(_field.byteVal);
|
2013-12-09 16:38:27 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::STRING)
|
|
|
|
{
|
2014-07-14 20:45:24 +08:00
|
|
|
return static_cast<double>(utils::atof(_field.strVal->c_str()));
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::INTEGER)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return static_cast<double>(_field.intVal);
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2016-04-22 13:49:20 +08:00
|
|
|
if (_type == Type::UNSIGNED)
|
|
|
|
{
|
|
|
|
return static_cast<double>(_field.unsignedVal);
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::FLOAT)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return static_cast<double>(_field.floatVal);
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::BOOLEAN)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return _field.boolVal ? 1.0 : 0.0;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::asBool() const
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::BOOLEAN)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return _field.boolVal;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
if (_type == Type::BYTE)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return _field.byteVal == 0 ? false : true;
|
2013-12-09 16:38:27 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::STRING)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return (*_field.strVal == "0" || *_field.strVal == "false") ? false : true;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::INTEGER)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return _field.intVal == 0 ? false : true;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2016-04-22 13:49:20 +08:00
|
|
|
if (_type == Type::UNSIGNED)
|
|
|
|
{
|
|
|
|
return _field.unsignedVal == 0 ? false : true;
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::FLOAT)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return _field.floatVal == 0.0f ? false : true;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::DOUBLE)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return _field.doubleVal == 0.0 ? false : true;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2014-04-30 15:07:26 +08:00
|
|
|
return false;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Value::asString() const
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted");
|
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
if (_type == Type::STRING)
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
return *_field.strVal;
|
2013-12-04 16:18:22 +08:00
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-04 16:18:22 +08:00
|
|
|
std::stringstream ret;
|
2014-05-01 12:12:39 +08:00
|
|
|
|
|
|
|
switch (_type)
|
|
|
|
{
|
2013-12-09 16:38:27 +08:00
|
|
|
case Type::BYTE:
|
2014-05-01 12:12:39 +08:00
|
|
|
ret << _field.byteVal;
|
2013-12-09 16:38:27 +08:00
|
|
|
break;
|
2013-12-04 16:18:22 +08:00
|
|
|
case Type::INTEGER:
|
2014-05-01 12:12:39 +08:00
|
|
|
ret << _field.intVal;
|
2013-12-04 16:18:22 +08:00
|
|
|
break;
|
2016-04-22 13:49:20 +08:00
|
|
|
case Type::UNSIGNED:
|
|
|
|
ret << _field.unsignedVal;
|
|
|
|
break;
|
2013-12-04 16:18:22 +08:00
|
|
|
case Type::FLOAT:
|
2014-05-01 12:12:39 +08:00
|
|
|
ret << std::fixed << std::setprecision( 7 )<< _field.floatVal;
|
2013-12-04 16:18:22 +08:00
|
|
|
break;
|
|
|
|
case Type::DOUBLE:
|
2014-05-01 12:12:39 +08:00
|
|
|
ret << std::fixed << std::setprecision( 16 ) << _field.doubleVal;
|
2013-12-04 16:18:22 +08:00
|
|
|
break;
|
|
|
|
case Type::BOOLEAN:
|
2014-05-01 12:12:39 +08:00
|
|
|
ret << (_field.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()
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
CCASSERT(_type == Type::VECTOR, "The value type isn't Type::VECTOR");
|
|
|
|
return *_field.vectorVal;
|
2014-01-06 19:55:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const ValueVector& Value::asValueVector() const
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
CCASSERT(_type == Type::VECTOR, "The value type isn't Type::VECTOR");
|
|
|
|
return *_field.vectorVal;
|
2014-01-06 19:55:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ValueMap& Value::asValueMap()
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
CCASSERT(_type == Type::MAP, "The value type isn't Type::MAP");
|
|
|
|
return *_field.mapVal;
|
2014-01-06 19:55:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const ValueMap& Value::asValueMap() const
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
CCASSERT(_type == Type::MAP, "The value type isn't Type::MAP");
|
|
|
|
return *_field.mapVal;
|
2014-01-06 19:55:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ValueMapIntKey& Value::asIntKeyMap()
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
CCASSERT(_type == Type::INT_KEY_MAP, "The value type isn't Type::INT_KEY_MAP");
|
|
|
|
return *_field.intKeyMapVal;
|
2014-01-06 19:55:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const ValueMapIntKey& Value::asIntKeyMap() const
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
CCASSERT(_type == Type::INT_KEY_MAP, "The value type isn't Type::INT_KEY_MAP");
|
|
|
|
return *_field.intKeyMapVal;
|
2014-01-06 19:55:59 +08:00
|
|
|
}
|
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
static std::string getTabs(int depth)
|
|
|
|
{
|
|
|
|
std::string tabWidth;
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
for (int i = 0; i < depth; ++i)
|
|
|
|
{
|
|
|
|
tabWidth += "\t";
|
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
return tabWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string visit(const Value& v, int depth);
|
|
|
|
|
|
|
|
static std::string visitVector(const ValueVector& v, int depth)
|
|
|
|
{
|
|
|
|
std::stringstream ret;
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
if (depth > 0)
|
|
|
|
ret << "\n";
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
ret << getTabs(depth) << "[\n";
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
int i = 0;
|
|
|
|
for (const auto& child : v)
|
|
|
|
{
|
|
|
|
ret << getTabs(depth+1) << i << ": " << visit(child, depth + 1);
|
|
|
|
++i;
|
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
ret << getTabs(depth) << "]\n";
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
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;
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
if (depth > 0)
|
|
|
|
ret << "\n";
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
ret << getTabs(depth) << "{\n";
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
for (auto iter = v.begin(); iter != v.end(); ++iter)
|
|
|
|
{
|
|
|
|
ret << getTabs(depth + 1) << iter->first << ": ";
|
|
|
|
ret << visit(iter->second, depth + 1);
|
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
ret << getTabs(depth) << "}\n";
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
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:
|
2016-04-22 13:49:20 +08:00
|
|
|
case Value::Type::UNSIGNED:
|
2013-12-11 14:39:21 +08:00
|
|
|
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;
|
|
|
|
}
|
2014-05-01 12:12:39 +08:00
|
|
|
|
2013-12-11 14:39:21 +08:00
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
2015-03-09 21:34:24 +08:00
|
|
|
std::string Value::getDescription() const
|
2013-12-11 14:39:21 +08:00
|
|
|
{
|
|
|
|
std::string ret("\n");
|
|
|
|
ret += visit(*this, 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-09 16:38:27 +08:00
|
|
|
void Value::clear()
|
|
|
|
{
|
2014-05-01 12:12:39 +08:00
|
|
|
// Free memory the old value allocated
|
|
|
|
switch (_type)
|
|
|
|
{
|
|
|
|
case Type::BYTE:
|
|
|
|
_field.byteVal = 0;
|
|
|
|
break;
|
|
|
|
case Type::INTEGER:
|
|
|
|
_field.intVal = 0;
|
|
|
|
break;
|
2016-04-22 13:49:20 +08:00
|
|
|
case Type::UNSIGNED:
|
|
|
|
_field.unsignedVal = 0u;
|
|
|
|
break;
|
2014-05-01 12:12:39 +08:00
|
|
|
case Type::FLOAT:
|
|
|
|
_field.floatVal = 0.0f;
|
|
|
|
break;
|
|
|
|
case Type::DOUBLE:
|
|
|
|
_field.doubleVal = 0.0;
|
|
|
|
break;
|
|
|
|
case Type::BOOLEAN:
|
|
|
|
_field.boolVal = false;
|
|
|
|
break;
|
|
|
|
case Type::STRING:
|
|
|
|
CC_SAFE_DELETE(_field.strVal);
|
|
|
|
break;
|
|
|
|
case Type::VECTOR:
|
|
|
|
CC_SAFE_DELETE(_field.vectorVal);
|
|
|
|
break;
|
|
|
|
case Type::MAP:
|
|
|
|
CC_SAFE_DELETE(_field.mapVal);
|
|
|
|
break;
|
|
|
|
case Type::INT_KEY_MAP:
|
|
|
|
CC_SAFE_DELETE(_field.intKeyMapVal);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-04-22 13:49:20 +08:00
|
|
|
|
2014-05-12 23:32:51 +08:00
|
|
|
_type = Type::NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Value::reset(Type type)
|
|
|
|
{
|
|
|
|
if (_type == type)
|
|
|
|
return;
|
|
|
|
|
|
|
|
clear();
|
2014-05-01 12:12:39 +08:00
|
|
|
|
|
|
|
// Allocate memory for the new value
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case Type::STRING:
|
2015-12-16 14:02:55 +08:00
|
|
|
_field.strVal = new (std::nothrow) std::string();
|
2014-05-01 12:12:39 +08:00
|
|
|
break;
|
|
|
|
case Type::VECTOR:
|
2014-08-28 07:31:57 +08:00
|
|
|
_field.vectorVal = new (std::nothrow) ValueVector();
|
2014-05-01 12:12:39 +08:00
|
|
|
break;
|
|
|
|
case Type::MAP:
|
2014-08-28 07:31:57 +08:00
|
|
|
_field.mapVal = new (std::nothrow) ValueMap();
|
2014-05-01 12:12:39 +08:00
|
|
|
break;
|
|
|
|
case Type::INT_KEY_MAP:
|
2014-08-28 07:31:57 +08:00
|
|
|
_field.intKeyMapVal = new (std::nothrow) ValueMapIntKey();
|
2014-05-01 12:12:39 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_type = type;
|
2013-12-09 16:38:27 +08:00
|
|
|
}
|
|
|
|
|
2013-11-29 22:48:47 +08:00
|
|
|
NS_CC_END
|