C++ API Reference RNBO: src/RNBO_MessageEvent.h Source File

RNBO: src/RNBO_MessageEvent.h Source File

1 //
2 // RNBO_MessageEvent.h
3 //
4 //
5 
6 #ifndef _RNBO_MessageEvent_H_
7 #define _RNBO_MessageEvent_H_
8 
9 #include <memory>
10 
11 #include "RNBO_Types.h"
12 #include "RNBO_List.h"
13 #include "RNBO_PlatformInterface.h"
14 #include "RNBO_Logger.h"
15 #include "RNBO_Debug.h"
16 
17 #ifdef RNBO_NOSTDLIB
18 #define RNBO_NOMESSAGEEVENT // this is necessary until we have our own shared_ptr implementation
19 #endif
20 
21 
22 namespace RNBO {
23 
24  class PatcherEventTarget;
25 
26 #ifdef RNBO_NOMESSAGEEVENT
27 
28  class MessageEvent {
29  public:
30 
31  MillisecondTime getTime() const { return 0; }
32 
33  bool operator==(const MessageEvent& rhs) const
34  {
35  return false;
36  }
37 
38  PatcherEventTarget* getEventTarget() const { return nullptr; }
39 
40  void dumpEvent() const {}
41 
42  private:
43  friend class EventVariant;
44 
45  PatcherEventTarget* _eventTarget;
46 
47  void setTime(MillisecondTime eventTime) { }
48  };
49 #else
50 
53  class MessageEvent {
54 
55  public:
56 
57  enum Type {
58  Invalid = -1,
59  Number = 0,
60  List,
61  Bang,
62  Max_Type
63  };
64 
65 
66  MessageEvent()
67  : _tag(0)
68  , _objectId(0)
69  , _eventTime(0)
70  , _type(Invalid)
71  , _numValue(0)
72  , _eventTarget(nullptr)
73  {
74  }
75 
76  ~MessageEvent()
77  {
78  }
79 
80  MessageEvent(MessageTag tag, MillisecondTime eventTime, number numValue, MessageTag objectId = 0, PatcherEventTarget* eventTarget = nullptr)
81  : _tag(tag)
82  , _objectId(objectId)
83  , _eventTime(eventTime)
84  , _type(MessageEvent::Number)
85  , _eventTarget(eventTarget)
86  {
87  _numValue = numValue;
88  }
89 
90  MessageEvent(MessageTag tag, MillisecondTime eventTime, UniqueListPtr listValue, MessageTag objectId = 0, PatcherEventTarget* eventTarget = nullptr)
91  : _tag(tag)
92  , _objectId(objectId)
93  , _eventTime(eventTime)
94  , _type(MessageEvent::List)
95  , _listValue(std::move(listValue))
96  , _eventTarget(eventTarget)
97  {
98  }
99 
100  MessageEvent(MessageTag tag, MillisecondTime eventTime, MessageTag objectId = 0, PatcherEventTarget* eventTarget = nullptr)
101  : _tag(tag)
102  , _objectId(objectId)
103  , _eventTime(eventTime)
104  , _type(MessageEvent::Bang)
105  , _eventTarget(eventTarget)
106  {
107  }
108 
109  MessageEvent(const MessageEvent& other)
110  : _tag(other._tag)
111  , _objectId(other._objectId)
112  , _eventTime(other._eventTime)
113  , _eventTarget(other._eventTarget)
114  {
115  if (other._type == List) {
116  _listValue = other._listValue;
117  }
118  else if (other._type == Number){
119  _numValue = other._numValue;
120  }
121 
122  _type = other._type;
123  }
124 
125  MessageEvent(MessageEvent&& other)
126  : _tag(std::move(other._tag))
127  , _objectId(std::move(other._objectId))
128  , _eventTime(std::move(other._eventTime))
129  , _numValue(std::move(other._numValue))
130  , _listValue(std::move(other._listValue))
131  , _eventTarget(std::move(other._eventTarget))
132  {
133  _type = other._type;
134  other._type = Invalid;
135  }
136 
137 
138  MessageEvent& operator = (const MessageEvent& other)
139  {
140  _tag = other._tag;
141  _eventTime = other._eventTime;
142  _eventTarget = other._eventTarget;
143  _objectId = other._objectId;
144 
145  if (other._type == List) {
146  _listValue = other._listValue;
147  }
148  else if (other._type == Number) {
149  _numValue = other._numValue;
150  }
151 
152  _type = other._type;
153 
154  return *this;
155  }
156 
157  MessageEvent& operator=(MessageEvent&& other)
158  {
159  _tag = std::move(other._tag);
160  _objectId = std::move(other._objectId);
161  _eventTime = std::move(other._eventTime);
162  _eventTarget = std::move(other._eventTarget);
163  _numValue = std::move(other._numValue);
164  _listValue = std::move(other._listValue);
165 
166  _type = other._type;
167  other._type = Invalid;
168 
169  return *this;
170  }
171 
172  bool operator==(const MessageEvent& rhs) const
173  {
174  return rhs.getTag() == getTag()
175  && rhs.getObjectId() == getObjectId()
176  && rhs.getTime() == getTime()
177  && rhs.getType() == getType()
178  && ((getType() == Bang
179  || getType() == List ? rhs.getListValue() == getListValue() : rhs.getNumValue() == getNumValue()))
180  && rhs._eventTarget == _eventTarget;
181  }
182 
183  MessageTag getTag() const { return _tag; }
184  MessageTag getObjectId() const { return _objectId; }
185  MessageEvent::Type getType() const { return _type; }
186 
187  MillisecondTime getTime() const { return _eventTime; }
188  PatcherEventTarget* getEventTarget() const { return _eventTarget; }
189 
190  number getNumValue() const {
191  RNBO_ASSERT(_type == Number);
192  return _numValue;
193  }
194 
195  std::shared_ptr<const list> getListValue() const {
196  RNBO_ASSERT(_type == List);
197  return _listValue;
198  }
199 
200  // debugging
201  void dumpEvent() const {
202  // disabling for now to avoid requiring fprintf support in generated code
203 // if (_type == List) { }
204 // else {
205 // fprintf(stdout, "MessageEvent: _tag=%d time=%.3f value=%.4f", _tag, _eventTime, _numValue);
206 // }
207  }
208 
209  protected:
210 
211  MessageTag _tag; // the tag is a hashed string value
212  MessageTag _objectId; // the tag is a hashed string value
213  MillisecondTime _eventTime;
214  MessageEvent::Type _type = Invalid;
215 
216  number _numValue;
217  std::shared_ptr<list> _listValue;
218 
219  friend class EventVariant;
220  friend class PatcherEventSender;
221 
222  PatcherEventTarget* _eventTarget;
223 
224  void setTime(MillisecondTime eventTime) { _eventTime = eventTime; }
225  };
226 
227 #endif // RNBO_NOMESSAGEEVENT
228 
229 } // namespace RNBO
230 
231 
232 #endif // #ifndef _RNBO_MessageEvent_H_