Skip to content

Commit a585360

Browse files
committed
try_decode_json
1 parent 0750522 commit a585360

File tree

4 files changed

+117
-266
lines changed

4 files changed

+117
-266
lines changed

include/jsoncons/config/jsoncons_config.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,19 @@ namespace jsoncons {
217217
return jsoncons::wstring_view(w);
218218
}
219219

220+
// From boost 1_71
221+
template <typename T,typename U>
222+
T launder_cast(U* u)
223+
{
224+
#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
225+
return std::launder(reinterpret_cast<T>(u));
226+
#elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) > 800
227+
return __builtin_launder(reinterpret_cast<T>(u));
228+
#else
229+
return reinterpret_cast<T>(u);
230+
#endif
231+
}
232+
220233
} // namespace jsoncons
221234

222235
#define JSONCONS_EXPAND(X) X

include/jsoncons/reflect/conv_result.hpp

Lines changed: 41 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,51 @@ class conv_result
3030
T value_;
3131
};
3232
public:
33+
34+
// value constructors
35+
conv_result(const T& value)
36+
: has_value_(true)
37+
{
38+
construct(value);
39+
}
40+
41+
conv_result(T&& value) noexcept
42+
: has_value_(true)
43+
{
44+
construct(std::move(value));
45+
}
46+
3347
conv_result(std::error_code ec) noexcept
3448
: has_value_(false), ec_{ec}
3549
{
3650
}
3751

3852
// copy constructors
3953
conv_result(const conv_result<T>& other)
40-
: has_value_(false), ec_{other.ec_}
54+
: has_value_(other.has_value_)
4155
{
4256
if (other)
4357
{
44-
construct(*other);
58+
construct(other.value_);
59+
}
60+
else
61+
{
62+
ec_ = other.ec_;
4563
}
4664
}
4765

4866
// move constructors
49-
conv_result(conv_result<T>&& other)
50-
: has_value_(false), ec_{other.ec_}
51-
{
67+
conv_result(conv_result<T>&& other) noexcept
68+
: has_value_(other.has_value())
69+
{
5270
if (other)
5371
{
5472
construct(std::move(other.value_));
5573
}
56-
}
57-
58-
// value constructors
59-
conv_result(T&& value) // (8)
60-
: has_value_(true), value_(std::move(value))
61-
{
74+
else
75+
{
76+
ec_ = other.ec_;
77+
}
6278
}
6379

6480
~conv_result() noexcept
@@ -74,7 +90,8 @@ class conv_result
7490
}
7591
else
7692
{
77-
reset();
93+
destroy();
94+
ec_ = other.ec_;
7895
}
7996
return *this;
8097
}
@@ -87,7 +104,8 @@ class conv_result
87104
}
88105
else
89106
{
90-
reset();
107+
destroy();
108+
ec_ = other.ec_;
91109
}
92110
return *this;
93111
}
@@ -178,17 +196,23 @@ class conv_result
178196
conv_result& source = contains_a_value ? *this : other;
179197
conv_result& target = contains_a_value ? other : *this;
180198
target = conv_result<T>(*source);
181-
source.reset();
199+
source.destroy();
200+
source.ec_ = target.ec_;
182201
}
183202
}
184203
private:
185204
constexpr const T& get() const { return this->value_; }
186205
T& get() { return this->value_; }
187206

188-
template <typename... Args>
189-
void construct(Args&&... args)
207+
void construct(const T& value)
190208
{
191-
::new (static_cast<void*>(&this->value_)) T(std::forward<Args>(args)...);
209+
::new (&value_) T(value);
210+
has_value_ = true;
211+
}
212+
213+
void construct(T&& value) noexcept
214+
{
215+
::new (&value_) T(std::move(value));
192216
has_value_ = true;
193217
}
194218

@@ -221,109 +245,6 @@ swap(conv_result<T>& lhs, conv_result<T>& rhs) noexcept
221245
lhs.swap(rhs);
222246
}
223247

