| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583 |
- /****************************************************************************
- Copyright (c) 2016 Chukong Technologies Inc.
- Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
- 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.
- ****************************************************************************/
- #include "Value.hpp"
- #include "Object.hpp"
- namespace se {
- ValueArray EmptyValueArray;
- Value Value::Null = Value(Type::Null);
- Value Value::Undefined = Value(Type::Undefined);
- Value::Value()
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- memset(&_u, 0, sizeof(_u));
- }
- Value::Value(Type type)
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- memset(&_u, 0, sizeof(_u));
- reset(type);
- }
- Value::Value(const Value& v)
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- *this = v;
- }
- Value::Value(Value&& v)
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- *this = std::move(v);
- }
- Value::Value(bool v)
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- setBoolean(v);
- }
- Value::Value(int8_t v)
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- setInt8(v);
- }
- Value::Value(uint8_t v)
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- setUint8(v);
- }
- Value::Value(int32_t v)
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- setInt32(v);
- }
- Value::Value(uint32_t v)
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- setUint32(v);
- }
- Value::Value(int16_t v)
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- setInt16(v);
- }
- Value::Value(uint16_t v)
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- setUint16(v);
- }
- Value::Value(long v)
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- setLong(v);
- }
- Value::Value(unsigned long v)
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- setUlong(v);
- }
- Value::Value(float v)
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- setFloat(v);
- }
- Value::Value(double v)
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- setNumber(v);
- }
- Value::Value(const char* v)
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- setString(v);
- }
- Value::Value(const std::string& v)
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- setString(v);
- }
- Value::Value(Object* o, bool autoRootUnroot/* = false*/)
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- setObject(o, autoRootUnroot);
- }
- Value::Value(const HandleObject& o, bool autoRootUnroot/* = false*/)
- : _type(Type::Undefined)
- , _autoRootUnroot(false)
- {
- setObject(o, autoRootUnroot);
- }
- Value::~Value()
- {
- reset(Type::Undefined);
- }
- Value& Value::operator=(const Value& v)
- {
- if (this != &v)
- {
- reset(v.getType());
- switch (_type) {
- case Type::Null:
- case Type::Undefined:
- {
- memset(&_u, 0, sizeof(_u));
- break;
- }
- case Type::Number:
- _u._number = v._u._number;
- break;
- case Type::String:
- *_u._string = *v._u._string;
- break;
- case Type::Boolean:
- _u._boolean = v._u._boolean;
- break;
- case Type::Object:
- {
- setObject(v._u._object, v._autoRootUnroot);
- }
- break;
- default:
- break;
- }
- }
- return *this;
- }
- Value& Value::operator=(Value&& v)
- {
- if (this != &v)
- {
- reset(v.getType());
- switch (_type) {
- case Type::Null:
- case Type::Undefined:
- {
- memset(&_u, 0, sizeof(_u));
- break;
- }
- case Type::Number:
- _u._number = v._u._number;
- break;
- case Type::String:
- *_u._string = std::move(*v._u._string);
- break;
- case Type::Boolean:
- _u._boolean = v._u._boolean;
- break;
- case Type::Object:
- {
- if (_u._object != nullptr) // When old value is also an Object, reset will take no effect, therefore, _u._object may not be nullptr.
- {
- if (_autoRootUnroot)
- {
- _u._object->unroot();
- }
- _u._object->decRef();
- }
- _u._object = v._u._object;
- _autoRootUnroot = v._autoRootUnroot;
- v._u._object = nullptr; // Reset to nullptr here to avoid 'release' operation in v.reset(Type::Undefined) since it's a move operation here.
- v._autoRootUnroot = false;
- }
- break;
- default:
- break;
- }
- v.reset(Type::Undefined);
- }
- return *this;
- }
- // Value& Value::operator=(bool v)
- // {
- // setBoolean(v);
- // return *this;
- // }
- //
- // Value& Value::operator=(double v)
- // {
- // setNumber(v);
- // return *this;
- // }
- //
- // Value& Value::operator=(const std::string& v)
- // {
- // setString(v);
- // return *this;
- // }
- //
- // Value& Value::operator=(Object* o)
- // {
- // setObject(o);
- // return *this;
- // }
- //
- // Value& Value::operator=(const HandleObject& o)
- // {
- // setObject(o);
- // return *this;
- // }
- void Value::setUndefined()
- {
- reset(Type::Undefined);
- }
- void Value::setNull()
- {
- reset(Type::Null);
- }
- void Value::setBoolean(bool v)
- {
- reset(Type::Boolean);
- _u._boolean = v;
- }
- void Value::setInt8(int8_t v)
- {
- reset(Type::Number);
- _u._number = (double)v;
- }
- void Value::setUint8(uint8_t v)
- {
- reset(Type::Number);
- _u._number = (double)v;
- }
- void Value::setInt32(int32_t v)
- {
- reset(Type::Number);
- _u._number = (double)v;
- }
- void Value::setUint32(uint32_t v)
- {
- reset(Type::Number);
- _u._number = (double)v;
- }
- void Value::setInt16(int16_t v)
- {
- reset(Type::Number);
- _u._number = (double)v;
- }
- void Value::setUint16(uint16_t v)
- {
- reset(Type::Number);
- _u._number = (double)v;
- }
- void Value::setLong(long v)
- {
- reset(Type::Number);
- _u._number = (double)v;
- }
- void Value::setUlong(unsigned long v)
- {
- reset(Type::Number);
- _u._number = (double)v;
- }
- void Value::setFloat(float v)
- {
- reset(Type::Number);
- _u._number = (double)v;
- }
- void Value::setNumber(double v)
- {
- reset(Type::Number);
- _u._number = v;
- }
- void Value::setString(const char* v)
- {
- if (v != nullptr)
- {
- reset(Type::String);
- *_u._string = v;
- }
- else
- {
- reset(Type::Null);
- }
- }
- void Value::setString(const std::string& v)
- {
- reset(Type::String);
- *_u._string = v;
- }
- void Value::setObject(Object* object, bool autoRootUnroot/* = false*/)
- {
- if (object == nullptr)
- {
- reset(Type::Null);
- return;
- }
- if (_type != Type::Object)
- {
- reset(Type::Object);
- }
- if (_u._object != object)
- {
- if (object != nullptr)
- {
- object->incRef();
- if (autoRootUnroot)
- {
- object->root();
- }
- }
- if (_u._object != nullptr) // When old value is also an Object, reset will take no effect, therefore, _u._object may not be nullptr.
- {
- if (_autoRootUnroot)
- {
- _u._object->unroot();
- }
- _u._object->decRef();
- }
- _u._object = object;
- _autoRootUnroot = autoRootUnroot;
- }
- else
- {
- _autoRootUnroot = autoRootUnroot;
- if (_autoRootUnroot)
- {
- _u._object->root();
- }
- }
- }
- void Value::setObject(const HandleObject& o, bool autoRootUnroot/* = false*/)
- {
- setObject(o.get(), autoRootUnroot);
- }
- int8_t Value::toInt8() const
- {
- return static_cast<int8_t>(toNumber());
- }
- uint8_t Value::toUint8() const
- {
- return static_cast<uint8_t>(toNumber());
- }
- int16_t Value::toInt16() const
- {
- return static_cast<int16_t>(toNumber());
- }
- uint16_t Value::toUint16() const
- {
- return static_cast<uint16_t>(toNumber());
- }
- int32_t Value::toInt32() const
- {
- return static_cast<int32_t>(toNumber());
- }
- uint32_t Value::toUint32() const
- {
- return static_cast<uint32_t>(toNumber());
- }
- long Value::toLong() const
- {
- return static_cast<long>(toNumber());
- }
- unsigned long Value::toUlong() const
- {
- return static_cast<unsigned long>(toNumber());
- }
- float Value::toFloat() const
- {
- return static_cast<float>(toNumber());
- }
- double Value::toNumber() const
- {
- assert(_type == Type::Number || _type == Type::Boolean);
- if (_type == Type::Boolean)
- {
- if (_u._boolean)
- return 1.0;
- else
- return 0.0;
- }
- return _u._number;
- }
- bool Value::toBoolean() const
- {
- assert(_type == Type::Boolean);
- return _u._boolean;
- }
- const std::string& Value::toString() const
- {
- assert(_type == Type::String);
- return *_u._string;
- }
- std::string Value::toStringForce() const
- {
- std::string ret;
- if (_type == Type::String)
- {
- ret = *_u._string;
- }
- else if (_type == Type::Boolean)
- {
- ret = _u._boolean ? "true" : "false";
- }
- else if (_type == Type::Number)
- {
- char tmp[50] = {0};
- snprintf(tmp, sizeof(tmp), "%.17g", _u._number);
- ret = tmp;
- }
- else if (_type == Type::Object)
- {
- ret = toObject()->toString();
- }
- else if (_type == Type::Null)
- {
- ret = "null";
- }
- else if (_type == Type::Undefined)
- {
- ret = "undefined";
- }
- else
- {
- assert(false);
- }
- return ret;
- }
- Object* Value::toObject() const
- {
- assert(isObject());
- return _u._object;
- }
- void Value::reset(Type type)
- {
- if (_type != type)
- {
- switch (_type) {
- case Type::String:
- delete _u._string;
- break;
- case Type::Object:
- {
- if (_u._object != nullptr)
- {
- if (_autoRootUnroot)
- {
- _u._object->unroot();
- }
- _u._object->decRef();
- _u._object = nullptr;
- }
- _autoRootUnroot = false;
- break;
- }
- default:
- break;
- }
- memset(&_u, 0, sizeof(_u));
- switch (type) {
- case Type::String:
- _u._string = new std::string();
- break;
- default:
- break;
- }
- _type = type;
- }
- }
- } // namespace se {
|