Silicon
A realtime platform for creating interactive media.
Event.hpp
Go to the documentation of this file.
1// BSD 2-Clause License
2//
3// Copyright (c) 2022, Matthew McCall
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8//
9// 1. Redistributions of source code must retain the above copyright notice, this
10// list of conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright notice,
13// this list of conditions and the following disclaimer in the documentation
14// and/or other materials provided with the distribution.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27//
28// Created by Matthew McCall on 8/20/22.
29//
30
31#ifndef SILICON_EVENT_HPP
32#define SILICON_EVENT_HPP
33
34#include <algorithm>
35#include <cstdint>
36#include <functional>
37
38#include "SDL_events.h"
39
40#include "Types.hpp"
41
42namespace Si {
43namespace Event {
44
50 struct AppQuit {};
51
52 struct WindowResize {};
53
54 void Process();
55
56}
57
58template <typename T>
59void Pub(const T& data);
60
65template <typename T>
66class Sub
67{
68 friend void Si::Pub<T>(const T& data);
69 using Callback = std::function<void(const T&)>;
70
71public:
76 explicit Sub(Callback func)
77 : Func(func)
78 {
79 Sub<T>::Subscribers.push_back(NotNull<Sub<T>*>(this));
80 }
81
83 {
84 const auto& i = std::find(Sub<T>::Subscribers.begin(), Sub<T>::Subscribers.end(), this);
85
86 if (i != Sub<T>::Subscribers.end())
87 Sub<T>::Subscribers.erase(i);
88 }
89
90private:
91 static std::vector<NotNull<Sub<T>*>> Subscribers;
92 Callback Func;
93};
94
100template <class T>
101void Pub(const T& data)
102{
103 for (Sub<T>* i : Sub<T>::Subscribers) {
104 i->Func(data);
105 }
106}
107
108template <typename T>
109std::vector<NotNull<Sub<T>*>> Sub<T>::Subscribers;
110
111}
112
113#endif // SILICON_EVENT_HPP
The Subscriber class allows you to listen in on published events of type T.
Definition: Event.hpp:67
Sub(Callback func)
Creates a new subscriber with the function to call on an event publish.
Definition: Event.hpp:76
~Sub()
Definition: Event.hpp:82
T find(T... args)
void Process()
Definition: Event.cpp:37
Definition: Allocator.hpp:36
void Pub(const T &data)
Publishes an event of type T to all subscribers immediately.
Definition: Event.hpp:101
gsl::strict_not_null< T > NotNull
Definition: Types.hpp:48
Published when the Engine begins it's shutdown procedures.
Definition: Event.hpp:50