[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppStateMultiFile

(C++) State in multiple files

The files in this example are:
  • main.cpp, containing the program itself
  • Enum.h, contains the possible States
  • Context.h
  • Context.cpp
  • State.h
  • State.cpp
All header files have an #include guard.

In its header file, the State has a forward declarion to its Context. Therefore, in its implementation file (State.cpp), the header file of the Context needs to be #included.

main.cpp

  1. include <memory>
  2. include "Context.h"
int main() { const std::auto_ptr<Context> myContext(new Context); myContext->setState(plus); myContext->doIt(); myContext->setState(minus); myContext->doIt(); }


Enum.h

  1. ifndef __ENUM_H
  2. define __ENUM_H
enum enumState{ plus, minus}; //End of: #ifndef __ENUM_H
  1. endif


Context.h

  1. ifndef __CONTEXT_H
  2. define __CONTEXT_H
  1. include <iostream>
  2. include <memory>
  3. include <cassert>
  4. include "State.h"
  5. include "Enum.h"
class Context { friend class StateBase; friend class StatePlus; friend class StateMinus; public: Context(); void setState(const enumState&); void doIt() const; private: std::auto_ptr<StateBase> mState; const int mValue1; const int mValue2; }; //End of: #ifndef __CONTEXT_H
  1. endif


Context.cpp

  1. include "Context.h"
//--------------------------------------------------------------------------- Context::Context() : mState(new StatePlus(this)), mValue1(std::rand()%100), mValue2(std::rand()%100) { //Nothing } //--------------------------------------------------------------------------- void Context::setState(const enumState& state) { switch(state) { case plus: mState.reset(new StatePlus(this)); break; case minus: mState.reset(new StateMinus(this)); break; default: assert(!"Unknown state"); std::exit(1); } } //--------------------------------------------------------------------------- void Context::doIt() const { //The function doIt is delegated to the State std::cout << "Doing it with " << mValue1 << " and " << mValue2 << ": " << mState->doIt() << std::endl; } //---------------------------------------------------------------------------


State.h

  1. ifndef __STATE_H
  2. define __STATE_H
class Context; //Forward declaration of Context class StateBase { public: StateBase() { mContext = 0; } virtual int doIt() const = 0; protected: Context * mContext; }; class StatePlus : public StateBase { public: StatePlus(Context *); int doIt() const; }; class StateMinus : public StateBase { public: StateMinus(Context *); int doIt() const; }; //End of: #ifndef __STATE_H
  1. endif


State.cpp

  1. include "State.h"
  1. include "Context.h"
//--------------------------------------------------------------------------- StatePlus::StatePlus(Context * context) { mContext = context; } //--------------------------------------------------------------------------- StateMinus::StateMinus(Context * context) { mContext = context; } //--------------------------------------------------------------------------- int StatePlus::doIt() const { return (mContext->mValue1 + mContext->mValue2); } //--------------------------------------------------------------------------- int StateMinus::doIt() const { return (mContext->mValue1 - mContext->mValue2); } //---------------------------------------------------------------------------


last edited (November 16, 2006) by bilderbikkel, Number of views: 2046, Current Rev: 6 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.