JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
VirtualObject.h
Go to the documentation of this file.
1 //
2 // Created by Nico on 27/08/2019.
3 //
4 
5 #ifndef JODA_VIRTUALOBJECT_H
6 #define JODA_VIRTUALOBJECT_H
7 
8 #include <glog/logging.h>
9 #include <joda/misc/RJFwd.h>
10 #include <string>
11 #include "ViewStructure.h"
12 
14  public:
15  VirtualObject() = default;
16  explicit VirtualObject(ViewStructure *struc);
17  VirtualObject(const VirtualObject &o) = delete;
18  VirtualObject &operator=(const VirtualObject &o) = delete;
19  VirtualObject(VirtualObject &&o) = default;
21 
22  /*
23  * Special Access
24  */
25  size_t size() const;
26  std::vector<std::string> attributes() const;
27  RJValue deepCopy(RJMemoryPoolAlloc &alloc) const;
28 
29  void addMember(ViewStructure::ID, const RJValue *val);
30  void addMember(ViewStructure::ID, const VirtualObject *val);
31  void reserve(size_t r);
32 
33  private:
34  size_t start_ = 0, end_ = 0, size_ = 0;
35  ViewStructure *struc = nullptr;
36 
37  template <typename F>
38  auto iterateMemberRetAll(F &&lambda) const {
39  std::vector<
40  typename std::result_of<F(const ViewStructure::ObjectMember &)>::type>
41  ret;
42  iterateMember([&ret, &lambda](const ViewStructure::ObjectMember &o) {
43  ret.emplace_back(lambda(o));
44  });
45  return ret;
46  }
47 
48  template <typename Ret, typename F>
49  auto iterateMemberRetOne(F &&lambda, Ret &&init) const {
50  Ret ret = init;
51  iterateMember([&ret, &lambda](const ViewStructure::ObjectMember &o) {
52  ret = lambda(o, ret);
53  });
54  return ret;
55  }
56 
57  template <typename F>
58  void iterateMember(F &&lambda) const {
59  auto it = struc->beginMember();
60  it += start_;
61  DCHECK(end_ - start_ >= size_);
62  for (size_t j = 0; j < size_; ++j) {
63  const auto &member = *it;
64  lambda(member);
65  it++;
66  }
67  }
68 
69  public:
70  /*
71  * Accepter
72  */
73  template <typename Handler>
74  bool Accept(Handler &handler) const {
75  if (RAPIDJSON_UNLIKELY(!handler.StartObject())) return false;
76 
77  if (!iterateMemberRetOne<bool>(
78  [this, &handler](const ViewStructure::ObjectMember &member,
79  bool &prev) {
80  if (!prev) return false;
81  auto name = struc->getKey(member.key);
82  DCHECK(name != nullptr);
83  if (RAPIDJSON_UNLIKELY(
84  !handler.Key(name->c_str(), name->size(), true)))
85  return false;
86 
87  if (member.val != nullptr) {
88  if (RAPIDJSON_UNLIKELY(!member.val->Accept(handler)))
89  return false;
90  } else {
91  if (member.obj == nullptr) {
92  if (RAPIDJSON_UNLIKELY(!RJValue().Accept(handler)))
93  return false;
94  } else {
95  if (RAPIDJSON_UNLIKELY(!member.obj->Accept(handler)))
96  return false;
97  }
98  }
99  return true;
100  },
101  true))
102  return false;
103 
104  return handler.EndObject(size_);
105  }
106 };
107 
108 #endif // JODA_VIRTUALOBJECT_H
rapidjson::MemoryPoolAllocator< RJBaseAlloc > RJMemoryPoolAlloc
Definition: RJFwd.h:26
rapidjson::GenericValue< RJChar, RJMemoryPoolAlloc > RJValue
Definition: RJFwd.h:29
Definition: ViewStructure.h:14
const KEY * getKey(const ID &id) const
Definition: ViewStructure.cpp:32
size_t ID
Definition: ViewStructure.h:26
MemberIterator beginMember() const
Definition: ViewStructure.cpp:51
Definition: VirtualObject.h:13
VirtualObject & operator=(const VirtualObject &o)=delete
std::vector< std::string > attributes() const
Definition: VirtualObject.cpp:29
VirtualObject(const VirtualObject &o)=delete
void reserve(size_t r)
Definition: VirtualObject.cpp:23
size_t size() const
Definition: VirtualObject.cpp:7
bool Accept(Handler &handler) const
Definition: VirtualObject.h:74
void addMember(ViewStructure::ID, const RJValue *val)
Definition: VirtualObject.cpp:9
VirtualObject(VirtualObject &&o)=default
VirtualObject & operator=(VirtualObject &&o)=default
VirtualObject()=default
RJValue deepCopy(RJMemoryPoolAlloc &alloc) const
Definition: VirtualObject.cpp:35
Definition: ViewStructure.h:55
const RJValue * val
Definition: ViewStructure.h:65
ViewStructure::ID key
Definition: ViewStructure.h:64