Skip to content

Commit 3d653de

Browse files
committed
fixed potential ODR issues relating to exception mode handling (fixes #6)
also: - fixed truncation of floating-point values when using ostreams - fixed some minor documentation issues (fixes #8) - fixed missing value deduction guides for dates and times - added serialization round-trip tests (closes #9) - added node::is_number() - added node_view::is_number() - added node_view::value_or() (closes #7) - added hexfloat parsing support for all implementations, not just <charconv> ones
1 parent 2219fd2 commit 3d653de

24 files changed

+449
-184
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
[![GitHub](https://img.shields.io/github/license/marzer/tomlplusplus)](https://github.com/marzer/tomlplusplus/blob/master/LICENSE)
66

77
`toml++` is a header-only toml parser and serializer for C++17, C++20 and whatever comes after.
8+
- Fully [TOML v0.5.0]-compliant
9+
- Modern C++17 (with some C++20 features where supported)
10+
- Proper UTF-8 handling (incl. BOM)
11+
- Works with or without exceptions
12+
- Doesn't require RTTI
13+
- First-class support for serializing to JSON
14+
- Supports a number of 'unreleased' TOML features (optional)
815

916
<br>
1017

@@ -25,10 +32,10 @@ Reading it in C++ is easy with `toml++`:
2532
auto config = toml::parse_file( "configuration.toml" );
2633

2734
// get key-value pairs
28-
std::string_view library_name = config["library"]["name"].as_string()->get();
29-
std::string_view library_version = config["library"]["version"].as_string()->get();
30-
std::string_view library_author = config["library"]["authors"][0].as_string()->get();
31-
int64_t depends_on_cpp_version = config["dependencies"]["cpp"].as_integer()->get();
35+
std::string_view library_name = config["library"]["name"].value_or(""sv);
36+
std::string_view library_version = config["library"]["version"].value_or(""sv);
37+
std::string_view library_author = config["library"]["authors"][0].value_or(""sv);
38+
int64_t depends_on_cpp_version = config["dependencies"]["cpp"].value_or(0);
3239

3340
// modify the data
3441
config.insert_or_assign("alternatives", toml::array{
@@ -80,7 +87,7 @@ won't need to mess with these at all, butif you do, set them before including to
8087
| Option | Type | Default | Description |
8188
|----------------------------|:--------------:|-----------------------------------|----------------------------------------------------------------------------------------------------------|
8289
| `TOML_ASSERT(expr)` | function macro | `assert(expr)`<br>(or undefined) | Sets the assert function used by the library. |
83-
| `TOML_CHAR_8_STRINGS` | boolean | `0` | Uses C++20 [char8_t]-based strings as the toml string data type. |
90+
| `TOML_CHAR_8_STRINGS` | boolean | `0` | Uses C++20 [char8_t]-based strings as the toml string data type. **_Experimental!_** |
8491
| `TOML_CONFIG_HEADER` | string literal | undefined | Includes the given header file before the rest of the library. |
8592
| `TOML_LARGE_FILES` | boolean | `0` | Uses 32-bit integers for line and column indices (instead of 16-bit). |
8693
| `TOML_SMALL_FLOAT_TYPE` | type name | undefined | If your codebase has an additional 'small' float type (e.g. half-precision), this tells toml++ about it. |

docs/Doxyfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ PREDEFINED = TOML_DOXYGEN=1 \
339339
TOML_ALWAYS_INLINE=inline \
340340
TOML_MAY_THROW= \
341341
TOML_NODISCARD_CTOR= \
342+
TOML_ASYMMETRICAL_EQUALITY_OPS= \
343+
TOML_START="namespace toml" \
344+
TOML_END= \
342345
__cpp_lib_char8_t=201811L
343346
EXPAND_AS_DEFINED =
344347
SKIP_FUNCTION_MACROS = NO

examples/parse_file.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include <toml++/toml.h>
21
#include <iostream>
32
#include <fstream>
3+
#include <toml++/toml.h>
44
#ifdef _WIN32
55
#define WIN32_LEAN_AND_MEAN
66
#include <Windows.h>
@@ -22,9 +22,8 @@ int main(int argc, char** argv)
2222
}
2323
try
2424
{
25-
const auto table = toml::parse(file, std::move(path));
26-
27-
std::cout << table << std::endl;
25+
const auto tbl = toml::parse(file, std::move(path));
26+
std::cout << tbl << std::endl;
2827
}
2928
catch (const toml::parse_error& err)
3029
{

examples/toml_to_json_transcoder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include <toml++/toml.h>
21
#include <iostream>
32
#include <fstream>
3+
#include <toml++/toml.h>
44
#ifdef _WIN32
55
#define WIN32_LEAN_AND_MEAN
66
#include <Windows.h>

include/toml++/toml.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
// macro hygiene
2222
#if TOML_UNDEF_MACROS
23-
#undef TOML_EXCEPTIONS
2423
#undef TOML_USE_STREAMS_FOR_FLOATS
2524
#undef TOML_GCC_ATTR
2625
#undef TOML_PUSH_WARNINGS
@@ -54,6 +53,10 @@
5453
#undef TOML_DOXYGEN
5554
#undef TOML_RELOPS_REORDERING
5655
#undef TOML_ASYMMETRICAL_EQUALITY_OPS
56+
#undef TOML_START
57+
#undef TOML_END
58+
#undef TOML_IMPL_START
59+
#undef TOML_IMPL_END
5760
#endif
5861

5962
/// \mainpage toml++
@@ -126,7 +129,7 @@
126129
///
127130
/// \ecpp
128131
///
129-
/// When exceptions are disabled parsing methods return a toml::parse_error and it is up to the caller
132+
/// When exceptions are disabled parsing methods return a toml::parse_result and it is up to the caller
130133
/// to check if parsing has been successful by examining the return value:
131134
/// \cpp
132135
/// #include <iostream>
@@ -151,7 +154,11 @@
151154
/// return 0;
152155
/// }
153156
/// \ecpp
154-
/// \see toml::parse_file()
157+
///
158+
/// \see
159+
/// - toml::parse_file()
160+
/// - toml::parse_result
161+
/// - toml::parse_error
155162
///
156163
///////////////////////////////////
157164
///
@@ -189,7 +196,7 @@
189196
/// name = "toml++"
190197
/// version = "0.1.0"
191198
/// \eout
192-
/// \see toml::parse()
199+
/// \see toml::parse()
193200
///
194201
///////////////////////////////////
195202
///
@@ -252,7 +259,11 @@
252259
/// 'dinosaurs':
253260
/// \eout
254261
///
255-
/// \see toml::node, toml::node_view, toml::array, toml::table
262+
/// \see
263+
/// - toml::node
264+
/// - toml::node_view
265+
/// - toml::array
266+
/// - toml::table
256267
///
257268
///////////////////////////////////
258269
///
@@ -317,7 +328,9 @@
317328
/// ]
318329
/// }
319330
/// \eout
320-
/// \see toml::default_formatter, toml::json_formatter
331+
/// \see
332+
/// - toml::default_formatter
333+
/// - toml::json_formatter
321334
///
322335
///////////////////////////////////////////////////////////////////////
323336
///

include/toml++/toml_array.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
#include "toml_value.h"
33

4-
namespace toml::impl
4+
TOML_IMPL_START
55
{
66
template <bool is_const>
77
class array_iterator final
@@ -174,8 +174,9 @@ namespace toml::impl
174174
}
175175
}
176176
}
177+
TOML_IMPL_END
177178

178-
namespace toml
179+
TOML_START
179180
{
180181
[[nodiscard]] bool operator == (const table& lhs, const table& rhs) noexcept;
181182

@@ -945,3 +946,4 @@ namespace toml
945946
friend inline std::basic_ostream<CHAR>& operator << (std::basic_ostream<CHAR>&, const array&) TOML_MAY_THROW;
946947
};
947948
}
949+
TOML_END

include/toml++/toml_common.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,17 @@
229229
__VA_ARGS__ [[nodiscard]] friend bool operator != (RHS rhs, LHS lhs) noexcept { return !(lhs == rhs); }
230230
#endif
231231

232+
#if !TOML_DOXYGEN
233+
#if TOML_EXCEPTIONS
234+
#define TOML_START namespace toml { inline namespace wex
235+
#else
236+
#define TOML_START namespace toml { inline namespace woex
237+
#endif
238+
#define TOML_END }
239+
#endif
240+
#define TOML_IMPL_START TOML_START { namespace impl
241+
#define TOML_IMPL_END } TOML_END
242+
232243
#include "toml_version.h"
233244

234245
#define TOML_MAKE_VERSION(maj, min, rev) \
@@ -292,7 +303,9 @@ TOML_POP_WARNINGS
292303
// clang-format on
293304

294305
/// \brief The root namespace for all toml++ functions and types.
295-
namespace toml
306+
namespace toml { }
307+
308+
TOML_START
296309
{
297310
using namespace std::string_literals;
298311
using namespace std::string_view_literals;
@@ -621,9 +634,9 @@ namespace toml
621634

622635
TOML_POP_WARNINGS
623636
}
637+
TOML_END
624638

625-
/// \brief Internal implementation details. No user-serviceable parts within.
626-
namespace toml::impl
639+
TOML_IMPL_START
627640
{
628641
template <typename T>
629642
using string_map = std::map<string, T, std::less<>>; //heterogeneous lookup
@@ -840,8 +853,9 @@ namespace toml::impl
840853

841854
#undef TOML_P2S_DECL
842855
}
856+
TOML_IMPL_END
843857

844-
namespace toml
858+
TOML_START
845859
{
846860
/// \brief Metafunction for determining if a type is a toml::table.
847861
template <typename T>
@@ -905,3 +919,4 @@ namespace toml
905919
}
906920
}
907921
}
922+
TOML_END

include/toml++/toml_date_time.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
#include "toml_common.h"
33

4-
namespace toml
4+
TOML_START
55
{
66
/// \brief A local date.
77
struct date final
@@ -301,3 +301,4 @@ namespace toml
301301
}
302302
};
303303
}
304+
TOML_END

