Cocos2dx tips (16) Discuss visit (getDescription) and cocos2dxvisit again
The previous two articles are about Value-related topics. I will continue to talk about this topic, just like a "Value three-board ax series ...".
A long time ago I wrote a blog about how to view the elements in CCArray and CCDictionary: http://blog.csdn.net/star530/article/details/23877429.
Now that both of them are in the past, the previous viewing method is certainly not available. Value, as one of their replacements, naturally has a way to view elements, that isGetDescription ()Here is a simple example:
Value a (10); std: string a_str = a. getDescription (); CCLOG ("a = % s", a_str.c_str (); // print the Value ofNext, let's take a look at the program running result and output the following information:
There seems to be no problem with the output, but there is a question here,
Why is a = not in the same line as 10?
What happened to the interface getDescription?With questions and curiosity, let's go to the definition of getDescription () to find out the actual situation.
Std: string Value: getDescription () {std: string ret ("\ n"); // Insert the linefeed ret + = visit (* this, 0 ); return ret ;}After reading the code, I found that,
Originally, a "\ n" line break was inserted into the returned string.
Our story should have ended here, but some obsessive-compulsive children have a bit of heart. Since we have seen this, why don't we look at it again?
What have been done by the visit (* this, 0) function?
Okay, now that you have asked me like this, let's continue to look at it:
Static std: string visit (const Value & v, int depth) {std: stringstream ret; // create a string stream // determine the type switch (v. getType () {case Value: Type: NONE: case Value: Type: BYTE: case Value: Type: INTEGER: case Value: Type :: FLOAT: case Value: Type: DOUBLE: case Value: Type: BOOLEAN: case Value: Type: STRING: ret <v. asString () <"\ n"; // if it is of the above types, convert it to the string type directly. Break; case Value: Type: VECTOR: ret <visitVector (v. asValueVector (), depth); break; case Value: Type: MAP: ret <visitMap (v. asValueMap (), depth); break; case Value: Type: INT_KEY_MAP: ret <visitMap (v. asIntKeyMap (), depth); break; default: CCASSERT (false, "Invalid type! "); Break;} return ret. str ();}I didn't make too many comments on the above Code. We only need to pay attention to two points:
1. Common data types, such as int and string, can be directly converted to the string type (asString () and placed in the ret stream. If the element type is Vector or Map, further processing is required;
2. What is the second depth parameter of visit (const Value & v, int depth? I found that when we further process Vector elements, the visitMap (v. asValueMap (), depth) function is called, and depth is the second parameter of the function.
With doubts, let's continue:
Static std: string visitVector (const ValueVector & v, int depth) {std: stringstream ret; if (depth> 0) ret <"\ n "; ret <getTabs (depth) <"[\ n"; int I = 0; for (const auto & child: v) {ret <getTabs (depth + 1) <I <":" <visit (child, depth + 1); // read the element ++ I;} ret <getTabs (depth) in the Vector) <"] \ n"; return ret. str ();}This function is a bit confusing at first glance. In fact, we only need to know
GetTabs (int depth )..
Static std: string getTabs (int depth) {std: string tabWidth; for (int I = 0; I <depth; ++ I) {tabWidth + = "\ t"; // insert a tab} return tabWidth ;}
The original getTabs () function is to insert a tab (the so-called tab is the tab key), and add as many tabs as the input parameter depth.
Well, the process is like this. I think a lot of people are still blind to their heads. It's okay. Let me give you a chestnut, then let's take a look at the output of the program and the code of visit () and visitVector () mentioned above.
Value a (10); Value B ("star is so cool"); ValueVector star_vec; star_vec.push_back (a); star_vec.push_back (B ); // convert the ValueVector type to the Value type before calling getDescription () std: string star_str = (Value) star_vec ). getDescription (); CCLOG ("---------------------"); CCLOG ("% s", star_str.c_str (); CCLOG ("-----------------------");After the program runs, the output result is as follows:
Well, let's talk about it. If you can understand it, you can see your understanding.
Respect Original, reprinted please indicate Source: http://blog.csdn.net/star530/article/details/38071517