JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
ParameterPack.h
Go to the documentation of this file.
1 #ifndef JODA_PARAMETERPACK_H
2 #define JODA_PARAMETERPACK_H
3 #include <cstdint>
4 
5 #include "ValueAccepter.h"
6 namespace joda::query::values {
7 
9  public:
11 };
12 
17 template <std::size_t k>
18 struct NoParameter {
19  static constexpr void check(
20  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
21  const std::string &name) {
22  if (k < parameters.size())
23  throw WrongParameterCountException(parameters.size(), k, name);
24  return;
25  }
26 };
27 
34 template <std::size_t k, class T>
36  typedef std::optional<typename T::ReturnT> ReturnT;
37 
38  static void check(
39  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
40  const std::string &name) {
41  if (k >= parameters.size()) return;
42  return T::check(parameters, name);
43  }
44 
46  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
47  const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) {
48  if (k >= parameters.size()) return {};
49  return std::make_optional(T::extractValue(parameters));
50  }
51 };
52 
59 template <std::size_t k, class T, class Default>
61  typedef typename T::ReturnT ReturnT;
62 
63  static void check(
64  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
65  const std::string &name) {
66  if (k >= parameters.size()) return;
67  return T::check(parameters, name);
68  }
69 
71  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
72  const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) {
73  if (k >= parameters.size()) return Default::value;
74  return T::extractValue(parameters, json, alloc);
75  }
76 };
77 
78 struct TrueDefault {
79  static constexpr bool value = true;
80 };
81 
82 struct FalseDefault {
83  static constexpr bool value = false;
84 };
85 
93 template <std::size_t k, class T1, class T2, class Default>
94 struct OrParameter {
95  typedef typename std::variant<typename T1::ReturnT, typename T2::ReturnT>
97 
98  static void check(
99  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
100  const std::string &name) {
101  try { // Catch first wrong parameter exception
102  T1::check(parameters, name);
103  } catch (WrongParameterTypeException w) {
104  T2::check(parameters, name); // Do not catch second
105  }
106  }
107 
109  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
110  const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) {
111  try { // Catch first wrong parameter exception
112  return T1::extractValue(parameters, json, alloc);
113  } catch (IncompatibleTypeException e) {
114  T2::extractValue(parameters, json, alloc); // Do not catch second
115  }
116  }
117 };
118 
124 template <std::size_t k>
126  typedef std::string ReturnT;
127 
128  static void check(
129  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
130  const std::string &name) {
131  if (k >= parameters.size()) throw MissingParameterException(k, name);
132  if (!(parameters[k]->isString() || parameters[k]->isAny()))
134  return;
135  }
136 
138  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
139  const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) {
140  auto &val = parameters[k];
141  if (!val->isAtom()) {
142  auto *retPtr = val->getValue(json, alloc);
143  if (retPtr != nullptr && retPtr->IsString()) {
144  return retPtr->GetString();
145  }
146  } else if (val->isString()) {
147  auto retVal = val->getAtomValue(json, alloc);
148  if (retVal.IsString()) {
149  return retVal.GetString();
150  }
151  }
153  }
154 };
155 
161 template <std::size_t k>
163  typedef bool ReturnT;
164 
165  static void check(
166  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
167  const std::string &name) {
168  if (k >= parameters.size()) throw MissingParameterException(k, name);
169  if (!(parameters[k]->isBool() || parameters[k]->isAny()))
171  return;
172  }
173 
175  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
176  const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) {
177  auto &val = parameters[k];
178  if (!val->isAtom()) {
179  auto *retPtr = val->getValue(json, alloc);
180  if (retPtr != nullptr && retPtr->IsBool()) {
181  return retPtr->GetBool();
182  }
183  } else if (val->isBool()) {
184  auto retVal = val->getAtomValue(json, alloc);
185  if (retVal.IsBool()) {
186  return retVal.GetBool();
187  }
188  }
190  }
191 };
192 
198 template <std::size_t k>
201 
202  static void check(
203  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
204  const std::string &name) {
205  if (k >= parameters.size()) throw MissingParameterException(k, name);
206  if (!(parameters[k]->isObject() || parameters[k]->isAny()))
208  return;
209  }
210 
212  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
213  const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) {
214  auto &param = parameters[k];
215  return ValueAccepter(param);
216  }
217 
218  template <class Handler>
219  static bool accept(
220  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
221  Handler &handler, const RapidJsonDocument &json,
222  RJMemoryPoolAlloc &alloc) {
223  auto &param = parameters[k];
224  ValueAccepter accepter(param);
225  if (param->isAtom()) {
226  auto v = param->getAtomValue(json, alloc);
227  return v.Accept(handler);
228  }
229  return accepter.Accept(param, json, alloc, handler);
230  }
231 };
232 
238 template <std::size_t k>
239 struct AnyParameter {
241 
242  static void check(
243  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
244  const std::string &name) {
245  if (k >= parameters.size()) throw MissingParameterException(k, name);
246  return;
247  }
248 
250  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
251  const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) {
252  auto &param = parameters[k];
253  return ValueAccepter(param);
254  }
255 
256  template <class Handler>
257  static bool accept(
258  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
259  Handler &handler, const RapidJsonDocument &json,
260  RJMemoryPoolAlloc &alloc) {
261  auto &param = parameters[k];
262  ValueAccepter accepter(param);
263  if (param->isAtom()) {
264  auto v = param->getAtomValue(json, alloc);
265  return v.Accept(handler);
266  }
267  return accepter.Accept(param, json, alloc, handler);
268  }
269 };
270 
271 } // namespace joda::query::values
272 
273 #endif // JODA_PARAMETERPACK_H
rapidjson::MemoryPoolAllocator< RJBaseAlloc > RJMemoryPoolAlloc
Definition: RJFwd.h:26
Definition: RapidJsonDocument.h:22
Definition: IValueProvider.h:117
Definition: ValueAccepter.h:16
bool Accept(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc, Handler &h) const
Definition: ValueAccepter.h:32
Definition: IValueProvider.h:96
Definition: IValueProvider.h:46
Definition: IValueProvider.h:60
IncompatibleTypeException()
Definition: ParameterPack.h:10
Definition: ParameterPack.h:6
@ IV_Object
Definition: IValueProvider.h:37
@ IV_Bool
Definition: IValueProvider.h:36
@ IV_String
Definition: IValueProvider.h:34
Definition: ParameterPack.h:239
ValueAccepter ReturnT
Definition: ParameterPack.h:240
static bool accept(const std::vector< std::unique_ptr< IValueProvider >> &parameters, Handler &handler, const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc)
Definition: ParameterPack.h:257
static ReturnT extractValue(const std::vector< std::unique_ptr< IValueProvider >> &parameters, const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc)
Definition: ParameterPack.h:249
static void check(const std::vector< std::unique_ptr< IValueProvider >> &parameters, const std::string &name)
Definition: ParameterPack.h:242
Definition: ParameterPack.h:162
bool ReturnT
Definition: ParameterPack.h:163
static ReturnT extractValue(const std::vector< std::unique_ptr< IValueProvider >> &parameters, const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc)
Definition: ParameterPack.h:174
static void check(const std::vector< std::unique_ptr< IValueProvider >> &parameters, const std::string &name)
Definition: ParameterPack.h:165
Definition: ParameterPack.h:82
static constexpr bool value
Definition: ParameterPack.h:83
Definition: ParameterPack.h:18
static constexpr void check(const std::vector< std::unique_ptr< IValueProvider >> &parameters, const std::string &name)
Definition: ParameterPack.h:19
Definition: ParameterPack.h:199
static void check(const std::vector< std::unique_ptr< IValueProvider >> &parameters, const std::string &name)
Definition: ParameterPack.h:202
ValueAccepter ReturnT
Definition: ParameterPack.h:200
static ReturnT extractValue(const std::vector< std::unique_ptr< IValueProvider >> &parameters, const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc)
Definition: ParameterPack.h:211
static bool accept(const std::vector< std::unique_ptr< IValueProvider >> &parameters, Handler &handler, const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc)
Definition: ParameterPack.h:219
Definition: ParameterPack.h:35
static void check(const std::vector< std::unique_ptr< IValueProvider >> &parameters, const std::string &name)
Definition: ParameterPack.h:38
std::optional< typename T::ReturnT > ReturnT
Definition: ParameterPack.h:36
static ReturnT extractValue(const std::vector< std::unique_ptr< IValueProvider >> &parameters, const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc)
Definition: ParameterPack.h:45
static void check(const std::vector< std::unique_ptr< IValueProvider >> &parameters, const std::string &name)
Definition: ParameterPack.h:63
T::ReturnT ReturnT
Definition: ParameterPack.h:61
static ReturnT extractValue(const std::vector< std::unique_ptr< IValueProvider >> &parameters, const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc)
Definition: ParameterPack.h:70
Definition: ParameterPack.h:94
std::variant< typename T1::ReturnT, typename T2::ReturnT > ReturnT
Definition: ParameterPack.h:96
static void check(const std::vector< std::unique_ptr< IValueProvider >> &parameters, const std::string &name)
Definition: ParameterPack.h:98
static ReturnT extractValue(const std::vector< std::unique_ptr< IValueProvider >> &parameters, const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc)
Definition: ParameterPack.h:108
Definition: ParameterPack.h:125
static void check(const std::vector< std::unique_ptr< IValueProvider >> &parameters, const std::string &name)
Definition: ParameterPack.h:128
static ReturnT extractValue(const std::vector< std::unique_ptr< IValueProvider >> &parameters, const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc)
Definition: ParameterPack.h:137
std::string ReturnT
Definition: ParameterPack.h:126
Definition: ParameterPack.h:78
static constexpr bool value
Definition: ParameterPack.h:79