19 template <
typename R,
typename...
ARGS>
20 struct multicast_function_traits
22 using return_type = R;
23 using argument_types = std::tuple<
ARGS...>;
26 template <
typename R,
typename ARG>
27 struct multicast_function_traits<R,
ARG>
29 using return_type = R;
30 using argument_type =
ARG;
33 template <
typename R,
typename ARG1,
typename ARG2>
34 struct multicast_function_traits<R,
ARG1,
ARG2>
36 using return_type = R;
37 using first_argument_type =
ARG1;
38 using second_argument_type =
ARG2;
42 template <
typename R,
typename...
ARGS>
43 class mutlticast_function;
47 template <
typename R,
typename...
ARGS>
48 class mutlticast_function<R(
ARGS...)> :
public multicast_function_traits<R, ARGS...>
52 enum class handle :
size_t {};
54 mutlticast_function()
noexcept =
default;
55 mutlticast_function(mutlticast_function
const&)
noexcept =
default;
56 mutlticast_function(mutlticast_function&&)
noexcept =
default;
57 mutlticast_function& operator =(mutlticast_function
const&)
noexcept =
default;
58 mutlticast_function& operator =(mutlticast_function&&)
noexcept =
default;
70 const auto new_id = handle{ m_last_id++ };
76 void operator-=(handle handle) { remove(handle); }
81 m_listeners.erase(handle);
90 return call_helper<R>::template call(m_listeners, std::forward<CALL_ARGS>(
args)...);
100 auto listeners()
const {
return std::ranges::views::values(m_listeners); }
104 template <
typename R>
107 template <
typename... CALL_ARGS>
108 static std::vector<R> call(std::map<handle, std::function<R(ARGS...)>>
const& listeners, CALL_ARGS&&... args)
111 ret.reserve(listeners.size());
112 for (
auto& [handle, listener] : listeners)
113 ret.push_back(listener(std::forward<CALL_ARGS>(args)...));
121 template <
typename... CALL_ARGS>
122 static void call(std::map<handle, std::function<
void(ARGS...)>>
const& listeners, CALL_ARGS&&... args)
124 for (
auto& [handle, listener] : listeners)
125 listener(std::forward<CALL_ARGS>(args)...);
129 std::map<handle, std::function<R(
ARGS...)>> m_listeners;
130 size_t m_last_id = {};
135 template <
typename...
ARGS>
139 if (
func) std::exchange(
func, {})(std::forward<CALL_ARGS>(
args)...);
145 template <
typename FUNC>
152 template <
typename T,
typename FUNC>
153 auto transform(std::optional<T>
const& value,
FUNC&&
func) ->
decltype(std::optional{
func(value.value()) })
155 return value ? std::optional{
func(value.value()) } : std::nullopt;
auto listeners() const
Returns a view over all the added invocables.
void clear()
Removes all the invocables from this objects.
void remove(handle handle)
Removes the invocable associated with handle
handle add(F &&listener)
Adds a new invocable to the list.
auto operator()(CALL_ARGS &&... args) const
Calls all the invocables added to this object.
constexpr auto bit_count
Equal to the number of bits in the type.
auto make_single_time_function(std::function< void(ARGS...)> func)
Returns a function that calls func when invoked, but only the first time.
Primary namespace for everything in this library.