Skip to content

Commit 2472104

Browse files
committed
chore: update meojson to v5.1.1
1 parent 3011f69 commit 2472104

11 files changed

Lines changed: 418 additions & 177 deletions

File tree

include/meojson/common/array.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class array
8181
bool> = true>
8282
array(const arr_t<value_t, size>& arr)
8383
{
84+
_array_data.reserve(size);
8485
for (size_t i = 0; i < size; i++) {
8586
_array_data.emplace_back(arr.at(i));
8687
}
@@ -97,6 +98,7 @@ class array
9798
bool> = true>
9899
array(arr_t<value_t, size>&& arr)
99100
{
101+
_array_data.reserve(size);
100102
for (size_t i = 0; i < size; i++) {
101103
_array_data.emplace_back(std::move(arr.at(i)));
102104
}
@@ -111,6 +113,9 @@ class array
111113
bool> = true>
112114
array(const collection_t& coll)
113115
{
116+
if constexpr (_utils::has_size<collection_t>::value) {
117+
_array_data.reserve(coll.size());
118+
}
114119
for (const auto& val : coll) {
115120
_array_data.emplace_back(val);
116121
}
@@ -124,6 +129,9 @@ class array
124129
bool> = true>
125130
array(collection_t&& coll)
126131
{
132+
if constexpr (_utils::has_size<collection_t>::value) {
133+
_array_data.reserve(coll.size());
134+
}
127135
for (auto& val : coll) {
128136
_array_data.emplace_back(std::move(val));
129137
}
@@ -141,6 +149,7 @@ class array
141149
array(const tuple_t<args_t...>& tpl)
142150
{
143151
constexpr size_t tuple_size = std::tuple_size_v<tuple_t<args_t...>>;
152+
_array_data.reserve(tuple_size);
144153
construct_from_tuple_helper(tpl, std::make_index_sequence<tuple_size>());
145154
}
146155

@@ -155,6 +164,7 @@ class array
155164
array(tuple_t<args_t...>&& tpl)
156165
{
157166
constexpr size_t tuple_size = std::tuple_size_v<tuple_t<args_t...>>;
167+
_array_data.reserve(tuple_size);
158168
construct_from_tuple_move_helper(std::move(tpl), std::make_index_sequence<tuple_size>());
159169
}
160170

@@ -207,6 +217,9 @@ class array
207217
template <typename tuple_t, size_t... Is>
208218
void move_as_tuple_helper(tuple_t& result, std::index_sequence<Is...>);
209219

220+
void dump_to(std::string& out) const;
221+
void format_to(std::string& out, size_t indent, size_t indent_times) const;
222+
210223
public:
211224

212225
// Usage: get(key_1, key_2, ..., default_value);
@@ -215,6 +228,7 @@ class array
215228

216229
template <typename value_t = value>
217230
std::optional<value_t> find(size_t pos) const;
231+
const value* find_value(size_t pos) const;
218232

219233
template <typename... args_t>
220234
decltype(auto) emplace_back(args_t&&... args);

include/meojson/common/array_impl.hpp

Lines changed: 70 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@
44

55
namespace json
66
{
7+
namespace _array_impl_detail
8+
{
9+
inline void append_array(array::raw_array& dst, const array& src)
10+
{
11+
dst.reserve(dst.size() + src.size());
12+
dst.insert(dst.end(), src.begin(), src.end());
13+
}
14+
15+
inline void append_array_move(array::raw_array& dst, array& src)
16+
{
17+
dst.reserve(dst.size() + src.size());
18+
dst.insert(dst.end(), std::make_move_iterator(src.begin()), std::make_move_iterator(src.end()));
19+
}
20+
} // namespace _array_impl_detail
21+
722
inline array::array() = default;
823
inline array::array(const array& rhs) = default;
924
inline array::array(array&& rhs) noexcept = default;
@@ -56,7 +71,11 @@ inline bool array::erase(size_t pos)
5671

5772
inline bool array::erase(iterator iter)
5873
{
59-
return _array_data.erase(iter) != _array_data.end();
74+
if (iter == _array_data.end()) {
75+
return false;
76+
}
77+
_array_data.erase(iter);
78+
return true;
6079
}
6180

6281
template <typename... args_t>
@@ -79,32 +98,44 @@ inline const value& array::at(size_t pos) const
7998

8099
inline std::string array::to_string() const
81100
{
82-
std::string str { '[' };
101+
std::string str;
102+
dump_to(str);
103+
return str;
104+
}
105+
106+
inline void array::dump_to(std::string& out) const
107+
{
108+
out.push_back('[');
83109
for (auto iter = _array_data.cbegin(); iter != _array_data.cend();) {
84-
str += iter->to_string();
110+
iter->dump_to(out);
85111
if (++iter != _array_data.cend()) {
86-
str += ',';
112+
out.push_back(',');
87113
}
88114
}
89-
str += ']';
90-
return str;
115+
out.push_back(']');
91116
}
92117

93118
inline std::string array::format(size_t indent, size_t indent_times) const
94119
{
95-
const std::string tail_indent(indent * indent_times, ' ');
96-
const std::string body_indent(indent * (indent_times + 1), ' ');
120+
std::string str;
121+
format_to(str, indent, indent_times);
122+
return str;
123+
}
97124

98-
std::string str { '[', '\n' };
125+
inline void array::format_to(std::string& out, size_t indent, size_t indent_times) const
126+
{
127+
out.push_back('[');
128+
out.push_back('\n');
99129
for (auto iter = _array_data.cbegin(); iter != _array_data.cend();) {
100-
str += body_indent + iter->format(indent, indent_times + 1);
130+
out.append(indent * (indent_times + 1), ' ');
131+
iter->format_to(out, indent, indent_times + 1);
101132
if (++iter != _array_data.cend()) {
102-
str += ',';
133+
out.push_back(',');
103134
}
104-
str += '\n';
135+
out.push_back('\n');
105136
}
106-
str += tail_indent + ']';
107-
return str;
137+
out.append(indent * indent_times, ' ');
138+
out.push_back(']');
108139
}
109140

110141
inline std::string array::dumps(std::optional<size_t> indent) const
@@ -144,63 +175,36 @@ inline auto array::get(std::tuple<key_then_default_value_t...> keys_then_default
144175
template <typename value_t, typename... rest_keys_t>
145176
inline auto array::get_helper(const value_t& default_value, size_t pos, rest_keys_t&&... rest) const
146177
{
147-
constexpr bool is_json = std::is_same_v<value, value_t> || std::is_same_v<array, value_t> || std::is_same_v<object, value_t>;
148-
constexpr bool is_string = std::is_constructible_v<std::string, value_t> && !is_json;
149-
150178
if (!contains(pos)) {
151-
if constexpr (is_string) {
152-
return std::string(default_value);
153-
}
154-
else {
155-
return value_t(default_value);
156-
}
179+
return _utils::default_or_string(default_value);
157180
}
158181

159-
return at(pos).get_helper(default_value, std::forward<rest_keys_t>(rest)...);
182+
return _array_data[pos].get_helper(default_value, std::forward<rest_keys_t>(rest)...);
160183
}
161184

162185
template <typename value_t>
163186
inline auto array::get_helper(const value_t& default_value, size_t pos) const
164187
{
165-
constexpr bool is_json = std::is_same_v<value, value_t> || std::is_same_v<array, value_t> || std::is_same_v<object, value_t>;
166-
constexpr bool is_string = std::is_constructible_v<std::string, value_t> && !is_json;
167-
168188
if (!contains(pos)) {
169-
if constexpr (is_string) {
170-
return std::string(default_value);
171-
}
172-
else {
173-
return value_t(default_value);
174-
}
189+
return _utils::default_or_string(default_value);
175190
}
176191

177-
auto val = _array_data[pos];
178-
if (val.template is<value_t>()) {
179-
if constexpr (is_string) {
180-
return val.template as<std::string>();
181-
}
182-
else {
183-
return val.template as<value_t>();
184-
}
185-
}
186-
else {
187-
if constexpr (is_string) {
188-
return std::string(default_value);
189-
}
190-
else {
191-
return value_t(default_value);
192-
}
193-
}
192+
return _utils::json_value_or_default(_array_data[pos], default_value);
194193
}
195194

196195
template <typename value_t>
197196
inline std::optional<value_t> array::find(size_t pos) const
198197
{
199-
if (!contains(pos)) {
198+
const auto* val = find_value(pos);
199+
if (!val) {
200200
return std::nullopt;
201201
}
202-
const auto& val = _array_data.at(pos);
203-
return val.template is<value_t>() ? std::optional<value_t>(val.template as<value_t>()) : std::nullopt;
202+
return val->template is<value_t>() ? std::optional<value_t>(val->template as<value_t>()) : std::nullopt;
203+
}
204+
205+
inline const value* array::find_value(size_t pos) const
206+
{
207+
return contains(pos) ? &_array_data[pos] : nullptr;
204208
}
205209

206210
inline typename array::iterator array::begin() noexcept
@@ -276,38 +280,38 @@ inline const value& array::operator[](size_t pos) const
276280
inline array array::operator+(const array& rhs) const&
277281
{
278282
array temp = *this;
279-
temp._array_data.insert(temp._array_data.end(), rhs.begin(), rhs.end());
283+
_array_impl_detail::append_array(temp._array_data, rhs);
280284
return temp;
281285
}
282286

283287
inline array array::operator+(array&& rhs) const&
284288
{
285289
array temp = *this;
286-
temp._array_data.insert(temp._array_data.end(), std::make_move_iterator(rhs.begin()), std::make_move_iterator(rhs.end()));
290+
_array_impl_detail::append_array_move(temp._array_data, rhs);
287291
return temp;
288292
}
289293

290294
inline array array::operator+(const array& rhs) &&
291295
{
292-
_array_data.insert(_array_data.end(), rhs.begin(), rhs.end());
296+
_array_impl_detail::append_array(_array_data, rhs);
293297
return std::move(*this);
294298
}
295299

296300
inline array array::operator+(array&& rhs) &&
297301
{
298-
_array_data.insert(_array_data.end(), std::make_move_iterator(rhs.begin()), std::make_move_iterator(rhs.end()));
302+
_array_impl_detail::append_array_move(_array_data, rhs);
299303
return std::move(*this);
300304
}
301305

302306
inline array& array::operator+=(const array& rhs)
303307
{
304-
_array_data.insert(_array_data.end(), rhs.begin(), rhs.end());
308+
_array_impl_detail::append_array(_array_data, rhs);
305309
return *this;
306310
}
307311

308312
inline array& array::operator+=(array&& rhs)
309313
{
310-
_array_data.insert(_array_data.end(), std::make_move_iterator(rhs.begin()), std::make_move_iterator(rhs.end()));
314+
_array_impl_detail::append_array_move(_array_data, rhs);
311315
return *this;
312316
}
313317

@@ -344,6 +348,9 @@ inline T array::as() const&
344348
}
345349
else if constexpr (_utils::is_collection<T>) {
346350
T result;
351+
if constexpr (_utils::has_reserve<T>::value) {
352+
result.reserve(_array_data.size());
353+
}
347354
for (const auto& val : _array_data) {
348355
if constexpr (_utils::has_emplace_back<T>::value) {
349356
result.emplace_back(val.as<typename T::value_type>());
@@ -387,6 +394,9 @@ inline T array::as() &&
387394
}
388395
else if constexpr (_utils::is_collection<T>) {
389396
T result;
397+
if constexpr (_utils::has_reserve<T>::value) {
398+
result.reserve(_array_data.size());
399+
}
390400
for (auto& val : _array_data) {
391401
if constexpr (_utils::has_emplace_back<T>::value) {
392402
result.emplace_back(std::move(val).as<typename T::value_type>());
@@ -427,4 +437,4 @@ inline void array::move_as_tuple_helper(tuple_t& result, std::index_sequence<Is.
427437
using std::get;
428438
((get<Is>(result) = std::move(_array_data[Is]).template as<std::tuple_element_t<Is, tuple_t>>()), ...);
429439
}
430-
} // namespace json
440+
} // namespace json

include/meojson/common/object.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class object
130130

131131
template <typename value_t = value>
132132
std::optional<value_t> find(const std::string& key) const;
133+
const value* find_value(const std::string& key) const;
133134

134135
template <typename... args_t>
135136
decltype(auto) emplace(args_t&&... args);
@@ -236,6 +237,9 @@ class object
236237
template <typename value_t>
237238
auto get_helper(const value_t& default_value, const std::string& key) const;
238239

240+
void dump_to(std::string& out) const;
241+
void format_to(std::string& out, size_t indent, size_t indent_times) const;
242+
239243
std::string format(size_t indent, size_t indent_times) const;
240244

241245
private:

0 commit comments

Comments
 (0)