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