Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
TListener.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3#include <memory>
4#include <misc/global.h>
5
6namespace sf
7{
8
13{
14 public:
18 struct base_type
19 {
20 virtual ~base_type() = default;
21
26 virtual bool validListener() = 0;
27 };
28
33
39
44 size_t removeListener(const base_type* entry);
45
53
54 private:
59 void appendListener(base_type* entry);
60
61 typedef std::vector<base_type*> list_type;
63 list_type _list;
64
68 template<typename... Args>
69 friend class TListener;
70};
71
77template<typename... Args>
79{
80 public:
84 typedef std::function<void(Args...)> func_type;
90
96 explicit TListener(ListenerList* list, const func_type& lambda);
97
101 TListener(const TListener&) = delete;
102
106 ~TListener() override;
107
112 {
113 public:
117 emitter_type() = default;
118
123
128 TListener* link(ListenerList* list, const func_type& function) const;
129
134 template<typename T>
135 TListener* link(ListenerList* list, T* obj, void (T::*method)(Args... args)) const;
136
141 void call(Args... args);
142
147 size_t cleanup();
148
152 void flush();
153
154 private:
158 std::vector<std::weak_ptr<func_type>> _list;
159 };
160
161 private:
166 bool validListener() override;
167
171 ListenerList* _owner;
175 std::shared_ptr<func_type> _ptr{};
176};
177
178}// namespace sf
179
180// Include all inlined functions and template implementations.
181#include <misc/gen/TListener.hpp>
Base class used as container for classes having listeners created by sf::TListener template.
Definition TListener.h:13
size_t cleanupListeners()
Removes all invalid/unused entries. There is no real need call this method explicitly since it is cal...
size_t flushListeners()
Deletes all the linked entries in the list.
size_t removeListener(const base_type *entry)
Removes the given entry from the list.
~ListenerList()
Destructor deleting all entries by calling flushListeners().
Emitter class type for creating an instance which is emitting events to listeners.
Definition TListener.h:112
size_t cleanup()
Removes expired listeners.
TListener * link(ListenerList *list, T *obj, void(T::*method)(Args... args)) const
Assigns a listener instance to this handler instance and also to the passed listener list.
TListener * link(ListenerList *list, const func_type &function) const
Assigns a listener instance to this handler instance and also to the passed listener list.
void flush()
Removes all listeners.
void call(Args... args)
Calls the linked listeners and also cleans up when pointer expired.
~emitter_type()
Destructor clearing all shared pointers.
Template class used to bind listeners to a handler_type instance.
Definition TListener.h:79
~TListener() override
Overridden destructor from ListenerList::base_type in order to delete entries of any instantiated tem...
TListener listener_type
Listener type of the lambda or function. This type is wraps around a std::function.
Definition TListener.h:89
TListener(ListenerList *list, const func_type &lambda)
Constructor.
std::function< void(Args...)> func_type
Function type of the lambda or function.
Definition TListener.h:84
TListener(const TListener &)=delete
Do not allow copying.
#define _MISC_CLASS
Definition misc/global.h:40
Definition Application.h:10
Type definition of a class having a virtual destructor used as a base class for sf::TListener.
Definition TListener.h:19
virtual ~base_type()=default
virtual bool validListener()=0
Will be overloaded in template to check the emitter has gone away.