//---------------------------------------------------------------------------
/*
CppDesignPatternState, demonstrates the State design pattern
Copyright (C) 2011 Richel Bilderbeek
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppDesignPatternState.htm
//---------------------------------------------------------------------------
#include <iostream>
//---------------------------------------------------------------------------
#include "gumballmachine.h"
#include "hasquarterstate.h"
#include "noquarterstate.h"
#include "soldoutstate.h"
#include "soldstate.h"
#include "state.h"
//---------------------------------------------------------------------------
GumballMachine::GumballMachine(const int n_gumballs)
: m_has_quarter_state(new HasQuarterState(this)),
m_no_quarter_state(new NoQuarterState(this)),
m_sold_out_state(new SoldOutState(this)),
m_sold_state(new SoldState(this)),
m_count(n_gumballs)
{
//delete m_state; //Should not be possible
if (m_count)
{
SetState(GetNoQuarterState());
}
else
{
SetState(GetSoldOutState());
}
}
//---------------------------------------------------------------------------
void GumballMachine::EjectQuarter()
{
m_state->EjectQuarter();
}
//---------------------------------------------------------------------------
int GumballMachine::GetCount() const
{
assert(m_count >= 0);
return m_count;
}
//---------------------------------------------------------------------------
State * GumballMachine::GetHasQuarterState()
{
return m_has_quarter_state.get();
}
//---------------------------------------------------------------------------
State * GumballMachine::GetNoQuarterState()
{
return m_no_quarter_state.get();
}
//---------------------------------------------------------------------------
State * GumballMachine::GetSoldOutState()
{
return m_sold_out_state.get();
}
//---------------------------------------------------------------------------
State * GumballMachine::GetSoldState()
{
return m_sold_state.get();
}
//---------------------------------------------------------------------------
const State * GumballMachine::GetState() const
{
return m_state;
}
//---------------------------------------------------------------------------
void GumballMachine::InsertQuarter()
{
m_state->InsertQuarter();
}
//---------------------------------------------------------------------------
void GumballMachine::ReleaseBall()
{
std::cout << "A gumball comes rolling out of the solt...\n";
--m_count;
assert(m_count >= 0);
}
//---------------------------------------------------------------------------
void GumballMachine::SetState(State * const state)
{
assert(state);
//No need to delete the old m_state, this is done
//by the boost::scoped_ptr's
m_state = state;
}
//---------------------------------------------------------------------------
void GumballMachine::TurnCrank()
{
m_state->TurnCrank();
}
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const GumballMachine& gumballMachine)
{
os
<< "Mighty Gumball, Inc.\n"
<< "C++-enabled Standing Gumball Model #2004\n"
<< "Inventory: " << gumballMachine.GetCount()
<< " gumballs\n"
<< gumballMachine.GetState()->GetDescription()
<< '\n';
return os;
}
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const GumballMachine * const gumballMachine)
{
assert(gumballMachine);
return operator<<(os,*gumballMachine);
}
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const boost::scoped_ptr<GumballMachine>& gumballMachine)
{
assert(gumballMachine);
return operator<<(os,gumballMachine.get());
}
//---------------------------------------------------------------------------
|