NAP
apiargument.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 "apivalue.h"
9 
10 // External Includes
11 #include <rtti/rtti.h>
12 #include <nap/resource.h>
13 
14 namespace nap
15 {
22  class NAPAPI APIArgument final
23  {
24  public:
29  APIArgument(std::unique_ptr<APIBaseValue> value) : mAPIValue(std::move(value)) { }
30 
31  // Default destructor
32  ~APIArgument() = default;
33 
37  APIArgument(APIArgument&) = delete;
38  APIArgument& operator=(const APIArgument&) = delete;
39 
46  const rtti::TypeInfo getValueType() const;
47 
51  const APIBaseValue& getValue() const { return *mAPIValue; }
52 
57  template<typename T>
58  T* get();
59 
64  template<typename T>
65  const T* get() const;
66 
70  template<typename T>
71  T getCopy();
72 
76  bool isArray() const;
77 
81  template<typename T>
82  const std::vector<T>* asArray() const;
83 
87  template<typename T>
88  std::vector<T>* asArray();
89 
93  bool isString() const;
94 
98  std::string asString() const;
99 
103  bool isChar() const;
104 
108  char asChar() const;
109 
113  bool isByte() const;
114 
118  uint8_t asByte() const;
119 
123  bool isFloat() const;
124 
128  float asFloat() const;
129 
133  bool isDouble() const;
134 
138  double asDouble() const;
139 
143  bool isInt() const;
144 
148  int asInt() const;
149 
153  bool isBool() const;
154 
158  bool asBool() const;
159 
163  bool isLong() const;
164 
168  int64_t asLong() const;
169 
173  const std::string& getName() const;
174 
175  private:
176  std::unique_ptr<APIBaseValue> mAPIValue = nullptr;
177  };
178 
179 
181  // Template definitions
183 
184  template<typename T>
186  {
187  return rtti_cast<T>(mAPIValue.get());
188  }
189 
190 
191  template<typename T>
192  const T* nap::APIArgument::get() const
193  {
194  return rtti_cast<T>(mAPIValue.get());
195  }
196 
197 
198  template<typename T>
200  {
201  T* val_ptr = this->get<T>();
202  assert(val_ptr != nullptr);
203  return *val_ptr;
204  }
205 
206 
207  template<typename T>
208  const std::vector<T>* nap::APIArgument::asArray() const
209  {
210  const APIValue<std::vector<T>>* api_value = get<APIValue<std::vector<T>>>();
211  if (api_value == nullptr)
212  return nullptr;
213 
214  // Return array
215  return &api_value->mValue;
216  }
217 
218 
219  template<typename T>
220  std::vector<T>* nap::APIArgument::asArray()
221  {
222  APIValue<std::vector<T>>* api_value = get<APIValue<std::vector<T>>>();
223  if (api_value == nullptr)
224  return nullptr;
225 
226  // Return array
227  return &api_value->mValue;
228  }
229 }
nap::APIValue
Definition: apivalue.h:56
nap::APIArgument::getCopy
T getCopy()
Definition: apiargument.h:199
nap::APIArgument::APIArgument
APIArgument(std::unique_ptr< APIBaseValue > value)
Definition: apiargument.h:29
nap::APIArgument::getValue
const APIBaseValue & getValue() const
Definition: apiargument.h:51
nap
Definition: templateapp.h:17
nap::APIArgument
Definition: apiargument.h:22
nap::APIBaseValue
Definition: apivalue.h:21
nap::rtti::TypeInfo
rttr::type TypeInfo
Definition: typeinfo.h:140
nap::APIArgument::asArray
const std::vector< T > * asArray() const
Definition: apiargument.h:208
nap::APIArgument::get
T * get()
Definition: apiargument.h:185