Last active
February 8, 2019 15:51
-
-
Save nihil84/16464283bc58be6e0058a325463787d6 to your computer and use it in GitHub Desktop.
ReduCpp basic usage example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <reducpp/store.hpp> | |
#include <reducpp/action.hpp> | |
using namespace reducpp; | |
struct mystate | |
{ | |
int value; | |
}; | |
class myaction : public reducpp::action | |
{ | |
public: | |
enum TYPE { INCREMENT, DECREMENT }; | |
myaction(TYPE type) : m_type(type) { } | |
int type() const { return m_type; } | |
private: | |
TYPE m_type; | |
}; | |
store<mystate, myaction> counter(const mystate& state, const myaction& action) | |
{ | |
mystate newstate = state; | |
switch (action.type()) { | |
case myaction::INCREMENT: newstate.value++; break; | |
case myaction::DECREMENT: newstate.value--; break; | |
} | |
return std::move(newstate); | |
} | |
int main(int argc, char* argv[] | |
{ | |
store<mystate, myaction> store(counter); | |
sut.dispatch(myaction(myaction::INCREMENT)); | |
// 1 | |
sut.dispatch(myaction(myaction::INCREMENT)); | |
// 2 | |
sut.dispatch(myaction(myaction::DECREMENT)); | |
// 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment