Skip to content

Commit

Permalink
fixed compilation on older implementations without std::launder
Browse files Browse the repository at this point in the history
also:
- fixed `json_formatter` type deduction on older compilers
- added build configuration option for compiling examples
  • Loading branch information
marzer committed Mar 18, 2020
1 parent fe0ef67 commit ee9b30c
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 97 deletions.
1 change: 1 addition & 0 deletions include/toml++/toml.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
#undef TOML_IMPLEMENTATION
#undef TOML_INLINE_FUNC_IMPL
#undef TOML_COMPILER_EXCEPTIONS
#undef TOML_LAUNDER
#endif

/// \mainpage toml++
Expand Down
9 changes: 9 additions & 0 deletions include/toml++/toml_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@
TOML_PUSH_WARNINGS
TOML_DISABLE_ALL_WARNINGS

#if __has_include(<version>)
#include <version>
#endif
#include <cstdint>
#include <cstring> //memcpy, memset
#include <memory>
Expand Down Expand Up @@ -350,6 +353,12 @@ TOML_POP_WARNINGS
#define TOML_STRING_PREFIX(S) S
#endif

#ifdef __cpp_lib_launder
#define TOML_LAUNDER(x) std::launder(x)
#else
#define TOML_LAUNDER(x) x
#endif

////////// FORWARD DECLARATIONS & TYPEDEFS
// clang-format on

Expand Down
4 changes: 4 additions & 0 deletions include/toml++/toml_default_formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ TOML_START
friend std::basic_ostream<T>& operator << (std::basic_ostream<T>&, default_formatter<U>&&) TOML_MAY_THROW;
};

default_formatter(const table&) -> default_formatter<char>;
default_formatter(const array&) -> default_formatter<char>;
template <typename T> default_formatter(const value<T>&) -> default_formatter<char>;

