summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hana/example/map/map.cpp
blob: a04ecfd739dc6b775e2bf3fd1b8c357c8119960f (plain)
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
// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.

#include <boost/hana/assert.hpp>
#include <boost/hana/at_key.hpp>
#include <boost/hana/contains.hpp>
#include <boost/hana/map.hpp>
#include <boost/hana/pair.hpp>
#include <boost/hana/string.hpp>

#include <functional>
#include <string>
#include <vector>
namespace hana = boost::hana;


template <typename ...Events>
struct event_system {
    using Callback = std::function<void()>;
    hana::map<hana::pair<Events, std::vector<Callback>>...> map_;

    template <typename Event, typename F>
    void on(Event e, F handler) {
        auto is_known_event = hana::contains(map_, e);
        static_assert(is_known_event,
            "trying to add a handler to an unknown event");

        map_[e].push_back(handler);
    }

    template <typename Event>
    void trigger(Event e) const {
        auto is_known_event = hana::contains(map_, e);
        static_assert(is_known_event,
            "trying to trigger an unknown event");

        for (auto& handler : this->map_[e])
            handler();
    }
};

template <typename ...Events>
event_system<Events...> make_event_system(Events ...events) {
    return {};
}


int main() {
    auto events = make_event_system(
        BOOST_HANA_STRING("foo"),
        BOOST_HANA_STRING("bar"),
        BOOST_HANA_STRING("baz")
    );

    std::vector<std::string> triggered_events;
    events.on(BOOST_HANA_STRING("foo"), [&] {
        triggered_events.push_back("foo:1");
    });
    events.on(BOOST_HANA_STRING("foo"), [&] {
        triggered_events.push_back("foo:2");
    });
    events.on(BOOST_HANA_STRING("bar"), [&] {
        triggered_events.push_back("bar:1");
    });
    events.on(BOOST_HANA_STRING("baz"), [&] {
        triggered_events.push_back("baz:1");
    });

    events.trigger(BOOST_HANA_STRING("foo"));
    events.trigger(BOOST_HANA_STRING("bar"));
    events.trigger(BOOST_HANA_STRING("baz"));

    BOOST_HANA_RUNTIME_CHECK(triggered_events == std::vector<std::string>{
        "foo:1", "foo:2", "bar:1", "baz:1"
    });
}