src/dict.hpp

filter: Statementstmt 83.3% 25 / 30 Branchbran 67.6% 23 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.0% 48 / 64 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« XS.xs ↑ Summary blib/arch/JSON/Immutable/XS.pm »
1 #pragma once
2 #include <charconv>
3 #include <iostream>
4 #include <map>
5 #include <variant>
6 #include <vector>
7  
8 #pragma GCC diagnostic push
9 #pragma GCC diagnostic ignored "-Wclass-memaccess"
10 #include <rapidjson/document.h>
11 #include <rapidjson/rapidjson.h>
12  
13 #include "rapidjson/error/en.h"
14 #pragma GCC diagnostic pop
15  
16 #include <panda/log.h>
17 #include <panda/string.h>
18 #include <panda/string_view.h>
19  
20 template <class... Ts>
21 struct overloaded : Ts... { using Ts::operator()...; };
22 template <class... Ts>
23 overloaded(Ts...)->overloaded<Ts...>;
24  
25 namespace json_tree {
26 struct Undef {};
27  
28 struct Dict {
29 Dict(){};
30 2 ▶ Dict(panda::string filename) { load_dict(filename); }
Branch: 2 branches
BranchTrueFalseTotal
Dict(panda::string filename) { load_dict(filename); } 2 0 2
Dict(panda::string filename) { load_dict(filename); } 2 0 2
31 Dict(rapidjson::Value* node, rapidjson::Document::AllocatorType& allocator);
32 0 ~Dict(){};
33  
34 void load_dict(panda::string filename);
35 void process_node(rapidjson::Value* node, rapidjson::Document::AllocatorType& allocator);
36 bool is_empty() const {
37 return this->value.index() == 0;
38 }
39  
40 template <typename T>
41 constexpr std::add_pointer_t<const T> get_value(const std::initializer_list<panda::string>& path) const {
42 if (auto d = get(path))
43 return std::get_if<T>(&d->value);
44 return nullptr;
45 }
46  
47 template <typename T>
48 T get_value(const std::initializer_list<panda::string>& path, T def_value) const {
49 if (auto d = get(path))
50 if (auto v_p = std::get_if<T>(&d->value))
51 return *v_p;
52 return def_value;
53 }
54  
55 template <typename T>
56 constexpr std::add_pointer_t<const T> get_value(const panda::string& key) const {
57 if (auto d = get(key))
58 return std::get_if<T>(&d->value);
59 return nullptr;
60 }
61  
62 template <typename T>
63 T get_value(const panda::string& key, T def_value) const {
64 if (auto d = get(key))
65 if (auto v_p = std::get_if<T>(&d->value))
66 return *v_p;
67 return def_value;
68 }
69 //single key
70 const Dict* get(const panda::string& key) const {
71 return visit(overloaded{
72 [&](const ObjectMap& m) -> const Dict* {
73 auto i = m.find(key);
74 if (i == m.end()) return nullptr;
75 return &i->second;
76 },
77 [&](const ObjectArr& a) -> const Dict* {
78 uint64_t i;
79 auto [p, ec] = std::from_chars(key.data(), key.data() + key.size(), i);
80 if (ec != std::errc() || i >= a.size()) return nullptr;
81 return &a[i];
82 },
83 [](auto) -> const Dict* {
84 return nullptr;
85 }},
86 this->value);
87 }
88  
89 //single key rvalue TODO: remove copypaste
90 template <size_t size>
91 const Dict* get(char (&literal)[size]) const {
92 panda::string key = literal;
93 return visit(overloaded{
94 [&](const ObjectMap& m) -> const Dict* {
95 auto i = m.find(key);
96 if (i == m.end()) return nullptr;
97 return &i->second;
98 },
99 [&](const ObjectArr& a) -> const Dict* {
100 uint64_t i;
101 auto [p, ec] = std::from_chars(key.data(), key.data() + key.size(), i);
102 if (ec != std::errc() || i >= a.size()) return nullptr;
103 return &a[i];
104 },
105 [](auto) -> const Dict* {
106 return nullptr;
107 }},
108 this->value);
109 }
110 template <typename... Args>
111 const Dict* fget(Args... args) {
112 std::array<panda::string, sizeof...(args)> arr = {args...};
113 return get(arr);
114 }
115  
116 //TODO: make fold expression version
117 template <typename Range>
118 109 auto get(const Range& keys, uint64_t index = 0) const
119 -> std::enable_if_t<
120 std::is_same<std::decay_t<decltype(keys[0])>, panda::string>::value &&
121 !std::is_same<Range, std::initializer_list<panda::string>>::value,
122 const Dict*> {
123 109 ▶ if (index >= keys.size()) return this;
Branch: 1 branch
BranchTrueFalseTotal
if (index >= keys.size()) return this; 47 62 109
124  
125 62 return visit(overloaded{
126 54 [&](const ObjectMap& m) -> const Dict* {
127 54 ▶ auto i = m.find(keys[index]);
Branch: 2 branches
BranchTrueFalseTotal
auto i = m.find(keys[index]); 54 0 54
auto i = m.find(keys[index]); 54 0 54
128 54 ▶ if (i == m.end()) return nullptr;
Branch: 1 branch
BranchTrueFalseTotal
if (i == m.end()) return nullptr; 3 51 54
129 51 ▶ return i->second.get(keys, index + 1);
Branch: 1 branch
BranchTrueFalseTotal
return i->second.get(keys, index + 1); 51 0 51
130 },
131 6 [&](const ObjectArr& a) -> const Dict* {
132 6 ▶ panda::string key = keys[index];
Branch: 1 branch
BranchTrueFalseTotal
panda::string key = keys[index]; 6 0 6
133 uint64_t i;
134 6 ▶ auto [p, ec] = std::from_chars(key.data(), key.data() + key.size(), i);
Branch: 1 branch
BranchTrueFalseTotal
auto [p, ec] = std::from_chars(key.data(), key.data() + key.size(), i); 6 0 6
135 6 ▶ if (ec != std::errc() || i >= a.size()) return nullptr;
Branch: 3 branches
BranchTrueFalseTotal
if (ec != std::errc() || i >= a.size()) return nullptr; 6 0 6
if (ec != std::errc() || i >= a.size()) return nullptr; 1 5 6
if (ec != std::errc() || i >= a.size()) return nullptr; 1 5 6
136 5 ▶ return a[i].get(keys, index + 1);
Branch: 1 branch
BranchTrueFalseTotal
return a[i].get(keys, index + 1); 5 0 5
137 6 },
138 0 [](auto) -> const Dict* {
139 2 return nullptr;
140 }},
141 62 ▶ this->value);
Branch: 1 branch
BranchTrueFalseTotal
this->value); 62 0 62
142 }
143  
144 auto get(const std::initializer_list<panda::string>& keys, uint64_t index = 0) const {
145 if (index >= keys.size()) return this;
146  
147 return visit(overloaded{
148 [&](const ObjectMap& m) -> const Dict* {
149 auto i = m.find(*(keys.begin() + index));
150 if (i == m.end()) return nullptr;
151 return i->second.get(keys, index + 1);
152 },
153 [&](const ObjectArr& a) -> const Dict* {
154 panda::string key = *(keys.begin() + index);
155 uint64_t i;
156 auto [p, ec] = std::from_chars(key.data(), key.data() + key.size(), i);
157 if (ec != std::errc() || i >= a.size()) return nullptr;
158 return a[i].get(keys, index + 1);
159 },
160 [](auto) -> const Dict* {
161 return nullptr;
162 }},
163 this->value);
164 }
165  
166 // template<typename Range>
167 // const Dict* get( const Range& keys, uint64_t index = 0 ) const {
168 // if ( index >= keys.size() ) return this;
169  
170 // return visit( overloaded{
171 // [&](const ObjectMap& m) -> const Dict* {
172 // auto i = m.find(keys[index]);
173 // if ( i == m.end() ) return nullptr;
174 // return i->second.get( keys, index + 1 );
175 // },
176 // [&](const ObjectArr& a) -> const Dict* {
177 // panda::string key = keys[index];
178 // uint64_t i;
179 // auto [p, ec] = std::from_chars(key.data(), key.data()+key.size(), i);
180 // if ( ec != std::errc() || i >= a.size() ) return nullptr;
181 // return a[i].get( keys, index + 1 );
182 // },
183 // [](auto) -> const Dict* {
184 // return nullptr;
185 // }
186 // }, this->value );
187 // }
188  
189 2 std::vector<panda::string> keys() const {
190 2 std::vector<panda::string> ret;
191 2 ▶ if (this->value.index() != 1) return ret;
Branch: 1 branch
BranchTrueFalseTotal
if (this->value.index() != 1) return ret; 1 1 2
192  
193 1 auto hval = std::get_if<ObjectMap>(&this->value);
194  
195 4 ▶ for (auto it = hval->begin(); it != hval->end(); ++it) {
Branch: 1 branch
BranchTrueFalseTotal
for (auto it = hval->begin(); it != hval->end(); ++it) { 3 1 4
196 3 ▶ ret.push_back(it->first);
Branch: 1 branch
BranchTrueFalseTotal
ret.push_back(it->first); 3 0 3
197 }
198  
199 1 return ret;
200 }
201 panda::string to_str() const;
202 void dump(uint32_t level = 0) const;
203  
204 using ObjectMap = std::map<panda::string, Dict>;
205 using ObjectArr = std::vector<Dict>;
206  
207 std::variant<Undef, ObjectMap, ObjectArr, panda::string, int64_t, double, bool> value;
208  
209 private:
210 void _to_str(panda::string& out) const;
211 };
212  
213 } // namespace json_tree
« XS.xs ↑ Summary blib/arch/JSON/Immutable/XS.pm »