2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2017 Chukong Technologies
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-07-15 12:21:38 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-01-04 12:36:20 +08:00
|
|
|
https://adxeproject.github.io/
|
2019-11-23 20:27:39 +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:
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "base/CCValue.h"
|
|
|
|
#include <cmath>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include "base/ccUtils.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
const ValueVector ValueVectorNull;
|
|
|
|
const ValueMap ValueMapNull;
|
|
|
|
const ValueMapIntKey ValueMapIntKeyNull;
|
|
|
|
|
|
|
|
const Value Value::Null;
|
|
|
|
|
2020-03-03 19:24:07 +08:00
|
|
|
const std::string Value::NullString;
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value::Value() : _type(Type::NONE)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
memset(&_field, 0, sizeof(_field));
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:29:15 +08:00
|
|
|
Value::Value(unsigned char v) : _type(Type::INT_UI32)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-11-09 12:16:27 +08:00
|
|
|
_field.uintVal = v;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-11-09 14:29:15 +08:00
|
|
|
Value::Value(int v) : _type(Type::INT_I32)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_field.intVal = v;
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:29:15 +08:00
|
|
|
Value::Value(unsigned int v) : _type(Type::INT_UI32)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-11-09 12:16:27 +08:00
|
|
|
_field.uintVal = v;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-11-09 14:29:15 +08:00
|
|
|
/** Create a Value by an integer value. */
|
|
|
|
Value::Value(int64_t v) : _type(Type::INT_I64)
|
|
|
|
{
|
|
|
|
_field.int64Val = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Create a Value by an integer value. */
|
|
|
|
Value::Value(uint64_t v) : _type(Type::INT_UI64)
|
|
|
|
{
|
|
|
|
_field.uint64Val = v;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value::Value(float v) : _type(Type::FLOAT)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_field.floatVal = v;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value::Value(double v) : _type(Type::DOUBLE)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_field.doubleVal = v;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value::Value(bool v) : _type(Type::BOOLEAN)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_field.boolVal = v;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value::Value(const char* v) : _type(Type::STRING)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_field.strVal = new std::string(v ? v : "");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Value::Value(std::string_view v) : _type(Type::STRING)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_field.strVal = new std::string(v);
|
2021-06-25 07:30:47 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value::Value(std::string&& v) : _type(Type::STRING)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_field.strVal = new std::string(std::move(v));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value::Value(const ValueVector& v) : _type(Type::VECTOR)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_field.vectorVal = new ValueVector(v);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value::Value(ValueVector&& v) : _type(Type::VECTOR)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_field.vectorVal = new ValueVector(std::move(v));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value::Value(const ValueMap& v) : _type(Type::MAP)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_field.mapVal = new ValueMap(v);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value::Value(ValueMap&& v) : _type(Type::MAP)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_field.mapVal = new ValueMap(std::move(v));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value::Value(const ValueMapIntKey& v) : _type(Type::INT_KEY_MAP)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_field.intKeyMapVal = new ValueMapIntKey(v);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value::Value(ValueMapIntKey&& v) : _type(Type::INT_KEY_MAP)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_field.intKeyMapVal = new ValueMapIntKey(std::move(v));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value::Value(const Value& other) : _type(Type::NONE)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value::Value(Value&& other) : _type(Type::NONE)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
*this = std::move(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::~Value()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value& Value::operator=(const Value& other)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +08:00
|
|
|
if (this != &other)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
reset(other._type);
|
|
|
|
|
2021-11-09 14:29:15 +08:00
|
|
|
switch (other.getTypeFamily())
|
2021-10-09 13:48:56 +08:00
|
|
|
{
|
|
|
|
case Type::INTEGER:
|
2021-11-09 14:29:15 +08:00
|
|
|
_field.uint64Val = other._field.uint64Val;
|
2021-10-09 13:48:56 +08:00
|
|
|
break;
|
|
|
|
case Type::FLOAT:
|
|
|
|
_field.floatVal = other._field.floatVal;
|
|
|
|
break;
|
|
|
|
case Type::DOUBLE:
|
|
|
|
_field.doubleVal = other._field.doubleVal;
|
|
|
|
break;
|
|
|
|
case Type::BOOLEAN:
|
|
|
|
_field.boolVal = other._field.boolVal;
|
|
|
|
break;
|
|
|
|
case Type::STRING:
|
|
|
|
if (_field.strVal == nullptr)
|
|
|
|
{
|
|
|
|
_field.strVal = new std::string();
|
|
|
|
}
|
|
|
|
*_field.strVal = *other._field.strVal;
|
|
|
|
break;
|
|
|
|
case Type::VECTOR:
|
|
|
|
if (_field.vectorVal == nullptr)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_field.vectorVal = new ValueVector();
|
2021-10-09 13:48:56 +08:00
|
|
|
}
|
|
|
|
*_field.vectorVal = *other._field.vectorVal;
|
|
|
|
break;
|
|
|
|
case Type::MAP:
|
|
|
|
if (_field.mapVal == nullptr)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_field.mapVal = new ValueMap();
|
2021-10-09 13:48:56 +08:00
|
|
|
}
|
|
|
|
*_field.mapVal = *other._field.mapVal;
|
|
|
|
break;
|
|
|
|
case Type::INT_KEY_MAP:
|
|
|
|
if (_field.intKeyMapVal == nullptr)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_field.intKeyMapVal = new ValueMapIntKey();
|
2021-10-09 13:48:56 +08:00
|
|
|
}
|
|
|
|
*_field.intKeyMapVal = *other._field.intKeyMapVal;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value& Value::operator=(Value&& other)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (this != &other)
|
|
|
|
{
|
|
|
|
clear();
|
2021-11-09 14:29:15 +08:00
|
|
|
switch (other.getTypeFamily())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +08:00
|
|
|
case Type::INTEGER:
|
2021-11-09 12:16:27 +08:00
|
|
|
_field.uintVal = other._field.uintVal;
|
2021-10-09 13:48:56 +08:00
|
|
|
break;
|
|
|
|
case Type::FLOAT:
|
|
|
|
_field.floatVal = other._field.floatVal;
|
|
|
|
break;
|
|
|
|
case Type::DOUBLE:
|
|
|
|
_field.doubleVal = other._field.doubleVal;
|
|
|
|
break;
|
|
|
|
case Type::BOOLEAN:
|
|
|
|
_field.boolVal = other._field.boolVal;
|
|
|
|
break;
|
|
|
|
case Type::STRING:
|
|
|
|
_field.strVal = other._field.strVal;
|
|
|
|
break;
|
|
|
|
case Type::VECTOR:
|
|
|
|
_field.vectorVal = other._field.vectorVal;
|
|
|
|
break;
|
|
|
|
case Type::MAP:
|
|
|
|
_field.mapVal = other._field.mapVal;
|
|
|
|
break;
|
|
|
|
case Type::INT_KEY_MAP:
|
|
|
|
_field.intKeyMapVal = other._field.intKeyMapVal;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
_type = other._type;
|
|
|
|
|
|
|
|
memset(&other._field, 0, sizeof(other._field));
|
|
|
|
other._type = Type::NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value& Value::operator=(unsigned char v)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-11-09 14:29:15 +08:00
|
|
|
reset(Type::INT_UI32);
|
2021-11-09 12:16:27 +08:00
|
|
|
_field.uintVal = v;
|
2019-11-23 20:27:39 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value& Value::operator=(int v)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-11-09 14:29:15 +08:00
|
|
|
reset(Type::INT_I32);
|
2019-11-23 20:27:39 +08:00
|
|
|
_field.intVal = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value& Value::operator=(unsigned int v)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-11-09 14:29:15 +08:00
|
|
|
reset(Type::INT_UI32);
|
2021-11-09 12:16:27 +08:00
|
|
|
_field.uintVal = v;
|
2019-11-23 20:27:39 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:29:15 +08:00
|
|
|
Value& Value::operator=(int64_t v)
|
|
|
|
{
|
|
|
|
reset(Type::INT_I64);
|
|
|
|
_field.int64Val = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value& Value::operator=(uint64_t v)
|
|
|
|
{
|
|
|
|
reset(Type::INT_UI64);
|
|
|
|
_field.uint64Val = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value& Value::operator=(float v)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
reset(Type::FLOAT);
|
|
|
|
_field.floatVal = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value& Value::operator=(double v)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
reset(Type::DOUBLE);
|
|
|
|
_field.doubleVal = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value& Value::operator=(bool v)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
reset(Type::BOOLEAN);
|
|
|
|
_field.boolVal = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value& Value::operator=(const char* v)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
reset(Type::STRING);
|
|
|
|
*_field.strVal = v ? v : "";
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Value& Value::operator=(std::string_view v)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
reset(Type::STRING);
|
|
|
|
*_field.strVal = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value& Value::operator=(std::string&& v)
|
|
|
|
{
|
2021-06-25 07:30:47 +08:00
|
|
|
reset(Type::STRING);
|
|
|
|
*_field.strVal = std::move(v);
|
2021-10-09 13:48:56 +08:00
|
|
|
return *this;
|
2021-06-25 07:30:47 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value& Value::operator=(const ValueVector& v)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
reset(Type::VECTOR);
|
|
|
|
*_field.vectorVal = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value& Value::operator=(ValueVector&& v)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
reset(Type::VECTOR);
|
|
|
|
*_field.vectorVal = std::move(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value& Value::operator=(const ValueMap& v)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
reset(Type::MAP);
|
|
|
|
*_field.mapVal = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value& Value::operator=(ValueMap&& v)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
reset(Type::MAP);
|
|
|
|
*_field.mapVal = std::move(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value& Value::operator=(const ValueMapIntKey& v)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
reset(Type::INT_KEY_MAP);
|
|
|
|
*_field.intKeyMapVal = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
Value& Value::operator=(ValueMapIntKey&& v)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
reset(Type::INT_KEY_MAP);
|
|
|
|
*_field.intKeyMapVal = std::move(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
bool Value::operator!=(const Value& v)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return !(*this == v);
|
|
|
|
}
|
2021-10-09 13:48:56 +08:00
|
|
|
bool Value::operator!=(const Value& v) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return !(*this == v);
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
bool Value::operator==(const Value& v)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +08:00
|
|
|
const auto& t = *this;
|
2019-11-23 20:27:39 +08:00
|
|
|
return t == v;
|
|
|
|
}
|
2021-10-09 13:48:56 +08:00
|
|
|
bool Value::operator==(const Value& v) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +08:00
|
|
|
if (this == &v)
|
|
|
|
return true;
|
|
|
|
if (v._type != this->_type)
|
|
|
|
return false;
|
|
|
|
if (this->isNull())
|
|
|
|
return true;
|
2021-11-09 14:40:00 +08:00
|
|
|
switch (getType())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-11-09 14:40:00 +08:00
|
|
|
case Type::INT_I32:
|
|
|
|
return v._field.intVal == this->_field.intVal;
|
|
|
|
case Type::INT_UI32:
|
|
|
|
return v._field.uintVal == this->_field.uintVal;
|
|
|
|
case Type::INT_I64:
|
|
|
|
return v._field.int64Val == this->_field.int64Val;
|
|
|
|
case Type::INT_UI64:
|
|
|
|
return v._field.int64Val == this->_field.int64Val;
|
2021-10-09 13:48:56 +08:00
|
|
|
case Type::BOOLEAN:
|
|
|
|
return v._field.boolVal == this->_field.boolVal;
|
|
|
|
case Type::STRING:
|
|
|
|
return *v._field.strVal == *this->_field.strVal;
|
|
|
|
case Type::FLOAT:
|
|
|
|
return std::abs(v._field.floatVal - this->_field.floatVal) <= FLT_EPSILON;
|
|
|
|
case Type::DOUBLE:
|
|
|
|
return std::abs(v._field.doubleVal - this->_field.doubleVal) <= DBL_EPSILON;
|
|
|
|
case Type::VECTOR:
|
|
|
|
{
|
|
|
|
const auto& v1 = *(this->_field.vectorVal);
|
|
|
|
const auto& v2 = *(v._field.vectorVal);
|
|
|
|
const auto size = v1.size();
|
|
|
|
if (size == v2.size())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +08:00
|
|
|
for (size_t i = 0; i < size; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +08:00
|
|
|
if (v1[i] != v2[i])
|
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-10-09 13:48:56 +08:00
|
|
|
return true;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-10-09 13:48:56 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
case Type::MAP:
|
|
|
|
{
|
|
|
|
const auto& map1 = *(this->_field.mapVal);
|
|
|
|
const auto& map2 = *(v._field.mapVal);
|
|
|
|
for (const auto& kvp : map1)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +08:00
|
|
|
auto it = map2.find(kvp.first);
|
|
|
|
if (it == map2.end() || it->second != kvp.second)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +08:00
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-10-09 13:48:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case Type::INT_KEY_MAP:
|
|
|
|
{
|
|
|
|
const auto& map1 = *(this->_field.intKeyMapVal);
|
|
|
|
const auto& map2 = *(v._field.intKeyMapVal);
|
|
|
|
for (const auto& kvp : map1)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +08:00
|
|
|
auto it = map2.find(kvp.first);
|
|
|
|
if (it == map2.end() || it->second != kvp.second)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +08:00
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-10-09 13:48:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert value to a specified type
|
2021-07-15 12:21:38 +08:00
|
|
|
unsigned char Value::asByte(unsigned char defaultValue) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +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");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
switch (_type)
|
|
|
|
{
|
2021-11-09 14:29:15 +08:00
|
|
|
case Type::INT_UI32:
|
2021-11-09 12:16:27 +08:00
|
|
|
return static_cast<unsigned char>(_field.uintVal);
|
2021-11-09 14:29:15 +08:00
|
|
|
case Type::INT_I32:
|
|
|
|
return static_cast<unsigned char>(_field.intVal);
|
|
|
|
case Type::INT_I64:
|
|
|
|
return static_cast<unsigned char>(_field.int64Val);
|
|
|
|
case Type::INT_UI64:
|
|
|
|
return static_cast<unsigned char>(_field.uint64Val);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::STRING:
|
2019-11-23 20:27:39 +08:00
|
|
|
return static_cast<unsigned char>(atoi(_field.strVal->c_str()));
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::FLOAT:
|
2019-11-23 20:27:39 +08:00
|
|
|
return static_cast<unsigned char>(_field.floatVal);
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::DOUBLE:
|
2019-11-23 20:27:39 +08:00
|
|
|
return static_cast<unsigned char>(_field.doubleVal);
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::BOOLEAN:
|
2019-11-23 20:27:39 +08:00
|
|
|
return _field.boolVal ? 1 : 0;
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
default:
|
|
|
|
return defaultValue;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-07-15 12:21:38 +08:00
|
|
|
int Value::asInt(int defaultValue) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +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");
|
|
|
|
switch (_type)
|
|
|
|
{
|
2021-11-09 14:29:15 +08:00
|
|
|
case Type::INT_I32:
|
|
|
|
return (_field.intVal);
|
|
|
|
case Type::INT_UI32:
|
|
|
|
return static_cast<int>(_field.uintVal);
|
|
|
|
case Type::INT_I64:
|
|
|
|
return static_cast<int>(_field.int64Val);
|
|
|
|
case Type::INT_UI64:
|
|
|
|
return static_cast<int>(_field.uint64Val);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::STRING:
|
2019-11-23 20:27:39 +08:00
|
|
|
return atoi(_field.strVal->c_str());
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::FLOAT:
|
2019-11-23 20:27:39 +08:00
|
|
|
return static_cast<int>(_field.floatVal);
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::DOUBLE:
|
2019-11-23 20:27:39 +08:00
|
|
|
return static_cast<int>(_field.doubleVal);
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::BOOLEAN:
|
2019-11-23 20:27:39 +08:00
|
|
|
return _field.boolVal ? 1 : 0;
|
2021-10-09 13:48:56 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
return defaultValue;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:29:15 +08:00
|
|
|
unsigned int Value::asUint(unsigned int defaultValue) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +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");
|
|
|
|
switch (_type)
|
|
|
|
{
|
2021-11-09 14:29:15 +08:00
|
|
|
case Type::INT_UI32:
|
|
|
|
return (_field.uintVal);
|
|
|
|
case Type::INT_I32:
|
2019-11-23 20:27:39 +08:00
|
|
|
return static_cast<unsigned int>(_field.intVal);
|
2021-11-09 14:29:15 +08:00
|
|
|
case Type::INT_I64:
|
|
|
|
return static_cast<unsigned int>(_field.int64Val);
|
|
|
|
case Type::INT_UI64:
|
|
|
|
return static_cast<unsigned int>(_field.uint64Val);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::STRING:
|
2019-11-23 20:27:39 +08:00
|
|
|
// NOTE: strtoul is required (need to augment on unsupported platforms)
|
|
|
|
return static_cast<unsigned int>(strtoul(_field.strVal->c_str(), nullptr, 10));
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::FLOAT:
|
2019-11-23 20:27:39 +08:00
|
|
|
return static_cast<unsigned int>(_field.floatVal);
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::DOUBLE:
|
2019-11-23 20:27:39 +08:00
|
|
|
return static_cast<unsigned int>(_field.doubleVal);
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::BOOLEAN:
|
2019-11-23 20:27:39 +08:00
|
|
|
return _field.boolVal ? 1u : 0u;
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
default:
|
|
|
|
return defaultValue;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-11-09 14:29:15 +08:00
|
|
|
int64_t Value::asInt64(int64_t defaultValue) const
|
|
|
|
{
|
|
|
|
CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP,
|
|
|
|
"Only base type (bool, string, float, double, int) could be converted");
|
|
|
|
switch (_type)
|
|
|
|
{
|
|
|
|
case Type::INT_I64:
|
|
|
|
return (_field.int64Val);
|
|
|
|
case Type::INT_I32:
|
|
|
|
return static_cast<int64_t>(_field.intVal);
|
|
|
|
case Type::INT_UI32:
|
|
|
|
return static_cast<int64_t>(_field.uintVal);
|
|
|
|
case Type::INT_UI64:
|
|
|
|
return static_cast<int64_t>(_field.uint64Val);
|
|
|
|
|
|
|
|
case Type::STRING:
|
|
|
|
// NOTE: strtoul is required (need to augment on unsupported platforms)
|
|
|
|
return static_cast<int64_t>(strtoul(_field.strVal->c_str(), nullptr, 10));
|
|
|
|
|
|
|
|
case Type::FLOAT:
|
|
|
|
return static_cast<int64_t>(_field.floatVal);
|
|
|
|
|
|
|
|
case Type::DOUBLE:
|
|
|
|
return static_cast<int64_t>(_field.doubleVal);
|
|
|
|
|
|
|
|
case Type::BOOLEAN:
|
|
|
|
return _field.boolVal ? 1u : 0u;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t Value::asUint64(uint64_t defaultValue) const
|
|
|
|
{
|
|
|
|
CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP,
|
|
|
|
"Only base type (bool, string, float, double, int) could be converted");
|
|
|
|
switch (_type)
|
|
|
|
{
|
|
|
|
case Type::INT_UI64:
|
|
|
|
return (_field.uint64Val);
|
|
|
|
case Type::INT_I32:
|
|
|
|
return static_cast<uint64_t>(_field.intVal);
|
|
|
|
case Type::INT_UI32:
|
|
|
|
return static_cast<uint64_t>(_field.uintVal);
|
|
|
|
case Type::INT_I64:
|
|
|
|
return static_cast<uint64_t>(_field.int64Val);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-11-09 14:29:15 +08:00
|
|
|
case Type::STRING:
|
|
|
|
// NOTE: strtoul is required (need to augment on unsupported platforms)
|
|
|
|
return static_cast<uint64_t>(strtoull(_field.strVal->c_str(), nullptr, 10));
|
|
|
|
|
|
|
|
case Type::FLOAT:
|
|
|
|
return static_cast<uint64_t>(_field.floatVal);
|
|
|
|
|
|
|
|
case Type::DOUBLE:
|
|
|
|
return static_cast<uint64_t>(_field.doubleVal);
|
|
|
|
|
|
|
|
case Type::BOOLEAN:
|
|
|
|
return _field.boolVal ? 1u : 0u;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 12:21:38 +08:00
|
|
|
float Value::asFloat(float defaultValue) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +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");
|
|
|
|
switch (_type)
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::FLOAT:
|
2019-11-23 20:27:39 +08:00
|
|
|
return _field.floatVal;
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::STRING:
|
2021-06-25 07:30:47 +08:00
|
|
|
return static_cast<float>(utils::atof(_field.strVal->c_str()));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-11-09 14:29:15 +08:00
|
|
|
case Type::INT_I32:
|
2019-11-23 20:27:39 +08:00
|
|
|
return static_cast<float>(_field.intVal);
|
2021-11-09 14:29:15 +08:00
|
|
|
case Type::INT_UI32:
|
2021-11-09 12:16:27 +08:00
|
|
|
return static_cast<float>(_field.uintVal);
|
2021-11-09 14:29:15 +08:00
|
|
|
case Type::INT_I64:
|
|
|
|
return static_cast<float>(_field.int64Val);
|
|
|
|
case Type::INT_UI64:
|
|
|
|
return static_cast<float>(_field.uint64Val);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::DOUBLE:
|
2019-11-23 20:27:39 +08:00
|
|
|
return static_cast<float>(_field.doubleVal);
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::BOOLEAN:
|
2019-11-23 20:27:39 +08:00
|
|
|
return _field.boolVal ? 1.0f : 0.0f;
|
2021-10-09 13:48:56 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
return defaultValue;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 12:21:38 +08:00
|
|
|
double Value::asDouble(double defaultValue) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +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");
|
|
|
|
switch (_type)
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::DOUBLE:
|
2019-11-23 20:27:39 +08:00
|
|
|
return _field.doubleVal;
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::STRING:
|
2019-11-23 20:27:39 +08:00
|
|
|
return static_cast<double>(utils::atof(_field.strVal->c_str()));
|
|
|
|
|
2021-11-09 14:29:15 +08:00
|
|
|
case Type::INT_I32:
|
2019-11-23 20:27:39 +08:00
|
|
|
return static_cast<double>(_field.intVal);
|
2021-11-09 14:29:15 +08:00
|
|
|
case Type::INT_UI32:
|
2021-11-09 12:16:27 +08:00
|
|
|
return static_cast<double>(_field.uintVal);
|
2021-11-09 14:29:15 +08:00
|
|
|
case Type::INT_I64:
|
|
|
|
return static_cast<double>(_field.int64Val);
|
|
|
|
case Type::INT_UI64:
|
|
|
|
return static_cast<double>(_field.uint64Val);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::FLOAT:
|
2019-11-23 20:27:39 +08:00
|
|
|
return static_cast<double>(_field.floatVal);
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::BOOLEAN:
|
2019-11-23 20:27:39 +08:00
|
|
|
return _field.boolVal ? 1.0 : 0.0;
|
2021-10-09 13:48:56 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
return defaultValue;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 12:21:38 +08:00
|
|
|
bool Value::asBool(bool defaultValue) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +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");
|
|
|
|
switch (_type)
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::BOOLEAN:
|
2019-11-23 20:27:39 +08:00
|
|
|
return _field.boolVal;
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::STRING:
|
2019-11-23 20:27:39 +08:00
|
|
|
return (*_field.strVal == "0" || *_field.strVal == "false") ? false : true;
|
|
|
|
|
2021-11-09 14:29:15 +08:00
|
|
|
case Type::INT_I32:
|
|
|
|
return !!_field.intVal;
|
|
|
|
case Type::INT_UI32:
|
|
|
|
return !!_field.uintVal;
|
|
|
|
case Type::INT_I64:
|
|
|
|
return !!_field.int64Val;
|
|
|
|
case Type::INT_UI64:
|
|
|
|
return !!_field.uint64Val;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::FLOAT:
|
2019-11-23 20:27:39 +08:00
|
|
|
return _field.floatVal == 0.0f ? false : true;
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
case Type::DOUBLE:
|
2019-11-23 20:27:39 +08:00
|
|
|
return _field.doubleVal == 0.0 ? false : true;
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
default:
|
|
|
|
return defaultValue;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
std::string Value::asString() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +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");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (_type == Type::STRING)
|
|
|
|
{
|
|
|
|
return *_field.strVal;
|
|
|
|
}
|
|
|
|
|
2021-10-09 13:48:56 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
NUMBER_MAX_DIGITS = 63
|
|
|
|
};
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-08-06 19:39:50 +08:00
|
|
|
std::string ret;
|
|
|
|
size_t n = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
switch (_type)
|
|
|
|
{
|
2021-11-09 14:29:15 +08:00
|
|
|
case Type::INT_I32:
|
2021-10-09 13:48:56 +08:00
|
|
|
ret = std::to_string(_field.intVal);
|
|
|
|
break;
|
2021-11-09 14:29:15 +08:00
|
|
|
case Type::INT_UI32:
|
2021-11-09 12:16:27 +08:00
|
|
|
ret = std::to_string(_field.uintVal);
|
2021-10-09 13:48:56 +08:00
|
|
|
break;
|
2021-11-09 14:29:15 +08:00
|
|
|
case Type::INT_I64:
|
|
|
|
ret = std::to_string(_field.int64Val);
|
|
|
|
break;
|
|
|
|
case Type::INT_UI64:
|
|
|
|
ret = std::to_string(_field.uint64Val);
|
|
|
|
break;
|
2021-10-09 13:48:56 +08:00
|
|
|
case Type::FLOAT:
|
|
|
|
ret.resize(NUMBER_MAX_DIGITS);
|
|
|
|
n = snprintf(&ret.front(), NUMBER_MAX_DIGITS + 1, "%.*g", 7 /*precision*/, _field.floatVal);
|
|
|
|
if (n > 0)
|
|
|
|
ret.resize(n);
|
|
|
|
break;
|
|
|
|
case Type::DOUBLE:
|
|
|
|
ret.resize(NUMBER_MAX_DIGITS);
|
|
|
|
n = snprintf(&ret.front(), NUMBER_MAX_DIGITS + 1, "%.*g", 17 /*precision*/, _field.doubleVal);
|
|
|
|
if (n > 0)
|
|
|
|
ret.resize(n);
|
|
|
|
break;
|
|
|
|
case Type::BOOLEAN:
|
|
|
|
ret = (_field.boolVal ? "true" : "false");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-10-09 13:48:56 +08:00
|
|
|
|
2020-08-06 19:39:50 +08:00
|
|
|
return ret;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view Value::asStringRef() const
|
2020-03-03 19:24:07 +08:00
|
|
|
{
|
2020-08-06 19:39:50 +08:00
|
|
|
if (_type == Type::STRING)
|
|
|
|
return *_field.strVal;
|
|
|
|
return Value::NullString;
|
2020-03-03 19:24:07 +08:00
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ValueVector& Value::asValueVector()
|
|
|
|
{
|
|
|
|
CCASSERT(_type == Type::VECTOR, "The value type isn't Type::VECTOR");
|
|
|
|
return *_field.vectorVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ValueVector& Value::asValueVector() const
|
|
|
|
{
|
|
|
|
CCASSERT(_type == Type::VECTOR, "The value type isn't Type::VECTOR");
|
|
|
|
return *_field.vectorVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueMap& Value::asValueMap()
|
|
|
|
{
|
|
|
|
CCASSERT(_type == Type::MAP, "The value type isn't Type::MAP");
|
|
|
|
return *_field.mapVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ValueMap& Value::asValueMap() const
|
|
|
|
{
|
|
|
|
CCASSERT(_type == Type::MAP, "The value type isn't Type::MAP");
|
|
|
|
return *_field.mapVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueMapIntKey& Value::asIntKeyMap()
|
|
|
|
{
|
|
|
|
CCASSERT(_type == Type::INT_KEY_MAP, "The value type isn't Type::INT_KEY_MAP");
|
|
|
|
return *_field.intKeyMapVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ValueMapIntKey& Value::asIntKeyMap() const
|
|
|
|
{
|
|
|
|
CCASSERT(_type == Type::INT_KEY_MAP, "The value type isn't Type::INT_KEY_MAP");
|
|
|
|
return *_field.intKeyMapVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2021-10-09 13:48:56 +08:00
|
|
|
ret << getTabs(depth + 1) << i << ": " << visit(child, depth + 1);
|
2019-11-23 20:27:39 +08:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret << getTabs(depth) << "]\n";
|
|
|
|
|
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
static std::string visitMap(const T& v, int depth)
|
|
|
|
{
|
|
|
|
std::stringstream ret;
|
|
|
|
|
|
|
|
if (depth > 0)
|
|
|
|
ret << "\n";
|
|
|
|
|
|
|
|
ret << getTabs(depth) << "{\n";
|
|
|
|
|
|
|
|
for (auto& iter : v)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2021-11-09 14:29:15 +08:00
|
|
|
switch (v.getTypeFamily())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +08:00
|
|
|
case Value::Type::NONE:
|
|
|
|
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:
|
|
|
|
ret << visitMap(v.asIntKeyMap(), depth);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CCASSERT(false, "Invalid type!");
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Value::getDescription() const
|
|
|
|
{
|
|
|
|
std::string ret("\n");
|
|
|
|
ret += visit(*this, 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Value::clear()
|
|
|
|
{
|
|
|
|
// Free memory the old value allocated
|
2021-11-09 14:29:15 +08:00
|
|
|
switch (getTypeFamily())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-09 13:48:56 +08:00
|
|
|
case Type::INTEGER:
|
2021-11-09 14:29:15 +08:00
|
|
|
_field.uint64Val = 0;
|
2021-10-09 13:48:56 +08:00
|
|
|
break;
|
|
|
|
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;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_type = Type::NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Value::reset(Type type)
|
|
|
|
{
|
|
|
|
if (_type == type)
|
|
|
|
return;
|
|
|
|
|
|
|
|
clear();
|
|
|
|
|
|
|
|
// Allocate memory for the new value
|
|
|
|
switch (type)
|
|
|
|
{
|
2021-10-09 13:48:56 +08:00
|
|
|
case Type::STRING:
|
2021-12-08 00:11:53 +08:00
|
|
|
_field.strVal = new std::string();
|
2021-10-09 13:48:56 +08:00
|
|
|
break;
|
|
|
|
case Type::VECTOR:
|
2021-12-08 00:11:53 +08:00
|
|
|
_field.vectorVal = new ValueVector();
|
2021-10-09 13:48:56 +08:00
|
|
|
break;
|
|
|
|
case Type::MAP:
|
2021-12-08 00:11:53 +08:00
|
|
|
_field.mapVal = new ValueMap();
|
2021-10-09 13:48:56 +08:00
|
|
|
break;
|
|
|
|
case Type::INT_KEY_MAP:
|
2021-12-08 00:11:53 +08:00
|
|
|
_field.intKeyMapVal = new ValueMapIntKey();
|
2021-10-09 13:48:56 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|