remove overload function, change default_value to rvalue

This commit is contained in:
chenguoping 2020-06-17 15:05:28 +08:00
parent a3df26b771
commit 8aaa4013a3
2 changed files with 12 additions and 60 deletions

View file

@ -3770,7 +3770,7 @@ class basic_json
template<class ValueType, typename std::enable_if< template<class ValueType, typename std::enable_if<
std::is_convertible<basic_json_t, ValueType>::value std::is_convertible<basic_json_t, ValueType>::value
and not std::is_same<value_t, ValueType>::value, int>::type = 0> and not std::is_same<value_t, ValueType>::value, int>::type = 0>
ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const ValueType value(const typename object_t::key_type& key, ValueType && default_value) const
{ {
// at only works for objects // at only works for objects
if (JSON_HEDLEY_LIKELY(is_object())) if (JSON_HEDLEY_LIKELY(is_object()))
@ -3782,36 +3782,12 @@ class basic_json
return *it; return *it;
} }
return default_value; return std::move(default_value);
} }
JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()))); JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
} }
/*!
@brief overload for a default value of type rvalue
@copydoc basic_json::value(const typename object_t::key_type&, const ValueType&) const
*/
template<class ValueType, typename std::enable_if<
std::is_convertible<basic_json_t, detail::uncvref_t<ValueType>>::value
and not std::is_same<value_t, ValueType>::value, int>::type = 0>
detail::uncvref_t<ValueType> value(const typename object_t::key_type& key, ValueType && default_value) &&
{
// only works for objects
if (JSON_HEDLEY_LIKELY(is_object()))
{
// if key is found, return value and given default value otherwise
const auto it = find(key);
if (it != end())
{
return std::move(it->template get_ref<ValueType&>());
}
return std::forward<ValueType>(default_value);
}
JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
}
/*! /*!
@brief overload for a default value of type const char* @brief overload for a default value of type const char*
@ -3819,7 +3795,7 @@ class basic_json
*/ */
string_t value(const typename object_t::key_type& key, const char* default_value) const string_t value(const typename object_t::key_type& key, const char* default_value) const
{ {
return value(key, string_t(default_value)); return value(key, std::move(string_t(default_value)));
} }
/*! /*!
@ -3867,7 +3843,7 @@ class basic_json
*/ */
template<class ValueType, typename std::enable_if< template<class ValueType, typename std::enable_if<
std::is_convertible<basic_json_t, ValueType>::value, int>::type = 0> std::is_convertible<basic_json_t, ValueType>::value, int>::type = 0>
ValueType value(const json_pointer& ptr, const ValueType& default_value) const ValueType value(const json_pointer& ptr, ValueType && default_value) const
{ {
// at only works for objects // at only works for objects
if (JSON_HEDLEY_LIKELY(is_object())) if (JSON_HEDLEY_LIKELY(is_object()))
@ -3879,7 +3855,7 @@ class basic_json
} }
JSON_INTERNAL_CATCH (out_of_range&) JSON_INTERNAL_CATCH (out_of_range&)
{ {
return default_value; return std::move(default_value);
} }
} }
@ -3893,7 +3869,7 @@ class basic_json
JSON_HEDLEY_NON_NULL(3) JSON_HEDLEY_NON_NULL(3)
string_t value(const json_pointer& ptr, const char* default_value) const string_t value(const json_pointer& ptr, const char* default_value) const
{ {
return value(ptr, string_t(default_value)); return value(ptr, std::move(string_t(default_value)));
} }
/*! /*!

View file

@ -19548,7 +19548,7 @@ class basic_json
template<class ValueType, typename std::enable_if< template<class ValueType, typename std::enable_if<
std::is_convertible<basic_json_t, ValueType>::value std::is_convertible<basic_json_t, ValueType>::value
and not std::is_same<value_t, ValueType>::value, int>::type = 0> and not std::is_same<value_t, ValueType>::value, int>::type = 0>
ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const ValueType value(const typename object_t::key_type& key, ValueType && default_value) const
{ {
// at only works for objects // at only works for objects
if (JSON_HEDLEY_LIKELY(is_object())) if (JSON_HEDLEY_LIKELY(is_object()))
@ -19560,36 +19560,12 @@ class basic_json
return *it; return *it;
} }
return default_value; return std::move(default_value);
} }
JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()))); JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
} }
/*!
@brief overload for a default value of type rvalue
@copydoc basic_json::value(const typename object_t::key_type&, const ValueType&) const
*/
template<class ValueType, typename std::enable_if<
std::is_convertible<basic_json_t, detail::uncvref_t<ValueType>>::value
and not std::is_same<value_t, ValueType>::value, int>::type = 0>
detail::uncvref_t<ValueType> value(const typename object_t::key_type& key, ValueType && default_value) &&
{
// only works for objects
if (JSON_HEDLEY_LIKELY(is_object()))
{
// if key is found, return value and given default value otherwise
const auto it = find(key);
if (it != end())
{
return std::move(it->template get_ref<ValueType&>());
}
return std::forward<ValueType>(default_value);
}
JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
}
/*! /*!
@brief overload for a default value of type const char* @brief overload for a default value of type const char*
@ -19597,7 +19573,7 @@ class basic_json
*/ */
string_t value(const typename object_t::key_type& key, const char* default_value) const string_t value(const typename object_t::key_type& key, const char* default_value) const
{ {
return value(key, string_t(default_value)); return value(key, std::move(string_t(default_value)));
} }
/*! /*!
@ -19645,7 +19621,7 @@ class basic_json
*/ */
template<class ValueType, typename std::enable_if< template<class ValueType, typename std::enable_if<
std::is_convertible<basic_json_t, ValueType>::value, int>::type = 0> std::is_convertible<basic_json_t, ValueType>::value, int>::type = 0>
ValueType value(const json_pointer& ptr, const ValueType& default_value) const ValueType value(const json_pointer& ptr, ValueType && default_value) const
{ {
// at only works for objects // at only works for objects
if (JSON_HEDLEY_LIKELY(is_object())) if (JSON_HEDLEY_LIKELY(is_object()))
@ -19657,7 +19633,7 @@ class basic_json
} }
JSON_INTERNAL_CATCH (out_of_range&) JSON_INTERNAL_CATCH (out_of_range&)
{ {
return default_value; return std::move(default_value);
} }
} }
@ -19671,7 +19647,7 @@ class basic_json
JSON_HEDLEY_NON_NULL(3) JSON_HEDLEY_NON_NULL(3)
string_t value(const json_pointer& ptr, const char* default_value) const string_t value(const json_pointer& ptr, const char* default_value) const
{ {
return value(ptr, string_t(default_value)); return value(ptr, std::move(string_t(default_value)));
} }
/*! /*!