JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
ListAttributesProvider.h
Go to the documentation of this file.
1 //
2 // Created by Nico Schäfer on 12/7/17.
3 //
4 
5 #ifndef JODA_LISTATTRIBUTESPROVIDER_H
6 #define JODA_LISTATTRIBUTESPROVIDER_H
7 
8 #include "IValueProvider.h"
9 #include "ParameterPack.h"
10 #include "TemplateProvider.h"
12 
14  public:
15  typedef char Ch;
16 
17  constexpr bool Null() {
18  if (!firstObject) return false;
19  return true;
20  }
21 
22  constexpr bool Bool(bool b) {
23  if (!firstObject) return false;
24  return true;
25  }
26 
27  constexpr bool Int(int i) {
28  if (!firstObject) return false;
29  return true;
30  }
31 
32  constexpr bool Uint(unsigned i) {
33  if (!firstObject) return false;
34  return true;
35  }
36 
37  constexpr bool Int64(int64_t i) {
38  if (!firstObject) return false;
39  return true;
40  }
41 
42  constexpr bool Uint64(uint64_t i) {
43  if (!firstObject) return false;
44  return true;
45  }
46 
47  constexpr bool Double(double d) {
48  if (!firstObject) return false;
49  return true;
50  }
51 
52  constexpr bool RawNumber(const Ch *str, rapidjson::SizeType length,
53  bool copy) {
54  if (!firstObject) return false;
55  return true;
56  }
57 
58  constexpr bool String(const Ch *str, rapidjson::SizeType length, bool copy) {
59  if (!firstObject) return false;
60  return true;
61  }
62 
63  bool StartObject() {
64  if (stack == 0) firstObject = true;
65  stack++;
66  return true;
67  }
68 
69  bool Key(const Ch *str, rapidjson::SizeType length, bool copy) {
70  if (!firstObject) return false;
71  if (stack == 1) attributes.emplace_back(str, length);
72  return true;
73  }
74 
75  bool EndObject(rapidjson::SizeType memberCount) {
76  stack--;
77  return true;
78  }
79 
80  constexpr bool StartArray() {
81  if (!firstObject) return false;
82  stack++;
83  return true;
84  }
85 
86  constexpr bool EndArray(rapidjson::SizeType elementCount) {
87  stack--;
88  return true;
89  }
90 
91  std::vector<std::string> getAttributes() const { return attributes; }
92 
93  void reset() {
94  attributes.clear();
95  stack = 0;
96  }
97 
98  private:
99  std::vector<std::string> attributes;
100  int stack = 0;
101  bool firstObject = false;
102 };
103 
105  public:
106  // Parameters
112 
113  // State
114  typedef bool State;
115 
116  // Return value of the function
117  static constexpr IValueType retType = IV_Array;
118 
119  static constexpr auto name = "LISTATTRIBUTES";
125  static void checkParameters(
126  State &state,
127  const std::vector<std::unique_ptr<IValueProvider>> &parameters){
128 
129  };
130 
132  const State &state,
133  const std::vector<std::unique_ptr<IValueProvider>> &parameters,
134  const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) {
135  auto accepter = P0::extractValue(parameters, json, alloc);
136  RJValue ret(rapidjson::kArrayType);
137 
138  auto ptr = accepter.getPointer(json, alloc);
139  if (ptr != nullptr) {
140  if (!ptr->IsObject()) return RJValue();
141  ret.Reserve(ptr->MemberCount(), alloc);
142  for (typename RJDocument::ConstMemberIterator m = ptr->MemberBegin();
143  m != ptr->MemberEnd(); ++m) {
144  ret.PushBack(RJValue(m->name, alloc), alloc);
145  }
146  return ret;
147  }
148 
149  auto vo = accepter.getObjVO(json, alloc);
150  if (vo != nullptr) {
151  ret.Reserve(vo->size(), alloc);
152  for (const auto &mem : vo->attributes()) {
153  ret.PushBack(RJValue(mem, alloc), alloc);
154  }
155  return ret;
156  }
157 
158  ListAttributesHandler handler;
159  auto res = accepter.Accept(json, alloc, handler);
160  if (!res) return RJValue();
161 
162  auto atts = handler.getAttributes();
163  ret.Reserve(atts.size(), alloc);
164  for (auto &&att : atts) {
165  ret.PushBack(RJValue(att, alloc), alloc);
166  }
167  return ret;
168  };
169 };
170 } // namespace joda::query::providers::listattributes
171 
172 namespace joda::query {
173 template class TemplateProvider<
175 
176 typedef TemplateProvider<
179 } // namespace joda::query
180 
181 #endif // JODA_LISTATTRIBUTESPROVIDER_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
values::NoParameter< 3 > P3
Definition: ListAttributesProvider.h:110
static void checkParameters(State &state, const std::vector< std::unique_ptr< IValueProvider >> &parameters)
Definition: ListAttributesProvider.h:125
bool State
Definition: ListAttributesProvider.h:114
values::NoParameter< 1 > P1
Definition: ListAttributesProvider.h:108
values::ObjectParameter< 0 > P0
Definition: ListAttributesProvider.h:107
static RJValue calculate(const State &state, const std::vector< std::unique_ptr< IValueProvider >> &parameters, const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc)
Definition: ListAttributesProvider.h:131
values::NoParameter< 2 > P2
Definition: ListAttributesProvider.h:109
static constexpr IValueType retType
Definition: ListAttributesProvider.h:117
values::NoParameter< 4 > P4
Definition: ListAttributesProvider.h:111
static constexpr auto name
Definition: ListAttributesProvider.h:119
constexpr bool StartArray()
Definition: ListAttributesProvider.h:80
constexpr bool Null()
Definition: ListAttributesProvider.h:17
constexpr bool Double(double d)
Definition: ListAttributesProvider.h:47
constexpr bool Int64(int64_t i)
Definition: ListAttributesProvider.h:37
constexpr bool String(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: ListAttributesProvider.h:58
constexpr bool RawNumber(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: ListAttributesProvider.h:52
constexpr bool Int(int i)
Definition: ListAttributesProvider.h:27
bool StartObject()
Definition: ListAttributesProvider.h:63
std::vector< std::string > getAttributes() const
Definition: ListAttributesProvider.h:91
constexpr bool EndArray(rapidjson::SizeType elementCount)
Definition: ListAttributesProvider.h:86
constexpr bool Bool(bool b)
Definition: ListAttributesProvider.h:22
constexpr bool Uint64(uint64_t i)
Definition: ListAttributesProvider.h:42
bool Key(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: ListAttributesProvider.h:69
bool EndObject(rapidjson::SizeType memberCount)
Definition: ListAttributesProvider.h:75
constexpr bool Uint(unsigned i)
Definition: ListAttributesProvider.h:32
char Ch
Definition: ListAttributesProvider.h:15
void reset()
Definition: ListAttributesProvider.h:93
Definition: ListAttributesProvider.h:11
Definition: AttributeStatAggregator.h:12
IValueType
Definition: IValueProvider.h:33
@ IV_Array
Definition: IValueProvider.h:38
TemplateProvider< joda::query::providers::listattributes::ListAttributesCalculator > ListAttributesProvider
Definition: ListAttributesProvider.h:178
Definition: ParameterPack.h:18
Definition: ParameterPack.h:199
static ReturnT extractValue(const std::vector< std::unique_ptr< IValueProvider >> &parameters, const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc)
Definition: ParameterPack.h:211