2014-03-10 14:04:58 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2011-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CC_LUA_VALUE_H_
|
|
|
|
#define __CC_LUA_VALUE_H_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "lua.h"
|
|
|
|
}
|
|
|
|
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccTypes.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "base/CCRef.h"
|
2014-03-10 14:04:58 +08:00
|
|
|
|
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY
|
|
|
|
using std::memset;
|
|
|
|
using std::memcpy;
|
|
|
|
#endif
|
|
|
|
|
2015-03-26 22:07:44 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup lua
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2014-03-10 14:04:58 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
typedef int LUA_FUNCTION;
|
|
|
|
typedef int LUA_TABLE;
|
|
|
|
typedef int LUA_STRING;
|
|
|
|
|
|
|
|
class LuaValue;
|
|
|
|
|
|
|
|
typedef std::map<std::string, LuaValue> LuaValueDict;
|
|
|
|
typedef LuaValueDict::const_iterator LuaValueDictIterator;
|
|
|
|
typedef std::list<LuaValue> LuaValueArray;
|
|
|
|
typedef LuaValueArray::const_iterator LuaValueArrayIterator;
|
|
|
|
|
2015-03-20 14:39:33 +08:00
|
|
|
/// @cond
|
2014-03-10 14:04:58 +08:00
|
|
|
typedef enum {
|
|
|
|
LuaValueTypeInt,
|
|
|
|
LuaValueTypeFloat,
|
|
|
|
LuaValueTypeBoolean,
|
|
|
|
LuaValueTypeString,
|
|
|
|
LuaValueTypeDict,
|
|
|
|
LuaValueTypeArray,
|
|
|
|
LuaValueTypeObject
|
|
|
|
} LuaValueType;
|
2015-03-20 14:39:33 +08:00
|
|
|
/// @endcond
|
2014-03-10 14:04:58 +08:00
|
|
|
|
2015-03-20 14:39:33 +08:00
|
|
|
/// @cond
|
2014-03-10 14:04:58 +08:00
|
|
|
typedef union {
|
|
|
|
int intValue;
|
|
|
|
float floatValue;
|
|
|
|
bool booleanValue;
|
|
|
|
std::string* stringValue;
|
|
|
|
LuaValueDict* dictValue;
|
|
|
|
LuaValueArray* arrayValue;
|
|
|
|
Ref* ccobjectValue;
|
|
|
|
} LuaValueField;
|
2015-03-20 14:39:33 +08:00
|
|
|
/// @endcond
|
2014-03-10 14:04:58 +08:00
|
|
|
|
2015-03-20 14:39:33 +08:00
|
|
|
/**
|
|
|
|
* 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.
|
2015-03-27 15:27:08 +08:00
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
* @js NA
|
2015-03-20 14:39:33 +08:00
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
class LuaValue
|
|
|
|
{
|
|
|
|
public:
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a LuaValue object by a int value.
|
|
|
|
*
|
|
|
|
* @param intValue a int value.
|
|
|
|
* @return a LuaValue object.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
static const LuaValue intValue(const int intValue);
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a LuaValue object by a float value.
|
|
|
|
*
|
|
|
|
* @param floatValue a float value.
|
|
|
|
* @return a LuaValue object.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
static const LuaValue floatValue(const float floatValue);
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a LuaValue object by a boolean value.
|
|
|
|
*
|
|
|
|
* @param booleanValue a bool value.
|
|
|
|
* @return a LuaValue object.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
static const LuaValue booleanValue(const bool booleanValue);
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a LuaValue object by a string pointer.
|
|
|
|
*
|
|
|
|
* @param stringValue a string pointer.
|
|
|
|
* @return a LuaValue object.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
static const LuaValue stringValue(const char* stringValue);
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a LuaValue object by a std::string object.
|
|
|
|
*
|
|
|
|
* @param stringValue a std::string object.
|
|
|
|
* @return a LuaValue object.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
static const LuaValue stringValue(const std::string& stringValue);
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a LuaValue object by a LuaValueDict value.
|
|
|
|
*
|
|
|
|
* @param dictValue a LuaValueDict object.
|
|
|
|
* @return a LuaValue object.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
static const LuaValue dictValue(const LuaValueDict& dictValue);
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a LuaValue object by a LuaValueArray value.
|
|
|
|
*
|
|
|
|
* @param arrayValue a LuaValueArray object.
|
|
|
|
* @return a LuaValue object.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
static const LuaValue arrayValue(const LuaValueArray& arrayValue);
|
2015-03-20 14:39:33 +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.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
static const LuaValue ccobjectValue(Ref* ccobjectValue, const char* objectTypename);
|
2015-03-20 14:39:33 +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.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
static const LuaValue ccobjectValue(Ref* ccobjectValue, const std::string& objectTypename);
|
|
|
|
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
2015-11-03 03:31:17 +08:00
|
|
|
* Default constructor of LuaValue.
|
2015-03-20 14:39:33 +08:00
|
|
|
* Set the default value for _type(LuaValueTypeInt) and _ccobjectType(nullptr),and init the _field.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
LuaValue(void)
|
|
|
|
: _type(LuaValueTypeInt)
|
2014-04-09 22:53:59 +08:00
|
|
|
, _ccobjectType(nullptr)
|
2014-03-10 14:04:58 +08:00
|
|
|
{
|
|
|
|
memset(&_field, 0, sizeof(_field));
|
|
|
|
}
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor of Data.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
LuaValue(const LuaValue& rhs);
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-26 22:07:44 +08:00
|
|
|
* Override of operator= .
|
2015-03-20 14:39:33 +08:00
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
LuaValue& operator=(const LuaValue& rhs);
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
~LuaValue(void);
|
|
|
|
|
2015-03-20 14:39:33 +08:00
|
|
|
/**
|
|
|
|
* Get the type of LuaValue object.
|
|
|
|
*
|
|
|
|
* @return the type of LuaValue object.
|
|
|
|
*/
|
2016-03-04 12:48:28 +08:00
|
|
|
LuaValueType getType() const {
|
2014-03-10 14:04:58 +08:00
|
|
|
return _type;
|
|
|
|
}
|
|
|
|
|
2015-03-20 14:39:33 +08:00
|
|
|
/**
|
|
|
|
* Get the typename of the Ref object.
|
|
|
|
*
|
2015-10-14 11:42:51 +08:00
|
|
|
* @return the reference of _ccobjectType.
|
2015-03-20 14:39:33 +08:00
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
const std::string& getObjectTypename(void) const {
|
|
|
|
return *_ccobjectType;
|
|
|
|
}
|
|
|
|
|
2015-03-20 14:39:33 +08:00
|
|
|
/**
|
|
|
|
* Get the int value of LuaValue object.
|
|
|
|
*
|
|
|
|
* @return the int value.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
int intValue(void) const {
|
|
|
|
return _field.intValue;
|
|
|
|
}
|
|
|
|
|
2015-03-20 14:39:33 +08:00
|
|
|
/**
|
|
|
|
* Get the float value of LuaValue object.
|
|
|
|
*
|
|
|
|
* @return the float value.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
float floatValue(void) const {
|
|
|
|
return _field.floatValue;
|
|
|
|
}
|
|
|
|
|
2015-03-20 14:39:33 +08:00
|
|
|
/**
|
|
|
|
* Get the boolean value of LuaValue object.
|
|
|
|
*
|
|
|
|
* @return the boolean value.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
bool booleanValue(void) const {
|
|
|
|
return _field.booleanValue;
|
|
|
|
}
|
|
|
|
|
2015-03-20 14:39:33 +08:00
|
|
|
/**
|
|
|
|
* Get the boolean value of LuaValue object.
|
|
|
|
*
|
2015-10-14 11:42:51 +08:00
|
|
|
* @return the reference about string value.
|
2015-03-20 14:39:33 +08:00
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
const std::string& stringValue(void) const {
|
|
|
|
return *_field.stringValue;
|
|
|
|
}
|
|
|
|
|
2015-03-20 14:39:33 +08:00
|
|
|
/**
|
|
|
|
* Get the LuaValueDict value of LuaValue object.
|
|
|
|
*
|
|
|
|
* @return the LuaValueDict value.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
const LuaValueDict& dictValue(void) const {
|
|
|
|
return *_field.dictValue;
|
|
|
|
}
|
|
|
|
|
2015-03-20 14:39:33 +08:00
|
|
|
/**
|
|
|
|
* Get the LuaValueArray value of LuaValue object.
|
|
|
|
*
|
|
|
|
* @return the LuaValueArray value.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
const LuaValueArray& arrayValue(void) const {
|
|
|
|
return *_field.arrayValue;
|
|
|
|
}
|
|
|
|
|
2015-03-20 14:39:33 +08:00
|
|
|
/**
|
|
|
|
* Get the Ref object of LuaValue object.
|
|
|
|
*
|
|
|
|
* @return the pointer point to a Ref object.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
Ref* ccobjectValue(void) const {
|
|
|
|
return _field.ccobjectValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
LuaValueField _field;
|
|
|
|
LuaValueType _type;
|
|
|
|
std::string* _ccobjectType;
|
|
|
|
|
|
|
|
void copy(const LuaValue& rhs);
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
2015-03-26 22:07:44 +08:00
|
|
|
// end group
|
|
|
|
/// @}
|
2014-03-10 14:04:58 +08:00
|
|
|
#endif // __CC_LUA_VALUE_H_
|