224-
template <class T1, typename T2>
225-
constexpr bool operator==(const conv_result<T1>& lhs, const conv_result<T2>& rhs) noexcept
226-
{
227-
return lhs.has_value() == rhs.has_value() && (!lhs.has_value() || *lhs == *rhs);
228-
}
229-
230-
template <class T1, typename T2>
231-
constexpr bool operator!=(const conv_result<T1>& lhs, const conv_result<T2>& rhs) noexcept
232-
{
233-
return lhs.has_value() != rhs.has_value() || (lhs.has_value() && *lhs != *rhs);
234-
}
235-
236-
template <class T1, typename T2>
237-
constexpr bool operator<(const conv_result<T1>& lhs, const conv_result<T2>& rhs) noexcept
238-
{
239-
return rhs.has_value() && (!lhs.has_value() || *lhs < *rhs);
240-
}
241-
242-
template <class T1, typename T2>
243-
constexpr bool operator>(const conv_result<T1>& lhs, const conv_result<T2>& rhs) noexcept
244-
{
245-
return lhs.has_value() && (!rhs.has_value() || *lhs > *rhs);
246-
}
247-
248-
template <class T1, typename T2>
249-
constexpr bool operator<=(const conv_result<T1>& lhs, const conv_result<T2>& rhs) noexcept
250-
{
251-
return !lhs.has_value() || (rhs.has_value() && *lhs <= *rhs);
252-
}
253-
254-
template <class T1, typename T2>
255-
constexpr bool operator>=(const conv_result<T1>& lhs, const conv_result<T2>& rhs) noexcept
256-
{
257-
return !rhs.has_value() || (lhs.has_value() && *lhs >= *rhs);
258-
}
259-
260-
template <class T1, typename T2>
261-
constexpr bool operator==(const conv_result<T1>& lhs, const T2& rhs) noexcept
262-
{
263-
return lhs ? *lhs == rhs : false;
264-
}
265-
template <class T1, typename T2>
266-
constexpr bool operator==(const T1& lhs, const conv_result<T2>& rhs) noexcept
267-
{
268-
return rhs ? lhs == *rhs : false;
269-
}
270-
271-
template <class T1, typename T2>
272-
constexpr bool operator!=(const conv_result<T1>& lhs, const T2& rhs) noexcept
273-
{
274-
return lhs ? *lhs != rhs : true;
275-
}
276-
template <class T1, typename T2>
277-
constexpr bool operator!=(const T1& lhs, const conv_result<T2>& rhs) noexcept
278-
{
279-
return rhs ? lhs != *rhs : true;
280-
}
281-
282-
template <class T1, typename T2>
283-
constexpr bool operator<(const conv_result<T1>& lhs, const T2& rhs) noexcept
284-
{
285-
return lhs ? *lhs < rhs : true;
286-
}
287-
template <class T1, typename T2>
288-
constexpr bool operator<(const T1& lhs, const conv_result<T2>& rhs) noexcept
289-
{
290-
return rhs ? lhs < *rhs : false;
291-
}
292-
293-
template <class T1, typename T2>
294-
constexpr bool operator<=(const conv_result<T1>& lhs, const T2& rhs) noexcept
295-
{
296-
return lhs ? *lhs <= rhs : true;
297-
}
298-
template <class T1, typename T2>
299-
constexpr bool operator<=(const T1& lhs, const conv_result<T2>& rhs) noexcept
300-
{
301-
return rhs ? lhs <= *rhs : false;
302-
}
303-
304-
template <class T1, typename T2>
305-
constexpr bool operator>(const conv_result<T1>& lhs, const T2& rhs) noexcept
306-
{
307-
return lhs ? *lhs > rhs : false;
308-
}
309-
310-
template <class T1, typename T2>
311-
constexpr bool operator>(const T1& lhs, const conv_result<T2>& rhs) noexcept
312-
{
313-
return rhs ? lhs > *rhs : true;
314-
}
315-
316-
template <class T1, typename T2>
317-
constexpr bool operator>=(const conv_result<T1>& lhs, const T2& rhs) noexcept
318-
{
319-
return lhs ? *lhs >= rhs : false;
320-
}
321-
template <class T1, typename T2>
322-
constexpr bool operator>=(const T1& lhs, const conv_result<T2>& rhs) noexcept
323-
{
324-
return rhs ? lhs >= *rhs : true;
325-
}
326-
327248
} // reflect
328249
} // jsoncons
329250

0 commit comments

Comments
 (0)