aboutsummaryrefslogtreecommitdiff
path: root/src/cru_event.hpp
blob: 76a36b226adb113558ffd37954db95bedf1b5edf (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
77
78
79
80
81
82
83
84
85
86
#pragma once

#include <type_traits>
#include <functional>
#include <unordered_map>

#include "base.hpp"

namespace cru {
	//Base class of all event args.
	class BasicEventArgs : public Object
	{
	public:
		explicit BasicEventArgs(Object* sender)
            : sender_(sender)
		{
		    
		}
	    BasicEventArgs(const BasicEventArgs& other) = default;
	    BasicEventArgs(BasicEventArgs&& other) = default;
	    BasicEventArgs& operator=(const BasicEventArgs& other) = default;
	    BasicEventArgs& operator=(BasicEventArgs&& other) = default;
		~BasicEventArgs() override = default;

		//Get the sender of the event.
		Object* GetSender() const
		{
            return sender_;
		}

	private:
		Object* sender_;
	};


	//A non-copyable non-movable Event class.
	//It stores a list of event handlers.
	//TArgsType must be subclass of BasicEventArgs.
    template<typename TArgsType>
    class Event
	{
    public:
		static_assert(std::is_base_of_v<BasicEventArgs, TArgsType>,
			"TArgsType must be subclass of BasicEventArgs.");


        using ArgsType = TArgsType;
        using EventHandler = std::function<void(ArgsType&)>;
        using EventHandlerToken = long;

        Event() = default;
        Event(const Event&) = delete;
        Event& operator = (const Event&) = delete;
        Event(Event&&) = delete;
        Event& operator = (Event&&) = delete;
        ~Event() = default;

        EventHandlerToken AddHandler(const EventHandler& handler)
		{
            const auto token = current_token_++;
		    handlers_.emplace(token, handler);
            return token;
		}

		void RemoveHandler(const EventHandlerToken token) {
			auto find_result = handlers_.find(token);
			if (find_result != handlers_.cend())
				handlers_.erase(find_result);
		}

		void Raise(ArgsType& args) {
			for (const auto& handler : handlers_)
				(handler.second)(args);
		}

        bool IsNoHandler() const
		{
		    return handlers_.empty();
		}

    private:
        std::unordered_map<EventHandlerToken, EventHandler> handlers_;

        EventHandlerToken current_token_ = 0;
    };
}