SPH
CallbackSet.h
Go to the documentation of this file.
1 #pragma once
2 
7 
11 
13 
14 template <typename TSignature>
16 
17 template <typename... TArgs>
18 class CallbackSet<void(TArgs...)> {
19 public:
20  struct Callback {
21  Function<void(TArgs...)> functor;
23  };
24 
25 
26 private:
27  Array<Callback> callbacks;
28 
29 public:
30  CallbackSet() = default;
31 
32  CallbackSet(const CallbackSet& other)
33  : callbacks(other.callbacks.clone()) {}
34 
35  void insert(const SharedToken& owner, const Function<void(TArgs...)>& functor) {
36  if (owner) {
37  callbacks.push(Callback{ functor, owner });
38  }
39  }
40 
42  void operator()(TArgs... args) const {
43  for (const Callback& callback : callbacks) {
44  if (auto owner = callback.owner.lock()) {
45  callback.functor(args...);
46  }
47  }
48  }
49 
51  return callbacks.begin();
52  }
53 
55  return callbacks.end();
56  }
57 
58  Size size() const {
59  return callbacks.size();
60  }
61 
62  bool empty() const {
63  return callbacks.empty();
64  }
65 };
66 
68 template <typename... TArgs>
69 class CallbackSet<Function<void(TArgs...)>> : public CallbackSet<void(TArgs...)> {};
70 
Generic dynamically allocated resizable storage.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Generic wrappers of lambdas, functors and other callables.
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
#define NAMESPACE_SPH_END
Definition: Object.h:12
Helper class allowing to own a SharedPtr without knowing its type.
INLINE Iterator< StorageType > end() noexcept
Definition: Array.h:462
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
Definition: Array.h:306
INLINE TCounter size() const noexcept
Definition: Array.h:193
INLINE bool empty() const noexcept
Definition: Array.h:201
INLINE Iterator< StorageType > begin() noexcept
Definition: Array.h:450
void insert(const SharedToken &owner, const Function< void(TArgs...)> &functor)
Definition: CallbackSet.h:35
CallbackSet(const CallbackSet &other)
Definition: CallbackSet.h:32
Iterator< const Callback > end() const
Definition: CallbackSet.h:54
void operator()(TArgs... args) const
Calls all registered callbacks.
Definition: CallbackSet.h:42
Iterator< const Callback > begin() const
Definition: CallbackSet.h:50
Simple (forward) iterator over continuous array of objects of type T.
Definition: Iterator.h:18
Function< void(TArgs...)> functor
Definition: CallbackSet.h:21