NAP
oscevent.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 "oscargument.h"
9 
10 // External Includes
11 #include <nap/event.h>
12 #include <nap/numeric.h>
13 #include <utility/uniqueptrvectoriterator.h>
14 
15 namespace nap
16 {
17  using OSCArgumentList = std::vector<std::unique_ptr<OSCArgument>>;
18 
25  class NAPAPI OSCEvent : public Event
26  {
27  RTTI_ENABLE(Event)
28  public:
30 
31  OSCEvent() = delete;
32 
37  OSCEvent(const std::string& address);
38 
43  OSCEvent(const std::string&& address);
44 
48  const std::string& getAddress() const { return mAddress; }
49 
60  template<typename T, typename... Args>
61  OSCArgument* addArgument(Args&&... args);
62 
74  template<typename T, typename... Args>
75  OSCArgument* addValue(Args&&... args);
76 
83  OSCArgument* addString(const std::string& string);
84 
88  int getCount() const { return static_cast<int>(mArguments.size()); }
89 
93  const ArgumentConstIterator getArguments() const { return ArgumentConstIterator(mArguments); }
94 
99  const OSCArgument* getArgument(int index) const;
100 
105  OSCArgument* getArgument(int index);
106 
111  std::size_t getSize() const;
112 
117  OSCArgument& operator[](std::size_t idx) { return *getArgument(static_cast<int>(idx)); }
118 
123  const OSCArgument& operator[](std::size_t idx) const { return *getArgument(static_cast<int>(idx)); }
124 
125  private:
126  OSCArgumentList mArguments; // All the arguments associated with the event
127  std::string mAddress; // The osc event address
128  };
129 
131  // Template definitions
133 
134  template<typename T, typename... Args>
136  {
137  assert(RTTI_OF(T).is_derived_from(RTTI_OF(nap::OSCBaseValue)));
138 
139  // Create value
140  std::unique_ptr<T> value = std::make_unique<T>(std::forward<Args>(args)...);
141 
142  // Create argument and move value
143  std::unique_ptr<OSCArgument> argument = std::make_unique<OSCArgument>(std::move(value));
144 
145  mArguments.emplace_back(std::move(argument));
146  return mArguments.back().get();
147  }
148 
149 
150  template<typename T, typename... Args>
152  {
153  return addArgument<nap::OSCValue<T>>(std::forward<Args>(args)...);
154  }
155 
156  using OSCEventPtr = std::unique_ptr<nap::OSCEvent>;
157 }
nap::OSCEvent
Definition: oscevent.h:25
nap::utility::UniquePtrConstVectorWrapper
Definition: uniqueptrvectoriterator.h:70
nap::OSCEvent::addValue
OSCArgument * addValue(Args &&... args)
Definition: oscevent.h:151
nap::OSCBaseValue
Definition: oscargument.h:153
nap::OSCArgumentList
std::vector< std::unique_ptr< OSCArgument > > OSCArgumentList
Definition: oscevent.h:17
nap::Event
Definition: event.h:17
nap::OSCEvent::getArguments
const ArgumentConstIterator getArguments() const
Definition: oscevent.h:93
nap::OSCArgument
Definition: oscargument.h:25
nap::OSCEvent::getCount
int getCount() const
Definition: oscevent.h:88
nap
Definition: templateapp.h:17
nap::OSCEvent::getAddress
const std::string & getAddress() const
Definition: oscevent.h:48
nap::OSCEvent::addArgument
OSCArgument * addArgument(Args &&... args)
Definition: oscevent.h:135
nap::OSCEvent::operator[]
OSCArgument & operator[](std::size_t idx)
Definition: oscevent.h:117
nap::OSCEventPtr
std::unique_ptr< nap::OSCEvent > OSCEventPtr
Definition: oscevent.h:156
nap::OSCEvent::operator[]
const OSCArgument & operator[](std::size_t idx) const
Definition: oscevent.h:123