Do {} while (0) is a perfect match with CC_BREAK_IF, whilecc_break_if

Source: Internet
Author: User

Do {} while (0) is a perfect match with CC_BREAK_IF, whilecc_break_if

From the very beginning, I felt that it was unnecessary and very useful. I 've been using this structure for about two months. Let's summarize how to use this structure.

First case: When loading a file, if the file fails to be loaded, an error must be reported.

Currently, you can use the try {} catch () {} finally {} structure, but this will greatly increase the size of the compiled file, using do {} while (0) add CC_BREAK_IF to achieve the same effect without increasing the file size.

The following is an example:

Bool GameScene: setScene () {do {auto node = SceneReader: getInstance ()-> createNodeWithSceneFile ("publish/GameScene. csb"); CC_BREAK_IF (! Node); m_UI_Background-> addChild (node); auto Checkerboard = (ComRender *) (node-> getChildByTag (10009)-> getComponent ("GUIComponent ")); checkerboardUI = Checkerboard-> getNode (); // set the Button m_btn_Setting = dynamic_cast <Button *> (CheckerboardUI-> getChildByName ("Button_Setting"); CC_BREAK_IF (! M_btn_Setting); // music Button m_btn_Music = dynamic_cast <Button *> (CheckerboardUI-> getChildByName ("Button_Music"); CC_BREAK_IF (! M_btn_Music); // the chessboard lock m_btn_Lock = dynamic_cast <Button *> (CheckerboardUI-> getChildByName ("Button_Lock"); CC_BREAK_IF (! M_btn_Lock); // the chessboard image m_Image_Checkerboard = dynamic_cast <ImageView *> (CheckerboardUI-> getChildByName ("Image_Checkerboard"); CC_BREAK_IF (! M_Image_Checkerboard); m_Checkerboard = Checkerboard: create (m_Image_Checkerboard); auto SelectPeople = (ComRender *) (node-> getChildByTag (10031)-> getComponent ("GUIComponent ")); selectPeopleUI = SelectPeople-> getNode (); this-> setinclutn (); return true;} while (0); CCLOG ("ERROR: Load Resources Fail in GameScene :: setScene "); return false ;}

If any variable is still nullptr after execution, CC_BREAK_IF will be returned, the error log will be displayed, and false will be returned to the upper-layer function for processing. This processing method is not only elegant and convenient, but also avoids the possibility of incorrect use of null pointers.


Case 2: In any case, you need to clean up the function at the end.

The following is a typical example. A new pointer (non-intelligent pointer) is declared at the beginning of the function. delete is required according to the normal process, however, we cannot guarantee that the program will return in the middle, because the maintainer does not know that the cleanup operation still needs to be executed. The following is a comparison example:

//errorvoid test(){GameScene* gamescene = new GameScene;// doSomething...if(gamescene->getChildByTag(111) == nullptr){return;}// doSomething...delete gamescene;gamescene = nullptr;}//rightvoid test1(){GameScene* gamescene = new GameScene;do{// doSomething...CC_BREAK_IF(gamescene->getChildByTag(111) == nullptr);// doSomething...}while(0);delete gamescene;gamescene = nullptr;}

Using CC_BREAK_IF ensures that the last two rows are always executed, so that memory leakage does not occur.


However, this combination is not omnipotent. Here is a situation that is not suitable for this structure. When the do {} while (0) parentheses contain loops, this combination is not applicable, because break can only jump out of a loop after all. The following is an example:

Void test2 () {do {for (int I = 0; I <5; ++ I) {// doSomething... CC_BREAK_IF (I> 4); // doSomething ...} CCLOG ("always show this line of log");} while (0 );}

We hope that CC_BREAK_IF can jump out of the do {} while (0) loop, but in fact, only the for loop exists, so when do {} while (0) and CC_BREAK_IF are used, make sure that there are no other loops in it. If there are loops, try {} catch () {} is recommended (){}


What is the role of do {} while (0?

Cyclic Functions
Do {// execute code
} While () // if the condition is true, the code in the "continue coaching do" is "0". If it is a false execution, it will go down.

Meaning of an example of dowhile (0)

Avoid using goto.
In the past, the traditional practice of c was to use goto to handle exceptions. However, goto is not easy to manage. So we replaced it with while (0 ).

Related Article

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.