add swap function for binary type

This commit is contained in:
Niels Lohmann 2020-05-04 22:07:36 +02:00
parent a50a14088c
commit 48aa441661
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
2 changed files with 68 additions and 2 deletions

View file

@ -876,7 +876,6 @@ class basic_json
@since version 3.8.0 @since version 3.8.0
*/ */
using binary_t = BinaryType; using binary_t = BinaryType;
/*! /*!
@ -1204,6 +1203,7 @@ class basic_json
assert(m_type != value_t::object or m_value.object != nullptr); assert(m_type != value_t::object or m_value.object != nullptr);
assert(m_type != value_t::array or m_value.array != nullptr); assert(m_type != value_t::array or m_value.array != nullptr);
assert(m_type != value_t::string or m_value.string != nullptr); assert(m_type != value_t::string or m_value.string != nullptr);
assert(m_type != value_t::binary or m_value.binary != nullptr);
} }
public: public:
@ -6015,6 +6015,39 @@ class basic_json
} }
} }
/*!
@brief exchanges the values
Exchanges the contents of a JSON string with those of @a other. Does not
invoke any move, copy, or swap operations on individual elements. All
iterators and references remain valid. The past-the-end iterator is
invalidated.
@param[in,out] other binary to exchange the contents with
@throw type_error.310 when JSON value is not a string; example: `"cannot
use swap() with boolean"`
@complexity Constant.
@liveexample{The example below shows how strings can be swapped with
`swap()`.,swap__binary_t}
@since version 3.8.0
*/
void swap(binary_t& other)
{
// swap only works for strings
if (JSON_HEDLEY_LIKELY(is_binary()))
{
std::swap(*(m_value.binary), other);
}
else
{
JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name())));
}
}
/// @} /// @}
public: public:

View file

@ -16383,7 +16383,6 @@ class basic_json
@since version 3.8.0 @since version 3.8.0
*/ */
using binary_t = BinaryType; using binary_t = BinaryType;
/*! /*!
@ -16711,6 +16710,7 @@ class basic_json
assert(m_type != value_t::object or m_value.object != nullptr); assert(m_type != value_t::object or m_value.object != nullptr);
assert(m_type != value_t::array or m_value.array != nullptr); assert(m_type != value_t::array or m_value.array != nullptr);
assert(m_type != value_t::string or m_value.string != nullptr); assert(m_type != value_t::string or m_value.string != nullptr);
assert(m_type != value_t::binary or m_value.binary != nullptr);
} }
public: public:
@ -21522,6 +21522,39 @@ class basic_json
} }
} }
/*!
@brief exchanges the values
Exchanges the contents of a JSON string with those of @a other. Does not
invoke any move, copy, or swap operations on individual elements. All
iterators and references remain valid. The past-the-end iterator is
invalidated.
@param[in,out] other binary to exchange the contents with
@throw type_error.310 when JSON value is not a string; example: `"cannot
use swap() with boolean"`
@complexity Constant.
@liveexample{The example below shows how strings can be swapped with
`swap()`.,swap__binary_t}
@since version 3.8.0
*/
void swap(binary_t& other)
{
// swap only works for strings
if (JSON_HEDLEY_LIKELY(is_binary()))
{
std::swap(*(m_value.binary), other);
}
else
{
JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name())));
}
}
/// @} /// @}
public: public: