cocos2d::Value
来源:互联网 发布:unity3d游戏下载 编辑:程序博客网 时间:2024/06/10 22:07
cocos2d::Value
- Since: v3.0 beta
- Language: C++
Defined in the head "CCValue.h" loated in "COCOS2DX_ROOT/cocos/base"
1class Value;
cocos2d::Value
is a wrapper class for many primitives(int
,float
,double
,bool
,unsigned char
,char*
andstd::string
) plus std::vector<Value>
, std::unordered_map<std::string,Value>
andstd::unordered_map<int,Value>
.
You can put all the primitives mentioned above into a cocos2d::Value
object and convert it to the corresponding primitive. The opposite is vice verse.
Internally, cocos2d::Value
uses a union variable to hold all kinds of primitives which saves a lot of memory space.
Before cocos2d-x v3.0 beta, there are CCBool
, CCFloat
,CCDouble
, CCinteger
primitive wrapper. These classes will be deprecated in the future.
Note:When you deal with primitives and container, please use cocos2d::Vector<T>
,cocos2d::Map<K,V>
andcocos2d::Value
.
Memory Management
The memory of cocos2d::Value
is handled automatically by it's own destructor. So please stick to the best practice of c++ memory management rules when handling the memory ofcocos2d::Value
.
The cocos2d::Value
class contains the following data members:
123456789101112131415union{ unsigned char byteVal; int intVal; float floatVal; double doubleVal; bool boolVal;}_baseData;std::string _strData;ValueVector* _vectorData;ValueMap* _mapData;ValueMapIntKey* _intKeyMapData;Type _type;
From the code snippets, _baseData
, _strData
and _type
data members' memory are handled automatically by the compiler and their own destructors. The destructor ofcocos2d::Value
is responsible for deallocating all the resources of pointer member variables(_vectorData
,_mapData
and_intKeyMapData
).
WARNING: cocos2d::Value
doesn't use retain/release and refcount memory management like other cocos2d classes!
Basic Usage
The usage of cocos2d::Value
is very straightforward.
Here is a simple usage example:
1234567891011121314151617181920212223242526272829303132333435363738Value val; // call the default constructorif (val.isNull()) { log("val is null");}else{ std::string str =val.getDescription(); log("The description of val0:%s",str.c_str());}//----------------------------------------------------Value val1(65); // initialize with a integer//Value val1(3.4f); // initialize with a float value//Value val1(3.5); // initialize with a double valuelog("The description of the integer value:%s",val1.getDescription().c_str());log("val1.asByte() = %c",val1.asByte());//----------------------------------------------------std::string strV = "string";Value val2(strV); // initialize with stringlog("The description of the string value:%s",val2.getDescription().c_str());//----------------------------------------------------auto sp0 = Sprite::create();Vector<Object*>* vecV = new Vector<Object*>();vecV->pushBack(sp0);Value val3(vecV); // initialize with Vectorlog("The description of the Vector value:%s",val3.getDescription().c_str());delete vecV;//----------------------------------------------------Map<std::string, Object*>* mapV = new Map<std::string, Object*>();mapV->insert(strV,sp0);Value val4(mapV); // initialize with Maplog("The description of the Map value:%s",val4.getDescription().c_str());delete mapV;//----------------------------------------------------Value val6(&val4); // initialize with Maplog("The description of the Value-type value:%s",val6.getDescription().c_str());//----------------------------------------------------val2 = val1; // assigning between 2 Value-typelog("operator-> The description of val2:%s",val2.getDescription().c_str());val2 = 4; //assigning directlylog("operator-> The description of val4:%s",val2.getDescription().c_str());
output:
12345678910111213141516171819202122cocos2d: val is nullcocos2d: The description of the integer value:65cocos2d: val1.asByte() = Acocos2d: The description of the string value:stringcocos2d: The description of the Vector value:truecocos2d: The description of the Map value:truecocos2d: The description of the Value-type value:truecocos2d: operator-> The description of val2:65cocos2d: operator-> The description of val4:4
Best Practice
- Prefer
cocos2d::Value
and new template container(cocos2d::Vector<T>
andcocos2d::Map<K,V>
) overcocos2d::CCBool
,cocos2d::CCFloat
,cocos2d::CCDouble
,cocos2d::CCString
,cocos2d::CCInteger
and old Objective-c style container(cocos2d::CCArray
andcocos2d::CCDictionary
). - When you want to deal with primitives aggregate, wrap the primitives with
cocos2d::Value
and combine them with the new template containercocos2d::Vector<T>
andcocos2d::Map<K,V>
.
- cocos2d::Value
- cocos2d::Value
- [Cocos2d-x v3.x]cocos2d::Value
- Cocos2d-3.x_基本数据类型(cocos2d::Value)
- cocos2d-x Vector&map&value
- COCOS2D-3.9 Value接口分析
- 【cocos2d-x游戏开发】Value、Vector、Map
- Cocos2d-x 3.0final -Vector&map&value
- Cocos2d-x 3.0-Vector&map&value
- cocos2d lua 提示InterpolationMissingOptionError: Bad value substitution:
- cocos2d-x 3.0 数据结构(3/3) cocos2d::Value
- Cocos2d-x3.0模版容器详解:cocos2d::Value
- [Cocos2d-x v3.x官方文档]cocos2d::Value
- Cocos2d-x3.0中容器的介绍(cocos2d-x::Value)
- Cocos2d-x3.0模版容器详解之三:cocos2d::Value
- Cocos2d-x3.0模版容器详解之三:cocos2d::Value
- Cocos2d-x 3.0数据结构——cocos2d::Value
- cocos2d-x学习笔记(5)Value和vector
- const的总结
- R.D.团队开发章程
- VS2012 很卡,需要大化小化窗口
- 项目4-撞错体验
- 2014ACM Asia regional 区域赛(牡丹江赛区)赛后感想
- cocos2d::Value
- Seivlet处理请求
- 第7周项目3求两个正整数的正差值
- TCP/IP协议三次握手与四次握手流程解析
- hdu 2524
- 面试经验
- 可变参数列表
- 【Java TCP/IP Socket】UDP Socket(含代码)
- Canvas入门(1):绘制矩形、圆、直线、曲线等基本图形