Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C++11
Tip/Trick

Removing Ugly Flags in C++

Rate me:
Please Sign up or sign in to vote.
4.00/5 (13 votes)
18 Oct 2011CPOL 63K   6   29
Using the Call Gate idiom to reduce number of boolean flags and state variables.
Let's consider a typical use case.

C++
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:

C++
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 2 What looks ugly to me in the first p... Pin
nv314-Dec-11 4:16
nv314-Dec-11 4:16 
GeneralMy vote of 5: It is a nice way to hide the annoying state ca... Pin
semmel7125-Oct-11 2:06
semmel7125-Oct-11 2:06 
GeneralReason for my vote of 5 Nice! It centralizes the setup of th... Pin
semmel7125-Oct-11 2:02
semmel7125-Oct-11 2:02 
GeneralReason for my vote of 1 Why make it simple when you can make... Pin
YvesDaoust24-Oct-11 23:01
YvesDaoust24-Oct-11 23:01 
GeneralReason for my vote of 3 Replacing ugly Code with bloated ugl... Pin
Johann Anhofer24-Oct-11 10:01
Johann Anhofer24-Oct-11 10:01 
GeneralReason for my vote of 1 The article is way too short to expl... Pin
Wolfgang_Baron18-Oct-11 13:18
professionalWolfgang_Baron18-Oct-11 13:18 
GeneralFor a more complex example, please see the player type at th... Pin
egladysh18-Oct-11 7:06
egladysh18-Oct-11 7:06 
GeneralReason for my vote of 2 No practical application comes easil... Pin
xelous218-Oct-11 3:29
xelous218-Oct-11 3:29 
GeneralReason for my vote of 4 Interesting subject, would love to s... Pin
Julien Villers17-Oct-11 22:32
professionalJulien Villers17-Oct-11 22:32 
GeneralReason for my vote of 4 An interesting idiom. Pin
Jon Summers17-Oct-11 9:33
Jon Summers17-Oct-11 9:33 
QuestionWhy replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
Stefan_Lang18-Oct-11 3:54
Stefan_Lang18-Oct-11 3:54 
AnswerRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
egladysh18-Oct-11 7:40
egladysh18-Oct-11 7:40 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
Stefan_Lang18-Oct-11 23:26
Stefan_Lang18-Oct-11 23:26 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
xelous219-Oct-11 0:12
xelous219-Oct-11 0:12 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
egladysh19-Oct-11 7:54
egladysh19-Oct-11 7:54 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
Stefan_Lang19-Oct-11 22:49
Stefan_Lang19-Oct-11 22:49 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
egladysh20-Oct-11 6:50
egladysh20-Oct-11 6:50 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
Stefan_Lang21-Oct-11 0:40
Stefan_Lang21-Oct-11 0:40 
egladysh wrote:
Typically state machine transitions perform certain actions

We seem to disagree on that. Unfortunately I didn't find a concise definition of a state transition, the link I posted above only explains transition tables, some of which do, and some don't contain actions. The latter supports my interpretation, that a transition is just the edge in the graph describing a state machine. This edge just describes the possibility of a transition from one state to another. The transition is linked to an event or signal that triggers it, and there may or may not be an action bound to that transition. But that doesn't make the action part of the transition, at least not in my understanding. in my understanding the transitio and the action are just two independent things that are possibly related.

Anyway, that is what I meant: Your wires describe the edges of the graph of your state machine.

With respect to my other interpretations, You seem to have succeeded in utterly confusing me Wink | ;)

egladysh wrote:
in more consistent/scale-able/nicer/maintainable/generic way


Hmm. I'm sorry, but I don't agree:
1. consistent - I don't see how a single boolean variable can be less consistent than a model requiring two objects and a well-defined wiring, just to ensure these objects actually behave like a boolean state

2. scale-able - after looking at your player examlpe off-site, I am under the impression (which may be wrong) that the effort to set up replacement for a system of n flags is O(2^n). This would not only be not scalable, it'd be unsuable! Can you set up a system of ten flags in less than 1000 lines?

3. nicer - as you have agreed yourself, a system of two objects that additionally require to be set up properly before use may not fulfill everybody's understanding of nicer - it definitly isn't for me.

4. maintainable - how much effort does it take to scale up a system of 6 flags to 8 flags? How much effort does it take when you realize you need a flag with 3 or more states? The former requires quite a lot, the latter is impossible within your framework. I'm not saying that such changes wouldn't require a lot of work when starting on a system of flags, but I'll wager it's still a lot less.

You may argue that you have to touch every action code that changes a flag - but how many of these functions are there? Your system only allows for two - it seems there can never be more than two actions and events tied to one flag! For a boolean flag, if there are only two functions that touch it, then, ok, there's only two places to fix. If there are more, then replacing the system with yours migth be impossible to start with!

5. generic - as pointed out above, in your framework, every flag is represented by exactly two states, exactly two transitions, and exactly two events that trigger these transitions, and the actions associated with them. Not a lot you can be 'generic' about. The only thing you can do that a simple set of flags can't, is to restrict transitions. Unfortunately however, this is the O(2^n) part I mentioned above. I wouldn't really call it generic when you have to specify the behaviour for every single combination of the system individually!

I'm sorry I'm coming about so negative - your concept really looks interesting. In fact so interesting I am almost desperate to find a reasonable application for it. But somehow I can't. D'Oh! | :doh:
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
egladysh21-Oct-11 6:38
egladysh21-Oct-11 6:38 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
Stefan_Lang25-Oct-11 4:59
Stefan_Lang25-Oct-11 4:59 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
egladysh25-Oct-11 5:41
egladysh25-Oct-11 5:41 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
Stefan_Lang25-Oct-11 5:58
Stefan_Lang25-Oct-11 5:58 
GeneralRe: Why replace an ugly flag with two more ugly objects at the cost of functionality and maintainability? Pin
egladysh25-Oct-11 7:01
egladysh25-Oct-11 7:01 
GeneralFeedback Mechanism Absent Pin
xelous218-Oct-11 3:26
xelous218-Oct-11 3:26 
GeneralRe: Feedback Mechanism Absent Pin
egladysh18-Oct-11 7:50
egladysh18-Oct-11 7:50 

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

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