JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
CastProvider.h
Go to the documentation of this file.
1 
2 #ifndef JODA_CASTPROVIDER_H
3 #define JODA_CASTPROVIDER_H
4 
5 #include "IValueProvider.h"
6 #include "ParameterPack.h"
7 #include "TemplateProvider.h"
8 #include <joda/misc/RJFwd.h>
9 #include <rapidjson/stringbuffer.h>
10 #include <rapidjson/writer.h>
11 
12 
13 
14  // _____ _ _ _______
15  // |_ _| \ | |__ __|
16  // | | | \| | | |
17  // | | | . ` | | |
18  // _| |_| |\ | | |
19  // |_____|_| \_| |_|
20 
21 
22 
23 
25 
27  public:
28  typedef char Ch;
29 
30  bool Null() { return false; }
31 
32  bool Bool(bool b) {
33  if (b)
34  val = RJValue(1);
35  else
36  val = RJValue(0);
37  return false;
38  }
39 
40  bool Int(int i) {
41  val = RJValue(i);
42  return false;
43  }
44 
45  bool Uint(unsigned i) {
46  val = RJValue(i);
47  return false;
48  }
49 
50  bool Int64(int64_t i) {
51  val = RJValue(i);
52  return false;
53  }
54 
55  bool Uint64(uint64_t i) {
56  val = RJValue(i);
57  return false;
58  }
59 
60  bool Double(double d) {
61  val = RJValue((int64_t)d);
62  return false;
63  }
64 
65  bool RawNumber(const Ch *str, rapidjson::SizeType length, bool copy) {
66  return false;
67  }
68 
69  bool String(const Ch *str, rapidjson::SizeType length, bool copy) {
70  try {
71  val = RJValue((int64_t)std::stoll({str, length}));
72  } catch (const std::exception& e) {
73  val = RJValue();
74  }
75 
76  return false;
77  }
78 
79  bool StartObject() { return false; }
80 
81  bool Key(const Ch *str, rapidjson::SizeType length, bool copy) {
82  return false;
83  }
84 
85  bool EndObject(rapidjson::SizeType memberCount) { return false; }
86 
87  constexpr bool StartArray() { return false; }
88 
89  constexpr bool EndArray(rapidjson::SizeType elementCount) { return false; }
90  RJValue getValue() { return std::move(val); }
91 
92  private:
93  RJValue val;
94 };
95 
97  public:
98  // Parameters
104 
105  // State
106  typedef bool State;
107 
108  // Return value of the function
109  static constexpr IValueType retType = IV_Number;
110 
111  static constexpr auto name = "INT";
117  static void checkParameters(
118  State &state,
119  const std::vector<std::unique_ptr<IValueProvider>> &parameters){
120 
121  };
122 
124  const State &state,
125  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
126  const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) {
127  auto accepter = P0::extractValue(parameters, json, alloc);
128  IntCastHandler handler;
129  accepter.Accept(json, alloc, handler);
130  return handler.getValue();
131  };
132 };
133 } // namespace joda::query::providers::cast
134 
135 namespace joda::query {
136 template class TemplateProvider<
138 
141 } // namespace joda::query
142 
143 
144 
145 
146  // ______ _ ____ _______
147  // | ____| | / __ \ /\|__ __|
148  // | |__ | | | | | | / \ | |
149  // | __| | | | | | |/ /\ \ | |
150  // | | | |___| |__| / ____ \| |
151  // |_| |______\____/_/ \_\_|
152 
153 
154 
155 
156 
159  public:
160  typedef char Ch;
161 
162  bool Null() { return false; }
163 
164  bool Bool(bool b) {
165  if (b)
166  val = RJValue(1.0);
167  else
168  val = RJValue(0.0);
169  return false;
170  }
171 
172  bool Int(int i) {
173  val = RJValue((double)i);
174  return false;
175  }
176 
177  bool Uint(unsigned i) {
178  val = RJValue((double)i);
179  return false;
180  }
181 
182  bool Int64(int64_t i) {
183  val = RJValue((double)i);
184  return false;
185  }
186 
187  bool Uint64(uint64_t i) {
188  val = RJValue((double)i);
189  return false;
190  }
191 
192  bool Double(double d) {
193  val = RJValue(d);
194  return false;
195  }
196 
197  bool RawNumber(const Ch *str, rapidjson::SizeType length, bool copy) {
198  return false;
199  }
200 
201  bool String(const Ch *str, rapidjson::SizeType length, bool copy) {
202  try {
203  val = RJValue(std::stod({str, length}));
204  } catch (const std::exception& e) {
205  val = RJValue();
206  }
207 
208  return false;
209  }
210 
211  bool StartObject() { return false; }
212 
213  bool Key(const Ch *str, rapidjson::SizeType length, bool copy) {
214  return false;
215  }
216 
217  bool EndObject(rapidjson::SizeType memberCount) { return false; }
218 
219  constexpr bool StartArray() { return false; }
220 
221  constexpr bool EndArray(rapidjson::SizeType elementCount) { return false; }
222  RJValue getValue() { return std::move(val); }
223 
224  private:
225  RJValue val;
226 };
227 
229  public:
230  // Parameters
236 
237  // State
238  typedef bool State;
239 
240  // Return value of the function
241  static constexpr IValueType retType = IV_Number;
242 
243  static constexpr auto name = "FLOAT";
249  static void checkParameters(
250  State &state,
251  const std::vector<std::unique_ptr<IValueProvider>> &parameters){
252 
253  };
254 
256  const State &state,
257  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
258  const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) {
259  auto accepter = P0::extractValue(parameters, json, alloc);
260  FloatCastHandler handler;
261  accepter.Accept(json, alloc, handler);
262  return handler.getValue();
263  };
264 };
265 
266 } // namespace joda::query::providers::cast
267 
268 namespace joda::query {
269 template class TemplateProvider<
271 
274 } // namespace joda::query
275 
276 
277 
278  // _____ _______ _____ _____ _ _ _____
279  // / ____|__ __| __ \|_ _| \ | |/ ____|
280  // | (___ | | | |__) | | | | \| | | __
281  // \___ \ | | | _ / | | | . ` | | |_ |
282  // ____) | | | | | \ \ _| |_| |\ | |__| |
283  // |_____/ |_| |_| \_\_____|_| \_|\_____|
284 
285 
286 
287 
288 
289 
292  public:
293  StringCastHandler(RJMemoryPoolAlloc* alloc): alloc(alloc){}
294 
295  typedef char Ch;
296 
297  bool Null() {
298  val = RJValue("null",*alloc);
299  return false;
300  }
301 
302  bool Bool(bool b) {
303  if (b)
304  val = RJValue("true",*alloc);
305  else
306  val = RJValue("false",*alloc);
307  return false;
308  }
309 
310  bool Int(int i) {
311  val = RJValue(std::to_string(i),*alloc);
312  return false;
313  }
314 
315  bool Uint(unsigned i) {
316  val = RJValue(std::to_string(i),*alloc);
317  return false;
318  }
319 
320  bool Int64(int64_t i) {
321  val = RJValue(std::to_string(i),*alloc);
322  return false;
323  }
324 
325  bool Uint64(uint64_t i) {
326  val = RJValue(std::to_string(i),*alloc);
327  return false;
328  }
329 
330  bool Double(double d) {
331  val = RJValue(std::to_string(d),*alloc);
332  return false;
333  }
334 
335  bool RawNumber(const Ch *str, rapidjson::SizeType length, bool copy) {
336  val = RJValue(str,*alloc);
337  return false;
338  }
339 
340  bool String(const Ch *str, rapidjson::SizeType length, bool copy) {
341  val = RJValue(str,*alloc);
342  return false;
343  }
344 
345  bool StartObject() { return false; }
346 
347  bool Key(const Ch *str, rapidjson::SizeType length, bool copy) {
348  return false;
349  }
350 
351  bool EndObject(rapidjson::SizeType memberCount) { return false; }
352 
353  constexpr bool StartArray() { return false; }
354 
355  constexpr bool EndArray(rapidjson::SizeType elementCount) { return false; }
356  RJValue getValue() { return std::move(val); }
357 
358  private:
359  RJMemoryPoolAlloc* alloc;
360  RJValue val;
361 };
362 
363 struct DefaultBool {
364  static auto constexpr value = false;
365 };
367  public:
368  // Parameters
374 
375  // State
376  typedef bool State;
377 
378  // Return value of the function
379  static constexpr IValueType retType = IV_Number;
380 
381  static constexpr auto name = "STRING";
387  static void checkParameters(
388  State &state,
389  const std::vector<std::unique_ptr<IValueProvider>> &parameters){
390 
391  };
392 
394  const State &state,
395  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
396  const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) {
397  auto accepter = P0::extractValue(parameters, json, alloc);
398  auto complex = P1::extractValue(parameters, json, alloc);
399  if (complex){
400  rapidjson::StringBuffer buff;
401  rapidjson::Writer<rapidjson::StringBuffer> writer(buff);
402  accepter.Accept(json, alloc, writer);
403  return RJValue(buff.GetString(),alloc);
404  }else{
405  StringCastHandler handler(&alloc);
406  accepter.Accept(json, alloc, handler);
407  return handler.getValue();
408  }
409 
410  };
411 };
412 
413 } // namespace joda::query::providers::cast
414 
415 namespace joda::query {
416 template class TemplateProvider<
418 
421 } // namespace joda::query
422 #endif // JODA_CASTPROVIDER_H
rapidjson::MemoryPoolAllocator< RJBaseAlloc > RJMemoryPoolAlloc
Definition: RJFwd.h:26
rapidjson::GenericValue< RJChar, RJMemoryPoolAlloc > RJValue
Definition: RJFwd.h:29
Definition: RapidJsonDocument.h:22
Definition: TemplateProvider.h:44
bool State
Definition: CastProvider.h:238
static constexpr auto name
Definition: CastProvider.h:243
values::NoParameter< 3 > P3
Definition: CastProvider.h:234
static constexpr IValueType retType
Definition: CastProvider.h:241
values::NoParameter< 1 > P1
Definition: CastProvider.h:232
values::AnyParameter< 0 > P0
Definition: CastProvider.h:231
static void checkParameters(State &state, const std::vector< std::unique_ptr< IValueProvider >> &parameters)
Definition: CastProvider.h:249
static RJValue calculate(const State &state, const std::vector< std::unique_ptr< IValueProvider >> &parameters, const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc)
Definition: CastProvider.h:255
values::NoParameter< 4 > P4
Definition: CastProvider.h:235
values::NoParameter< 2 > P2
Definition: CastProvider.h:233
bool Uint(unsigned i)
Definition: CastProvider.h:177
bool Uint64(uint64_t i)
Definition: CastProvider.h:187
bool StartObject()
Definition: CastProvider.h:211
bool Null()
Definition: CastProvider.h:162
bool Double(double d)
Definition: CastProvider.h:192
char Ch
Definition: CastProvider.h:160
bool Int(int i)
Definition: CastProvider.h:172
bool Key(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: CastProvider.h:213
constexpr bool EndArray(rapidjson::SizeType elementCount)
Definition: CastProvider.h:221
constexpr bool StartArray()
Definition: CastProvider.h:219
bool RawNumber(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: CastProvider.h:197
RJValue getValue()
Definition: CastProvider.h:222
bool Int64(int64_t i)
Definition: CastProvider.h:182
bool String(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: CastProvider.h:201
bool EndObject(rapidjson::SizeType memberCount)
Definition: CastProvider.h:217
bool Bool(bool b)
Definition: CastProvider.h:164
static constexpr IValueType retType
Definition: CastProvider.h:109
static void checkParameters(State &state, const std::vector< std::unique_ptr< IValueProvider >> &parameters)
Definition: CastProvider.h:117
bool State
Definition: CastProvider.h:106
static constexpr auto name
Definition: CastProvider.h:111
values::NoParameter< 4 > P4
Definition: CastProvider.h:103
values::NoParameter< 3 > P3
Definition: CastProvider.h:102
values::NoParameter< 2 > P2
Definition: CastProvider.h:101
static RJValue calculate(const State &state, const std::vector< std::unique_ptr< IValueProvider >> &parameters, const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc)
Definition: CastProvider.h:123
values::NoParameter< 1 > P1
Definition: CastProvider.h:100
values::AnyParameter< 0 > P0
Definition: CastProvider.h:99
constexpr bool StartArray()
Definition: CastProvider.h:87
RJValue getValue()
Definition: CastProvider.h:90
bool StartObject()
Definition: CastProvider.h:79
char Ch
Definition: CastProvider.h:28
bool String(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: CastProvider.h:69
bool Int(int i)
Definition: CastProvider.h:40
bool Uint64(uint64_t i)
Definition: CastProvider.h:55
bool Uint(unsigned i)
Definition: CastProvider.h:45
bool Double(double d)
Definition: CastProvider.h:60
bool Null()
Definition: CastProvider.h:30
bool Int64(int64_t i)
Definition: CastProvider.h:50
bool Bool(bool b)
Definition: CastProvider.h:32
constexpr bool EndArray(rapidjson::SizeType elementCount)
Definition: CastProvider.h:89
bool Key(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: CastProvider.h:81
bool EndObject(rapidjson::SizeType memberCount)
Definition: CastProvider.h:85
bool RawNumber(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: CastProvider.h:65
bool State
Definition: CastProvider.h:376
values::OptionalWithDefaultParameter< 1, values::BoolParameter< 1 >, DefaultBool > P1
Definition: CastProvider.h:370
values::AnyParameter< 0 > P0
Definition: CastProvider.h:369
values::NoParameter< 4 > P4
Definition: CastProvider.h:373
static void checkParameters(State &state, const std::vector< std::unique_ptr< IValueProvider >> &parameters)
Definition: CastProvider.h:387
static constexpr auto name
Definition: CastProvider.h:381
values::NoParameter< 2 > P2
Definition: CastProvider.h:371
static constexpr IValueType retType
Definition: CastProvider.h:379
static RJValue calculate(const State &state, const std::vector< std::unique_ptr< IValueProvider >> &parameters, const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc)
Definition: CastProvider.h:393
values::NoParameter< 3 > P3
Definition: CastProvider.h:372
bool RawNumber(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: CastProvider.h:335
StringCastHandler(RJMemoryPoolAlloc *alloc)
Definition: CastProvider.h:293
RJValue getValue()
Definition: CastProvider.h:356
bool Bool(bool b)
Definition: CastProvider.h:302
bool Uint(unsigned i)
Definition: CastProvider.h:315
bool Key(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: CastProvider.h:347
bool Uint64(uint64_t i)
Definition: CastProvider.h:325
bool EndObject(rapidjson::SizeType memberCount)
Definition: CastProvider.h:351
bool String(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: CastProvider.h:340
constexpr bool StartArray()
Definition: CastProvider.h:353
constexpr bool EndArray(rapidjson::SizeType elementCount)
Definition: CastProvider.h:355
bool StartObject()
Definition: CastProvider.h:345
bool Int(int i)
Definition: CastProvider.h:310
bool Int64(int64_t i)
Definition: CastProvider.h:320
bool Null()
Definition: CastProvider.h:297
char Ch
Definition: CastProvider.h:295
bool Double(double d)
Definition: CastProvider.h:330
Definition: CastProvider.h:24
Definition: AttributeStatAggregator.h:12
TemplateProvider< joda::query::providers::cast::FloatCastCalculator > FloatCastProvider
Definition: CastProvider.h:273
TemplateProvider< joda::query::providers::cast::IntCastCalculator > IntCastProvider
Definition: CastProvider.h:140
IValueType
Definition: IValueProvider.h:33
@ IV_Number
Definition: IValueProvider.h:35
TemplateProvider< joda::query::providers::cast::StringCastCalculator > StringCastProvider
Definition: CastProvider.h:420
Definition: CastProvider.h:363
static constexpr auto value
Definition: CastProvider.h:364
Definition: ParameterPack.h:239
static ReturnT extractValue(const std::vector< std::unique_ptr< IValueProvider >> &parameters, const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc)
Definition: ParameterPack.h:249
Definition: ParameterPack.h:18
static ReturnT extractValue(const std::vector< std::unique_ptr< IValueProvider >> &parameters, const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc)
Definition: ParameterPack.h:70