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

RNBO: src/RNBO_ExternalData.h Source File

Go to the documentation of this file.
1 #ifndef _RNBO_ExternalData_h_
2 #define _RNBO_ExternalData_h_
3 
4 #include <memory>
5 #include <string>
6 #include <functional>
7 
8 #include "RNBO_Types.h"
9 #include "RNBO_String.h"
10 #include "RNBO_DataRef.h"
11 
17 namespace RNBO {
18 
23  using ExternalDataId = const char*;
24 
29  using ExternalDataIndex = int;
30 
35  using ReleaseCallback = std::function<void(ExternalDataId, char*)>;
36 
41  public:
42  ExternalDataRef(DataRefIndex index, DataRef *ref)
43  : _index(index)
44  , _dataRef(ref)
45  , _name(ref->getName())
46  {
47  if (ref->getFile() == nullptr) _file.clear();
48  else _file = ref->getFile();
49  if (ref->getTag() == nullptr) _tag.clear();
50  else _tag = ref->getTag();
51  _data = _dataRef->getData();
52  }
53 
54  const char* getMemoryId() const {
55  return _name.c_str();
56  }
57 
58  const char* getFile() const {
59  return _file.c_str();
60  }
61 
62  const char *getTag() const {
63  return _tag.c_str();
64  }
65 
66  DataRefIndex getInternalIndex() {
67  return _index;
68  }
69 
70  char* getData() const {
71  if (_dataRef) return _dataRef->getData();
72  else return _data;
73  }
74 
75  bool isValid() const {
76  return _dataRef != nullptr;
77  }
78 
79  void invalidate() {
80  _dataRef = nullptr;
81  }
82 
83  void revalidate(DataRefIndex index, DataRef *ref) {
84  RNBO_ASSERT(!isValid());
85  Platform::assertTrue(ref, "ref must be non null");
86 
87  if (_callback && _data && _data != ref->getData()) {
88  _callback(getMemoryId(), _data);
89  }
90 
91  _index = index;
92  _dataRef = ref;
93  if (ref->getFile() == nullptr) _file.clear();
94  else _file = ref->getFile();
95  if (ref->getTag() == nullptr) _tag.clear();
96  else _tag = ref->getTag();
97  }
98 
99  void updateDataRef(char* data, size_t sizeInBytes) {
100  Platform::assertTrue(_dataRef, "_dataRef must be non null");
101 
102  if (_callback && _dataRef->getData() && data != _dataRef->getData()) {
103  _callback(getMemoryId(), _dataRef->getData());
104  }
105 
106  _dataRef->setData(data, sizeInBytes);
107  _data = data;
108  }
109 
110  void updateDataRef(char* data, size_t sizeInBytes, DataType type) {
111  Platform::assertTrue(_dataRef, "_dataRef must be non null");
112 
113  updateDataRef(data, sizeInBytes);
114  _dataRef->setType(type);
115  }
116 
117  void updateDataRef(char* data, size_t sizeInBytes, ReleaseCallback callback) {
118  Platform::assertTrue(_dataRef, "_dataRef must be non null");
119 
120  updateDataRef(data, sizeInBytes);
121  _callback = callback;
122  }
123 
124  void updateDataRef(char* data, size_t sizeInBytes, DataType type, ReleaseCallback callback) {
125  updateDataRef(data, sizeInBytes, type);
126  _callback = callback;
127  }
128 
129  void setTouched(bool value) {
130  if (_dataRef) _dataRef->setTouched(value);
131  }
132 
133  bool getTouched() const {
134  if (_dataRef) return _dataRef->getTouched();
135  else return false;
136  }
137 
138  const DataType getType() const {
139  if (_dataRef) return _dataRef->getType();
140  else return UntypedDataBuffer();
141  }
142 
143  size_t getSizeInBytes() const {
144  if (_dataRef) return _dataRef->getSizeInBytes();
145  else return 0;
146  }
147 
148  ReleaseCallback getCallback() {
149  return _callback;
150  }
151 
152  private:
153  DataRefIndex _index = -1;
154  DataRef* _dataRef = nullptr;
155  std::string _name; // make a copy of the name to avoid accessing
156  // undefined const chars when the old patcher goes away
157  std::string _file;
158  std::string _tag;
159  char* _data = nullptr; // safe away the data pointer so we can hand it out for freeing
160  ReleaseCallback _callback = nullptr;
161  };
162 
166  class ExternalDataEvent
167  {
168  public:
169 
170  enum class EventAction {
171  Undefined,
172  SetExternalData,
173  ReleaseExternalData
174  };
175 
176  ExternalDataEvent()
177  : _data(nullptr)
178  , _sizeInBytes(0)
179  , _action(EventAction::Undefined)
180  , _callback(nullptr)
181  {}
182 
183  ExternalDataEvent(const ExternalDataEvent& other) = default;
184  ExternalDataEvent& operator = (const ExternalDataEvent& other) = default;
185 
186  ExternalDataEvent(String memoryId, char* data, size_t sizeInBytes, DataType type, EventAction action, ReleaseCallback callback)
187  : _memoryId(std::make_shared<String>(memoryId))
188  , _data(data)
189  , _sizeInBytes(sizeInBytes)
190  , _type(type)
191  , _action(action)
192  , _callback(callback)
193  {
194  }
195 
196  const String& getMemoryId() const { return *_memoryId; }
197  char *getData() const { return _data; }
198  size_t getSizeInBytes() const { return _sizeInBytes; }
199  DataType getType() const { return _type; }
200  EventAction getAction() const { return _action; }
201  ReleaseCallback getCallback() const { return _callback; }
202 
203  private:
204  std::shared_ptr<String> _memoryId;
205  char* _data;
206  size_t _sizeInBytes;
207  DataType _type;
208 
209  EventAction _action;
210  ReleaseCallback _callback;
211  };
212 
213 
214  using UpdateRefCallback = const std::function<void(DataRefIndex, char*, size_t, DataType)>;
215  using ReleaseRefCallback = const std::function<void(DataRefIndex)>;
216  using ConstRefList = const ExternalDataRef* const*;
217 
222  {
223  public:
224  virtual ~ExternalDataHandler() {}
225 
226  virtual void processBeginCallback(DataRefIndex numRefs, ConstRefList refList, UpdateRefCallback updateDataRef, ReleaseRefCallback releaseDataRef) = 0;
227  virtual void processEndCallback(DataRefIndex numRefs, ConstRefList refList) = 0;
228  };
229 
235  const char* file;
236  const char* tag;
237  };
238 
239  constexpr const char *InValidExternalDataId = "";
240 }
241 
242 #endif // _RNBO_ExternalData_h_