Skip to content

Commit f180983

Browse files
committed
Adding the proper polyfill for older standard libs used by GCC and Clang.
1 parent d4248f1 commit f180983

File tree

2 files changed

+24
-33
lines changed

2 files changed

+24
-33
lines changed

includes/sjson/platform.h renamed to includes/sjson/impl/compiler_utils.h

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,33 @@
2424
// SOFTWARE.
2525
////////////////////////////////////////////////////////////////////////////////
2626

27-
// NVIDIA CodeWorks used for Android has an old version of Clang that does not fully support C++11
28-
#if defined(__clang__) && __clang_major__ <= 3 && __clang_minor__ <= 8
29-
#include <stdlib.h>
30-
#else
27+
#if !defined(__GNUG__) || defined(_LIBCPP_VERSION) || defined(_GLIBCXX_USE_CXX11_ABI)
3128
#include <cstdlib>
29+
#else
30+
#include <stdlib.h>
3231
#endif
3332

3433
namespace sjson
3534
{
36-
namespace impl
35+
//////////////////////////////////////////////////////////////////////////
36+
// The version of the STL shipped with versions of GCC older than 5.1 are missing a number of type traits and functions,
37+
// such as std::is_trivially_default_constructible.
38+
// In this case, we polyfill the proper standard names using the deprecated std::has_trivial_default_constructor.
39+
// This must also be done when the compiler is clang when it makes use of the GCC implementation of the STL,
40+
// which is the default behavior on linux. Properly detecting the version of the GCC STL used by clang cannot
41+
// be done with the __GNUC__ macro, which are overridden by clang. Instead, we check for the definition
42+
// of the macro ``_GLIBCXX_USE_CXX11_ABI`` which is only defined with GCC versions greater than 5.
43+
//////////////////////////////////////////////////////////////////////////
44+
namespace sjson_impl
3745
{
38-
inline unsigned long long int strtoull(const char* str, char** endptr, int base)
39-
{
40-
#if defined(__clang__) && __clang_major__ <= 3 && __clang_minor__ <= 8
41-
return ::strtoull(str, endptr, base);
42-
#else
43-
return std::strtoull(str, endptr, base);
44-
#endif
45-
}
46-
47-
inline long long int strtoll(const char* str, char** endptr, int base)
48-
{
49-
#if defined(__clang__) && __clang_major__ <= 3 && __clang_minor__ <= 8
50-
return ::strtoll(str, endptr, base);
51-
#else
52-
return std::strtoll(str, endptr, base);
53-
#endif
54-
}
55-
56-
inline float strtof(const char* str, char** endptr)
57-
{
58-
#if defined(__clang__) && __clang_major__ <= 3 && __clang_minor__ <= 8
59-
return ::strtof(str, endptr);
46+
#if !defined(__GNUG__) || defined(_LIBCPP_VERSION) || defined(_GLIBCXX_USE_CXX11_ABI)
47+
using std::strtoull;
48+
using std::strtoll;
49+
using std::strtof;
6050
#else
61-
return std::strtof(str, endptr);
51+
using ::strtoull;
52+
using ::strtoll;
53+
using ::strtof;
6254
#endif
63-
}
6455
}
6556
}

includes/sjson/parser.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
#include "sjson/parser_error.h"
3333
#include "sjson/parser_state.h"
34-
#include "sjson/platform.h"
34+
#include "sjson/impl/compiler_utils.h"
3535
#include "sjson/string_view.h"
3636

3737
#include <cctype>
@@ -723,7 +723,7 @@ namespace sjson
723723
if (dbl_value != nullptr)
724724
*dbl_value = std::strtod(slice, &last_used_symbol);
725725
else
726-
*flt_value = impl::strtof(slice, &last_used_symbol);
726+
*flt_value = sjson_impl::strtof(slice, &last_used_symbol);
727727

728728
if (last_used_symbol != slice + length)
729729
{
@@ -800,7 +800,7 @@ namespace sjson
800800
char* last_used_symbol = nullptr;
801801
if (std::is_unsigned<IntegralType>::value)
802802
{
803-
const uint64_t raw_value = impl::strtoull(slice, &last_used_symbol, base);
803+
const uint64_t raw_value = sjson_impl::strtoull(slice, &last_used_symbol, base);
804804
value = static_cast<IntegralType>(raw_value);
805805

806806
if (static_cast<uint64_t>(value) != raw_value)
@@ -811,7 +811,7 @@ namespace sjson
811811
}
812812
else
813813
{
814-
const int64_t raw_value = impl::strtoll(slice, &last_used_symbol, base);
814+
const int64_t raw_value = sjson_impl::strtoll(slice, &last_used_symbol, base);
815815
value = static_cast<IntegralType>(raw_value);
816816

817817
if (static_cast<int64_t>(value) != raw_value)

0 commit comments

Comments
 (0)