Skip to content

Commit

Permalink
Merge branch 'feature/nodiscard' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Jan 20, 2019
2 parents 6de4df2 + e36593e commit e89c946
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 3 deletions.
13 changes: 13 additions & 0 deletions include/nlohmann/detail/macro_scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@
#define JSON_DEPRECATED
#endif

// allow for portable nodiscard warnings
#if defined(__has_cpp_attribute)
#if __has_cpp_attribute(nodiscard)
#define JSON_NODISCARD [[nodiscard]]
#elif __has_cpp_attribute(gnu::warn_unused_result)
#define JSON_NODISCARD [[gnu::warn_unused_result]]
#else
#define JSON_NODISCARD
#endif
#else
#define JSON_NODISCARD
#endif

// allow to disable exceptions
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
#define JSON_THROW(exception) throw exception
Expand Down
1 change: 1 addition & 0 deletions include/nlohmann/detail/macro_unscope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#undef JSON_LIKELY
#undef JSON_UNLIKELY
#undef JSON_DEPRECATED
#undef JSON_NODISCARD
#undef JSON_HAS_CPP_14
#undef JSON_HAS_CPP_17
#undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
Expand Down
13 changes: 13 additions & 0 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ class basic_json
@since 2.1.0
*/
JSON_NODISCARD
static basic_json meta()
{
basic_json result;
Expand Down Expand Up @@ -1489,6 +1490,7 @@ class basic_json
@since version 1.0.0
*/
JSON_NODISCARD
static basic_json array(initializer_list_t init = {})
{
return basic_json(init, false, value_t::array);
Expand Down Expand Up @@ -1532,6 +1534,7 @@ class basic_json
@since version 1.0.0
*/
JSON_NODISCARD
static basic_json object(initializer_list_t init = {})
{
return basic_json(init, false, value_t::object);
Expand Down Expand Up @@ -6051,6 +6054,7 @@ class basic_json
@since version 2.0.3 (contiguous containers)
*/
JSON_NODISCARD
static basic_json parse(detail::input_adapter&& i,
const parser_callback_t cb = nullptr,
const bool allow_exceptions = true)
Expand Down Expand Up @@ -6826,6 +6830,7 @@ class basic_json
@a strict parameter since 3.0.0; added @a allow_exceptions parameter
since 3.2.0
*/
JSON_NODISCARD
static basic_json from_cbor(detail::input_adapter&& i,
const bool strict = true,
const bool allow_exceptions = true)
Expand All @@ -6841,6 +6846,7 @@ class basic_json
*/
template<typename A1, typename A2,
detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
JSON_NODISCARD
static basic_json from_cbor(A1 && a1, A2 && a2,
const bool strict = true,
const bool allow_exceptions = true)
Expand Down Expand Up @@ -6933,6 +6939,7 @@ class basic_json
@a strict parameter since 3.0.0; added @a allow_exceptions parameter
since 3.2.0
*/
JSON_NODISCARD
static basic_json from_msgpack(detail::input_adapter&& i,
const bool strict = true,
const bool allow_exceptions = true)
Expand All @@ -6948,6 +6955,7 @@ class basic_json
*/
template<typename A1, typename A2,
detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
JSON_NODISCARD
static basic_json from_msgpack(A1 && a1, A2 && a2,
const bool strict = true,
const bool allow_exceptions = true)
Expand Down Expand Up @@ -7019,6 +7027,7 @@ class basic_json
@since version 3.1.0; added @a allow_exceptions parameter since 3.2.0
*/
JSON_NODISCARD
static basic_json from_ubjson(detail::input_adapter&& i,
const bool strict = true,
const bool allow_exceptions = true)
Expand All @@ -7034,6 +7043,7 @@ class basic_json
*/
template<typename A1, typename A2,
detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
JSON_NODISCARD
static basic_json from_ubjson(A1 && a1, A2 && a2,
const bool strict = true,
const bool allow_exceptions = true)
Expand Down Expand Up @@ -7104,6 +7114,7 @@ class basic_json
@sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the
related UBJSON format
*/
JSON_NODISCARD
static basic_json from_bson(detail::input_adapter&& i,
const bool strict = true,
const bool allow_exceptions = true)
Expand All @@ -7119,6 +7130,7 @@ class basic_json
*/
template<typename A1, typename A2,
detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
JSON_NODISCARD
static basic_json from_bson(A1 && a1, A2 && a2,
const bool strict = true,
const bool allow_exceptions = true)
Expand Down Expand Up @@ -7710,6 +7722,7 @@ class basic_json
@since version 2.0.0
*/
JSON_NODISCARD
static basic_json diff(const basic_json& source, const basic_json& target,
const std::string& path = "")
{
Expand Down
27 changes: 27 additions & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,19 @@ using json = basic_json<>;
#define JSON_DEPRECATED
#endif

// allow for portable nodiscard warnings
#if defined(__has_cpp_attribute)
#if __has_cpp_attribute(nodiscard)
#define JSON_NODISCARD [[nodiscard]]
#elif __has_cpp_attribute(gnu::warn_unused_result)
#define JSON_NODISCARD [[gnu::warn_unused_result]]
#else
#define JSON_NODISCARD
#endif
#else
#define JSON_NODISCARD
#endif

// allow to disable exceptions
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
#define JSON_THROW(exception) throw exception
Expand Down Expand Up @@ -12803,6 +12816,7 @@ class basic_json

@since 2.1.0
*/
JSON_NODISCARD
static basic_json meta()
{
basic_json result;
Expand Down Expand Up @@ -13975,6 +13989,7 @@ class basic_json

@since version 1.0.0
*/
JSON_NODISCARD
static basic_json array(initializer_list_t init = {})
{
return basic_json(init, false, value_t::array);
Expand Down Expand Up @@ -14018,6 +14033,7 @@ class basic_json

@since version 1.0.0
*/
JSON_NODISCARD
static basic_json object(initializer_list_t init = {})
{
return basic_json(init, false, value_t::object);
Expand Down Expand Up @@ -18537,6 +18553,7 @@ class basic_json

@since version 2.0.3 (contiguous containers)
*/
JSON_NODISCARD
static basic_json parse(detail::input_adapter&& i,
const parser_callback_t cb = nullptr,
const bool allow_exceptions = true)
Expand Down Expand Up @@ -19312,6 +19329,7 @@ class basic_json
@a strict parameter since 3.0.0; added @a allow_exceptions parameter
since 3.2.0
*/
JSON_NODISCARD
static basic_json from_cbor(detail::input_adapter&& i,
const bool strict = true,
const bool allow_exceptions = true)
Expand All @@ -19327,6 +19345,7 @@ class basic_json
*/
template<typename A1, typename A2,
detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
JSON_NODISCARD
static basic_json from_cbor(A1 && a1, A2 && a2,
const bool strict = true,
const bool allow_exceptions = true)
Expand Down Expand Up @@ -19419,6 +19438,7 @@ class basic_json
@a strict parameter since 3.0.0; added @a allow_exceptions parameter
since 3.2.0
*/
JSON_NODISCARD
static basic_json from_msgpack(detail::input_adapter&& i,
const bool strict = true,
const bool allow_exceptions = true)
Expand All @@ -19434,6 +19454,7 @@ class basic_json
*/
template<typename A1, typename A2,
detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
JSON_NODISCARD
static basic_json from_msgpack(A1 && a1, A2 && a2,
const bool strict = true,
const bool allow_exceptions = true)
Expand Down Expand Up @@ -19505,6 +19526,7 @@ class basic_json

@since version 3.1.0; added @a allow_exceptions parameter since 3.2.0
*/
JSON_NODISCARD
static basic_json from_ubjson(detail::input_adapter&& i,
const bool strict = true,
const bool allow_exceptions = true)
Expand All @@ -19520,6 +19542,7 @@ class basic_json
*/
template<typename A1, typename A2,
detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
JSON_NODISCARD
static basic_json from_ubjson(A1 && a1, A2 && a2,
const bool strict = true,
const bool allow_exceptions = true)
Expand Down Expand Up @@ -19590,6 +19613,7 @@ class basic_json
@sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the
related UBJSON format
*/
JSON_NODISCARD
static basic_json from_bson(detail::input_adapter&& i,
const bool strict = true,
const bool allow_exceptions = true)
Expand All @@ -19605,6 +19629,7 @@ class basic_json
*/
template<typename A1, typename A2,
detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
JSON_NODISCARD
static basic_json from_bson(A1 && a1, A2 && a2,
const bool strict = true,
const bool allow_exceptions = true)
Expand Down Expand Up @@ -20196,6 +20221,7 @@ class basic_json

@since version 2.0.0
*/
JSON_NODISCARD
static basic_json diff(const basic_json& source, const basic_json& target,
const std::string& path = "")
{
Expand Down Expand Up @@ -20515,6 +20541,7 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std
#undef JSON_LIKELY
#undef JSON_UNLIKELY
#undef JSON_DEPRECATED
#undef JSON_NODISCARD
#undef JSON_HAS_CPP_14
#undef JSON_HAS_CPP_17
#undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
Expand Down
2 changes: 1 addition & 1 deletion test/src/unit-cbor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1962,7 +1962,7 @@ TEST_CASE("all CBOR first bytes", "[!throws]")

try
{
json::from_cbor(std::vector<uint8_t>(1, byte));
auto res = json::from_cbor(std::vector<uint8_t>(1, byte));
}
catch (const json::parse_error& e)
{
Expand Down
2 changes: 1 addition & 1 deletion test/src/unit-readme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ TEST_CASE("README", "[hide]")
// }

// calculate a JSON patch from two JSON values
json::diff(j_result, j_original);
auto res = json::diff(j_result, j_original);
// [
// { "op":" replace", "path": "/baz", "value": ["one", "two", "three"] },
// { "op":"remove","path":"/hello" },
Expand Down
2 changes: 1 addition & 1 deletion test/src/unit-ubjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2128,7 +2128,7 @@ TEST_CASE("all UBJSON first bytes", "[!throws]")

try
{
json::from_ubjson(std::vector<uint8_t>(1, byte));
auto res = json::from_ubjson(std::vector<uint8_t>(1, byte));
}
catch (const json::parse_error& e)
{
Expand Down

0 comments on commit e89c946

Please sign in to comment.