JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
MemberCountProvider.h
Go to the documentation of this file.
1 //
2 // Created by Nico on 16/07/2019.
3 //
5 #include "ValueAccepter.h"
6 #ifndef JODA_MEMBERCOUNTPROVIDER_H
7 #define JODA_MEMBERCOUNTPROVIDER_H
10  public:
11  typedef char Ch;
12 
13  constexpr bool Null() {
14  if (!firstObject) return false;
15  return true;
16  }
17 
18  constexpr bool Bool(bool b) {
19  if (!firstObject) return false;
20  return true;
21  }
22 
23  constexpr bool Int(int i) {
24  if (!firstObject) return false;
25  return true;
26  }
27 
28  constexpr bool Uint(unsigned i) {
29  if (!firstObject) return false;
30  return true;
31  }
32 
33  constexpr bool Int64(int64_t i) {
34  if (!firstObject) return false;
35  return true;
36  }
37 
38  constexpr bool Uint64(uint64_t i) {
39  if (!firstObject) return false;
40  return true;
41  }
42 
43  constexpr bool Double(double d) {
44  if (!firstObject) return false;
45  return true;
46  }
47 
48  constexpr bool RawNumber(const Ch *str, rapidjson::SizeType length,
49  bool copy) {
50  if (!firstObject) return false;
51  return true;
52  }
53 
54  constexpr bool String(const Ch *str, rapidjson::SizeType length, bool copy) {
55  if (!firstObject) return false;
56  return true;
57  }
58 
59  bool StartObject() {
60  if (stack == 0) firstObject = true;
61  stack++;
62  return true;
63  }
64 
65  bool Key(const Ch *str, rapidjson::SizeType length, bool copy) {
66  if (stack == 1) count++;
67  return true;
68  }
69 
70  bool EndObject(rapidjson::SizeType memberCount) {
71  stack--;
72  return true;
73  }
74 
75  constexpr bool StartArray() {
76  if (!firstObject) return false;
77  stack++;
78  return true;
79  }
80 
81  constexpr bool EndArray(rapidjson::SizeType elementCount) {
82  stack--;
83  return true;
84  }
85 
86  size_t getCount() const { return count; }
87 
88  void reset() {
89  stack = 0;
90  count = 0;
91  firstObject = false;
92  }
93 
94  private:
95  int stack = 0;
96  size_t count = 0;
97  bool firstObject = false;
98 };
99 
101  public:
102  static constexpr IValueType retType = IV_Number;
103  static constexpr IValueType inType = IV_Object;
104  static constexpr bool acceptAll = false;
105  static constexpr auto name = "MEMCOUNT";
106 
107  static RJValue accept(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc,
108  const ValueAccepter &accepter) {
109  MemberCountHandler handler;
110  auto res = accepter.Accept(json, alloc, handler);
111  if(!res) return RJValue();
112  return RJValue(handler.getCount());
113  }
114 
115  static RJValue pointer(const RJValue *ptr) {
116  if (ptr == nullptr || !ptr->IsObject()) return RJValue();
117  return RJValue(ptr->MemberCount());
118  }
119 
120  static RJValue virtualObject(const VirtualObject *vo) {
121  if (vo == nullptr) return RJValue();
122  return RJValue(vo->size());
123  }
124 };
125 } // namespace joda::query::providers::membercount
126 
127 namespace joda::query {
128 template class UnaryPointerAcceptProvider<
130 
134 } // namespace joda::query
135 #endif // JODA_MEMBERCOUNTPROVIDER_H
rapidjson::MemoryPoolAllocator< RJBaseAlloc > RJMemoryPoolAlloc
Definition: RJFwd.h:26
rapidjson::GenericValue< RJChar, RJMemoryPoolAlloc > RJValue
Definition: RJFwd.h:29
Definition: RapidJsonDocument.h:22
Definition: VirtualObject.h:13
size_t size() const
Definition: VirtualObject.cpp:7
Definition: UnaryPointerAcceptProvider.h:25
Definition: ValueAccepter.h:16
bool Accept(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc, Handler &h) const
Definition: ValueAccepter.h:32
static RJValue virtualObject(const VirtualObject *vo)
Definition: MemberCountProvider.h:120
static constexpr bool acceptAll
Definition: MemberCountProvider.h:104
static constexpr auto name
Definition: MemberCountProvider.h:105
static constexpr IValueType inType
Definition: MemberCountProvider.h:103
static constexpr IValueType retType
Definition: MemberCountProvider.h:102
static RJValue accept(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc, const ValueAccepter &accepter)
Definition: MemberCountProvider.h:107
static RJValue pointer(const RJValue *ptr)
Definition: MemberCountProvider.h:115
constexpr bool String(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: MemberCountProvider.h:54
constexpr bool Double(double d)
Definition: MemberCountProvider.h:43
bool EndObject(rapidjson::SizeType memberCount)
Definition: MemberCountProvider.h:70
constexpr bool RawNumber(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: MemberCountProvider.h:48
size_t getCount() const
Definition: MemberCountProvider.h:86
bool Key(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: MemberCountProvider.h:65
constexpr bool StartArray()
Definition: MemberCountProvider.h:75
constexpr bool Null()
Definition: MemberCountProvider.h:13
constexpr bool Uint64(uint64_t i)
Definition: MemberCountProvider.h:38
constexpr bool EndArray(rapidjson::SizeType elementCount)
Definition: MemberCountProvider.h:81
constexpr bool Int(int i)
Definition: MemberCountProvider.h:23
void reset()
Definition: MemberCountProvider.h:88
constexpr bool Bool(bool b)
Definition: MemberCountProvider.h:18
constexpr bool Uint(unsigned i)
Definition: MemberCountProvider.h:28
constexpr bool Int64(int64_t i)
Definition: MemberCountProvider.h:33
char Ch
Definition: MemberCountProvider.h:11
bool StartObject()
Definition: MemberCountProvider.h:59
Definition: MemberCountProvider.h:8
Definition: AttributeStatAggregator.h:12
UnaryPointerAcceptProvider< joda::query::providers::membercount::MemberCountCalculator > MemberCountProvider
Definition: MemberCountProvider.h:133
IValueType
Definition: IValueProvider.h:33
@ IV_Object
Definition: IValueProvider.h:37
@ IV_Number
Definition: IValueProvider.h:35