Skip to content

Instantly share code, notes, and snippets.

@nihil84
Last active February 8, 2019 15:51
Show Gist options
  • Save nihil84/16464283bc58be6e0058a325463787d6 to your computer and use it in GitHub Desktop.
Save nihil84/16464283bc58be6e0058a325463787d6 to your computer and use it in GitHub Desktop.
ReduCpp basic usage example
#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