2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2011-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
http://www.cocos2d-x.org
|
2021-12-25 10:04:45 +08:00
|
|
|
|
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:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CC_LUA_VALUE_H_
|
|
|
|
#define __CC_LUA_VALUE_H_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "lua.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "base/ccTypes.h"
|
|
|
|
#include "base/CCRef.h"
|
|
|
|
|
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY
|
|
|
|
using std::memcpy;
|
2021-12-25 10:04:45 +08:00
|
|
|
using std::memset;
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup lua
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
typedef int LUA_FUNCTION;
|
|
|
|
typedef int LUA_TABLE;
|
|
|
|
typedef int LUA_STRING;
|
|
|
|
|
|
|
|
class LuaValue;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
typedef std::map<std::string, LuaValue> LuaValueDict;
|
|
|
|
typedef LuaValueDict::const_iterator LuaValueDictIterator;
|
|
|
|
typedef std::list<LuaValue> LuaValueArray;
|
|
|
|
typedef LuaValueArray::const_iterator LuaValueArrayIterator;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/// @cond
|
2021-12-25 10:04:45 +08:00
|
|
|
typedef enum
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
LuaValueTypeInt,
|
|
|
|
LuaValueTypeFloat,
|
|
|
|
LuaValueTypeBoolean,
|
|
|
|
LuaValueTypeString,
|
|
|
|
LuaValueTypeDict,
|
|
|
|
LuaValueTypeArray,
|
|
|
|
LuaValueTypeObject
|
|
|
|
} LuaValueType;
|
|
|
|
/// @endcond
|
|
|
|
|
|
|
|
/// @cond
|
2021-12-25 10:04:45 +08:00
|
|
|
typedef union
|
|
|
|
{
|
|
|
|
int intValue;
|
|
|
|
float floatValue;
|
|
|
|
bool booleanValue;
|
|
|
|
std::string* stringValue;
|
|
|
|
LuaValueDict* dictValue;
|
|
|
|
LuaValueArray* arrayValue;
|
|
|
|
Ref* ccobjectValue;
|
2019-11-23 20:27:39 +08:00
|
|
|
} LuaValueField;
|
|
|
|
/// @endcond
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrap different general types of data into a same specific type named LuaValue.
|
|
|
|
* The general types supported as follows:int,float,bool,std::string,const char*,LuaValueDict,LuaValueArray,Ref.
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
class LuaValue
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Construct a LuaValue object by a int value.
|
|
|
|
*
|
|
|
|
* @param intValue a int value.
|
|
|
|
* @return a LuaValue object.
|
|
|
|
*/
|
|
|
|
static const LuaValue intValue(const int intValue);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Construct a LuaValue object by a float value.
|
|
|
|
*
|
|
|
|
* @param floatValue a float value.
|
|
|
|
* @return a LuaValue object.
|
|
|
|
*/
|
|
|
|
static const LuaValue floatValue(const float floatValue);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Construct a LuaValue object by a boolean value.
|
|
|
|
*
|
|
|
|
* @param booleanValue a bool value.
|
|
|
|
* @return a LuaValue object.
|
|
|
|
*/
|
|
|
|
static const LuaValue booleanValue(const bool booleanValue);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Construct a LuaValue object by a string pointer.
|
|
|
|
*
|
|
|
|
* @param stringValue a string pointer.
|
|
|
|
* @return a LuaValue object.
|
|
|
|
*/
|
|
|
|
static const LuaValue stringValue(const char* stringValue);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Construct a LuaValue object by a std::string object.
|
|
|
|
*
|
|
|
|
* @param stringValue a std::string object.
|
|
|
|
* @return a LuaValue object.
|
|
|
|
*/
|
2021-12-28 21:00:45 +08:00
|
|
|
static const LuaValue stringValue(const std::string& stringValue);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Construct a LuaValue object by a LuaValueDict value.
|
|
|
|
*
|
|
|
|
* @param dictValue a LuaValueDict object.
|
|
|
|
* @return a LuaValue object.
|
|
|
|
*/
|
|
|
|
static const LuaValue dictValue(const LuaValueDict& dictValue);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Construct a LuaValue object by a LuaValueArray value.
|
|
|
|
*
|
|
|
|
* @param arrayValue a LuaValueArray object.
|
|
|
|
* @return a LuaValue object.
|
|
|
|
*/
|
|
|
|
static const LuaValue arrayValue(const LuaValueArray& arrayValue);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Construct a LuaValue object by a Ref object.
|
|
|
|
*
|
|
|
|
* @param ccobjectValue a Ref object.
|
|
|
|
* @param objectTypename a string pointer point to the typename of object.
|
|
|
|
* @return a LuaValue object.
|
|
|
|
*/
|
|
|
|
static const LuaValue ccobjectValue(Ref* ccobjectValue, const char* objectTypename);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Construct a LuaValue object by a Ref object.
|
|
|
|
*
|
|
|
|
* @param ccobjectValue a Ref object.
|
|
|
|
* @param objectTypename a std::string object represent the typename of object.
|
|
|
|
* @return a LuaValue object.
|
|
|
|
*/
|
2021-12-28 21:00:45 +08:00
|
|
|
static const LuaValue ccobjectValue(Ref* ccobjectValue, const std::string& objectTypename);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Default constructor of LuaValue.
|
|
|
|
* Set the default value for _type(LuaValueTypeInt) and _ccobjectType(nullptr),and init the _field.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
LuaValue() : _type(LuaValueTypeInt), _ccobjectType(nullptr) { memset(&_field, 0, sizeof(_field)); }
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Copy constructor of Data.
|
|
|
|
*/
|
|
|
|
LuaValue(const LuaValue& rhs);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Override of operator= .
|
|
|
|
*/
|
|
|
|
LuaValue& operator=(const LuaValue& rhs);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
~LuaValue();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Get the type of LuaValue object.
|
2021-12-25 10:04:45 +08:00
|
|
|
*
|
2019-11-23 20:27:39 +08:00
|
|
|
* @return the type of LuaValue object.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
LuaValueType getType() const { return _type; }
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Get the typename of the Ref object.
|
|
|
|
*
|
|
|
|
* @return the reference of _ccobjectType.
|
|
|
|
*/
|
2021-12-28 21:00:45 +08:00
|
|
|
const std::string& getObjectTypename() const { return *_ccobjectType; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Get the int value of LuaValue object.
|
|
|
|
*
|
|
|
|
* @return the int value.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
int intValue() const { return _field.intValue; }
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Get the float value of LuaValue object.
|
|
|
|
*
|
|
|
|
* @return the float value.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
float floatValue() const { return _field.floatValue; }
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Get the boolean value of LuaValue object.
|
|
|
|
*
|
|
|
|
* @return the boolean value.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
bool booleanValue() const { return _field.booleanValue; }
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Get the boolean value of LuaValue object.
|
|
|
|
*
|
|
|
|
* @return the reference about string value.
|
|
|
|
*/
|
2021-12-28 21:00:45 +08:00
|
|
|
const std::string& stringValue() const { return *_field.stringValue; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Get the LuaValueDict value of LuaValue object.
|
|
|
|
*
|
|
|
|
* @return the LuaValueDict value.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
const LuaValueDict& dictValue() const { return *_field.dictValue; }
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Get the LuaValueArray value of LuaValue object.
|
|
|
|
*
|
|
|
|
* @return the LuaValueArray value.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
const LuaValueArray& arrayValue() const { return *_field.arrayValue; }
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Get the Ref object of LuaValue object.
|
|
|
|
*
|
|
|
|
* @return the pointer point to a Ref object.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
Ref* ccobjectValue() const { return _field.ccobjectValue; }
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
private:
|
|
|
|
LuaValueField _field;
|
2021-12-25 10:04:45 +08:00
|
|
|
LuaValueType _type;
|
|
|
|
std::string* _ccobjectType;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
void copy(const LuaValue& rhs);
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
// end group
|
|
|
|
/// @}
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif // __CC_LUA_VALUE_H_
|