2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // Official repository: https://github.com/boostorg/json
10 #ifndef BOOST_JSON_IMPL_VALUE_IPP
11 #define BOOST_JSON_IMPL_VALUE_IPP
13 #include <boost/json/value.hpp>
26 case json::kind::null:
27 case json::kind::bool_:
28 case json::kind::int64:
29 case json::kind::uint64:
30 case json::kind::double_:
34 case json::kind::string:
38 case json::kind::array:
42 case json::kind::object:
55 case json::kind::null:
60 case json::kind::bool_:
66 case json::kind::int64:
72 case json::kind::uint64:
78 case json::kind::double_:
84 case json::kind::string:
90 case json::kind::array:
96 case json::kind::object:
105 value(value&& other) noexcept
107 relocate(this, other);
108 ::new(&other.sca_) scalar(sp_);
118 case json::kind::null:
123 case json::kind::bool_:
125 other.sca_.b, std::move(sp));
128 case json::kind::int64:
130 other.sca_.i, std::move(sp));
133 case json::kind::uint64:
135 other.sca_.u, std::move(sp));
138 case json::kind::double_:
140 other.sca_.d, std::move(sp));
143 case json::kind::string:
145 std::move(other.str_),
149 case json::kind::array:
151 std::move(other.arr_),
155 case json::kind::object:
157 std::move(other.obj_),
163 //----------------------------------------------------------
167 //----------------------------------------------------------
171 std::initializer_list<value_ref> init,
174 if(value_ref::maybe_object(init))
176 value_ref::make_object(
177 init, std::move(sp)));
180 value_ref::make_array(
181 init, std::move(sp)));
184 //----------------------------------------------------------
188 //----------------------------------------------------------
192 operator=(value const& other)
195 storage()).swap(*this);
201 operator=(value&& other)
203 value(std::move(other),
204 storage()).swap(*this);
211 std::initializer_list<value_ref> init)
214 storage()).swap(*this);
220 operator=(string_view s)
222 value(s, storage()).swap(*this);
228 operator=(char const* s)
230 value(s, storage()).swap(*this);
236 operator=(string const& str)
238 value(str, storage()).swap(*this);
244 operator=(string&& str)
246 value(std::move(str),
247 storage()).swap(*this);
253 operator=(array const& arr)
255 value(arr, storage()).swap(*this);
261 operator=(array&& arr)
263 value(std::move(arr),
264 storage()).swap(*this);
270 operator=(object const& obj)
272 value(obj, storage()).swap(*this);
278 operator=(object&& obj)
280 value(std::move(obj),
281 storage()).swap(*this);
285 //----------------------------------------------------------
289 //----------------------------------------------------------
293 emplace_string() noexcept
295 return *::new(&str_) string(destroy());
300 emplace_array() noexcept
302 return *::new(&arr_) array(destroy());
307 emplace_object() noexcept
309 return *::new(&obj_) object(destroy());
316 if(*storage() == *other.storage())
326 relocate(&u.tmp, *this);
327 relocate(this, other);
328 relocate(&other, u.tmp);
340 ::new(&other) value(pilfer(temp1));
342 ::new(this) value(pilfer(temp2));
345 //----------------------------------------------------------
349 //----------------------------------------------------------
357 case json::kind::null:
358 case json::kind::bool_:
359 case json::kind::int64:
360 case json::kind::uint64:
361 case json::kind::double_:
364 case json::kind::string:
366 auto sp = str_.storage();
371 case json::kind::array:
373 auto sp = arr_.storage();
378 case json::kind::object:
380 auto sp = obj_.storage();
386 return std::move(sp_);
391 equal(value const& other) const noexcept
395 default: // unreachable()?
396 case json::kind::null:
397 return other.kind() == json::kind::null;
399 case json::kind::bool_:
401 other.kind() == json::kind::bool_ &&
402 get_bool() == other.get_bool();
404 case json::kind::int64:
407 case json::kind::int64:
408 return get_int64() == other.get_int64();
409 case json::kind::uint64:
412 return static_cast<std::uint64_t>(
413 get_int64()) == other.get_uint64();
418 case json::kind::uint64:
421 case json::kind::uint64:
422 return get_uint64() == other.get_uint64();
423 case json::kind::int64:
424 if(other.get_int64() < 0)
426 return static_cast<std::uint64_t>(
427 other.get_int64()) == get_uint64();
432 case json::kind::double_:
434 other.kind() == json::kind::double_ &&
435 get_double() == other.get_double();
437 case json::kind::string:
439 other.kind() == json::kind::string &&
440 get_string() == other.get_string();
442 case json::kind::array:
444 other.kind() == json::kind::array &&
445 get_array() == other.get_array();
447 case json::kind::object:
449 other.kind() == json::kind::object &&
450 get_object() == other.get_object();
454 //----------------------------------------------------------
458 //----------------------------------------------------------
460 // empty keys point here
461 BOOST_JSON_REQUIRE_CONST_INIT
463 key_value_pair::empty_[1] = { 0 };
467 pilfered<json::value> key,
468 pilfered<json::value> value) noexcept
472 key_ = access::release_key(key.get(), len);
473 len_ = static_cast<std::uint32_t>(len);
478 key_value_pair const& other,
480 : value_(other.value_, std::move(sp))
482 auto p = reinterpret_cast<
483 char*>(value_.storage()->
484 allocate(other.len_ + 1,
487 p, other.key_, other.len_);
493 //----------------------------------------------------------