minor changes

This commit is contained in:
Niels 2015-02-24 23:19:19 +01:00
parent ec42245951
commit 63c70fa07e
3 changed files with 145 additions and 56 deletions

View file

@ -274,6 +274,7 @@ class basic_json
{ {
return true; return true;
} }
default: default:
{ {
return false; return false;
@ -291,6 +292,7 @@ class basic_json
{ {
return true; return true;
} }
default: default:
{ {
return false; return false;
@ -317,7 +319,12 @@ class basic_json
// constructors // // constructors //
////////////////// //////////////////
/// create an empty value with a given type /*!
@brief create an empty value with a given type
@param value the type to create an value of
@exception std::bad_alloc if allocation for object, array, or string fails.
*/
inline basic_json(const value_t value) inline basic_json(const value_t value)
: m_type(value) : m_type(value)
{ {
@ -578,6 +585,9 @@ class basic_json
/*! /*!
@brief copy constructor @brief copy constructor
@exception std::bad_alloc if allocation for object, array, or string fails.
@ingroup container @ingroup container
*/ */
inline basic_json(const basic_json& other) inline basic_json(const basic_json& other)
@ -589,6 +599,7 @@ class basic_json
{ {
break; break;
} }
case (value_t::object): case (value_t::object):
{ {
Allocator<object_t> alloc; Allocator<object_t> alloc;
@ -596,6 +607,7 @@ class basic_json
alloc.construct(m_value.object, *other.m_value.object); alloc.construct(m_value.object, *other.m_value.object);
break; break;
} }
case (value_t::array): case (value_t::array):
{ {
Allocator<array_t> alloc; Allocator<array_t> alloc;
@ -603,6 +615,7 @@ class basic_json
alloc.construct(m_value.array, *other.m_value.array); alloc.construct(m_value.array, *other.m_value.array);
break; break;
} }
case (value_t::string): case (value_t::string):
{ {
Allocator<string_t> alloc; Allocator<string_t> alloc;
@ -610,16 +623,19 @@ class basic_json
alloc.construct(m_value.string, *other.m_value.string); alloc.construct(m_value.string, *other.m_value.string);
break; break;
} }
case (value_t::boolean): case (value_t::boolean):
{ {
m_value.boolean = other.m_value.boolean; m_value.boolean = other.m_value.boolean;
break; break;
} }
case (value_t::number_integer): case (value_t::number_integer):
{ {
m_value.number_integer = other.m_value.number_integer; m_value.number_integer = other.m_value.number_integer;
break; break;
} }
case (value_t::number_float): case (value_t::number_float):
{ {
m_value.number_float = other.m_value.number_float; m_value.number_float = other.m_value.number_float;
@ -706,8 +722,9 @@ class basic_json
/*! /*!
@brief serialization @brief serialization
Serialization function for JSON objects. The function tries to mimick Python's Serialization function for JSON objects. The function tries to mimick
@p json.dumps() function, and currently supports its @p indent parameter. Python's @p json.dumps() function, and currently supports its @p indent
parameter.
@param indent sif indent is nonnegative, then array elements and object @param indent sif indent is nonnegative, then array elements and object
members will be pretty-printed with that indent level. An indent level of 0 members will be pretty-printed with that indent level. An indent level of 0
@ -771,7 +788,7 @@ class basic_json
} }
/// return the type of the object (implicit) /// return the type of the object (implicit)
operator value_t() const noexcept inline operator value_t() const noexcept
{ {
return m_type; return m_type;
} }
@ -1487,7 +1504,7 @@ class basic_json
@brief comparison: equal @brief comparison: equal
@ingroup container @ingroup container
*/ */
friend bool operator==(const_reference lhs, const_reference rhs) friend bool operator==(const_reference lhs, const_reference rhs) noexcept
{ {
switch (lhs.type()) switch (lhs.type())
{ {
@ -1564,13 +1581,13 @@ class basic_json
@brief comparison: not equal @brief comparison: not equal
@ingroup container @ingroup container
*/ */
friend bool operator!=(const_reference lhs, const_reference rhs) friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
{ {
return not (lhs == rhs); return not (lhs == rhs);
} }
/// comparison: less than /// comparison: less than
friend bool operator<(const_reference lhs, const_reference rhs) friend bool operator<(const_reference lhs, const_reference rhs) noexcept
{ {
switch (lhs.type()) switch (lhs.type())
{ {
@ -1646,19 +1663,19 @@ class basic_json
} }
/// comparison: less than or equal /// comparison: less than or equal
friend bool operator<=(const_reference lhs, const_reference rhs) friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
{ {
return not (rhs < lhs); return not (rhs < lhs);
} }
/// comparison: greater than /// comparison: greater than
friend bool operator>(const_reference lhs, const_reference rhs) friend bool operator>(const_reference lhs, const_reference rhs) noexcept
{ {
return not (lhs <= rhs); return not (lhs <= rhs);
} }
/// comparison: greater than or equal /// comparison: greater than or equal
friend bool operator>=(const_reference lhs, const_reference rhs) friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
{ {
return not (lhs < rhs); return not (lhs < rhs);
} }
@ -1783,36 +1800,42 @@ class basic_json
result += "\\\""; result += "\\\"";
break; break;
} }
// reverse solidus (0x5c) // reverse solidus (0x5c)
case '\\': case '\\':
{ {
result += "\\\\"; result += "\\\\";
break; break;
} }
// backspace (0x08) // backspace (0x08)
case '\b': case '\b':
{ {
result += "\\b"; result += "\\b";
break; break;
} }
// formfeed (0x0c) // formfeed (0x0c)
case '\f': case '\f':
{ {
result += "\\f"; result += "\\f";
break; break;
} }
// newline (0x0a) // newline (0x0a)
case '\n': case '\n':
{ {
result += "\\n"; result += "\\n";
break; break;
} }
// carriage return (0x0d) // carriage return (0x0d)
case '\r': case '\r':
{ {
result += "\\r"; result += "\\r";
break; break;
} }
// horizontal tab (0x09) // horizontal tab (0x09)
case '\t': case '\t':
{ {
@ -2027,7 +2050,7 @@ class basic_json
inline iterator() = default; inline iterator() = default;
/// constructor for a given JSON instance /// constructor for a given JSON instance
inline iterator(pointer object) : m_object(object) inline iterator(pointer object) noexcept : m_object(object)
{ {
switch (m_object->m_type) switch (m_object->m_type)
{ {
@ -2505,7 +2528,7 @@ class basic_json
inline const_iterator() = default; inline const_iterator() = default;
/// constructor for a given JSON instance /// constructor for a given JSON instance
inline const_iterator(pointer object) : m_object(object) inline const_iterator(pointer object) noexcept : m_object(object)
{ {
switch (m_object->m_type) switch (m_object->m_type)
{ {
@ -2528,7 +2551,7 @@ class basic_json
} }
/// copy constructor given a nonconst iterator /// copy constructor given a nonconst iterator
inline const_iterator(const iterator& other) : m_object(other.m_object) inline const_iterator(const iterator& other) noexcept : m_object(other.m_object)
{ {
switch (m_object->m_type) switch (m_object->m_type)
{ {
@ -4268,7 +4291,6 @@ basic_json_parser_59:
/// default JSON class /// default JSON class
using json = basic_json<>; using json = basic_json<>;
} }

View file

@ -274,6 +274,7 @@ class basic_json
{ {
return true; return true;
} }
default: default:
{ {
return false; return false;
@ -291,6 +292,7 @@ class basic_json
{ {
return true; return true;
} }
default: default:
{ {
return false; return false;
@ -317,7 +319,12 @@ class basic_json
// constructors // // constructors //
////////////////// //////////////////
/// create an empty value with a given type /*!
@brief create an empty value with a given type
@param value the type to create an value of
@exception std::bad_alloc if allocation for object, array, or string fails.
*/
inline basic_json(const value_t value) inline basic_json(const value_t value)
: m_type(value) : m_type(value)
{ {
@ -578,6 +585,9 @@ class basic_json
/*! /*!
@brief copy constructor @brief copy constructor
@exception std::bad_alloc if allocation for object, array, or string fails.
@ingroup container @ingroup container
*/ */
inline basic_json(const basic_json& other) inline basic_json(const basic_json& other)
@ -589,6 +599,7 @@ class basic_json
{ {
break; break;
} }
case (value_t::object): case (value_t::object):
{ {
Allocator<object_t> alloc; Allocator<object_t> alloc;
@ -596,6 +607,7 @@ class basic_json
alloc.construct(m_value.object, *other.m_value.object); alloc.construct(m_value.object, *other.m_value.object);
break; break;
} }
case (value_t::array): case (value_t::array):
{ {
Allocator<array_t> alloc; Allocator<array_t> alloc;
@ -603,6 +615,7 @@ class basic_json
alloc.construct(m_value.array, *other.m_value.array); alloc.construct(m_value.array, *other.m_value.array);
break; break;
} }
case (value_t::string): case (value_t::string):
{ {
Allocator<string_t> alloc; Allocator<string_t> alloc;
@ -610,16 +623,19 @@ class basic_json
alloc.construct(m_value.string, *other.m_value.string); alloc.construct(m_value.string, *other.m_value.string);
break; break;
} }
case (value_t::boolean): case (value_t::boolean):
{ {
m_value.boolean = other.m_value.boolean; m_value.boolean = other.m_value.boolean;
break; break;
} }
case (value_t::number_integer): case (value_t::number_integer):
{ {
m_value.number_integer = other.m_value.number_integer; m_value.number_integer = other.m_value.number_integer;
break; break;
} }
case (value_t::number_float): case (value_t::number_float):
{ {
m_value.number_float = other.m_value.number_float; m_value.number_float = other.m_value.number_float;
@ -706,8 +722,9 @@ class basic_json
/*! /*!
@brief serialization @brief serialization
Serialization function for JSON objects. The function tries to mimick Python's Serialization function for JSON objects. The function tries to mimick
@p json.dumps() function, and currently supports its @p indent parameter. Python's @p json.dumps() function, and currently supports its @p indent
parameter.
@param indent sif indent is nonnegative, then array elements and object @param indent sif indent is nonnegative, then array elements and object
members will be pretty-printed with that indent level. An indent level of 0 members will be pretty-printed with that indent level. An indent level of 0
@ -771,7 +788,7 @@ class basic_json
} }
/// return the type of the object (implicit) /// return the type of the object (implicit)
operator value_t() const noexcept inline operator value_t() const noexcept
{ {
return m_type; return m_type;
} }
@ -1487,7 +1504,7 @@ class basic_json
@brief comparison: equal @brief comparison: equal
@ingroup container @ingroup container
*/ */
friend bool operator==(const_reference lhs, const_reference rhs) friend bool operator==(const_reference lhs, const_reference rhs) noexcept
{ {
switch (lhs.type()) switch (lhs.type())
{ {
@ -1564,13 +1581,13 @@ class basic_json
@brief comparison: not equal @brief comparison: not equal
@ingroup container @ingroup container
*/ */
friend bool operator!=(const_reference lhs, const_reference rhs) friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
{ {
return not (lhs == rhs); return not (lhs == rhs);
} }
/// comparison: less than /// comparison: less than
friend bool operator<(const_reference lhs, const_reference rhs) friend bool operator<(const_reference lhs, const_reference rhs) noexcept
{ {
switch (lhs.type()) switch (lhs.type())
{ {
@ -1646,19 +1663,19 @@ class basic_json
} }
/// comparison: less than or equal /// comparison: less than or equal
friend bool operator<=(const_reference lhs, const_reference rhs) friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
{ {
return not (rhs < lhs); return not (rhs < lhs);
} }
/// comparison: greater than /// comparison: greater than
friend bool operator>(const_reference lhs, const_reference rhs) friend bool operator>(const_reference lhs, const_reference rhs) noexcept
{ {
return not (lhs <= rhs); return not (lhs <= rhs);
} }
/// comparison: greater than or equal /// comparison: greater than or equal
friend bool operator>=(const_reference lhs, const_reference rhs) friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
{ {
return not (lhs < rhs); return not (lhs < rhs);
} }
@ -1783,36 +1800,42 @@ class basic_json
result += "\\\""; result += "\\\"";
break; break;
} }
// reverse solidus (0x5c) // reverse solidus (0x5c)
case '\\': case '\\':
{ {
result += "\\\\"; result += "\\\\";
break; break;
} }
// backspace (0x08) // backspace (0x08)
case '\b': case '\b':
{ {
result += "\\b"; result += "\\b";
break; break;
} }
// formfeed (0x0c) // formfeed (0x0c)
case '\f': case '\f':
{ {
result += "\\f"; result += "\\f";
break; break;
} }
// newline (0x0a) // newline (0x0a)
case '\n': case '\n':
{ {
result += "\\n"; result += "\\n";
break; break;
} }
// carriage return (0x0d) // carriage return (0x0d)
case '\r': case '\r':
{ {
result += "\\r"; result += "\\r";
break; break;
} }
// horizontal tab (0x09) // horizontal tab (0x09)
case '\t': case '\t':
{ {
@ -2027,7 +2050,7 @@ class basic_json
inline iterator() = default; inline iterator() = default;
/// constructor for a given JSON instance /// constructor for a given JSON instance
inline iterator(pointer object) : m_object(object) inline iterator(pointer object) noexcept : m_object(object)
{ {
switch (m_object->m_type) switch (m_object->m_type)
{ {
@ -2505,7 +2528,7 @@ class basic_json
inline const_iterator() = default; inline const_iterator() = default;
/// constructor for a given JSON instance /// constructor for a given JSON instance
inline const_iterator(pointer object) : m_object(object) inline const_iterator(pointer object) noexcept : m_object(object)
{ {
switch (m_object->m_type) switch (m_object->m_type)
{ {
@ -2528,7 +2551,7 @@ class basic_json
} }
/// copy constructor given a nonconst iterator /// copy constructor given a nonconst iterator
inline const_iterator(const iterator& other) : m_object(other.m_object) inline const_iterator(const iterator& other) noexcept : m_object(other.m_object)
{ {
switch (m_object->m_type) switch (m_object->m_type)
{ {
@ -3044,7 +3067,8 @@ class basic_json
@see <http://en.wikipedia.org/wiki/UTF-8#Sample_code> @see <http://en.wikipedia.org/wiki/UTF-8#Sample_code>
*/ */
inline static string_t to_unicode(const size_t codepoint1, const size_t codepoint2 = 0) inline static string_t to_unicode(const size_t codepoint1,
const size_t codepoint2 = 0)
{ {
string_t result; string_t result;
@ -3072,7 +3096,7 @@ class basic_json
if (codepoint <= 0x7f) if (codepoint <= 0x7f)
{ {
// 1-byte characters: 0xxxxxxx (ASCI) // 1-byte characters: 0xxxxxxx (ASCII)
result.append(1, static_cast<typename string_t::value_type>(codepoint)); result.append(1, static_cast<typename string_t::value_type>(codepoint));
} }
else if (codepoint <= 0x7ff) else if (codepoint <= 0x7ff)
@ -3617,7 +3641,6 @@ class basic_json
/// default JSON class /// default JSON class
using json = basic_json<>; using json = basic_json<>;
} }

View file

@ -6796,6 +6796,8 @@ TEST_CASE("algorithms")
} }
TEST_CASE("concepts") TEST_CASE("concepts")
{
SECTION("class json")
{ {
SECTION("DefaultConstructible") SECTION("DefaultConstructible")
{ {
@ -6832,3 +6834,45 @@ TEST_CASE("concepts")
CHECK(std::is_standard_layout<json>::value); CHECK(std::is_standard_layout<json>::value);
} }
} }
SECTION("class iterator")
{
SECTION("CopyConstructible")
{
CHECK(std::is_nothrow_copy_constructible<json::iterator>::value);
CHECK(std::is_nothrow_copy_constructible<json::const_iterator>::value);
}
SECTION("CopyAssignable")
{
CHECK(std::is_nothrow_copy_assignable<json::iterator>::value);
CHECK(std::is_nothrow_copy_assignable<json::const_iterator>::value);
}
SECTION("Destructible")
{
CHECK(std::is_nothrow_destructible<json::iterator>::value);
CHECK(std::is_nothrow_destructible<json::const_iterator>::value);
}
SECTION("Swappable")
{
{
json j {1, 2, 3};
json::iterator it1 = j.begin();
json::iterator it2 = j.end();
std::swap(it1, it2);
CHECK(it1 == j.end());
CHECK(it2 == j.begin());
}
{
json j {1, 2, 3};
json::const_iterator it1 = j.cbegin();
json::const_iterator it2 = j.cend();
std::swap(it1, it2);
CHECK(it1 == j.end());
CHECK(it2 == j.begin());
}
}
}
}