Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 |
SwitchState<State, States, StateStore, NoStack> startWaiting = stateMgr.getSwitchState (waiting); |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
#include <map> #include <stack> #include <boost\function.hpp> #include <boost\shared_ptr.hpp> template <class State, class Key> class StateStore { public: void registerState (boost::shared_ptr<State> state, Key key) { mStates.insert (std::make_pair (key, state) ); } boost::shared_ptr<State> getState (Key key) { return mStates [key]; } private: std::map<Key, boost::shared_ptr<State>> mStates; }; template <class State, class Key> class StateCreator { public: void registerState (boost::function<boost::shared_ptr<State> ()> creator, Key key) { mCreators.insert (std::make_pair (key, creator) ); } boost::shared_ptr<State> getState (Key key) { return mCreators [key] (); } private: std::map<Key, boost::function<boost::shared_ptr<State> ()>> mCreators; }; template <class State> class NoStack { public: NoStack (boost::shared_ptr<State> initialState) : mCurrentState (initialState) { } void switchState (boost::shared_ptr<State> state) { mCurrentState = state; } boost::shared_ptr<State> useCurrentState () const { return mCurrentState; } private: boost::shared_ptr<State> mCurrentState; }; template <class State> class PopState; template <class State> class StateStack { public: StateStack (boost::shared_ptr<State> initialState) { mStack.push (initialState); } void switchState (boost::shared_ptr<State> state) { mStack.push (state); } boost::shared_ptr<State> useCurrentState () const { return mStack.top (); } void popState () { mStack.pop (); } PopState<State> getPopState (); private: std::stack<boost::shared_ptr<State>> mStack; }; template <class State> class PopState { public: PopState (StateStack<State>* stateStack) : mStateStack (stateStack) { } void operator () () { mStateStack->popState (); } private: StateStack<State>* mStateStack; }; template <class State> PopState<State> StateStack<State>::getPopState () { return PopState<State> (this); } template <class State, class Key, template <class, class> class StateRegister, template <class> class UseStack> class SwitchState; template <class State, class Key, template <class, class> class StateRegister, template <class> class UseStack> class StateManager : public StateRegister<State, Key>, public UseStack<State> { public: StateManager (boost::shared_ptr<State> initialState) : UseStack<State> (initialState) { } SwitchState<State, Key, StateRegister, UseStack> getSwitchState (Key key); private: }; template <class State, class Key, template <class, class> class StateRegister, template <class> class UseStack> class SwitchState { public: SwitchState (StateManager<State, Key, StateRegister, UseStack>* stateMgr, Key key) : mStateMgr (stateMgr), mKey (key) { } void operator () () { mStateMgr->switchState (mStateMgr->getState (mKey) ); } private: StateManager<State, Key, StateRegister, UseStack>* mStateMgr; Key mKey; }; template <class State, class Key, template <class, class> class StateRegister, template <class> class UseStack> SwitchState<State, Key, StateRegister, UseStack> StateManager<State, Key, StateRegister, UseStack>::getSwitchState (Key key) { return SwitchState<State, Key, StateRegister, UseStack> (this, key); } |
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von »BlueCobold« (09.08.2012, 07:37)