💄 some cleanup

This commit is contained in:
Niels Lohmann 2017-01-25 22:21:07 +01:00
parent 94d9b7bc86
commit 4d3053cd42
2 changed files with 50 additions and 38 deletions

View file

@ -204,14 +204,14 @@ using enable_if_t = typename std::enable_if<B, T>::type;
template<typename T> template<typename T>
using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type; using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
// Taken from http://stackoverflow.com/questions/26936640/how-to-implement-is-enum-class-type-trait // taken from http://stackoverflow.com/a/26936864/266378
template<typename T> template<typename T>
using is_unscoped_enum = using is_unscoped_enum =
std::integral_constant<bool, std::is_convertible<T, int>::value and std::integral_constant<bool, std::is_convertible<T, int>::value and
std::is_enum<T>::value>; std::is_enum<T>::value>;
/* /*
Implementation of 2 C++17 constructs: conjunction, negation. This is needed Implementation of two C++17 constructs: conjunction, negation. This is needed
to avoid evaluating all the traits in a condition to avoid evaluating all the traits in a condition
For example: not std::is_same<void, T>::value and has_value_type<T>::value For example: not std::is_same<void, T>::value and has_value_type<T>::value
@ -393,6 +393,7 @@ NLOHMANN_JSON_HAS_HELPER(iterator);
#undef NLOHMANN_JSON_HAS_HELPER #undef NLOHMANN_JSON_HAS_HELPER
template<bool B, class RealType, class CompatibleObjectType> template<bool B, class RealType, class CompatibleObjectType>
struct is_compatible_object_type_impl : std::false_type {}; struct is_compatible_object_type_impl : std::false_type {};
@ -468,7 +469,7 @@ struct is_compatible_integer_type
}; };
// This trait checks if JSONSerializer<T>::from_json(json const&, udt&) exists // trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
template<typename BasicJsonType, typename T> template<typename BasicJsonType, typename T>
struct has_from_json struct has_from_json
{ {
@ -530,8 +531,7 @@ void to_json(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
template<typename BasicJsonType, typename CompatibleString, template<typename BasicJsonType, typename CompatibleString,
enable_if_t<std::is_constructible<typename BasicJsonType::string_t, enable_if_t<std::is_constructible<typename BasicJsonType::string_t,
CompatibleString>::value, CompatibleString>::value, int> = 0>
int> = 0>
void to_json(BasicJsonType& j, const CompatibleString& s) void to_json(BasicJsonType& j, const CompatibleString& s)
{ {
external_constructor<value_t::string>::construct(j, s); external_constructor<value_t::string>::construct(j, s);
@ -547,8 +547,7 @@ void to_json(BasicJsonType& j, FloatType val) noexcept
template < template <
typename BasicJsonType, typename CompatibleNumberUnsignedType, typename BasicJsonType, typename CompatibleNumberUnsignedType,
enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t,
CompatibleNumberUnsignedType>::value, CompatibleNumberUnsignedType>::value, int> = 0 >
int> = 0 >
void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
{ {
external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val)); external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
@ -557,8 +556,7 @@ void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
template < template <
typename BasicJsonType, typename CompatibleNumberIntegerType, typename BasicJsonType, typename CompatibleNumberIntegerType,
enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t,
CompatibleNumberIntegerType>::value, CompatibleNumberIntegerType>::value, int> = 0 >
int> = 0 >
void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
{ {
external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val)); external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
@ -690,7 +688,6 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr)
} }
// forward_list doesn't have an insert method // forward_list doesn't have an insert method
// TODO find a way to avoid including forward_list
template<typename BasicJsonType, typename T, typename Allocator> template<typename BasicJsonType, typename T, typename Allocator>
void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l) void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
{ {
@ -781,14 +778,15 @@ void from_json(const BasicJsonType& j, CompatibleObjectType& obj)
using std::begin; using std::begin;
using std::end; using std::end;
// we could avoid the assignment, but this might require a for loop, which // we could avoid the assignment, but this might require a for loop, which
// might be less efficient than the container constructor for some containers (would it?) // might be less efficient than the container constructor for some
// containers (would it?)
obj = CompatibleObjectType(begin(*inner_object), end(*inner_object)); obj = CompatibleObjectType(begin(*inner_object), end(*inner_object));
} }
// overload for arithmetic types, not chosen for basic_json template arguments (BooleanType, etc..) // overload for arithmetic types, not chosen for basic_json template arguments
// // (BooleanType, etc..); note: Is it really necessary to provide explicit
// note: Is it really necessary to provide explicit overloads for boolean_t etc.. // overloads for boolean_t etc. in case of a custom BooleanType which is not
// in case of a custom BooleanType which is not an arithmetic type? // an arithmetic type?
template<typename BasicJsonType, typename ArithmeticType, template<typename BasicJsonType, typename ArithmeticType,
enable_if_t < enable_if_t <
std::is_arithmetic<ArithmeticType>::value and std::is_arithmetic<ArithmeticType>::value and
@ -802,20 +800,30 @@ void from_json(const BasicJsonType& j, ArithmeticType& val)
switch (static_cast<value_t>(j)) switch (static_cast<value_t>(j))
{ {
case value_t::number_unsigned: case value_t::number_unsigned:
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>()); val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
break; break;
}
case value_t::number_integer: case value_t::number_integer:
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>()); val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
break; break;
}
case value_t::number_float: case value_t::number_float:
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>()); val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
break; break;
}
case value_t::boolean: case value_t::boolean:
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>()); val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
break; break;
}
default: default:
{
JSON_THROW(std::domain_error("type must be number, but is " + j.type_name())); JSON_THROW(std::domain_error("type must be number, but is " + j.type_name()));
} }
}
} }
struct to_json_fn struct to_json_fn
@ -3160,8 +3168,7 @@ class basic_json
detail::enable_if_t < detail::enable_if_t <
not std::is_same<basic_json_t, U>::value and not std::is_same<basic_json_t, U>::value and
detail::has_from_json<basic_json_t, U>::value and detail::has_from_json<basic_json_t, U>::value and
not detail::has_non_default_from_json<basic_json_t, not detail::has_non_default_from_json<basic_json_t, U>::value,
U>::value,
int > = 0 > int > = 0 >
U get() const noexcept(noexcept(JSONSerializer<U>::from_json( U get() const noexcept(noexcept(JSONSerializer<U>::from_json(
std::declval<const basic_json_t&>(), std::declval<U&>()))) std::declval<const basic_json_t&>(), std::declval<U&>())))
@ -3196,8 +3203,7 @@ class basic_json
typename U = detail::uncvref_t<T>, typename U = detail::uncvref_t<T>,
detail::enable_if_t<not std::is_same<basic_json_t, U>::value and detail::enable_if_t<not std::is_same<basic_json_t, U>::value and
detail::has_non_default_from_json<basic_json_t, detail::has_non_default_from_json<basic_json_t,
U>::value, U>::value, int> = 0 >
int> = 0 >
U get() const noexcept(noexcept(JSONSerializer<T>::from_json(std::declval<const basic_json_t&>()))) U get() const noexcept(noexcept(JSONSerializer<T>::from_json(std::declval<const basic_json_t&>())))
{ {
static_assert(not std::is_reference<T>::value, "get cannot be used with reference types, you might want to use get_ref"); static_assert(not std::is_reference<T>::value, "get cannot be used with reference types, you might want to use get_ref");

View file

@ -204,14 +204,14 @@ using enable_if_t = typename std::enable_if<B, T>::type;
template<typename T> template<typename T>
using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type; using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
// Taken from http://stackoverflow.com/questions/26936640/how-to-implement-is-enum-class-type-trait // taken from http://stackoverflow.com/a/26936864/266378
template<typename T> template<typename T>
using is_unscoped_enum = using is_unscoped_enum =
std::integral_constant<bool, std::is_convertible<T, int>::value and std::integral_constant<bool, std::is_convertible<T, int>::value and
std::is_enum<T>::value>; std::is_enum<T>::value>;
/* /*
Implementation of 2 C++17 constructs: conjunction, negation. This is needed Implementation of two C++17 constructs: conjunction, negation. This is needed
to avoid evaluating all the traits in a condition to avoid evaluating all the traits in a condition
For example: not std::is_same<void, T>::value and has_value_type<T>::value For example: not std::is_same<void, T>::value and has_value_type<T>::value
@ -393,6 +393,7 @@ NLOHMANN_JSON_HAS_HELPER(iterator);
#undef NLOHMANN_JSON_HAS_HELPER #undef NLOHMANN_JSON_HAS_HELPER
template<bool B, class RealType, class CompatibleObjectType> template<bool B, class RealType, class CompatibleObjectType>
struct is_compatible_object_type_impl : std::false_type {}; struct is_compatible_object_type_impl : std::false_type {};
@ -468,7 +469,7 @@ struct is_compatible_integer_type
}; };
// This trait checks if JSONSerializer<T>::from_json(json const&, udt&) exists // trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
template<typename BasicJsonType, typename T> template<typename BasicJsonType, typename T>
struct has_from_json struct has_from_json
{ {
@ -530,8 +531,7 @@ void to_json(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
template<typename BasicJsonType, typename CompatibleString, template<typename BasicJsonType, typename CompatibleString,
enable_if_t<std::is_constructible<typename BasicJsonType::string_t, enable_if_t<std::is_constructible<typename BasicJsonType::string_t,
CompatibleString>::value, CompatibleString>::value, int> = 0>
int> = 0>
void to_json(BasicJsonType& j, const CompatibleString& s) void to_json(BasicJsonType& j, const CompatibleString& s)
{ {
external_constructor<value_t::string>::construct(j, s); external_constructor<value_t::string>::construct(j, s);
@ -547,8 +547,7 @@ void to_json(BasicJsonType& j, FloatType val) noexcept
template < template <
typename BasicJsonType, typename CompatibleNumberUnsignedType, typename BasicJsonType, typename CompatibleNumberUnsignedType,
enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t,
CompatibleNumberUnsignedType>::value, CompatibleNumberUnsignedType>::value, int> = 0 >
int> = 0 >
void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
{ {
external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val)); external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
@ -557,8 +556,7 @@ void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
template < template <
typename BasicJsonType, typename CompatibleNumberIntegerType, typename BasicJsonType, typename CompatibleNumberIntegerType,
enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t,
CompatibleNumberIntegerType>::value, CompatibleNumberIntegerType>::value, int> = 0 >
int> = 0 >
void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
{ {
external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val)); external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
@ -690,7 +688,6 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr)
} }
// forward_list doesn't have an insert method // forward_list doesn't have an insert method
// TODO find a way to avoid including forward_list
template<typename BasicJsonType, typename T, typename Allocator> template<typename BasicJsonType, typename T, typename Allocator>
void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l) void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
{ {
@ -781,14 +778,15 @@ void from_json(const BasicJsonType& j, CompatibleObjectType& obj)
using std::begin; using std::begin;
using std::end; using std::end;
// we could avoid the assignment, but this might require a for loop, which // we could avoid the assignment, but this might require a for loop, which
// might be less efficient than the container constructor for some containers (would it?) // might be less efficient than the container constructor for some
// containers (would it?)
obj = CompatibleObjectType(begin(*inner_object), end(*inner_object)); obj = CompatibleObjectType(begin(*inner_object), end(*inner_object));
} }
// overload for arithmetic types, not chosen for basic_json template arguments (BooleanType, etc..) // overload for arithmetic types, not chosen for basic_json template arguments
// // (BooleanType, etc..); note: Is it really necessary to provide explicit
// note: Is it really necessary to provide explicit overloads for boolean_t etc.. // overloads for boolean_t etc. in case of a custom BooleanType which is not
// in case of a custom BooleanType which is not an arithmetic type? // an arithmetic type?
template<typename BasicJsonType, typename ArithmeticType, template<typename BasicJsonType, typename ArithmeticType,
enable_if_t < enable_if_t <
std::is_arithmetic<ArithmeticType>::value and std::is_arithmetic<ArithmeticType>::value and
@ -802,20 +800,30 @@ void from_json(const BasicJsonType& j, ArithmeticType& val)
switch (static_cast<value_t>(j)) switch (static_cast<value_t>(j))
{ {
case value_t::number_unsigned: case value_t::number_unsigned:
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>()); val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
break; break;
}
case value_t::number_integer: case value_t::number_integer:
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>()); val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
break; break;
}
case value_t::number_float: case value_t::number_float:
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>()); val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
break; break;
}
case value_t::boolean: case value_t::boolean:
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>()); val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
break; break;
}
default: default:
{
JSON_THROW(std::domain_error("type must be number, but is " + j.type_name())); JSON_THROW(std::domain_error("type must be number, but is " + j.type_name()));
} }
}
} }
struct to_json_fn struct to_json_fn
@ -3160,8 +3168,7 @@ class basic_json
detail::enable_if_t < detail::enable_if_t <
not std::is_same<basic_json_t, U>::value and not std::is_same<basic_json_t, U>::value and
detail::has_from_json<basic_json_t, U>::value and detail::has_from_json<basic_json_t, U>::value and
not detail::has_non_default_from_json<basic_json_t, not detail::has_non_default_from_json<basic_json_t, U>::value,
U>::value,
int > = 0 > int > = 0 >
U get() const noexcept(noexcept(JSONSerializer<U>::from_json( U get() const noexcept(noexcept(JSONSerializer<U>::from_json(
std::declval<const basic_json_t&>(), std::declval<U&>()))) std::declval<const basic_json_t&>(), std::declval<U&>())))
@ -3196,8 +3203,7 @@ class basic_json
typename U = detail::uncvref_t<T>, typename U = detail::uncvref_t<T>,
detail::enable_if_t<not std::is_same<basic_json_t, U>::value and detail::enable_if_t<not std::is_same<basic_json_t, U>::value and
detail::has_non_default_from_json<basic_json_t, detail::has_non_default_from_json<basic_json_t,
U>::value, U>::value, int> = 0 >
int> = 0 >
U get() const noexcept(noexcept(JSONSerializer<T>::from_json(std::declval<const basic_json_t&>()))) U get() const noexcept(noexcept(JSONSerializer<T>::from_json(std::declval<const basic_json_t&>())))
{ {
static_assert(not std::is_reference<T>::value, "get cannot be used with reference types, you might want to use get_ref"); static_assert(not std::is_reference<T>::value, "get cannot be used with reference types, you might want to use get_ref");