1.概述
定义在 “COCOS2DX_ROOT/cocos/base” 路径下的 "
CCValue.h
" 的头文件中。
cocos2d::Valie 是一个包含了很多原生类型(int,float,double,bool,unsigned char,char* 和 std::string)外加 std::vector<Value>,std::unordered_map<std::string,Value> 和 std::unordered_map<int,Value> 的类。
你可以把所有上面的提及的原生类型放入 cocos2d::Value 对象中,然后将它们转化为对应的原生类型,反之亦然。
在内部,cocos2d::Value 使用了
一个联合变量来保存各种原生类型,这样可以节省很多的内存空间。
在 Cocos2d-x v3.0 beta 之前,存在着一些原生类型的封装类,如 CCBool,CCFloat,CCDouble,CCinteger,这些将会被弃用。
注意:
当你在处理原生类型和容器的时候,请使用 cocos2d::Vector<T>,cocos2d::Map<K,V> 和 cocos2d::Value。
2.内存管理
cocos2d::Value 的内存是由它自己的析构
函数自动处理的。所以当处理 cocos2d::Value 的内存时请坚持以 c++ 内存管理规则进行最佳实践。
cocos2d::Value 类包含了以下的数据成员:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
union
{
unsigned
char
byteVal;
int
intVal;
float
floatVal;
double
doubleVal;
bool
boolVal;
}_baseData;
std::string _strData;
ValueVector* _vectorData;
ValueMapIntKey* _intKeyMapData;
Type _type;
|
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Value val;
if
(val.isNull()) {
log
(
"val is null"
);
}
else
{
std::string str =val.getDescription();
"The description of val0:%s"
,str.c_str());
}
Value val1(65);
"The description of the integer value:%s"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,val1.getDescription().c_str());
"val1.asByte() = %c"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,val1.asByte());
std::string strV =
"string"
;
Value val2(strV);
"The description of the string value:%s"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,val2.getDescription().c_str());
auto
sp0 = Sprite::create();
Vector<Object*>* vecV =
new
Vector<Object*>();
vecV->pushBack(sp0);
Value val3(vecV);
"The description of the Vector value:%s"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,val3.getDescription().c_str());
delete
vecV;
Map<std::string,Object*>* mapV =
Map<std::string,Object*>();
mapV->insert(strV,sp0);
Value val4(mapV);
@H_582_ 404@
"The description of the Map value:%s"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,val4.getDescription().c_str());
mapV;
Value val6(&val4);
"The description of the Value-type value:%s"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,val6.getDescription().c_str());
val2 = val1;
"operator-> The description of val2:%s"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,val2.getDescription().c_str());
val2 = 4;
"operator-> The description of val4:%s"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,val2.getDescription().c_str());
|