include/toml++/toml_default_formatter.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "toml_array.h"
55
#include "toml_utf8.h"
66

7-
namespace toml::impl
7+
TOML_IMPL_START
88
{
99
TOML_PUSH_WARNINGS
1010
TOML_DISABLE_ALL_WARNINGS
@@ -131,8 +131,9 @@ namespace toml::impl
131131
return (default_formatter_inline_columns(node) + starting_column_bias) > 120_sz;
132132
}
133133
}
134+
TOML_IMPL_END
134135

135-
namespace toml
136+
TOML_START
136137
{
137138
/// \brief A wrapper for printing TOML objects out to a stream as formatted TOML.
138139
///
@@ -494,3 +495,4 @@ namespace toml
494495
return lhs << default_formatter<CHAR>{ rhs };
495496
}
496497
}
498+
TOML_END

include/toml++/toml_formatter.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
#include "toml_print_to_stream.h"
33

4-
namespace toml
4+
TOML_START
55
{
66
/// \brief Format flags for modifying how TOML data is printed to streams.
77
enum class format_flags : uint8_t
@@ -18,8 +18,9 @@ namespace toml
1818
return static_cast<format_flags>( impl::unbox_enum(lhs) | impl::unbox_enum(rhs) );
1919
}
2020
}
21+
TOML_END
2122

22-
namespace toml::impl
23+
TOML_IMPL_START
2324
{
2425
template <typename CHAR = char>
2526
class formatter
@@ -142,3 +143,4 @@ namespace toml::impl
142143
{}
143144
};
144145
}
146+
TOML_IMPL_END

0 commit comments

Comments
 (0)