NAP
apievent.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Local Includes
8 #include "apiargument.h"
9 #include "apisignature.h"
10 
11 // External Includes
12 #include <nap/event.h>
13 #include <utility/uniqueptrvectoriterator.h>
14 #include <mathutils.h>
15 
16 namespace nap
17 {
18  using APIArgumentList = std::vector<std::unique_ptr<APIArgument>>;
19 
37  class NAPAPI APIEvent : public Event
38  {
39  RTTI_ENABLE(Event)
40  public:
42 
47  APIEvent(const std::string& name);
48 
53  APIEvent(std::string&& name);
54 
62  APIEvent(const std::string& name, const std::string& id);
63 
71  APIEvent(std::string&& name, std::string&& id);
72 
76  const std::string& getName() const { return mName; }
77 
81  const std::string& getID() const { return mID; }
82 
90  template<typename T, typename... Args>
91  APIArgument* addArgument(const std::string& name, Args&&... args);
92 
98  APIArgument* addArgument(std::unique_ptr<APIBaseValue> value);
99 
103  int getCount() const { return static_cast<int>(mArguments.size()); }
104 
108  const ArgumentConstIterator getArguments() const { return ArgumentConstIterator(mArguments); }
109 
114  const APIArgument* getArgument(int index) const;
115 
120  APIArgument* getArgument(int index);
121 
126  const APIArgument* getArgumentByName(std::string&& name) const;
127 
132  APIArgument* getArgumentByName(std::string&& name);
133 
139  bool matches(const nap::APISignature& signature) const;
140 
148  template<typename T>
149  const T& to() const;
150 
158  template<typename T>
159  T& to();
160 
165  APIArgument& operator[](std::size_t idx) { return *getArgument(static_cast<int>(idx)); }
166 
171  const APIArgument& operator[](std::size_t idx) const { return *getArgument(static_cast<int>(idx)); }
172 
173  private:
174  std::string mName;
175  APIArgumentList mArguments;
176  std::string mID;
177  };
178 
179  using APIEventPtr = std::unique_ptr<nap::APIEvent>;
180 
181 
183  // Template definitions
185 
186  template<typename T, typename... Args>
187  APIArgument* nap::APIEvent::addArgument(const std::string& name, Args&&... args)
188  {
189  assert(RTTI_OF(T).is_derived_from(RTTI_OF(nap::APIBaseValue)));
190 
191  // Create value
192  std::unique_ptr<T> value = std::make_unique<T>(name, std::forward<Args>(args)...);
193 
194  // Assign unique id
195  value->mID = math::generateUUID();
196 
197  // Create argument and move value
198  std::unique_ptr<APIArgument> argument = std::make_unique<APIArgument>(std::move(value));
199 
200  mArguments.emplace_back(std::move(argument));
201  return mArguments.back().get();
202  }
203 
204 
205  template<typename T>
206  const T& nap::APIEvent::to() const
207  {
208  const T* return_p = nullptr;
209  if (this->get_type().is_derived_from<T>())
210  return_p = reinterpret_cast<const T*>(this);
211  assert(return_p != nullptr);
212  return *return_p;
213  }
214 
215 
216  template<typename T>
218  {
219  T* cast_event = rtti_cast<T>(this);
220  assert(cast_event != nullptr);
221  return *cast_event;
222  }
223 
224 }
nap::utility::UniquePtrConstVectorWrapper
Definition: uniqueptrvectoriterator.h:70
nap::APISignature
Definition: apisignature.h:20
nap::math::generateUUID
std::string NAPAPI generateUUID()
nap::APIEvent::getName
const std::string & getName() const
Definition: apievent.h:76
nap::APIEvent::getArguments
const ArgumentConstIterator getArguments() const
Definition: apievent.h:108
nap::APIArgumentList
std::vector< std::unique_ptr< APIArgument > > APIArgumentList
Definition: apievent.h:18
nap::APIEvent::getCount
int getCount() const
Definition: apievent.h:103
nap::Event
Definition: event.h:17
nap::APIEvent::getID
const std::string & getID() const
Definition: apievent.h:81
nap::APIEventPtr
std::unique_ptr< nap::APIEvent > APIEventPtr
Definition: apievent.h:179
nap::APIEvent::operator[]
APIArgument & operator[](std::size_t idx)
Definition: apievent.h:165
nap
Definition: templateapp.h:17
nap::APIEvent::addArgument
APIArgument * addArgument(const std::string &name, Args &&... args)
Definition: apievent.h:187
nap::APIArgument
Definition: apiargument.h:22
nap::APIBaseValue
Definition: apivalue.h:21
nap::APIEvent::to
const T & to() const
Definition: apievent.h:206
nap::APIEvent::operator[]
const APIArgument & operator[](std::size_t idx) const
Definition: apievent.h:171
nap::APIEvent
Definition: apievent.h:37