Skip to content

Commit ee9b30c

Browse files
committed
fixed compilation on older implementations without std::launder
also: - fixed `json_formatter` type deduction on older compilers - added build configuration option for compiling examples
1 parent fe0ef67 commit ee9b30c

File tree

10 files changed

+146
-97
lines changed

10 files changed

+146
-97
lines changed

include/toml++/toml.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
#undef TOML_IMPLEMENTATION
8383
#undef TOML_INLINE_FUNC_IMPL
8484
#undef TOML_COMPILER_EXCEPTIONS
85+
#undef TOML_LAUNDER
8586
#endif
8687

8788
/// \mainpage toml++

include/toml++/toml_common.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@
318318
TOML_PUSH_WARNINGS
319319
TOML_DISABLE_ALL_WARNINGS
320320

321+
#if __has_include(<version>)
322+
#include <version>
323+
#endif
321324
#include <cstdint>
322325
#include <cstring> //memcpy, memset
323326
#include <memory>
@@ -350,6 +353,12 @@ TOML_POP_WARNINGS
350353
#define TOML_STRING_PREFIX(S) S
351354
#endif
352355

356+
#ifdef __cpp_lib_launder
357+
#define TOML_LAUNDER(x) std::launder(x)
358+
#else
359+
#define TOML_LAUNDER(x) x
360+
#endif
361+
353362
////////// FORWARD DECLARATIONS & TYPEDEFS
354363
// clang-format on
355364

include/toml++/toml_default_formatter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,10 @@ TOML_START
459459
friend std::basic_ostream<T>& operator << (std::basic_ostream<T>&, default_formatter<U>&&) TOML_MAY_THROW;
460460
};
461461

462+
default_formatter(const table&) -> default_formatter<char>;
463+
default_formatter(const array&) -> default_formatter<char>;
464+
template <typename T> default_formatter(const value<T>&) -> default_formatter<char>;
465+
462466
template <typename CHAR>
463467
inline void default_formatter<CHAR>::print_inline(const toml::table& tbl) TOML_MAY_THROW
464468
{

include/toml++/toml_json_formatter.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,8 @@ TOML_START
103103
/// \param source The source TOML object.
104104
/// \param flags Format option flags.
105105
TOML_NODISCARD_CTOR
106-
explicit json_formatter(
107-
const toml::node& source,
108-
format_flags flags = format_flags::quote_dates_and_times) noexcept
109-
: base{ source, flags }
106+
explicit json_formatter(const toml::node& source, format_flags flags = {}) noexcept
107+
: base{ source, flags | format_flags::quote_dates_and_times }
110108
{}
111109

112110
template <typename T, typename U>
@@ -115,6 +113,10 @@ TOML_START
115113
friend std::basic_ostream<T>& operator << (std::basic_ostream<T>&, json_formatter<U>&&) TOML_MAY_THROW;
116114
};
117115

