2 #include <ossia/detail/config.hpp>
4 #include <ossia/detail/mutex.hpp>
6 #if defined(__cpp_exceptions)
19 #if defined(__cpp_exceptions)
25 struct OSSIA_EXPORT invalid_callback_error final :
public std::exception
27 ~invalid_callback_error()
override;
28 invalid_callback_error();
29 const char* what() const noexcept override;
47 using mutex = OSSIA_CALLBACK_CONTAINER_MUTEX;
48 using lock_guard = std::lock_guard<mutex>;
54 lock_guard lck{other.m_mutx};
55 m_callbacks = other.m_callbacks;
59 lock_guard lck{other.m_mutx};
60 m_callbacks = std::move(other.m_callbacks);
64 lock_guard lck{other.m_mutx};
65 m_callbacks = other.m_callbacks;
70 lock_guard lck{other.m_mutx};
71 m_callbacks = std::move(other.m_callbacks);
83 using iterator =
typename impl::iterator;
94 lock_guard lck{m_mutx};
95 auto it = m_callbacks.insert(m_callbacks.begin(), std::move(callback));
96 if(m_callbacks.size() == 1)
97 on_first_callback_added();
102 #if defined(__cpp_exceptions)
103 throw invalid_callback_error{};
116 lock_guard lck{m_mutx};
117 if(m_callbacks.size() == 1)
118 on_removing_last_callback();
120 m_callbacks.erase(it);
128 lock_guard lck{m_mutx};
129 *m_callbacks.erase(it, it) = std::move(cb);
132 void replace_callbacks(impl&& cbs)
134 lock_guard lck{m_mutx};
135 m_callbacks = std::move(cbs);
138 class disabled_callback
141 explicit disabled_callback(callback_container&
self)
143 , old_callbacks{self.m_callbacks}
147 ~disabled_callback() {
self.replace_callbacks(std::move(old_callbacks)); }
150 callback_container&
self;
154 disabled_callback disable_callback(iterator it)
156 lock_guard lck{m_mutx};
157 disabled_callback dis{*
this};
161 m_callbacks.erase(it);
171 lock_guard lck{m_mutx};
172 return m_callbacks.size();
181 lock_guard lck{m_mutx};
182 return m_callbacks.empty();
189 template <
typename... Args>
192 lock_guard lck{m_mutx};
193 for(
auto& callback : m_callbacks)
196 callback(std::forward<Args>(args)...);
205 lock_guard lck{m_mutx};
206 if(!m_callbacks.empty())
207 on_removing_last_callback();
233 impl m_callbacks TS_GUARDED_BY(m_mutx);
234 mutable mutex m_mutx;
The callback_container class.
Definition: callback_container.hpp:46
void callbacks_clear()
clear Clears callbacks.
Definition: callback_container.hpp:203
virtual void on_removing_last_callback()
on_removing_last_callback
Definition: callback_container.hpp:229
bool callbacks_empty() const
callbacks_empty
Definition: callback_container.hpp:179
void replace_callback(iterator it, T &&cb)
Replaces an existing callback with another function.
Definition: callback_container.hpp:126
std::list< T > impl
impl How the callbackas are stored. A list is used since iterators to other callbacks must not be inv...
Definition: callback_container.hpp:82
virtual void on_first_callback_added()
on_first_callback_added
Definition: callback_container.hpp:222
std::size_t callback_count() const
callback_count
Definition: callback_container.hpp:169
void remove_callback(iterator it)
remove_callback Removes a callback identified by an iterator.
Definition: callback_container.hpp:114
iterator add_callback(T &&callback)
add_callback Add a new callback.
Definition: callback_container.hpp:90
void send(Args &&... args)
send Trigger all callbacks
Definition: callback_container.hpp:190