Cocos Basic Tutorial (4) Data structure introduction of Cocos2d::value

Source: Internet
Author: User

1. Overview

Cocos2d::valie is a containing many native types (int,float,double,bool,unsigned char,char* and std::string) plus STD::VECTOR<VALUE> STD Classes of::unordered_map<std::string,value> and std::unordered_map<int,value>.

You can put all of the above mentioned native types into the Cocos2d::value object, and then convert them to the corresponding native type, and vice versa.

Internally, Cocos2d::value uses a union variable to hold various native types, which can save a lot of memory space.

2. Memory Management

Cocos2d::value's memory is automatically processed by its own destructor. So stick with the C + + memory management rules for best practices when dealing with cocos2d::value memory.
The Cocos2d::value class contains the following data members:

 union{unsigned  char   Byteval;     int   Intval;     float   Floatval;     double   Doubleval;  bool   Boolval;} _basedata; std::  string   _strdata; Valuevector  * _vectordata; ValueMap  * _mapdata; Valuemapintkey  * _intkeymapdata; Type _type;  

As you can see from the code snippet, the memory of the _basedata,_strdata and _type data members is handled automatically by the compiler and their destructors. Cocos2d::value's destructor is responsible for releasing resources (_vectordata,_mapdata and _intkeymapdata) for all pointer member variables.
Warning: cocos2d::value no longer uses retain/release and reference count memory management like other cocos2d classes.

3. Basic usage

The use of Cocos2d::value is very simple.
A simple example is provided here:

Value Val;//calling the default constructorif(Val.isnull ()) {log ("Val is null");}Else{std::stringstr =val.getdescription (); Log ("The description of val0:%s", Str.c_str ());}//----------------------------------------------------Value Val1 ( $);//initialize with an int//Value val1 (3.4f); //initialize with a float//Value Val1 (3.5); //initialize with a doubleLog"The description of the integer value:%s", Val1.getdescription (). C_STR ()); Log ("val1.asbyte () =%c", Val1.asbyte ());//----------------------------------------------------STD::stringSTRV ="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 =NewVector<object*>(); Vecv-pushback (SP0);   Value Val3 (VECV); //Initialize with VectorLog"The description of the Vector value:%s", Val3.getdescription (). C_STR ());DeleteVecv;//----------------------------------------------------MAP&LT;STD::string, object*>* MAPV =NewMAP&LT;STD::string, object*>(); MAPV-Insert (STRV,SP0);   Value Val4 (MAPV); //Initialize with MapLog"The description of the Map value:%s", Val4.getdescription (). C_STR ());DeleteMAPV;//----------------------------------------------------Value val6 (&AMP;VAL4);//Initialize with MapLog"The description of the value-type value:%s", Val6.getdescription (). C_STR ());//----------------------------------------------------Val2 = Val1;//assign a value between two different reference typesLog"operator-> The description of val2:%s", Val2.getdescription (). C_STR ()); Val2=4;//Direct AssignmentLog"operator-> The description of val4:%s", Val2.getdescription (). C_STR ());

Output results

Cocos2d:val isNULLcocos2d:the Description of the integer value: $Cocos2d:val1.asByte ()=acocos2d:the Description of thestringValue: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: $Cocos2d:operator-The description of Val4:4

4. Best practices

    • Prefer to use cocos2d::value and new template containers (cocos2d::vector<t> and cocos2d::map<k,v>) instead of using cocos2d::ccbool,cocos2d:: Ccfloat,cocos2d::ccdouble,cocos2d::ccstring,cocos2d::ccinteger and old objective-c style containers (Cocos2d::ccarray and cocos2d:: Ccdictionary).
    • When you want to handle collections of native types, use Cocos2d::value to encapsulate native types and then combine them with the new template container cocos2d::vector<t> and cocos2d::map<k,v>.

Cocos Basic Tutorial (4) Data structure introduction of Cocos2d::value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.