Skip to content

Commit

Permalink
Use macros to make the state machine easier to read
Browse files Browse the repository at this point in the history
  • Loading branch information
jkflying committed Jun 25, 2019
1 parent 65122f9 commit 0c3a0f5
Show file tree
Hide file tree
Showing 3 changed files with 301 additions and 249 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ set (CMAKE_CXX_STANDARD 11)
project(state_machine)

add_executable(usm_test test/usm_test.cpp)

#install(TARGETS state_machine RUNTIME DESTINATION bin)
128 changes: 78 additions & 50 deletions include/usm.hpp
Original file line number Diff line number Diff line change
@@ -1,71 +1,99 @@
#pragma once

/*
* Copyright (c) 2019 Julian Kent. All rights reserved.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of libgnc nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*
* For usage examples, look at the tests.
* Copyright (c) 2019 Julian Kent. All rights reserved.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of libgnc nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*
* For usage examples, look at the tests.
*/

namespace usm {

enum Transition { REPEAT, NEXT, NEXT2, NEXT3, NEXT4, ERROR };
enum Transition { REPEAT, NEXT1, NEXT2, NEXT3, NEXT4, ERROR };

template <typename StateEnum> class StateMachine {
public:
StateMachine(StateEnum startingState);
void iterateOnce();
template <typename StateEnum>
class StateMachine {
public:
StateMachine(StateEnum startingState);
void iterateOnce();

StateEnum getState();
StateEnum getState();

protected:
virtual Transition runCurrentState(StateEnum currentState) = 0; // a big switch
virtual StateEnum chooseNextState(StateEnum currentState, Transition transition) = 0; // nested switches
protected:
virtual Transition runCurrentState(
StateEnum currentState) = 0; // a big switch
virtual StateEnum chooseNextState(
StateEnum currentState, Transition transition) = 0; // nested switches

private:
StateEnum m_currentState;
private:
StateEnum m_currentState;
};

/*---------------IMPLEMENTATION------------------*/

template <typename StateEnum>
StateMachine<StateEnum>::StateMachine(StateEnum startingState)
: m_currentState(startingState)
{
}
: m_currentState(startingState) {}

template <typename StateEnum> void StateMachine<StateEnum>::iterateOnce()
{
m_currentState = chooseNextState(m_currentState, runCurrentState(m_currentState));
template <typename StateEnum>
void StateMachine<StateEnum>::iterateOnce() {
Transition t = runCurrentState(m_currentState);
if (t != REPEAT) m_currentState = chooseNextState(m_currentState, t);
}

template <typename StateEnum> StateEnum StateMachine<StateEnum>::getState() { return m_currentState; }
template <typename StateEnum>
StateEnum StateMachine<StateEnum>::getState() {
return m_currentState;
}
}

/*---------------MACROS TO MAKE TRANSITION TABLES EASY------------------*/

// clang-format off
#define USM_TABLE(current_state, error, ...) \
switch (current_state) { \
__VA_ARGS__; \
default: break; \
} \
return error

#define USM_STATE(transition, start_state, ...) \
case start_state: \
switch (transition) { \
__VA_ARGS__; \
default: break; \
} \
break

#define USM_MAP(transition, next_state) \
case transition: return next_state

// clang-format on
Loading

0 comments on commit 0c3a0f5

Please sign in to comment.