Skip to content

Commit 6acab2f

Browse files
committed
add static_asserts on SAX interface
1 parent d9c695a commit 6acab2f

4 files changed

Lines changed: 361 additions & 0 deletions

File tree

include/nlohmann/detail/input/binary_reader.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <nlohmann/detail/input/json_sax.hpp>
1818
#include <nlohmann/detail/exceptions.hpp>
1919
#include <nlohmann/detail/macro_scope.hpp>
20+
#include <nlohmann/detail/meta/is_sax.hpp>
2021
#include <nlohmann/detail/value_t.hpp>
2122

2223
namespace nlohmann
@@ -47,6 +48,7 @@ class binary_reader
4748
*/
4849
explicit binary_reader(input_adapter_t adapter) : ia(std::move(adapter))
4950
{
51+
(void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
5052
assert(ia);
5153
}
5254

include/nlohmann/detail/input/parser.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <nlohmann/detail/exceptions.hpp>
1111
#include <nlohmann/detail/macro_scope.hpp>
12+
#include <nlohmann/detail/meta/is_sax.hpp>
1213
#include <nlohmann/detail/input/input_adapters.hpp>
1314
#include <nlohmann/detail/input/json_sax.hpp>
1415
#include <nlohmann/detail/input/lexer.hpp>
@@ -145,6 +146,7 @@ class parser
145146
template <typename SAX>
146147
bool sax_parse(SAX* sax, const bool strict = true)
147148
{
149+
// (void)detail::is_sax_static_asserts<SAX, BasicJsonType>{};
148150
const bool result = sax_parse_internal(sax);
149151

150152
// strict mode: next byte must be EOF
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#pragma once
2+
3+
#include <cstdint> // size_t
4+
#include <utility> // declval
5+
6+
#include <nlohmann/detail/meta/detected.hpp>
7+
#include <nlohmann/detail/meta/type_traits.hpp>
8+
9+
namespace nlohmann
10+
{
11+
namespace detail
12+
{
13+
template <typename T>
14+
using null_function_t = decltype(std::declval<T&>().null());
15+
16+
template <typename T>
17+
using boolean_function_t =
18+
decltype(std::declval<T&>().boolean(std::declval<bool>()));
19+
20+
template <typename T, typename Integer>
21+
using number_integer_function_t =
22+
decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
23+
24+
template <typename T, typename Unsigned>
25+
using number_unsigned_function_t =
26+
decltype(std::declval<T &>().number_unsigned(std::declval<Unsigned>()));
27+
28+
template <typename T, typename Float, typename String>
29+
using number_float_function_t = decltype(std::declval<T &>().number_float(
30+
std::declval<Float>(), std::declval<const String &>()));
31+
32+
template <typename T, typename String>
33+
using string_function_t =
34+
decltype(std::declval<T &>().string(std::declval<String &>()));
35+
36+
template <typename T>
37+
using start_object_function_t =
38+
decltype(std::declval<T &>().start_object(std::declval<std::size_t>()));
39+
40+
template <typename T, typename String>
41+
using key_function_t =
42+
decltype(std::declval<T &>().key(std::declval<String &>()));
43+
44+
template <typename T>
45+
using end_object_function_t = decltype(std::declval<T &>().end_object());
46+
47+
template <typename T>
48+
using start_array_function_t =
49+
decltype(std::declval<T &>().start_array(std::declval<std::size_t>()));
50+
51+
template <typename T>
52+
using end_array_function_t = decltype(std::declval<T &>().end_array());
53+
54+
template <typename T, typename Exception>
55+
using parse_error_function_t = decltype(std::declval<T &>().parse_error(
56+
std::declval<std::size_t>(), std::declval<const std::string &>(),
57+
std::declval<const Exception &>()));
58+
59+
template <typename SAX, typename BasicJsonType>
60+
struct is_sax
61+
{
62+
private:
63+
static_assert(is_basic_json<BasicJsonType>::value,
64+
"BasicJsonType must be of type basic_json<...>");
65+
66+
using number_integer_t = typename BasicJsonType::number_integer_t;
67+
using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
68+
using number_float_t = typename BasicJsonType::number_float_t;
69+
using string_t = typename BasicJsonType::string_t;
70+
using exception_t = typename BasicJsonType::exception;
71+
72+
public:
73+
static constexpr bool value =
74+
is_detected_exact<bool, null_function_t, SAX>::value &&
75+
is_detected_exact<bool, boolean_function_t, SAX>::value &&
76+
is_detected_exact<bool, number_integer_function_t, SAX,
77+
number_integer_t>::value &&
78+
is_detected_exact<bool, number_unsigned_function_t, SAX,
79+
number_unsigned_t>::value &&
80+
is_detected_exact<bool, number_float_function_t, SAX, number_float_t,
81+
string_t>::value &&
82+
is_detected_exact<bool, string_function_t, SAX, string_t>::value &&
83+
is_detected_exact<bool, start_object_function_t, SAX>::value &&
84+
is_detected_exact<bool, key_function_t, SAX, string_t>::value &&
85+
is_detected_exact<bool, end_object_function_t, SAX>::value &&
86+
is_detected_exact<bool, start_array_function_t, SAX>::value &&
87+
is_detected_exact<bool, end_array_function_t, SAX>::value &&
88+
is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value;
89+
};
90+
91+
template <typename SAX, typename BasicJsonType>
92+
struct is_sax_static_asserts
93+
{
94+
private:
95+
static_assert(is_basic_json<BasicJsonType>::value,
96+
"BasicJsonType must be of type basic_json<...>");
97+
98+
using number_integer_t = typename BasicJsonType::number_integer_t;
99+
using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
100+
using number_float_t = typename BasicJsonType::number_float_t;
101+
using string_t = typename BasicJsonType::string_t;
102+
using exception_t = typename BasicJsonType::exception;
103+
104+
public:
105+
static_assert(is_detected_exact<bool, null_function_t, SAX>::value,
106+
"Missing/invalid function: bool null()");
107+
static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
108+
"Missing/invalid function: bool boolean(bool)");
109+
static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
110+
"Missing/invalid function: bool boolean(bool)");
111+
static_assert(
112+
is_detected_exact<bool, number_integer_function_t, SAX,
113+
number_integer_t>::value,
114+
"Missing/invalid function: bool number_integer(number_integer_t)");
115+
static_assert(
116+
is_detected_exact<bool, number_unsigned_function_t, SAX,
117+
number_unsigned_t>::value,
118+
"Missing/invalid function: bool number_unsigned(number_unsigned_t)");
119+
static_assert(is_detected_exact<bool, number_float_function_t, SAX,
120+
number_float_t, string_t>::value,
121+
"Missing/invalid function: bool number_float(number_float_t, const string_t&)");
122+
static_assert(
123+
is_detected_exact<bool, string_function_t, SAX, string_t>::value,
124+
"Missing/invalid function: bool string(string_t&)");
125+
static_assert(is_detected_exact<bool, start_object_function_t, SAX>::value,
126+
"Missing/invalid function: bool start_object(std::size_t)");
127+
static_assert(is_detected_exact<bool, key_function_t, SAX, string_t>::value,
128+
"Missing/invalid function: bool key(string_t&)");
129+
static_assert(is_detected_exact<bool, end_object_function_t, SAX>::value,
130+
"Missing/invalid function: bool end_object()");
131+
static_assert(is_detected_exact<bool, start_array_function_t, SAX>::value,
132+
"Missing/invalid function: bool start_array(std::size_t)");
133+
static_assert(is_detected_exact<bool, end_array_function_t, SAX>::value,
134+
"Missing/invalid function: bool end_array()");
135+
static_assert(
136+
is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value,
137+
"Missing/invalid function: bool parse_error(std::size_t, const "
138+
"std::string&, const exception&)");
139+
};
140+
}
141+
}

0 commit comments

Comments
 (0)