Skip to content

Instantly share code, notes, and snippets.

@odecaux

odecaux/main.cpp Secret

Created November 23, 2020 14:54
Show Gist options
  • Save odecaux/17cc74b005dbbdf76948ccaf153b93b7 to your computer and use it in GitHub Desktop.
Save odecaux/17cc74b005dbbdf76948ccaf153b93b7 to your computer and use it in GitHub Desktop.
Lager minimal test
#include <iostream>
#include <variant>
#include <optional>
#include <lager/util.hpp>
#include <lager/store.hpp>
#include <lager/event_loop/manual.hpp>
struct model
{
int value = 0;
};
struct increment_action {};
struct decrement_action {};
struct reset_action{ int new_value = 0;};
using action = std::variant<increment_action, decrement_action, reset_action>;
model update(model c, action action)
{
return std::visit(lager::visitor{
[&](increment_action) {
++c.value;
return c;
},
[&](decrement_action) {
--c.value;
return c;
},
[&](reset_action a) {
c.value = a.new_value;
return c;
},
},
action);
}
std::optional<action> intent(char event)
{
switch (event) {
case '+' :
return increment_action{};
case '-' :
return decrement_action{};
case '.' :
return reset_action{};
default:
return std::nullopt;
}
}
void draw(model prev, model curr)
{
std::cout << "last value: " << prev.value << '\n';
std::cout << "current value: " << curr.value << '\n';
}
int main()
{
auto store = lager::make_store<action>(
model{},
update,
lager::with_manual_event_loop{});
watch(store, draw);
auto event = char{};
while (std::cin >> event) {
if (auto act = intent(event))
store.dispatch(*act);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment