Click here to Skip to main content
11,496,146 members (775 online)
Click here to Skip to main content

Tagged as

Removing Ugly Flags in C++

, 18 Oct 2011 CPOL 24.8K 6
Using the Call Gate idiom to reduce number of boolean flags and state variables.
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
Let's consider a typical use case.

struct car
{
  car()
    :started_(false)
  {
  }
  ~car()
  {
    stop();
  }
 
  void start()
  {
    if(started_)
       return;
    do_start();
    started_=true;
  }
  void stop()
  {
    if(!started_)
       return;
    do_stop();
    started_=false;
  }
private:
  bool started_;
 
  // do actual work
  void do_start();
  void do_stop();
};
Doesn't the 'started_' flag look ugly? It does to me. With the Call Gate idiom, that could be changed to:

struct car
{
  car()
    :start_( boost::bind(&car::do_play, get_this()) )
    ,stop_( boost::bind(&car::do_stop, get_this()) )
  {
    //wire the gates
    start_.wire_on(stop_); //'start' opens the 'stop' gate
    stop_.wire_on(start_); //'stop' opens the 'start' gate
  }
  ~car()
  {
    stop();
  }
 
  void start()
  {
    start_();
  }
  void stop()
  {
    stop_();
  }
 
private:
  gate start_;
  gate stop_;
 
  car* get_this() { return this; }
 
  // do actual work
  void do_start();
  void do_stop();
};

Replacing only one bool with call gates is probably an overkill, but there is a more interesting sample in Call Gate Idiom, and an actual implementation of the call gates.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

egladysh

United States United States
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 2 What looks ugly to me in the first p... Pin
nv314-Dec-11 5:16
membernv314-Dec-11 5:16 
GeneralMy vote of 5: It is a nice way to hide the annoying state ca... Pin
semmel7125-Oct-11 3:06
membersemmel7125-Oct-11 3:06 
GeneralReason for my vote of 5 Nice! It centralizes the setup of th... Pin
semmel7125-Oct-11 3:02
membersemmel7125-Oct-11 3:02 
GeneralReason for my vote of 1 Why make it simple when you can make... Pin
YvesDaoust25-Oct-11 0:01
memberYvesDaoust25-Oct-11 0:01 
GeneralReason for my vote of 3 Replacing ugly Code with bloated ugl... Pin
Johann Anhofer24-Oct-11 11:01
memberJohann Anhofer24-Oct-11 11:01 
GeneralReason for my vote of 1 The article is way too short to expl... Pin
Wolfgang_Baron18-Oct-11 14:18
memberWolfgang_Baron18-Oct-11 14:18 
GeneralFor a more complex example, please see the player type at th... Pin
egladysh18-Oct-11 8:06
memberegladysh18-Oct-11 8:06 
GeneralReason for my vote of 2 No practical application comes easil... Pin
xelous218-Oct-11 4:29
memberxelous218-Oct-11 4:29 
GeneralReason for my vote of 4 Interesting subject, would love to s... Pin
Julien Villers17-Oct-11 23:32
memberJulien Villers17-Oct-11 23:32 
GeneralReason for my vote of 4 An interesting idiom. Pin
Jon Summers17-Oct-11 10:33
memberJon Summers17-Oct-11 10:33 
QuestionWhy replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
Stefan_Lang18-Oct-11 4:54
memberStefan_Lang18-Oct-11 4:54 
AnswerRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
egladysh18-Oct-11 8:40
memberegladysh18-Oct-11 8:40 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
Stefan_Lang19-Oct-11 0:26
memberStefan_Lang19-Oct-11 0:26 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
xelous219-Oct-11 1:12
memberxelous219-Oct-11 1:12 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
egladysh19-Oct-11 8:54
memberegladysh19-Oct-11 8:54 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
Stefan_Lang19-Oct-11 23:49
memberStefan_Lang19-Oct-11 23:49 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
egladysh20-Oct-11 7:50
memberegladysh20-Oct-11 7:50 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
Stefan_Lang21-Oct-11 1:40
memberStefan_Lang21-Oct-11 1:40 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
egladysh21-Oct-11 7:38
memberegladysh21-Oct-11 7:38 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
Stefan_Lang25-Oct-11 5:59
memberStefan_Lang25-Oct-11 5:59 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
egladysh25-Oct-11 6:41
memberegladysh25-Oct-11 6:41 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
Stefan_Lang25-Oct-11 6:58
memberStefan_Lang25-Oct-11 6:58 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
egladysh25-Oct-11 8:01
memberegladysh25-Oct-11 8:01 
GeneralFeedback Mechanism Absent Pin
xelous218-Oct-11 4:26
memberxelous218-Oct-11 4:26 
GeneralRe: Feedback Mechanism Absent Pin
egladysh18-Oct-11 8:50
memberegladysh18-Oct-11 8:50 
QuestionCouple of criticisms Pin
Don Clugston17-Oct-11 22:34
memberDon Clugston17-Oct-11 22:34 
AnswerRe: Couple of criticisms Pin
egladysh18-Oct-11 8:55
memberegladysh18-Oct-11 8:55 
AnswerRe: Couple of criticisms Pin
egladysh18-Oct-11 15:25
memberegladysh18-Oct-11 15:25 
AnswerDisagree Pin
ryo-oh-ki17-Oct-11 17:33
memberryo-oh-ki17-Oct-11 17:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 18 Oct 2011
Article Copyright 2011 by egladysh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid