5 #ifndef JODA_VIEWLAYER_H
6 #define JODA_VIEWLAYER_H
8 #include <glog/logging.h>
11 #include <rapidjson/stringbuffer.h>
14 #include <unordered_map>
35 std::vector<std::string>
const *viewPaths;
37 std::string prefix =
"";
38 std::unordered_map<std::string, VirtualObject> objects;
39 typedef typename std::unique_ptr<ViewCursor> Cursor;
42 bool pathOverwritten(
const std::string &path)
const;
51 void setPrefix(
const std::string &prefix);
56 template <
typename Handler>
60 auto res =
Accept(handler, prefix);
65 template <
typename Handler>
72 std::variant<const RJValue, std::optional<const RJValue *>,
75 auto obj = objects.find(ptr);
76 if (obj != objects.end()) {
79 auto *v =
RJPointer(ptr.c_str()).Get(*doc);
80 if (pathOverwritten(ptr)) {
84 if (v !=
nullptr)
return std::nullopt;
86 if (previousLayer !=
nullptr)
89 return std::optional<const RJValue *>(
nullptr);
93 template <
typename Handler>
94 bool Accept(Handler &handler,
const std::string &ptr) {
95 auto obj = objects.find(ptr);
96 if (obj != objects.end()) {
97 return obj->second.Accept(handler);
100 if (cursor ==
nullptr)
return true;
101 if (cursor->isShared(ptr)) {
102 return AcceptShared(handler, ptr);
104 auto *val = cursor->getVal();
105 if (val ==
nullptr) {
106 if (previousLayer ==
nullptr)
return handler.Null();
107 return previousLayer->
Accept(handler, ptr);
109 return val->Accept(handler);
120 void getCursor(
const std::string &ptr);
122 template <
typename Handler>
123 bool AcceptShared(Handler &handler,
const std::string &curr) {
124 auto members = GetMembers(curr);
125 auto baseAttPath = curr +
"/";
126 auto baseAttSize = baseAttPath.size();
127 if (RAPIDJSON_UNLIKELY(!handler.StartObject()))
return false;
136 for (
const auto &member : members) {
137 if (RAPIDJSON_UNLIKELY(
138 !handler.Key(member.name.c_str(), member.name.size(),
true)))
140 if (member.val !=
nullptr) {
142 obj.addMember(struc->
getOrAdd(member.name), member.val);
143 if (RAPIDJSON_UNLIKELY(!member.val->Accept(handler)))
return false;
145 cursor->followAtt(member.name);
146 baseAttPath.erase(baseAttSize);
147 baseAttPath.append(member.name);
148 if (!
Accept(handler, baseAttPath)) {
152 DCHECK(objects.count(baseAttPath) > 0);
153 obj.addMember(struc->
getOrAdd(member.name), &objects[baseAttPath]);
159 if (vos_enabled) objects[curr] = std::move(obj);
161 return handler.EndObject(members.size());
164 std::vector<ViewCursor::ViewObjectMember> GetMembers(
165 const std::string &currPath);
rapidjson::MemoryPoolAllocator< RJBaseAlloc > RJMemoryPoolAlloc
Definition: RJFwd.h:26
rapidjson::GenericPointer< RJValue, RJBaseAlloc > RJPointer
Definition: RJFwd.h:30
rapidjson::GenericValue< RJChar, RJMemoryPoolAlloc > RJValue
Definition: RJFwd.h:29
Definition: ViewLayer.h:22
const VirtualObject * getVO(const std::string &ptr) const
Definition: ViewLayer.cpp:84
void setPrefix(const std::string &prefix)
Definition: ViewLayer.cpp:68
std::variant< const RJValue, std::optional< const RJValue * >, const VirtualObject * > getPointerIfExists(const std::string &ptr, RJMemoryPoolAlloc &alloc) const
Definition: ViewLayer.h:74
bool Accept(Handler &handler)
Definition: ViewLayer.h:57
ViewLayer(ViewLayer &&other)=delete
bool operator()(Handler &handler)
Definition: ViewLayer.h:66
void updateDoc(const RJValue *doc)
Definition: ViewLayer.cpp:80
RJValue const * Get(const RJPointer &ptr)
Definition: ViewLayer.cpp:51
ViewLayer & operator=(ViewLayer &&other)=delete
ViewLayer & operator=(const ViewLayer &other)=delete
bool Accept(Handler &handler, const std::string &ptr)
Definition: ViewLayer.h:94
ViewLayer(const RJValue *doc, const std::vector< std::string > *viewPaths, ViewLayer *previousLayer, ViewStructure *struc)
Definition: ViewLayer.cpp:19
bool pointerIsOverwritten(const std::string &ptr) const
Definition: ViewLayer.cpp:72
ViewLayer(const ViewLayer &other)=delete
Definition: ViewStructure.h:14
ID getOrAdd(const KEY &key)
Definition: ViewStructure.cpp:9
std::pair< size_t, size_t > reserve(size_t size)
Definition: ViewStructure.cpp:41
Definition: VirtualObject.h:13
static bool enable_views_vo
Definition: config.h:83