Skip to content

Commit fba40f9

Browse files
committed
Replace deprecated std::char_traits calls with custom char_traits
1 parent c7a324c commit fba40f9

File tree

4 files changed

+114
-104
lines changed

4 files changed

+114
-104
lines changed

include/nlohmann/detail/input/binary_reader.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class binary_reader
7272
using binary_t = typename BasicJsonType::binary_t;
7373
using json_sax_t = SAX;
7474
using char_type = typename InputAdapterType::char_type;
75-
using char_int_type = typename std::char_traits<char_type>::int_type;
75+
using char_int_type = typename char_traits<char_type>::int_type;
7676

7777
public:
7878
/*!
@@ -145,7 +145,7 @@ class binary_reader
145145
get();
146146
}
147147

148-
if (JSON_HEDLEY_UNLIKELY(current != std::char_traits<char_type>::eof()))
148+
if (JSON_HEDLEY_UNLIKELY(current != char_traits<char_type>::eof()))
149149
{
150150
return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read,
151151
exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr));
@@ -228,7 +228,7 @@ class binary_reader
228228
exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr));
229229
}
230230

231-
return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != std::char_traits<char_type>::eof();
231+
return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != char_traits<char_type>::eof();
232232
}
233233

234234
/*!
@@ -422,7 +422,7 @@ class binary_reader
422422
switch (get_char ? get() : current)
423423
{
424424
// EOF
425-
case std::char_traits<char_type>::eof():
425+
case char_traits<char_type>::eof():
426426
return unexpect_eof(input_format_t::cbor, "value");
427427

428428
// Integer 0x00..0x17 (0..23)
@@ -1197,7 +1197,7 @@ class binary_reader
11971197
switch (get())
11981198
{
11991199
// EOF
1200-
case std::char_traits<char_type>::eof():
1200+
case char_traits<char_type>::eof():
12011201
return unexpect_eof(input_format_t::msgpack, "value");
12021202

12031203
// positive fixint
@@ -2299,7 +2299,7 @@ class binary_reader
22992299
{
23002300
switch (prefix)
23012301
{
2302-
case std::char_traits<char_type>::eof(): // EOF
2302+
case char_traits<char_type>::eof(): // EOF
23032303
return unexpect_eof(input_format, "value");
23042304

23052305
case 'T': // true
@@ -2744,7 +2744,7 @@ class binary_reader
27442744
27452745
This function provides the interface to the used input adapter. It does
27462746
not throw in case the input reached EOF, but returns a -'ve valued
2747-
`std::char_traits<char_type>::eof()` in that case.
2747+
`char_traits<char_type>::eof()` in that case.
27482748
27492749
@return character read from the input
27502750
*/
@@ -2886,7 +2886,7 @@ class binary_reader
28862886
JSON_HEDLEY_NON_NULL(3)
28872887
bool unexpect_eof(const input_format_t format, const char* context) const
28882888
{
2889-
if (JSON_HEDLEY_UNLIKELY(current == std::char_traits<char_type>::eof()))
2889+
if (JSON_HEDLEY_UNLIKELY(current == char_traits<char_type>::eof()))
28902890
{
28912891
return sax->parse_error(chars_read, "<end of file>",
28922892
parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
@@ -2953,7 +2953,7 @@ class binary_reader
29532953
InputAdapterType ia;
29542954

29552955
/// the current character
2956-
char_int_type current = std::char_traits<char_type>::eof();
2956+
char_int_type current = char_traits<char_type>::eof();
29572957

29582958
/// the number of characters read
29592959
std::size_t chars_read = 0;

include/nlohmann/detail/input/input_adapters.hpp

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -133,49 +133,6 @@ class input_stream_adapter
133133
};
134134
#endif // JSON_NO_IO
135135

136-
// Primary template of json_char_traits calls std char_traits
137-
template<typename T>
138-
struct char_traits : std::char_traits<T>
139-
{};
140-
141-
// Explicitly define char traits for unsigned char since it is not standard
142-
template<>
143-
struct char_traits<unsigned char> : std::char_traits<char>
144-
{
145-
using char_type = unsigned char;
146-
using int_type = uint64_t;
147-
148-
// Redefine to_int_type function
149-
static int_type to_int_type(char_type c)
150-
{
151-
return static_cast<int_type>(c);
152-
}
153-
154-
static int_type eof()
155-
{
156-
return static_cast<int_type>(EOF);
157-
}
158-
};
159-
160-
// Explicitly define char traits for signed char since it is not standard
161-
template<>
162-
struct char_traits<signed char> : std::char_traits<char>
163-
{
164-
using char_type = signed char;
165-
using int_type = uint64_t;
166-
167-
// Redefine to_int_type function
168-
static int_type to_int_type(char_type c)
169-
{
170-
return static_cast<int_type>(c);
171-
}
172-
173-
static int_type eof()
174-
{
175-
return static_cast<int_type>(EOF);
176-
}
177-
};
178-
179136
// General-purpose iterator-based adapter. It might not be as fast as
180137
// theoretically possible for some containers, but it is extremely versatile.
181138
template<typename IteratorType>

include/nlohmann/detail/meta/type_traits.hpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
1313
#include <utility> // declval
1414
#include <tuple> // tuple
15+
#include <string> // char_traits
1516

1617
#include <nlohmann/detail/iterators/iterator_traits.hpp>
1718
#include <nlohmann/detail/macro_scope.hpp>
@@ -181,6 +182,53 @@ struct actual_object_comparator
181182
template<typename BasicJsonType>
182183
using actual_object_comparator_t = typename actual_object_comparator<BasicJsonType>::type;
183184

185+
/////////////////
186+
// char_traits //
187+
/////////////////
188+
189+
// Primary template of char_traits calls std char_traits
190+
template<typename T>
191+
struct char_traits : std::char_traits<T>
192+
{};
193+
194+
// Explicitly define char traits for unsigned char since it is not standard
195+
template<>
196+
struct char_traits<unsigned char> : std::char_traits<char>
197+
{
198+
using char_type = unsigned char;
199+
using int_type = uint64_t;
200+
201+
// Redefine to_int_type function
202+
static int_type to_int_type(char_type c)
203+
{
204+
return static_cast<int_type>(c);
205+
}
206+
207+
static constexpr int_type eof()
208+
{
209+
return static_cast<int_type>(EOF);
210+
}
211+
};
212+
213+
// Explicitly define char traits for signed char since it is not standard
214+
template<>
215+
struct char_traits<signed char> : std::char_traits<char>
216+
{
217+
using char_type = signed char;
218+
using int_type = uint64_t;
219+
220+
// Redefine to_int_type function
221+
static int_type to_int_type(char_type c)
222+
{
223+
return static_cast<int_type>(c);
224+
}
225+
226+
static constexpr int_type eof()
227+
{
228+
return static_cast<int_type>(EOF);
229+
}
230+
};
231+
184232
///////////////////
185233
// is_ functions //
186234
///////////////////

single_include/nlohmann/json.hpp

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3236,6 +3236,7 @@ NLOHMANN_JSON_NAMESPACE_END
32363236
#include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
32373237
#include <utility> // declval
32383238
#include <tuple> // tuple
3239+
#include <string> // char_traits
32393240

32403241
// #include <nlohmann/detail/iterators/iterator_traits.hpp>
32413242
// __ _____ _____ _____
@@ -3588,6 +3589,53 @@ struct actual_object_comparator
35883589
template<typename BasicJsonType>
35893590
using actual_object_comparator_t = typename actual_object_comparator<BasicJsonType>::type;
35903591

3592+
/////////////////
3593+
// char_traits //
3594+
/////////////////
3595+
3596+
// Primary template of char_traits calls std char_traits
3597+
template<typename T>
3598+
struct char_traits : std::char_traits<T>
3599+
{};
3600+
3601+
// Explicitly define char traits for unsigned char since it is not standard
3602+
template<>
3603+
struct char_traits<unsigned char> : std::char_traits<char>
3604+
{
3605+
using char_type = unsigned char;
3606+
using int_type = uint64_t;
3607+
3608+
// Redefine to_int_type function
3609+
static int_type to_int_type(char_type c)
3610+
{
3611+
return static_cast<int_type>(c);
3612+
}
3613+
3614+
static constexpr int_type eof()
3615+
{
3616+
return static_cast<int_type>(EOF);
3617+
}
3618+
};
3619+
3620+
// Explicitly define char traits for signed char since it is not standard
3621+
template<>
3622+
struct char_traits<signed char> : std::char_traits<char>
3623+
{
3624+
using char_type = signed char;
3625+
using int_type = uint64_t;
3626+
3627+
// Redefine to_int_type function
3628+
static int_type to_int_type(char_type c)
3629+
{
3630+
return static_cast<int_type>(c);
3631+
}
3632+
3633+
static constexpr int_type eof()
3634+
{
3635+
return static_cast<int_type>(EOF);
3636+
}
3637+
};
3638+
35913639
///////////////////
35923640
// is_ functions //
35933641
///////////////////
@@ -6210,49 +6258,6 @@ class input_stream_adapter
62106258
};
62116259
#endif // JSON_NO_IO
62126260

6213-
// Primary template of json_char_traits calls std char_traits
6214-
template<typename T>
6215-
struct char_traits : std::char_traits<T>
6216-
{};
6217-
6218-
// Explicitly define char traits for unsigned char since it is not standard
6219-
template<>
6220-
struct char_traits<unsigned char> : std::char_traits<char>
6221-
{
6222-
using char_type = unsigned char;
6223-
using int_type = uint64_t;
6224-
6225-
// Redefine to_int_type function
6226-
static int_type to_int_type(char_type c)
6227-
{
6228-
return static_cast<int_type>(c);
6229-
}
6230-
6231-
static int_type eof()
6232-
{
6233-
return static_cast<int_type>(EOF);
6234-
}
6235-
};
6236-
6237-
// Explicitly define char traits for signed char since it is not standard
6238-
template<>
6239-
struct char_traits<signed char> : std::char_traits<char>
6240-
{
6241-
using char_type = signed char;
6242-
using int_type = uint64_t;
6243-
6244-
// Redefine to_int_type function
6245-
static int_type to_int_type(char_type c)
6246-
{
6247-
return static_cast<int_type>(c);
6248-
}
6249-
6250-
static int_type eof()
6251-
{
6252-
return static_cast<int_type>(EOF);
6253-
}
6254-
};
6255-
62566261
// General-purpose iterator-based adapter. It might not be as fast as
62576262
// theoretically possible for some containers, but it is extremely versatile.
62586263
template<typename IteratorType>
@@ -9194,7 +9199,7 @@ class binary_reader
91949199
using binary_t = typename BasicJsonType::binary_t;
91959200
using json_sax_t = SAX;
91969201
using char_type = typename InputAdapterType::char_type;
9197-
using char_int_type = typename std::char_traits<char_type>::int_type;
9202+
using char_int_type = typename char_traits<char_type>::int_type;
91989203

91999204
public:
92009205
/*!
@@ -9267,7 +9272,7 @@ class binary_reader
92679272
get();
92689273
}
92699274

9270-
if (JSON_HEDLEY_UNLIKELY(current != std::char_traits<char_type>::eof()))
9275+
if (JSON_HEDLEY_UNLIKELY(current != char_traits<char_type>::eof()))
92719276
{
92729277
return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read,
92739278
exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr));
@@ -9350,7 +9355,7 @@ class binary_reader
93509355
exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr));
93519356
}
93529357

9353-
return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != std::char_traits<char_type>::eof();
9358+
return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != char_traits<char_type>::eof();
93549359
}
93559360

93569361
/*!
@@ -9544,7 +9549,7 @@ class binary_reader
95449549
switch (get_char ? get() : current)
95459550
{
95469551
// EOF
9547-
case std::char_traits<char_type>::eof():
9552+
case char_traits<char_type>::eof():
95489553
return unexpect_eof(input_format_t::cbor, "value");
95499554

95509555
// Integer 0x00..0x17 (0..23)
@@ -10319,7 +10324,7 @@ class binary_reader
1031910324
switch (get())
1032010325
{
1032110326
// EOF
10322-
case std::char_traits<char_type>::eof():
10327+
case char_traits<char_type>::eof():
1032310328
return unexpect_eof(input_format_t::msgpack, "value");
1032410329

1032510330
// positive fixint
@@ -11421,7 +11426,7 @@ class binary_reader
1142111426
{
1142211427
switch (prefix)
1142311428
{
11424-
case std::char_traits<char_type>::eof(): // EOF
11429+
case char_traits<char_type>::eof(): // EOF
1142511430
return unexpect_eof(input_format, "value");
1142611431

1142711432
case 'T': // true
@@ -11866,7 +11871,7 @@ class binary_reader
1186611871

1186711872
This function provides the interface to the used input adapter. It does
1186811873
not throw in case the input reached EOF, but returns a -'ve valued
11869-
`std::char_traits<char_type>::eof()` in that case.
11874+
`char_traits<char_type>::eof()` in that case.
1187011875

1187111876
@return character read from the input
1187211877
*/
@@ -12008,7 +12013,7 @@ class binary_reader
1200812013
JSON_HEDLEY_NON_NULL(3)
1200912014
bool unexpect_eof(const input_format_t format, const char* context) const
1201012015
{
12011-
if (JSON_HEDLEY_UNLIKELY(current == std::char_traits<char_type>::eof()))
12016+
if (JSON_HEDLEY_UNLIKELY(current == char_traits<char_type>::eof()))
1201212017
{
1201312018
return sax->parse_error(chars_read, "<end of file>",
1201412019
parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
@@ -12075,7 +12080,7 @@ class binary_reader
1207512080
InputAdapterType ia;
1207612081

1207712082
/// the current character
12078-
char_int_type current = std::char_traits<char_type>::eof();
12083+
char_int_type current = char_traits<char_type>::eof();
1207912084

1208012085
/// the number of characters read
1208112086
std::size_t chars_read = 0;

0 commit comments

Comments
 (0)