template <typename CHAR>
inline void default_formatter<CHAR>::print_inline(const toml::table& tbl) TOML_MAY_THROW
{
Expand Down
10 changes: 6 additions & 4 deletions include/toml++/toml_json_formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,8 @@ TOML_START
/// \param source The source TOML object.
/// \param flags Format option flags.
TOML_NODISCARD_CTOR
explicit json_formatter(
const toml::node& source,
format_flags flags = format_flags::quote_dates_and_times) noexcept
: base{ source, flags }
explicit json_formatter(const toml::node& source, format_flags flags = {}) noexcept
: base{ source, flags | format_flags::quote_dates_and_times }
{}

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

json_formatter(const table&) -> json_formatter<char>;
json_formatter(const array&) -> json_formatter<char>;
template <typename T> json_formatter(const value<T>&) -> json_formatter<char>;

template <typename CHAR>
inline void json_formatter<CHAR>::print(const toml::table& tbl) TOML_MAY_THROW
{
Expand Down
16 changes: 8 additions & 8 deletions include/toml++/toml_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ TOML_START
void destroy() noexcept
{
if (is_err)
std::launder(reinterpret_cast<parse_error*>(&storage))->~parse_error();
TOML_LAUNDER(reinterpret_cast<parse_error*>(&storage))->~parse_error();
else
std::launder(reinterpret_cast<table*>(&storage))->~table();
TOML_LAUNDER(reinterpret_cast<table*>(&storage))->~table();
}

public:
Expand All @@ -70,38 +70,38 @@ TOML_START
[[nodiscard]] table& get() & noexcept
{
TOML_ASSERT(!is_err);
return *std::launder(reinterpret_cast<table*>(&storage));
return *TOML_LAUNDER(reinterpret_cast<table*>(&storage));
}
/// \brief Returns the internal toml::table (rvalue overload).
[[nodiscard]] table&& get() && noexcept
{
TOML_ASSERT(!is_err);
return std::move(*std::launder(reinterpret_cast<table*>(&storage)));
return std::move(*TOML_LAUNDER(reinterpret_cast<table*>(&storage)));
}
/// \brief Returns the internal toml::table (const lvalue overload).
[[nodiscard]] const table& get() const& noexcept
{
TOML_ASSERT(!is_err);
return *std::launder(reinterpret_cast<const table*>(&storage));
return *TOML_LAUNDER(reinterpret_cast<const table*>(&storage));
}

/// \brief Returns the internal toml::parse_error.
[[nodiscard]] parse_error& error() & noexcept
{
TOML_ASSERT(is_err);
return *std::launder(reinterpret_cast<parse_error*>(&storage));
return *TOML_LAUNDER(reinterpret_cast<parse_error*>(&storage));
}
/// \brief Returns the internal toml::parse_error (rvalue overload).
[[nodiscard]] parse_error&& error() && noexcept
{
TOML_ASSERT(is_err);
return std::move(*std::launder(reinterpret_cast<parse_error*>(&storage)));
return std::move(*TOML_LAUNDER(reinterpret_cast<parse_error*>(&storage)));
}
/// \brief Returns the internal toml::parse_error (const lvalue overload).
[[nodiscard]] const parse_error& error() const& noexcept
{
TOML_ASSERT(is_err);
return *std::launder(reinterpret_cast<const parse_error*>(&storage));
return *TOML_LAUNDER(reinterpret_cast<const parse_error*>(&storage));
}

/// \brief Returns the internal toml::table.
Expand Down
4 changes: 2 additions & 2 deletions include/toml++/toml_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#pragma once

#define TOML_LIB_MAJOR 0
#define TOML_LIB_MINOR 4
#define TOML_LIB_PATCH 4
#define TOML_LIB_MINOR 5
#define TOML_LIB_PATCH 0

#define TOML_LANG_MAJOR 0
#define TOML_LANG_MINOR 5
Expand Down
139 changes: 72 additions & 67 deletions meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project(
'tomlplusplus',
'cpp',
version : '0.4.4',
version : '0.5.0',
license : 'MIT',
default_options : [
'cpp_std=c++17',
Expand All @@ -11,67 +11,6 @@ project(
]
)

compiler = meson.get_compiler('cpp')
message(['compiler ID: ', compiler.get_id()])

if compiler.get_id() == 'gcc'
add_project_arguments([
'-g0',
'-fmax-errors=5',
'-Wno-init-list-lifetime'
],
language : 'cpp'
)
endif

if compiler.get_id() == 'clang'
add_project_arguments([
'-g0',
'-ferror-limit=5',
'-fchar8_t',
# '-Weverything',
'-Wno-c++98-compat',
'-Wno-c++98-compat-pedantic',
'-Wno-float-equal',
'-Wno-switch-enum',
'-Wno-documentation-unknown-command',
'-Wno-padded',
'-Wno-weak-vtables',
'-Wno-double-promotion'
#, '-ftime-trace'
],
language : 'cpp'
)
endif

if compiler.get_id() == 'intel-cl'
add_project_arguments([
'/Qoption,cpp,--unicode_source_kind,UTF-8',
'/std=c++latest',
'/wd82', # storage class is not first
'/wd280', # selector expression is constant (why the fuck is that a warning?)
'/wd411', # class provides no constructor (duh, it's an aggregate)
'/wd1011', # missing return statement (false negative)
'/wd1628', # function marked [[noreturn]] returns (false positive)
'/wd3280' # declaration hides member (triggered in Catch2)
],
language : 'cpp'
)
endif

compiler_supports_char8_strings = compiler.compiles('''
#include <string_view>
#include <string>
using namespace std::string_view_literals;
std::u8string func()
{
return std::u8string{ u8"this is a test."sv };
}
''',
name : 'char8 string check',
args : [ '-std=c++2a' ]
)

tomlplusplus_dep = declare_dependency(
include_directories : include_directories('include'),
version : meson.project_version(),
Expand All @@ -84,11 +23,77 @@ else
build_tests = get_option('BUILD_TESTS').enabled()
endif

if build_tests
inc = include_directories('include', 'extern')
subdir('tests')
build_examples = false
if get_option('BUILD_EXAMPLES').auto()
build_examples = (not meson.is_subproject())
else
message('Not building tests')
build_examples = get_option('BUILD_EXAMPLES').enabled()
endif

if build_tests or build_examples

compiler = meson.get_compiler('cpp')
message(['compiler ID: ', compiler.get_id()])
message(['compiler version: ', compiler.version()])

if compiler.get_id() == 'gcc'
add_project_arguments([
'-g0',
'-fmax-errors=5',
'-Wno-init-list-lifetime'
],
language : 'cpp'
)
endif

if compiler.get_id() == 'clang'
add_project_arguments([
'-g0',
'-ferror-limit=5',
'-fchar8_t',
# '-Weverything',
'-Wno-c++98-compat',
'-Wno-c++98-compat-pedantic',
'-Wno-float-equal',
'-Wno-switch-enum',
'-Wno-documentation-unknown-command',
'-Wno-padded',
'-Wno-weak-vtables',
'-Wno-double-promotion'
#, '-ftime-trace'
],
language : 'cpp'
)
endif

if compiler.get_id() == 'intel-cl'
add_project_arguments([
'/Qoption,cpp,--unicode_source_kind,UTF-8',
'/std=c++latest',
'/wd82', # storage class is not first
'/wd280', # selector expression is constant (why the fuck is that a warning?)
'/wd411', # class provides no constructor (duh, it's an aggregate)
'/wd1011', # missing return statement (false negative)
'/wd1628', # function marked [[noreturn]] returns (false positive)
'/wd3280' # declaration hides member (triggered in Catch2)
],
language : 'cpp'
)
endif

inc = include_directories('include', 'extern')

if build_tests
subdir('tests')
else
message('Not building tests')
endif

if build_examples
subdir('examples')
else
message('Not building examples')
endif

endif

# subdir('examples')
1 change: 1 addition & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
option('BUILD_TESTS', type : 'feature', value : 'auto', description : 'Whether to build tests (defaults to auto: only if not a subproject)')
option('BUILD_EXAMPLES', type : 'feature', value : 'auto', description : 'Whether to build examples (defaults to auto: only if not a subproject)')
13 changes: 12 additions & 1 deletion tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ toml_char8_strings = '-DTOML_CHAR_8_STRINGS=1'
manually_set_cpp_std = 'cpp_std=none'
cpp20 = '-std=c++2a'
use_tloptional = '-DTARTANLLAMA_OPTIONAL'

compiler_supports_char8_strings = compiler.compiles('''
#include <string_view>
#include <string>
using namespace std::string_view_literals;
std::u8string func()
{
return std::u8string{ u8"this is a test."sv };
}
''',
name : 'char8 string check',
args : [ '-std=c++2a' ]
)

############################################################################
### char
Expand Down
Loading

0 comments on commit ee9b30c

Please sign in to comment.