siplasplas
A library for C++ reflection and introspection
sink.hpp
1 #ifndef SIPLASPLAS_SIGNALS_SINK_HPP
2 #define SIPLASPLAS_SIGNALS_SINK_HPP
3 
4 #include <siplasplas/reflection/dynamic/object_manip.hpp>
5 #include <siplasplas/signals/export.hpp>
6 #include <type_traits>
7 
8 namespace cpp
9 {
10 
11 class SignalEmitter;
12 
39 class SIPLASPLAS_SIGNALS_EXPORT SignalSink
40 {
41 public:
42 
52  template<typename Caller, typename Callee>
53  SignalSink(Caller& caller, Callee& callee) :
54  _caller{&caller},
55  _callee{&callee}
56  {}
57 
68  template<typename Caller>
69  SignalSink(Caller& caller) :
70  _caller{&caller},
71  _callee{}
72  {}
73 
74  virtual ~SignalSink() = default;
75 
85  template<typename... Args>
86  void operator()(Args&&... args)
87  {
88  if(invokeWithoutCallee())
89  {
90  invoke(
91  cpp::dynamic_reflection::pack_to_vector(
92  std::forward<Args>(args)...
93  )
94  );
95  }
96  else
97  {
98  invoke(
99  cpp::dynamic_reflection::pack_to_vector(
100  _callee, std::forward<Args>(args)...
101  )
102  );
103  }
104  }
105 
113  {
114  return _callee.get<SignalEmitter*>();
115  }
116 
123  {
124  return _caller.get<SignalEmitter*>();
125  }
126 
127  virtual bool pull() = 0;
128 
129 protected:
130  virtual void invoke(const std::vector<cpp::dynamic_reflection::Object>& args) = 0;
131  virtual bool invokeWithoutCallee() const = 0;
132 
133 private:
134  cpp::dynamic_reflection::Object _caller, _callee;
135 };
136 
137 }
138 
139 #endif // SIPLASPLAS_SIGNALS_SINK_HPP
SignalEmitter * callee() const
Gives the callee of the connection.
Definition: sink.hpp:112
void operator()(Args &&...args)
Invokes the sink with the given arguments.
Definition: sink.hpp:86
Class that can send and receive signals from other emitters.
Definition: emitter.hpp:66
SignalSink(Caller &caller, Callee &callee)
Constructs a sink given the connection caller and the callee.
Definition: sink.hpp:53
Definition: canary_allocator.hpp:7
SignalEmitter * caller() const
Gives the caller of the connection.
Definition: sink.hpp:122
Interface to the signals sink API.
Definition: sink.hpp:39
SignalSink(Caller &caller)
Constructs a sink given the connection caller object.
Definition: sink.hpp:69