116+
json_formatter(const table&) -> json_formatter<char>;
117+
json_formatter(const array&) -> json_formatter<char>;
118+
template <typename T> json_formatter(const value<T>&) -> json_formatter<char>;
119+
118120
template <typename CHAR>
119121
inline void json_formatter<CHAR>::print(const toml::table& tbl) TOML_MAY_THROW
120122
{

include/toml++/toml_parser.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ TOML_START
5252
void destroy() noexcept
5353
{
5454
if (is_err)
55-
std::launder(reinterpret_cast<parse_error*>(&storage))->~parse_error();
55+
TOML_LAUNDER(reinterpret_cast<parse_error*>(&storage))->~parse_error();
5656
else
57-
std::launder(reinterpret_cast<table*>(&storage))->~table();
57+
TOML_LAUNDER(reinterpret_cast<table*>(&storage))->~table();
5858
}
5959

6060
public:
@@ -70,38 +70,38 @@ TOML_START
7070
[[nodiscard]] table& get() & noexcept
7171
{
7272
TOML_ASSERT(!is_err);
73-
return *std::launder(reinterpret_cast<table*>(&storage));
73+
return *TOML_LAUNDER(reinterpret_cast<table*>(&storage));
7474
}
7575
/// \brief Returns the internal toml::table (rvalue overload).
7676
[[nodiscard]] table&& get() && noexcept
7777
{
7878
TOML_ASSERT(!is_err);
79-
return std::move(*std::launder(reinterpret_cast<table*>(&storage)));
79+
return std::move(*TOML_LAUNDER(reinterpret_cast<table*>(&storage)));
8080
}
8181
/// \brief Returns the internal toml::table (const lvalue overload).
8282
[[nodiscard]] const table& get() const& noexcept
8383
{
8484
TOML_ASSERT(!is_err);
85-
return *std::launder(reinterpret_cast<const table*>(&storage));
85+
return *TOML_LAUNDER(reinterpret_cast<const table*>(&storage));
8686
}
8787

8888
/// \brief Returns the internal toml::parse_error.
8989
[[nodiscard]] parse_error& error() & noexcept
9090
{
9191
TOML_ASSERT(is_err);
92-
return *std::launder(reinterpret_cast<parse_error*>(&storage));
92+
return *TOML_LAUNDER(reinterpret_cast<parse_error*>(&storage));
9393
}
9494
/// \brief Returns the internal toml::parse_error (rvalue overload).
9595
[[nodiscard]] parse_error&& error() && noexcept
9696
{
9797
TOML_ASSERT(is_err);
98-
return std::move(*std::launder(reinterpret_cast<parse_error*>(&storage)));
98+
return std::move(*TOML_LAUNDER(reinterpret_cast<parse_error*>(&storage)));
9999
}
100100
/// \brief Returns the internal toml::parse_error (const lvalue overload).
101101
[[nodiscard]] const parse_error& error() const& noexcept
102102
{
103103
TOML_ASSERT(is_err);
104-
return *std::launder(reinterpret_cast<const parse_error*>(&storage));
104+
return *TOML_LAUNDER(reinterpret_cast<const parse_error*>(&storage));
105105
}
106106

107107
/// \brief Returns the internal toml::table.

include/toml++/toml_version.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#pragma once
66

77
#define TOML_LIB_MAJOR 0
8-
#define TOML_LIB_MINOR 4
9-
#define TOML_LIB_PATCH 4
8+
#define TOML_LIB_MINOR 5
9+
#define TOML_LIB_PATCH 0
1010

1111
#define TOML_LANG_MAJOR 0
1212
#define TOML_LANG_MINOR 5

meson.build

Lines changed: 72 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
project(
22
'tomlplusplus',
33
'cpp',
4-
version : '0.4.4',
4+
version : '0.5.0',
55
license : 'MIT',
66
default_options : [
77
'cpp_std=c++17',
@@ -11,67 +11,6 @@ project(
1111
]
1212
)
1313

14-
compiler = meson.get_compiler('cpp')
15-
message(['compiler ID: ', compiler.get_id()])
16-
17-
if compiler.get_id() == 'gcc'
18-
add_project_arguments([
19-
'-g0',
20-
'-fmax-errors=5',
21-
'-Wno-init-list-lifetime'
22-
],
23-
language : 'cpp'
24-
)
25-
endif
26-
27-
if compiler.get_id() == 'clang'
28-
add_project_arguments([
29-
'-g0',
30-
'-ferror-limit=5',
31-
'-fchar8_t',
32-
# '-Weverything',
33-
'-Wno-c++98-compat',
34-
'-Wno-c++98-compat-pedantic',
35-
'-Wno-float-equal',
36-
'-Wno-switch-enum',
37-
'-Wno-documentation-unknown-command',
38-
'-Wno-padded',
39-
'-Wno-weak-vtables',
40-
'-Wno-double-promotion'
41-
#, '-ftime-trace'
42-
],
43-
language : 'cpp'
44-
)
45-
endif
46-
47-
if compiler.get_id() == 'intel-cl'
48-
add_project_arguments([
49-
'/Qoption,cpp,--unicode_source_kind,UTF-8',
50-
'/std=c++latest',
51-
'/wd82', # storage class is not first
52-
'/wd280', # selector expression is constant (why the fuck is that a warning?)
53-
'/wd411', # class provides no constructor (duh, it's an aggregate)
54-
'/wd1011', # missing return statement (false negative)
55-
'/wd1628', # function marked [[noreturn]] returns (false positive)
56-
'/wd3280' # declaration hides member (triggered in Catch2)
57-
],
58-
language : 'cpp'
59-
)
60-
endif
61-
62-
compiler_supports_char8_strings = compiler.compiles('''
63-
#include <string_view>
64-
#include <string>
65-
using namespace std::string_view_literals;
66-
std::u8string func()
67-
{
68-
return std::u8string{ u8"this is a test."sv };
69-
}
70-
''',
71-
name : 'char8 string check',
72-
args : [ '-std=c++2a' ]
73-
)
74-
7514
tomlplusplus_dep = declare_dependency(
7615
include_directories : include_directories('include'),
7716
version : meson.project_version(),
@@ -84,11 +23,77 @@ else
8423
build_tests = get_option('BUILD_TESTS').enabled()
8524
endif
8625

87-
if build_tests
88-
inc = include_directories('include', 'extern')
89-
subdir('tests')
26+
build_examples = false
27+
if get_option('BUILD_EXAMPLES').auto()
28+
build_examples = (not meson.is_subproject())
9029
else
91-
message('Not building tests')
30+
build_examples = get_option('BUILD_EXAMPLES').enabled()
31+
endif
32+
33+
if build_tests or build_examples
34+
35+
compiler = meson.get_compiler('cpp')
36+
message(['compiler ID: ', compiler.get_id()])
37+
message(['compiler version: ', compiler.version()])
38+
39+
if compiler.get_id() == 'gcc'
40+
add_project_arguments([
41+
'-g0',
42+
'-fmax-errors=5',
43+
'-Wno-init-list-lifetime'
44+
],
45+
language : 'cpp'
46+
)
47+
endif
48+
49+
if compiler.get_id() == 'clang'
50+
add_project_arguments([
51+
'-g0',
52+
'-ferror-limit=5',
53+
'-fchar8_t',
54+
# '-Weverything',
55+
'-Wno-c++98-compat',
56+
'-Wno-c++98-compat-pedantic',
57+
'-Wno-float-equal',
58+
'-Wno-switch-enum',
59+
'-Wno-documentation-unknown-command',
60+
'-Wno-padded',
61+
'-Wno-weak-vtables',
62+
'-Wno-double-promotion'
63+
#, '-ftime-trace'
64+
],
65+
language : 'cpp'
66+
)
67+
endif
68+
69+
if compiler.get_id() == 'intel-cl'
70+
add_project_arguments([
71+
'/Qoption,cpp,--unicode_source_kind,UTF-8',
72+
'/std=c++latest',
73+
'/wd82', # storage class is not first
74+
'/wd280', # selector expression is constant (why the fuck is that a warning?)
75+
'/wd411', # class provides no constructor (duh, it's an aggregate)
76+
'/wd1011', # missing return statement (false negative)
77+
'/wd1628', # function marked [[noreturn]] returns (false positive)
78+
'/wd3280' # declaration hides member (triggered in Catch2)
79+
],
80+
language : 'cpp'
81+
)
82+
endif
83+
84+
inc = include_directories('include', 'extern')
85+
86+
if build_tests
87+
subdir('tests')
88+
else
89+
message('Not building tests')
90+
endif
91+
92+
if build_examples
93+
subdir('examples')
94+
else
95+
message('Not building examples')
96+
endif
97+
9298
endif
9399

94-
# subdir('examples')

meson_options.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
option('BUILD_TESTS', type : 'feature', value : 'auto', description : 'Whether to build tests (defaults to auto: only if not a subproject)')
2+
option('BUILD_EXAMPLES', type : 'feature', value : 'auto', description : 'Whether to build examples (defaults to auto: only if not a subproject)')

tests/meson.build

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,18 @@ toml_char8_strings = '-DTOML_CHAR_8_STRINGS=1'
2121
manually_set_cpp_std = 'cpp_std=none'
2222
cpp20 = '-std=c++2a'
2323
use_tloptional = '-DTARTANLLAMA_OPTIONAL'
24-
24+
compiler_supports_char8_strings = compiler.compiles('''
25+
#include <string_view>
26+
#include <string>
27+
using namespace std::string_view_literals;
28+
std::u8string func()
29+
{
30+
return std::u8string{ u8"this is a test."sv };
31+
}
32+
''',
33+
name : 'char8 string check',
34+
args : [ '-std=c++2a' ]
35+
)
2536

2637
############################################################################
2738
### char

0 commit comments

Comments
 (0)