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_;
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()) )
{
start_.wire_on(stop_); stop_.wire_on(start_); }
~car()
{
stop();
}
void start()
{
start_();
}
void stop()
{
stop_();
}
private:
gate start_;
gate stop_;
car* get_this() { return this; }
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.