Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Brad Phelan committed Mar 1, 2024
1 parent 032d53f commit 5bbfb9d
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion include/threepp/core/EventDispatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@

namespace threepp {

namespace concepts {
#if defined(__cpp_concepts) && (__cpp_concepts >= 201907L)
template<typename TEvent>
concept Event = requires(TEvent e) {
{ e.target } -> std::convertible_to<void*>;
{ e.unsubscribe } -> std::convertible_to<bool>;
};
#endif
}

/// An event listener is just a function that takes an argument of type TEvent
template<typename TEvent>
Expand All @@ -27,12 +36,16 @@ namespace threepp {

/// Allows one to use the << to push subscriptions onto the vector
inline
void operator<<(std::vector<Subscription>& subs, Subscription& sub) {
void operator<<(std::vector<Subscription>& subs, Subscription const & sub) {
subs.push_back(sub);
}

/// Generic event dispatch class
template <typename TEvent>
#if defined(__cpp_concepts) && (__cpp_concepts >= 201907L)
requires concepts::Event<TEvent>
#endif
// C++20 (and later) code
class TEventDispatcher {
public:
using EventListener = TEventListener<TEvent>;
Expand Down Expand Up @@ -69,6 +82,7 @@ namespace threepp {
s.subscribeOnce([sub](auto&) {});
}


/// Send an event to all listeners.
void send(TEvent & e){
std::vector<size_t> toUnsubscribe;
Expand All @@ -83,6 +97,11 @@ namespace threepp {
listeners_.erase(id);
}

/// Handle r-value versions of send
void send(TEvent && e) {
send(e);
}

virtual ~TEventDispatcher() = default;

private:
Expand Down

0 comments on commit 5bbfb9d

Please sign in to comment.