2020-01-04 14:21:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
////////// CONFIGURATION
|
|
|
|
// clang-format off
|
|
|
|
|
|
|
|
#ifdef TOML_CONFIG_HEADER
|
|
|
|
#include TOML_CONFIG_HEADER
|
|
|
|
#undef TOML_CONFIG_HEADER
|
|
|
|
#endif
|
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
#if !defined(TOML_ALL_INLINE) || (defined(TOML_ALL_INLINE) && TOML_ALL_INLINE)
|
|
|
|
#undef TOML_ALL_INLINE
|
|
|
|
#define TOML_ALL_INLINE 1
|
|
|
|
#endif
|
|
|
|
|
2020-03-02 10:14:54 +00:00
|
|
|
#if defined(TOML_IMPLEMENTATION) || TOML_ALL_INLINE
|
|
|
|
#undef TOML_IMPLEMENTATION
|
|
|
|
#define TOML_IMPLEMENTATION 1
|
|
|
|
#else
|
2020-03-01 14:56:40 +00:00
|
|
|
#define TOML_IMPLEMENTATION 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TOML_API
|
|
|
|
#define TOML_API
|
|
|
|
#endif
|
|
|
|
|
2020-01-04 14:21:38 +00:00
|
|
|
#ifndef TOML_CHAR_8_STRINGS
|
|
|
|
#define TOML_CHAR_8_STRINGS 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TOML_UNRELEASED_FEATURES
|
|
|
|
#define TOML_UNRELEASED_FEATURES 1
|
|
|
|
#endif
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
#ifndef TOML_LARGE_FILES
|
|
|
|
#define TOML_LARGE_FILES 0
|
|
|
|
#endif
|
|
|
|
|
2020-01-04 14:21:38 +00:00
|
|
|
#ifndef TOML_ASSERT
|
|
|
|
#ifdef assert
|
|
|
|
#define TOML_ASSERT(expr) assert(expr)
|
|
|
|
#else
|
|
|
|
#define TOML_ASSERT(expr) (void)0
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TOML_UNDEF_MACROS
|
|
|
|
#define TOML_UNDEF_MACROS 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
////////// COMPILER & ENVIRONMENT STUFF
|
|
|
|
|
|
|
|
#ifndef __cplusplus
|
|
|
|
#error toml++ is a C++ library.
|
|
|
|
#endif
|
|
|
|
|
2020-01-13 06:31:49 +00:00
|
|
|
#if defined(__clang__) || defined(__GNUC__)
|
2020-01-07 15:52:50 +00:00
|
|
|
#define TOML_GCC_ATTR(attr) __attribute__((attr))
|
|
|
|
#else
|
|
|
|
#define TOML_GCC_ATTR(attr)
|
|
|
|
#endif
|
|
|
|
|
2020-01-04 14:21:38 +00:00
|
|
|
#ifdef __clang__
|
|
|
|
|
|
|
|
#define TOML_PUSH_WARNINGS _Pragma("clang diagnostic push")
|
2020-01-07 15:52:50 +00:00
|
|
|
#define TOML_DISABLE_SWITCH_WARNINGS _Pragma("clang diagnostic ignored \"-Wswitch\"")
|
|
|
|
#define TOML_DISABLE_INIT_WARNINGS _Pragma("clang diagnostic ignored \"-Wmissing-field-initializers\"")
|
2020-01-04 14:21:38 +00:00
|
|
|
#define TOML_DISABLE_ALL_WARNINGS _Pragma("clang diagnostic ignored \"-Weverything\"")
|
|
|
|
#define TOML_POP_WARNINGS _Pragma("clang diagnostic pop")
|
2020-01-07 15:52:50 +00:00
|
|
|
#define TOML_ALWAYS_INLINE TOML_GCC_ATTR(__always_inline__) inline
|
2020-01-04 14:21:38 +00:00
|
|
|
#define TOML_ASSUME(cond) __builtin_assume(cond)
|
|
|
|
#define TOML_UNREACHABLE __builtin_unreachable()
|
|
|
|
|
|
|
|
#if __has_declspec_attribute(novtable)
|
|
|
|
#define TOML_INTERFACE __declspec(novtable)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __has_declspec_attribute(empty_bases)
|
|
|
|
#define TOML_EMPTY_BASES __declspec(empty_bases)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//floating-point from_chars and to_chars are not implemented in any version of clang as of 1/1/2020
|
|
|
|
#ifndef TOML_USE_STREAMS_FOR_FLOATS
|
|
|
|
#define TOML_USE_STREAMS_FOR_FLOATS 1
|
|
|
|
#endif
|
|
|
|
|
2020-02-29 20:34:08 +00:00
|
|
|
#elif defined(_MSC_VER) || (defined(__INTEL_COMPILER) && defined(__ICL))
|
2020-01-04 14:21:38 +00:00
|
|
|
|
|
|
|
#define TOML_CPP_VERSION _MSVC_LANG
|
|
|
|
#define TOML_PUSH_WARNINGS __pragma(warning(push))
|
2020-01-07 15:52:50 +00:00
|
|
|
#define TOML_DISABLE_SWITCH_WARNINGS __pragma(warning(disable: 4063))
|
2020-01-04 14:21:38 +00:00
|
|
|
#define TOML_DISABLE_ALL_WARNINGS __pragma(warning(pop)) \
|
|
|
|
__pragma(warning(push, 0))
|
|
|
|
#define TOML_POP_WARNINGS __pragma(warning(pop))
|
|
|
|
#define TOML_ALWAYS_INLINE __forceinline
|
|
|
|
#define TOML_ASSUME(cond) __assume(cond)
|
|
|
|
#define TOML_UNREACHABLE __assume(0)
|
|
|
|
#define TOML_INTERFACE __declspec(novtable)
|
|
|
|
#define TOML_EMPTY_BASES __declspec(empty_bases)
|
|
|
|
|
2020-02-20 21:08:20 +00:00
|
|
|
#if !defined(TOML_RELOPS_REORDERING) && defined(__cpp_impl_three_way_comparison)
|
|
|
|
#define TOML_RELOPS_REORDERING 1
|
|
|
|
#endif
|
|
|
|
|
2020-01-04 14:21:38 +00:00
|
|
|
#elif defined(__GNUC__)
|
|
|
|
|
|
|
|
#define TOML_PUSH_WARNINGS _Pragma("GCC diagnostic push")
|
2020-01-07 15:52:50 +00:00
|
|
|
#define TOML_DISABLE_SWITCH_WARNINGS _Pragma("GCC diagnostic ignored \"-Wswitch\"")
|
|
|
|
#define TOML_DISABLE_INIT_WARNINGS _Pragma("GCC diagnostic ignored \"-Wmissing-field-initializers\"") \
|
|
|
|
_Pragma("GCC diagnostic ignored \"-Wmaybe-uninitialized\"") \
|
|
|
|
_Pragma("GCC diagnostic ignored \"-Wuninitialized\"")
|
|
|
|
#define TOML_DISABLE_ALL_WARNINGS _Pragma("GCC diagnostic ignored \"-Wall\"") \
|
|
|
|
_Pragma("GCC diagnostic ignored \"-Wextra\"") \
|
|
|
|
_Pragma("GCC diagnostic ignored \"-Wchar-subscripts\"") \
|
2020-01-06 18:21:16 +00:00
|
|
|
_Pragma("GCC diagnostic ignored \"-Wtype-limits\"")
|
2020-01-04 14:21:38 +00:00
|
|
|
#define TOML_POP_WARNINGS _Pragma("GCC diagnostic pop")
|
2020-01-07 15:52:50 +00:00
|
|
|
#define TOML_ALWAYS_INLINE TOML_GCC_ATTR(__always_inline__) inline
|
2020-01-04 14:21:38 +00:00
|
|
|
#define TOML_UNREACHABLE __builtin_unreachable()
|
|
|
|
|
|
|
|
//floating-point from_chars and to_chars are not implemented in any version of gcc as of 1/1/2020
|
|
|
|
#ifndef TOML_USE_STREAMS_FOR_FLOATS
|
|
|
|
#define TOML_USE_STREAMS_FOR_FLOATS 1
|
|
|
|
#endif
|
|
|
|
|
2020-02-20 21:08:20 +00:00
|
|
|
#if !defined(TOML_RELOPS_REORDERING) && defined(__cpp_impl_three_way_comparison)
|
|
|
|
#define TOML_RELOPS_REORDERING 1
|
|
|
|
#endif
|
|
|
|
|
2020-01-04 14:21:38 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TOML_CPP_VERSION
|
|
|
|
#define TOML_CPP_VERSION __cplusplus
|
|
|
|
#endif
|
2020-01-07 15:52:50 +00:00
|
|
|
#if TOML_CPP_VERSION < 201103L
|
|
|
|
#error toml++ requires C++17 or higher. For a TOML parser supporting pre-C++11 see https://github.com/ToruNiina/Boost.toml
|
|
|
|
#elif TOML_CPP_VERSION < 201703L
|
|
|
|
#error toml++ requires C++17 or higher. For a TOML parser supporting C++11 see https://github.com/skystrife/cpptoml
|
|
|
|
#elif TOML_CPP_VERSION >= 202600L
|
2020-01-04 14:21:38 +00:00
|
|
|
#define TOML_CPP 26
|
|
|
|
#elif TOML_CPP_VERSION >= 202300L
|
|
|
|
#define TOML_CPP 23
|
|
|
|
#elif TOML_CPP_VERSION >= 202000L
|
|
|
|
#define TOML_CPP 20
|
|
|
|
#elif TOML_CPP_VERSION >= 201703L
|
|
|
|
#define TOML_CPP 17
|
|
|
|
#endif
|
2020-03-03 21:28:24 +00:00
|
|
|
#if !defined(__EXCEPTIONS) && !defined(__cpp_exceptions) && !defined(_CPPUNWIND)
|
2020-03-08 21:54:57 +00:00
|
|
|
#if defined(TOML_EXCEPTIONS) && TOML_EXCEPTIONS
|
|
|
|
#error TOML_EXCEPTIONS was explicitly enabled but exceptions are disabled/unsupported by the compiler.
|
|
|
|
#endif
|
2020-03-03 21:28:24 +00:00
|
|
|
#undef TOML_EXCEPTIONS
|
|
|
|
#define TOML_EXCEPTIONS 0
|
2020-03-08 21:54:57 +00:00
|
|
|
#else
|
|
|
|
#ifndef TOML_EXCEPTIONS
|
|
|
|
#define TOML_EXCEPTIONS 1
|
|
|
|
#endif
|
2020-03-03 21:28:24 +00:00
|
|
|
#endif
|
|
|
|
#if TOML_EXCEPTIONS
|
2020-01-04 14:21:38 +00:00
|
|
|
#define TOML_MAY_THROW
|
2020-01-22 21:29:46 +00:00
|
|
|
#define TOML_MAY_THROW_UNLESS(...) noexcept(__VA_ARGS__)
|
2020-03-03 21:28:24 +00:00
|
|
|
#define TOML_NS1_EX
|
2020-01-04 14:21:38 +00:00
|
|
|
#else
|
2020-01-22 21:29:46 +00:00
|
|
|
#define TOML_MAY_THROW noexcept
|
|
|
|
#define TOML_MAY_THROW_UNLESS(...) noexcept
|
2020-03-03 21:28:24 +00:00
|
|
|
#define TOML_NS1_EX _noex
|
2020-02-26 23:28:20 +00:00
|
|
|
#endif
|
|
|
|
#ifndef TOML_DOXYGEN
|
|
|
|
#define TOML_DOXYGEN 0
|
2020-01-04 14:21:38 +00:00
|
|
|
#endif
|
2020-01-07 15:52:50 +00:00
|
|
|
#ifndef TOML_DISABLE_INIT_WARNINGS
|
|
|
|
#define TOML_DISABLE_INIT_WARNINGS
|
2020-01-04 14:21:38 +00:00
|
|
|
#endif
|
|
|
|
#ifndef TOML_USE_STREAMS_FOR_FLOATS
|
|
|
|
#define TOML_USE_STREAMS_FOR_FLOATS 0
|
|
|
|
#endif
|
|
|
|
#ifndef TOML_PUSH_WARNINGS
|
|
|
|
#define TOML_PUSH_WARNINGS
|
|
|
|
#endif
|
|
|
|
#ifndef TOML_DISABLE_ALL_WARNINGS
|
|
|
|
#define TOML_DISABLE_ALL_WARNINGS
|
|
|
|
#endif
|
|
|
|
#ifndef TOML_POP_WARNINGS
|
|
|
|
#define TOML_POP_WARNINGS
|
|
|
|
#endif
|
|
|
|
#ifndef TOML_INTERFACE
|
|
|
|
#define TOML_INTERFACE
|
|
|
|
#endif
|
|
|
|
#ifndef TOML_EMPTY_BASES
|
|
|
|
#define TOML_EMPTY_BASES
|
|
|
|
#endif
|
|
|
|
#ifndef TOML_ALWAYS_INLINE
|
|
|
|
#define TOML_ALWAYS_INLINE inline
|
|
|
|
#endif
|
|
|
|
#ifndef TOML_ASSUME
|
|
|
|
#define TOML_ASSUME(cond) (void)0
|
|
|
|
#endif
|
|
|
|
#ifndef TOML_UNREACHABLE
|
2020-01-06 18:21:16 +00:00
|
|
|
#define TOML_UNREACHABLE TOML_ASSERT(false)
|
2020-01-04 14:21:38 +00:00
|
|
|
#endif
|
|
|
|
#define TOML_NO_DEFAULT_CASE default: TOML_UNREACHABLE
|
|
|
|
#ifdef __cpp_consteval
|
|
|
|
#define TOML_CONSTEVAL consteval
|
|
|
|
#else
|
|
|
|
#define TOML_CONSTEVAL constexpr
|
|
|
|
#endif
|
|
|
|
#ifndef __INTELLISENSE__
|
|
|
|
#if __has_cpp_attribute(likely)
|
|
|
|
#define TOML_LIKELY [[likely]]
|
|
|
|
#endif
|
|
|
|
#if __has_cpp_attribute(unlikely)
|
|
|
|
#define TOML_UNLIKELY [[unlikely]]
|
|
|
|
#endif
|
|
|
|
#if __has_cpp_attribute(no_unique_address)
|
|
|
|
#define TOML_NO_UNIQUE_ADDRESS [[no_unique_address]]
|
|
|
|
#endif
|
|
|
|
#if __has_cpp_attribute(nodiscard) >= 201907L
|
|
|
|
#define TOML_NODISCARD_CTOR [[nodiscard]]
|
|
|
|
#endif
|
|
|
|
#endif //__INTELLISENSE__
|
|
|
|
#ifndef TOML_LIKELY
|
|
|
|
#define TOML_LIKELY
|
|
|
|
#endif
|
|
|
|
#ifndef TOML_UNLIKELY
|
|
|
|
#define TOML_UNLIKELY
|
|
|
|
#endif
|
|
|
|
#ifndef TOML_NO_UNIQUE_ADDRESS
|
|
|
|
#define TOML_NO_UNIQUE_ADDRESS
|
|
|
|
#endif
|
|
|
|
#ifndef TOML_NODISCARD_CTOR
|
|
|
|
#define TOML_NODISCARD_CTOR
|
|
|
|
#endif
|
2020-02-20 21:08:20 +00:00
|
|
|
#ifndef TOML_RELOPS_REORDERING
|
|
|
|
#define TOML_RELOPS_REORDERING 0
|
|
|
|
#endif
|
|
|
|
#if TOML_RELOPS_REORDERING
|
|
|
|
#define TOML_ASYMMETRICAL_EQUALITY_OPS(...)
|
|
|
|
#else
|
|
|
|
#define TOML_ASYMMETRICAL_EQUALITY_OPS(LHS, RHS, ...) \
|
|
|
|
__VA_ARGS__ [[nodiscard]] friend bool operator == (RHS rhs, LHS lhs) noexcept { return lhs == rhs; } \
|
|
|
|
__VA_ARGS__ [[nodiscard]] friend bool operator != (LHS lhs, RHS rhs) noexcept { return !(lhs == rhs); } \
|
|
|
|
__VA_ARGS__ [[nodiscard]] friend bool operator != (RHS rhs, LHS lhs) noexcept { return !(lhs == rhs); }
|
|
|
|
#endif
|
2020-03-01 14:56:40 +00:00
|
|
|
#if TOML_ALL_INLINE
|
|
|
|
#define TOML_INLINE_FUNC_IMPL inline
|
|
|
|
#else
|
|
|
|
#define TOML_INLINE_FUNC_IMPL
|
|
|
|
#endif
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-01-06 18:21:16 +00:00
|
|
|
#include "toml_version.h"
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-01-06 18:21:16 +00:00
|
|
|
#define TOML_MAKE_VERSION(maj, min, rev) \
|
2020-01-04 14:21:38 +00:00
|
|
|
((maj) * 1000 + (min) * 25 + (rev))
|
|
|
|
|
|
|
|
#if TOML_UNRELEASED_FEATURES
|
2020-01-06 18:21:16 +00:00
|
|
|
#define TOML_LANG_EFFECTIVE_VERSION \
|
2020-02-20 21:08:20 +00:00
|
|
|
TOML_MAKE_VERSION(TOML_LANG_MAJOR, TOML_LANG_MINOR, TOML_LANG_PATCH+1)
|
2020-01-04 14:21:38 +00:00
|
|
|
#else
|
2020-01-06 18:21:16 +00:00
|
|
|
#define TOML_LANG_EFFECTIVE_VERSION \
|
2020-02-20 21:08:20 +00:00
|
|
|
TOML_MAKE_VERSION(TOML_LANG_MAJOR, TOML_LANG_MINOR, TOML_LANG_PATCH)
|
2020-01-04 14:21:38 +00:00
|
|
|
#endif
|
|
|
|
|
2020-01-06 18:21:16 +00:00
|
|
|
#define TOML_LANG_HIGHER_THAN(maj, min, rev) \
|
|
|
|
(TOML_LANG_EFFECTIVE_VERSION > TOML_MAKE_VERSION(maj, min, rev))
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-01-06 18:21:16 +00:00
|
|
|
#define TOML_LANG_AT_LEAST(maj, min, rev) \
|
|
|
|
(TOML_LANG_EFFECTIVE_VERSION >= TOML_MAKE_VERSION(maj, min, rev))
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-01-06 18:21:16 +00:00
|
|
|
#define TOML_LANG_EXACTLY(maj, min, rev) \
|
|
|
|
(TOML_LANG_EFFECTIVE_VERSION == TOML_MAKE_VERSION(maj, min, rev))
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-03-03 21:28:24 +00:00
|
|
|
#ifdef TOML_OPTIONAL_TYPE
|
|
|
|
#define TOML_NS2_OPT _opt
|
|
|
|
#else
|
|
|
|
#define TOML_NS2_OPT
|
|
|
|
#endif
|
|
|
|
|
2020-02-26 23:28:20 +00:00
|
|
|
#if TOML_CHAR_8_STRINGS
|
2020-03-03 21:28:24 +00:00
|
|
|
#define TOML_NS3_CHAR8 _char8
|
2020-02-26 23:28:20 +00:00
|
|
|
#else
|
2020-03-03 21:28:24 +00:00
|
|
|
#define TOML_NS3_CHAR8
|
2020-02-26 23:28:20 +00:00
|
|
|
#endif
|
2020-02-23 11:13:18 +00:00
|
|
|
|
2020-03-03 21:28:24 +00:00
|
|
|
#define TOML_NS4
|
|
|
|
#define TOML_NS5
|
|
|
|
|
2020-02-26 23:28:20 +00:00
|
|
|
#if !TOML_DOXYGEN
|
2020-02-23 11:13:18 +00:00
|
|
|
|
2020-03-03 21:28:24 +00:00
|
|
|
#define TOML_START_2(VER, ARG1, ARG2, ARG3, ARG4, ARG5) \
|
|
|
|
namespace toml { inline namespace v##VER##ARG1##ARG2##ARG3##ARG4##ARG5
|
|
|
|
|
|
|
|
#define TOML_START_1(VER, ARG1, ARG2, ARG3, ARG4, ARG5) \
|
|
|
|
TOML_START_2(VER, ARG1, ARG2, ARG3, ARG4, ARG5)
|
|
|
|
|
|
|
|
#define TOML_START \
|
|
|
|
TOML_START_1( \
|
|
|
|
TOML_LIB_MAJOR, TOML_NS1_EX, TOML_NS2_OPT, \
|
|
|
|
TOML_NS3_CHAR8, TOML_NS4, TOML_NS5 \
|
|
|
|
)
|
|
|
|
|
2020-02-23 11:13:18 +00:00
|
|
|
#define TOML_END }
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#define TOML_IMPL_START TOML_START { namespace impl
|
|
|
|
#define TOML_IMPL_END } TOML_END
|
2020-02-20 21:08:20 +00:00
|
|
|
|
2020-01-04 14:21:38 +00:00
|
|
|
////////// INCLUDES
|
|
|
|
|
|
|
|
TOML_PUSH_WARNINGS
|
|
|
|
TOML_DISABLE_ALL_WARNINGS
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring> //memcpy, memset
|
|
|
|
#include <memory>
|
|
|
|
#include <string_view>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <iosfwd>
|
|
|
|
#include <charconv>
|
2020-03-03 21:28:24 +00:00
|
|
|
#ifndef TOML_OPTIONAL_TYPE
|
|
|
|
#include <optional>
|
|
|
|
#endif
|
2020-01-04 14:21:38 +00:00
|
|
|
#if TOML_USE_STREAMS_FOR_FLOATS
|
|
|
|
#include <sstream>
|
|
|
|
#endif
|
|
|
|
#if TOML_EXCEPTIONS
|
|
|
|
#include <stdexcept>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TOML_POP_WARNINGS
|
|
|
|
|
|
|
|
#if TOML_CHAR_8_STRINGS
|
|
|
|
#if !defined(__cpp_lib_char8_t)
|
|
|
|
#error toml++ requires implementation support to use char8_t strings, but yours does not provide it.
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define TOML_STRING_PREFIX_1(S) u8##S
|
|
|
|
#define TOML_STRING_PREFIX(S) TOML_STRING_PREFIX_1(S)
|
|
|
|
#else
|
|
|
|
#define TOML_STRING_PREFIX(S) S
|
|
|
|
#endif
|
|
|
|
|
|
|
|
////////// FORWARD DECLARATIONS & TYPEDEFS
|
|
|
|
// clang-format on
|
|
|
|
|
2020-01-11 21:15:24 +00:00
|
|
|
/// \brief The root namespace for all toml++ functions and types.
|
2020-02-22 14:10:32 +00:00
|
|
|
namespace toml { }
|
|
|
|
|
|
|
|
TOML_START
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-02-16 13:11:57 +00:00
|
|
|
using namespace std::string_literals;
|
|
|
|
using namespace std::string_view_literals;
|
2020-02-18 21:29:59 +00:00
|
|
|
using size_t = std::size_t;
|
|
|
|
using ptrdiff_t = std::ptrdiff_t;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-02-16 13:11:57 +00:00
|
|
|
[[nodiscard]] TOML_ALWAYS_INLINE
|
|
|
|
TOML_CONSTEVAL size_t operator"" _sz(unsigned long long n) noexcept
|
|
|
|
{
|
|
|
|
return static_cast<size_t>(n);
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if TOML_CHAR_8_STRINGS
|
|
|
|
|
|
|
|
using string_char = char8_t;
|
|
|
|
using string = std::u8string;
|
|
|
|
using string_view = std::u8string_view;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2020-01-12 15:37:02 +00:00
|
|
|
/// \brief The base character type for keys and string values.
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \remarks This will be an alias for char8_t if `TOML_CHAR_8_STRINGS` is `1`.
|
2020-01-04 14:21:38 +00:00
|
|
|
using string_char = char;
|
2020-01-11 21:15:24 +00:00
|
|
|
|
2020-01-12 15:37:02 +00:00
|
|
|
/// \brief The string type for keys and string values.
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \remarks This will be an alias for std::u8string if `TOML_CHAR_8_STRINGS` is `1`.
|
2020-01-04 14:21:38 +00:00
|
|
|
using string = std::string;
|
2020-01-11 21:15:24 +00:00
|
|
|
|
2020-01-12 15:37:02 +00:00
|
|
|
/// \brief The string type for keys and string values.
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \remarks This will be an alias for std::u8string_view if `TOML_CHAR_8_STRINGS` is `1`.
|
2020-01-04 14:21:38 +00:00
|
|
|
using string_view = std::string_view;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-01-11 21:15:24 +00:00
|
|
|
struct date;
|
|
|
|
struct time;
|
|
|
|
struct time_offset;
|
|
|
|
struct date_time;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
|
|
|
class node;
|
|
|
|
class array;
|
|
|
|
class table;
|
2020-03-03 08:10:07 +00:00
|
|
|
template <typename> class node_view;
|
|
|
|
template <typename> class value;
|
|
|
|
template <typename> class default_formatter;
|
|
|
|
template <typename> class json_formatter;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-01-11 21:15:24 +00:00
|
|
|
/// \brief TOML node type identifiers.
|
2020-01-07 15:52:50 +00:00
|
|
|
enum class node_type : uint8_t
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-02-20 21:08:20 +00:00
|
|
|
none, ///< Not-a-node.
|
2020-01-22 21:29:46 +00:00
|
|
|
table, ///< The node is a toml::table.
|
|
|
|
array, ///< The node is a toml::array.
|
|
|
|
string, ///< The node is a toml::value<toml::string>.
|
|
|
|
integer, ///< The node is a toml::value<int64_t>.
|
|
|
|
floating_point, ///< The node is a toml::value<double>.
|
|
|
|
boolean, ///< The node is a toml::value<bool>.
|
2020-02-16 13:11:57 +00:00
|
|
|
date, ///< The node is a toml::value<date>.
|
|
|
|
time, ///< The node is a toml::value<time>.
|
|
|
|
date_time ///< The node is a toml::value<date_time>.
|
2020-01-07 15:52:50 +00:00
|
|
|
};
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
#if TOML_LARGE_FILES
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
using source_index = uint32_t;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
#else
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-01-11 21:15:24 +00:00
|
|
|
/// \brief The integer type used to tally line numbers and columns.
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \remarks This will be an alias for uint32_t if `TOML_LARGE_FILES` is `1`.
|
2020-01-07 15:52:50 +00:00
|
|
|
using source_index = uint16_t;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
#endif
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-03-03 21:28:24 +00:00
|
|
|
#ifdef TOML_OPTIONAL_TYPE
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using optional = TOML_OPTIONAL_TYPE<T>;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/// \brief The 'optional' type used throughout the library.
|
|
|
|
///
|
|
|
|
/// \remarks By default this will be a simple alias for std::optional, but you can change the optional type
|
|
|
|
/// used by the library by setting `TOML_OPTIONAL_TYPE`.
|
|
|
|
template <typename T>
|
|
|
|
using optional = std::optional<T>;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-01-11 21:15:24 +00:00
|
|
|
/// \brief A source document line-and-column pair.
|
2020-02-03 09:12:43 +00:00
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// auto table = toml::parse_file("config.toml"sv);
|
|
|
|
/// std::cout << "The node 'description' was defined at "sv
|
|
|
|
/// << table.get("description")->source().begin()
|
|
|
|
/// << std::endl;
|
|
|
|
///
|
|
|
|
/// \ecpp
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \out
|
|
|
|
/// The value 'description' was defined at line 7, column 15
|
|
|
|
/// \eout
|
|
|
|
///
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \remarks toml++'s parser is unicode-aware insofar as it knows how to handle
|
|
|
|
/// various non-conventional whitespace and newline characters, but it doesn't give
|
|
|
|
/// much thought to combining marks, grapheme clusters vs. characters, et cetera.
|
|
|
|
/// If a TOML document contains lots of codepoints outside of the ASCII range
|
|
|
|
/// you may find that your source_positions don't match those given by a text editor
|
|
|
|
/// (typically the line numbers will be accurate but column numbers will be too high).
|
2020-02-18 21:29:59 +00:00
|
|
|
/// <strong>This is not an error.</strong> I've chosen this behaviour as a deliberate trade-off
|
2020-02-03 09:12:43 +00:00
|
|
|
/// between parser complexity and correctness.
|
2020-01-12 15:37:02 +00:00
|
|
|
struct source_position
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-01-11 21:15:24 +00:00
|
|
|
/// \brief The line number.
|
|
|
|
/// \remarks Valid line numbers start at 1.
|
|
|
|
source_index line;
|
|
|
|
|
|
|
|
/// \brief The column number.
|
|
|
|
/// \remarks Valid column numbers start at 1.
|
|
|
|
source_index column;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-01-11 21:15:24 +00:00
|
|
|
/// \brief Returns true if both line and column numbers are non-zero.
|
2020-01-04 14:21:38 +00:00
|
|
|
[[nodiscard]]
|
2020-01-06 18:21:16 +00:00
|
|
|
explicit constexpr operator bool () const noexcept
|
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
return line > source_index{} && column > source_index{};
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \brief Returns true if two source_positions represent the same line and column.
|
2020-01-06 18:21:16 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
friend constexpr bool operator == (const source_position& lhs, const source_position& rhs) noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
|
|
|
return lhs.line == rhs.line
|
|
|
|
&& lhs.column == rhs.column;
|
|
|
|
}
|
|
|
|
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \brief Returns true if two source_positions do not represent the same line and column.
|
2020-01-04 14:21:38 +00:00
|
|
|
[[nodiscard]]
|
2020-01-06 18:21:16 +00:00
|
|
|
friend constexpr bool operator != (const source_position& lhs, const source_position& rhs) noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
|
|
|
return lhs.line != rhs.line
|
|
|
|
|| lhs.column != rhs.column;
|
|
|
|
}
|
|
|
|
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \brief Returns true if the LHS position is before the RHS position.
|
2020-01-04 14:21:38 +00:00
|
|
|
[[nodiscard]]
|
2020-01-06 18:21:16 +00:00
|
|
|
friend constexpr bool operator < (const source_position& lhs, const source_position& rhs) noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
|
|
|
return lhs.line < rhs.line
|
|
|
|
|| (lhs.line == rhs.line && lhs.column < rhs.column);
|
|
|
|
}
|
|
|
|
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \brief Returns true if the LHS position is before the RHS position or equal to it.
|
2020-01-04 14:21:38 +00:00
|
|
|
[[nodiscard]]
|
2020-01-06 18:21:16 +00:00
|
|
|
friend constexpr bool operator <= (const source_position& lhs, const source_position& rhs) noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
|
|
|
return lhs.line < rhs.line
|
|
|
|
|| (lhs.line == rhs.line && lhs.column <= rhs.column);
|
|
|
|
}
|
2020-01-06 18:21:16 +00:00
|
|
|
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \brief Prints a source_position to a stream.
|
2020-02-18 21:29:59 +00:00
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// auto tbl = toml::parse("bar = 42"sv);
|
|
|
|
///
|
|
|
|
/// std::cout << "The value for 'bar' was found on "sv
|
|
|
|
/// << tbl.get("bar")->source()
|
|
|
|
/// << std::endl;
|
|
|
|
///
|
|
|
|
/// \ecpp
|
|
|
|
///
|
|
|
|
/// \out
|
|
|
|
/// The value for 'bar' was found on line 1, column 7
|
|
|
|
/// \eout
|
|
|
|
///
|
|
|
|
/// \param lhs The stream.
|
|
|
|
/// \param rhs The source_position.
|
|
|
|
///
|
|
|
|
/// \returns The input stream.
|
2020-01-06 18:21:16 +00:00
|
|
|
template <typename CHAR>
|
|
|
|
friend std::basic_ostream<CHAR>& operator << (std::basic_ostream<CHAR>& lhs, const source_position& rhs)
|
2020-02-03 09:12:43 +00:00
|
|
|
TOML_MAY_THROW;
|
2020-01-04 14:21:38 +00:00
|
|
|
};
|
|
|
|
|
2020-01-11 21:15:24 +00:00
|
|
|
/// \brief A pointer to a shared string resource containing a source path.
|
2020-01-06 18:21:16 +00:00
|
|
|
using source_path_ptr = std::shared_ptr<const std::string>;
|
|
|
|
|
2020-01-11 21:15:24 +00:00
|
|
|
/// \brief A source document region.
|
2020-02-03 09:12:43 +00:00
|
|
|
///
|
|
|
|
/// \detail \cpp
|
2020-02-18 21:29:59 +00:00
|
|
|
/// #include <fstream>
|
|
|
|
///
|
|
|
|
/// auto tbl = toml::parse_file("config.toml"sv);
|
|
|
|
/// if (auto server = tbl.get("server"))
|
|
|
|
/// {
|
|
|
|
/// std::cout << "begin: "sv << server->source().begin << std::endl;
|
|
|
|
/// std::cout << "end: "sv << server->source().end << std::endl;
|
|
|
|
/// std::cout << "path: "sv << *server->source().path << std::endl;
|
|
|
|
/// }
|
2020-02-03 09:12:43 +00:00
|
|
|
///
|
|
|
|
/// \ecpp
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \out
|
|
|
|
/// begin: line 3, column 1
|
|
|
|
/// end: line 3, column 22
|
|
|
|
/// path: config.toml
|
|
|
|
/// \eout
|
|
|
|
///
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \remarks toml++'s parser is unicode-aware insofar as it knows how to handle
|
|
|
|
/// various non-conventional whitespace and newline characters, but it doesn't give
|
|
|
|
/// much thought to combining marks, grapheme clusters vs. characters, et cetera.
|
|
|
|
/// If a TOML document contains lots of codepoints outside of the ASCII range
|
|
|
|
/// you may find that your source_positions don't match those given by a text editor
|
|
|
|
/// (typically the line numbers will be accurate but column numbers will be too high).
|
2020-02-18 21:29:59 +00:00
|
|
|
/// <strong>This is not an error.</strong> I've chosen this behaviour as a deliberate trade-off
|
2020-02-03 09:12:43 +00:00
|
|
|
/// between parser complexity and correctness.
|
2020-01-12 15:37:02 +00:00
|
|
|
struct source_region
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-01-11 21:15:24 +00:00
|
|
|
/// \brief The beginning of the region (inclusive).
|
2020-01-04 14:21:38 +00:00
|
|
|
source_position begin;
|
2020-01-11 21:15:24 +00:00
|
|
|
|
|
|
|
/// \brief The end of the region (exclusive).
|
2020-01-04 14:21:38 +00:00
|
|
|
source_position end;
|
2020-01-11 21:15:24 +00:00
|
|
|
|
|
|
|
/// \brief The path to the corresponding source document.
|
|
|
|
///
|
|
|
|
/// \remarks This will be `nullptr` if no path was provided to toml::parse().
|
2020-01-06 18:21:16 +00:00
|
|
|
source_path_ptr path;
|
2020-02-03 09:12:43 +00:00
|
|
|
|
|
|
|
/// \brief Prints a source_region to a stream.
|
2020-02-18 21:29:59 +00:00
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// auto tbl = toml::parse("bar = 42", "config.toml");
|
|
|
|
///
|
|
|
|
/// std::cout << "The value for 'bar' was found on "sv
|
|
|
|
/// << tbl.get("bar")->source()
|
|
|
|
/// << std::endl;
|
|
|
|
///
|
|
|
|
/// \ecpp
|
|
|
|
///
|
|
|
|
/// \out
|
|
|
|
/// The value for 'bar' was found on line 1, column 7 of 'config.toml'
|
|
|
|
/// \eout
|
|
|
|
///
|
|
|
|
/// \param lhs The stream.
|
|
|
|
/// \param rhs The source_position.
|
|
|
|
///
|
|
|
|
/// \returns The input stream.
|
2020-02-03 09:12:43 +00:00
|
|
|
template <typename CHAR>
|
|
|
|
friend std::basic_ostream<CHAR>& operator << (std::basic_ostream<CHAR>& lhs, const source_region& rhs)
|
|
|
|
TOML_MAY_THROW;
|
2020-01-04 14:21:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TOML_PUSH_WARNINGS
|
2020-01-07 15:52:50 +00:00
|
|
|
TOML_DISABLE_INIT_WARNINGS
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-01-13 06:31:49 +00:00
|
|
|
#if TOML_DOXYGEN || !TOML_EXCEPTIONS
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-01-13 06:31:49 +00:00
|
|
|
/// \brief An error thrown/returned when parsing fails.
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \remarks This class inherits from std::runtime_error when exceptions are enabled.
|
2020-01-13 06:31:49 +00:00
|
|
|
/// The public interface is the same regardless of exception mode.
|
2020-01-04 14:21:38 +00:00
|
|
|
class parse_error final
|
|
|
|
{
|
|
|
|
private:
|
2020-01-13 06:31:49 +00:00
|
|
|
std::string description_;
|
2020-01-07 15:52:50 +00:00
|
|
|
source_region source_;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2020-01-13 06:31:49 +00:00
|
|
|
TOML_NODISCARD_CTOR
|
|
|
|
parse_error(std::string&& desc, source_region&& src) noexcept
|
|
|
|
: description_{ std::move(desc) },
|
2020-01-11 21:15:24 +00:00
|
|
|
source_{ std::move(src) }
|
2020-01-07 15:52:50 +00:00
|
|
|
{}
|
|
|
|
|
2020-01-13 06:31:49 +00:00
|
|
|
TOML_NODISCARD_CTOR
|
|
|
|
parse_error(std::string&& desc, const source_region& src) noexcept
|
|
|
|
: parse_error{ std::move(desc), source_region{ src } }
|
2020-01-04 14:21:38 +00:00
|
|
|
{}
|
|
|
|
|
2020-01-13 06:31:49 +00:00
|
|
|
TOML_NODISCARD_CTOR
|
|
|
|
parse_error(std::string&& desc, const source_position& position, const source_path_ptr& path = {}) noexcept
|
|
|
|
: parse_error{ std::move(desc), source_region{ position, position, path } }
|
2020-01-07 15:52:50 +00:00
|
|
|
{}
|
|
|
|
|
2020-01-13 06:31:49 +00:00
|
|
|
|
|
|
|
/// \brief Returns a textual description of the error.
|
2020-01-07 15:52:50 +00:00
|
|
|
[[nodiscard]]
|
2020-01-11 21:15:24 +00:00
|
|
|
std::string_view description() const noexcept
|
|
|
|
{
|
2020-01-13 06:31:49 +00:00
|
|
|
return description_;
|
2020-01-11 21:15:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 06:31:49 +00:00
|
|
|
/// \brief Returns the region of the source document responsible for the error.
|
2020-01-11 21:15:24 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
const source_region& source() const noexcept
|
2020-01-07 15:52:50 +00:00
|
|
|
{
|
|
|
|
return source_;
|
|
|
|
}
|
2020-02-25 21:11:40 +00:00
|
|
|
|
|
|
|
/// \brief Prints a parse_error to a stream.
|
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// try
|
|
|
|
/// {
|
|
|
|
/// auto tbl = toml::parse("enabled = trUe"sv);
|
|
|
|
/// }
|
|
|
|
/// catch (const toml::parse_error & err)
|
|
|
|
/// {
|
|
|
|
/// std::cerr << "Parsing failed:\n"sv << err << std::endl;
|
|
|
|
/// }
|
|
|
|
/// \ecpp
|
|
|
|
///
|
|
|
|
/// \out
|
|
|
|
/// Parsing failed:
|
|
|
|
/// Encountered unexpected character while parsing boolean; expected 'true', saw 'trU'
|
|
|
|
/// (error occurred at line 1, column 13)
|
|
|
|
/// \eout
|
|
|
|
///
|
|
|
|
/// \param lhs The stream.
|
|
|
|
/// \param rhs The parse_error.
|
|
|
|
///
|
|
|
|
/// \returns The input stream.
|
|
|
|
template <typename CHAR>
|
|
|
|
friend std::basic_ostream<CHAR>& operator << (std::basic_ostream<CHAR>& lhs, const parse_error& rhs)
|
|
|
|
TOML_MAY_THROW;
|
2020-01-07 15:52:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
class parse_error final
|
2020-01-13 06:31:49 +00:00
|
|
|
: public std::runtime_error
|
2020-01-07 15:52:50 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
source_region source_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2020-01-13 06:31:49 +00:00
|
|
|
TOML_NODISCARD_CTOR TOML_GCC_ATTR(nonnull)
|
|
|
|
parse_error(const char* desc, source_region&& src) noexcept
|
|
|
|
: std::runtime_error{ desc },
|
2020-01-11 21:15:24 +00:00
|
|
|
source_{ std::move(src) }
|
2020-01-04 14:21:38 +00:00
|
|
|
{}
|
|
|
|
|
2020-01-13 06:31:49 +00:00
|
|
|
TOML_NODISCARD_CTOR TOML_GCC_ATTR(nonnull)
|
|
|
|
parse_error(const char* desc, const source_region& src) noexcept
|
|
|
|
: parse_error{ desc, source_region{ src } }
|
2020-01-04 14:21:38 +00:00
|
|
|
{}
|
|
|
|
|
2020-01-13 06:31:49 +00:00
|
|
|
TOML_NODISCARD_CTOR TOML_GCC_ATTR(nonnull)
|
|
|
|
parse_error(const char* desc, const source_position& position, const source_path_ptr& path = {}) noexcept
|
|
|
|
: parse_error{ desc, source_region{ position, position, path } }
|
2020-01-04 14:21:38 +00:00
|
|
|
{}
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
[[nodiscard]]
|
2020-01-11 21:15:24 +00:00
|
|
|
std::string_view description() const noexcept
|
2020-01-07 15:52:50 +00:00
|
|
|
{
|
2020-01-13 06:31:49 +00:00
|
|
|
return std::string_view{ what() };
|
2020-01-07 15:52:50 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 14:21:38 +00:00
|
|
|
[[nodiscard]]
|
2020-01-11 21:15:24 +00:00
|
|
|
const source_region& source() const noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
return source_;
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
2020-02-25 21:11:40 +00:00
|
|
|
|
|
|
|
template <typename CHAR>
|
|
|
|
friend std::basic_ostream<CHAR>& operator << (std::basic_ostream<CHAR>& lhs, const parse_error& rhs)
|
|
|
|
TOML_MAY_THROW;
|
2020-01-04 14:21:38 +00:00
|
|
|
};
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
TOML_POP_WARNINGS
|
|
|
|
}
|
2020-02-22 14:10:32 +00:00
|
|
|
TOML_END
|
2020-01-07 15:52:50 +00:00
|
|
|
|
2020-02-22 14:10:32 +00:00
|
|
|
TOML_IMPL_START
|
2020-01-07 15:52:50 +00:00
|
|
|
{
|
2020-01-11 21:15:24 +00:00
|
|
|
template <typename T>
|
|
|
|
using string_map = std::map<string, T, std::less<>>; //heterogeneous lookup
|
|
|
|
|
2020-02-03 09:12:43 +00:00
|
|
|
#if defined(__cpp_lib_remove_cvref) || (defined(_MSC_VER) && defined(_HAS_CXX20) && _HAS_CXX20)
|
2020-01-07 15:52:50 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using remove_cvref_t = std::remove_cvref_t<T>;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-01-22 21:29:46 +00:00
|
|
|
template <typename T, typename... U>
|
|
|
|
struct is_one_of_ : std::integral_constant<bool,
|
|
|
|
(false || ... || std::is_same_v<T, U>)
|
|
|
|
> {};
|
|
|
|
|
|
|
|
template <typename T, typename... U>
|
|
|
|
inline constexpr bool is_one_of = is_one_of_<T, U...>::value;
|
|
|
|
|
|
|
|
template <typename T, typename... U>
|
|
|
|
using enable_if_one_of = std::enable_if_t<is_one_of<T, U...>>;
|
|
|
|
|
|
|
|
template <typename T, typename... U>
|
|
|
|
using enable_if_not_one_of = std::enable_if_t<!is_one_of<T, U...>>;
|
|
|
|
|
2020-01-12 15:37:02 +00:00
|
|
|
template <typename T>
|
|
|
|
[[nodiscard]] TOML_ALWAYS_INLINE
|
2020-02-03 09:12:43 +00:00
|
|
|
constexpr std::underlying_type_t<T> unbox_enum(T val) noexcept
|
2020-01-12 15:37:02 +00:00
|
|
|
{
|
|
|
|
return static_cast<std::underlying_type_t<T>>(val);
|
|
|
|
}
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
// Q: "why not use std::find??"
|
|
|
|
// A: Because <algorithm> is _huge_ and std::find would be the only thing I used from it.
|
2020-02-16 13:11:57 +00:00
|
|
|
// I don't want to impose such a heavy compile-time burden on users.
|
2020-01-07 15:52:50 +00:00
|
|
|
|
|
|
|
template <typename T>
|
2020-03-03 21:28:24 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
inline const T* find(const std::vector<T>& haystack, const T& needle) noexcept
|
2020-01-07 15:52:50 +00:00
|
|
|
{
|
2020-02-16 13:11:57 +00:00
|
|
|
for (size_t i = 0, e = haystack.size(); i < e; i++)
|
2020-01-07 15:52:50 +00:00
|
|
|
if (haystack[i] == needle)
|
2020-03-03 21:28:24 +00:00
|
|
|
return haystack.data() + i;
|
|
|
|
return nullptr;
|
2020-01-07 15:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class parser;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline constexpr bool is_value =
|
|
|
|
std::is_same_v<T, string>
|
|
|
|
|| std::is_same_v<T, int64_t>
|
|
|
|
|| std::is_same_v<T, double>
|
|
|
|
|| std::is_same_v<T, bool>
|
|
|
|
|| std::is_same_v<T, date>
|
|
|
|
|| std::is_same_v<T, time>
|
|
|
|
|| std::is_same_v<T, date_time>;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline constexpr bool is_value_or_promotable =
|
|
|
|
is_value<T>
|
2020-02-03 09:12:43 +00:00
|
|
|
|| std::is_same_v<std::decay_t<T>, string_char*>
|
2020-01-07 15:52:50 +00:00
|
|
|
|| std::is_same_v<T, string_view>
|
|
|
|
|| std::is_same_v<T, int32_t>
|
|
|
|
|| std::is_same_v<T, int16_t>
|
|
|
|
|| std::is_same_v<T, int8_t>
|
|
|
|
|| std::is_same_v<T, uint32_t>
|
|
|
|
|| std::is_same_v<T, uint16_t>
|
|
|
|
|| std::is_same_v<T, uint8_t>
|
2020-01-13 06:31:49 +00:00
|
|
|
|| std::is_same_v<T, float>
|
|
|
|
#ifdef TOML_SMALL_FLOAT_TYPE
|
|
|
|
|| std::is_same_v<T, TOML_SMALL_FLOAT_TYPE>
|
|
|
|
#endif
|
2020-02-03 09:12:43 +00:00
|
|
|
#ifdef TOML_SMALL_INT_TYPE
|
|
|
|
|| std::is_same_v<T, TOML_SMALL_INT_TYPE>
|
|
|
|
#endif
|
|
|
|
;
|
2020-01-07 15:52:50 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline constexpr bool is_value_or_node =
|
|
|
|
is_value<T>
|
|
|
|
|| std::is_same_v<T, array>
|
|
|
|
|| std::is_same_v<T, table>;
|
|
|
|
|
|
|
|
template <typename T> struct node_wrapper { using type = T; };
|
|
|
|
template <> struct node_wrapper<string> { using type = value<string>; };
|
|
|
|
template <> struct node_wrapper<int64_t> { using type = value<int64_t>; };
|
|
|
|
template <> struct node_wrapper<double> { using type = value<double>; };
|
|
|
|
template <> struct node_wrapper<bool> { using type = value<bool>; };
|
|
|
|
template <> struct node_wrapper<date> { using type = value<date>; };
|
|
|
|
template <> struct node_wrapper<time> { using type = value<time>; };
|
|
|
|
template <> struct node_wrapper<date_time> { using type = value<date_time>; };
|
|
|
|
|
2020-02-16 13:11:57 +00:00
|
|
|
template <typename T>
|
|
|
|
using node_of = typename impl::node_wrapper<T>::type;
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
template <typename T> struct node_unwrapper { using type = T; };
|
|
|
|
template <typename T> struct node_unwrapper<value<T>> { using type = T; };
|
|
|
|
|
2020-02-03 09:12:43 +00:00
|
|
|
template <typename T> using unwrapped = typename impl::node_unwrapper<T>::type;
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
template <typename T> struct value_promoter { using type = T; };
|
2020-02-03 09:12:43 +00:00
|
|
|
template <size_t N> struct value_promoter<const string_char[N]> { using type = string; };
|
|
|
|
template <size_t N> struct value_promoter<const string_char(&)[N]> { using type = string; };
|
|
|
|
template <size_t N> struct value_promoter<const string_char(&&)[N]> { using type = string; };
|
|
|
|
template <> struct value_promoter<const string_char*> { using type = string; };
|
|
|
|
template <size_t N> struct value_promoter<string_char[N]> { using type = string; };
|
|
|
|
template <size_t N> struct value_promoter<string_char(&)[N]> { using type = string; };
|
|
|
|
template <size_t N> struct value_promoter<string_char(&&)[N]> { using type = string; };
|
|
|
|
template <> struct value_promoter<string_char*> { using type = string; };
|
2020-01-07 15:52:50 +00:00
|
|
|
template <> struct value_promoter<string_view> { using type = string; };
|
|
|
|
template <> struct value_promoter<int32_t> { using type = int64_t; };
|
|
|
|
template <> struct value_promoter<int16_t> { using type = int64_t; };
|
|
|
|
template <> struct value_promoter<int8_t> { using type = int64_t; };
|
|
|
|
template <> struct value_promoter<uint32_t> { using type = int64_t; };
|
|
|
|
template <> struct value_promoter<uint16_t> { using type = int64_t; };
|
|
|
|
template <> struct value_promoter<uint8_t> { using type = int64_t; };
|
|
|
|
template <> struct value_promoter<float> { using type = double; };
|
2020-01-13 06:31:49 +00:00
|
|
|
#ifdef TOML_SMALL_FLOAT_TYPE
|
|
|
|
template <> struct value_promoter<TOML_SMALL_FLOAT_TYPE> { using type = double; };
|
|
|
|
#endif
|
2020-02-03 09:12:43 +00:00
|
|
|
#ifdef TOML_SMALL_INT_TYPE
|
|
|
|
template <> struct value_promoter<TOML_SMALL_INT_TYPE> { using type = int64_t; };
|
|
|
|
#endif
|
2020-01-11 21:15:24 +00:00
|
|
|
template <typename T> using promoted = typename impl::value_promoter<T>::type;
|
2020-01-07 15:52:50 +00:00
|
|
|
|
2020-01-22 21:29:46 +00:00
|
|
|
template <typename T> struct node_type_of_;
|
|
|
|
template <> struct node_type_of_<table> { static constexpr auto value = node_type::table; };
|
|
|
|
template <> struct node_type_of_<array> { static constexpr auto value = node_type::array; };
|
|
|
|
template <> struct node_type_of_<string> { static constexpr auto value = node_type::string; };
|
|
|
|
template <> struct node_type_of_<int64_t> { static constexpr auto value = node_type::integer; };
|
|
|
|
template <> struct node_type_of_<double> { static constexpr auto value = node_type::floating_point; };
|
|
|
|
template <> struct node_type_of_<bool> { static constexpr auto value = node_type::boolean; };
|
|
|
|
template <> struct node_type_of_<date> { static constexpr auto value = node_type::date; };
|
|
|
|
template <> struct node_type_of_<time> { static constexpr auto value = node_type::time; };
|
|
|
|
template <> struct node_type_of_<date_time> { static constexpr auto value = node_type::date_time; };
|
|
|
|
|
|
|
|
template <typename T>
|
2020-02-20 21:08:20 +00:00
|
|
|
inline constexpr auto node_type_of = node_type_of_<promoted<typename node_unwrapper<remove_cvref_t<T>>::type>>::value;
|
2020-01-22 21:29:46 +00:00
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
inline constexpr toml::string_view low_character_escape_table[] =
|
|
|
|
{
|
|
|
|
TOML_STRING_PREFIX("\\u0000"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u0001"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u0002"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u0003"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u0004"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u0005"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u0006"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u0007"sv),
|
|
|
|
TOML_STRING_PREFIX("\\b"sv),
|
|
|
|
TOML_STRING_PREFIX("\\t"sv),
|
|
|
|
TOML_STRING_PREFIX("\\n"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u000B"sv),
|
|
|
|
TOML_STRING_PREFIX("\\f"sv),
|
|
|
|
TOML_STRING_PREFIX("\\r"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u000E"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u000F"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u0010"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u0011"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u0012"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u0013"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u0014"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u0015"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u0016"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u0017"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u0018"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u0019"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u001A"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u001B"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u001C"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u001D"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u001E"sv),
|
|
|
|
TOML_STRING_PREFIX("\\u001F"sv),
|
|
|
|
};
|
|
|
|
|
|
|
|
inline constexpr std::string_view node_type_friendly_names[] =
|
|
|
|
{
|
2020-02-20 21:08:20 +00:00
|
|
|
"none"sv,
|
2020-01-07 15:52:50 +00:00
|
|
|
"table"sv,
|
|
|
|
"array"sv,
|
|
|
|
"string"sv,
|
|
|
|
"integer"sv,
|
|
|
|
"floating-point"sv,
|
|
|
|
"boolean"sv,
|
|
|
|
"date"sv,
|
|
|
|
"time"sv,
|
|
|
|
"date-time"sv
|
|
|
|
};
|
2020-01-11 21:15:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define TOML_P2S_DECL(linkage, type) \
|
|
|
|
template <typename CHAR> \
|
|
|
|
linkage void print_to_stream(type, std::basic_ostream<CHAR>&) TOML_MAY_THROW
|
|
|
|
|
|
|
|
TOML_P2S_DECL(TOML_ALWAYS_INLINE, int8_t);
|
|
|
|
TOML_P2S_DECL(TOML_ALWAYS_INLINE, int16_t);
|
|
|
|
TOML_P2S_DECL(TOML_ALWAYS_INLINE, int32_t);
|
|
|
|
TOML_P2S_DECL(TOML_ALWAYS_INLINE, int64_t);
|
|
|
|
TOML_P2S_DECL(TOML_ALWAYS_INLINE, uint8_t);
|
|
|
|
TOML_P2S_DECL(TOML_ALWAYS_INLINE, uint16_t);
|
|
|
|
TOML_P2S_DECL(TOML_ALWAYS_INLINE, uint32_t);
|
|
|
|
TOML_P2S_DECL(TOML_ALWAYS_INLINE, uint64_t);
|
|
|
|
TOML_P2S_DECL(TOML_ALWAYS_INLINE, float);
|
|
|
|
TOML_P2S_DECL(TOML_ALWAYS_INLINE, double);
|
|
|
|
TOML_P2S_DECL(inline, const date&);
|
|
|
|
TOML_P2S_DECL(inline, const time&);
|
|
|
|
TOML_P2S_DECL(inline, time_offset);
|
|
|
|
TOML_P2S_DECL(inline, const date_time&);
|
|
|
|
|
|
|
|
#undef TOML_P2S_DECL
|
2020-01-07 15:52:50 +00:00
|
|
|
}
|
2020-02-22 14:10:32 +00:00
|
|
|
TOML_IMPL_END
|
2020-01-07 15:52:50 +00:00
|
|
|
|
2020-02-22 14:10:32 +00:00
|
|
|
TOML_START
|
2020-01-07 15:52:50 +00:00
|
|
|
{
|
2020-01-13 06:31:49 +00:00
|
|
|
/// \brief Metafunction for determining if a type is a toml::table.
|
|
|
|
template <typename T>
|
|
|
|
inline constexpr bool is_table = std::is_same_v<impl::remove_cvref_t<T>, table>;
|
|
|
|
/// \brief Metafunction for determining if a type is a toml::array.
|
|
|
|
template <typename T>
|
|
|
|
inline constexpr bool is_array = std::is_same_v<impl::remove_cvref_t<T>, array>;
|
2020-02-16 13:11:57 +00:00
|
|
|
/// \brief Metafunction for determining if a type is a toml::string or toml::value<toml::string>.
|
2020-01-13 06:31:49 +00:00
|
|
|
template <typename T>
|
2020-02-16 13:11:57 +00:00
|
|
|
inline constexpr bool is_string = std::is_same_v<impl::node_of<impl::remove_cvref_t<T>>, value<string>>;
|
|
|
|
/// \brief Metafunction for determining if a type is an int64_t or toml::value<int64_t>.
|
2020-01-13 06:31:49 +00:00
|
|
|
template <typename T>
|
2020-02-16 13:11:57 +00:00
|
|
|
inline constexpr bool is_integer = std::is_same_v<impl::node_of<impl::remove_cvref_t<T>>, value<int64_t>>;
|
|
|
|
/// \brief Metafunction for determining if a type is a double or toml::value<double>.
|
2020-01-13 06:31:49 +00:00
|
|
|
template <typename T>
|
2020-02-16 13:11:57 +00:00
|
|
|
inline constexpr bool is_floating_point = std::is_same_v<impl::node_of<impl::remove_cvref_t<T>>, value<double>>;
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Metafunction for determining if a type satisfies `toml::is_integer || toml::is_floating_point`.
|
|
|
|
template <typename T>
|
|
|
|
inline constexpr bool is_number = is_integer<T> || is_floating_point<T>;
|
2020-02-16 13:11:57 +00:00
|
|
|
/// \brief Metafunction for determining if a type is a bool toml::value<bool>.
|
2020-01-13 06:31:49 +00:00
|
|
|
template <typename T>
|
2020-02-16 13:11:57 +00:00
|
|
|
inline constexpr bool is_boolean = std::is_same_v<impl::node_of<impl::remove_cvref_t<T>>, value<bool>>;
|
|
|
|
/// \brief Metafunction for determining if a type is a toml::date or toml::value<date>.
|
2020-01-13 06:31:49 +00:00
|
|
|
template <typename T>
|
2020-02-16 13:11:57 +00:00
|
|
|
inline constexpr bool is_date = std::is_same_v<impl::node_of<impl::remove_cvref_t<T>>, value<date>>;
|
|
|
|
/// \brief Metafunction for determining if a type is a toml::time or toml::value<time>.
|
2020-01-13 06:31:49 +00:00
|
|
|
template <typename T>
|
2020-02-16 13:11:57 +00:00
|
|
|
inline constexpr bool is_time = std::is_same_v<impl::node_of<impl::remove_cvref_t<T>>, value<time>>;
|
|
|
|
/// \brief Metafunction for determining if a type is a toml::date_time or toml::value<date_time>.
|
2020-01-13 06:31:49 +00:00
|
|
|
template <typename T>
|
2020-02-16 13:11:57 +00:00
|
|
|
inline constexpr bool is_date_time = std::is_same_v<impl::node_of<impl::remove_cvref_t<T>>, value<date_time>>;
|
2020-01-12 15:37:02 +00:00
|
|
|
|
|
|
|
/// \brief Pretty-prints the value of a node_type to a stream.
|
2020-02-16 13:11:57 +00:00
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// auto arr = toml::array{ 1, 2.0, "3", false };
|
|
|
|
/// for (size_t i = 0; i < arr.size() i++)
|
|
|
|
/// std::cout << "Element ["sv << i << "] is: "sv << arr[i].type() << std::endl;
|
|
|
|
///
|
|
|
|
/// \ecpp
|
2020-02-18 21:29:59 +00:00
|
|
|
///
|
|
|
|
/// \out
|
|
|
|
/// Element [0] is: integer
|
|
|
|
/// Element [1] is: floating-point
|
|
|
|
/// Element [2] is: string
|
|
|
|
/// Element [3] is: boolean
|
|
|
|
/// \eout
|
2020-01-07 15:52:50 +00:00
|
|
|
template <typename CHAR>
|
|
|
|
inline std::basic_ostream<CHAR>& operator << (std::basic_ostream<CHAR>& lhs, node_type rhs) TOML_MAY_THROW
|
|
|
|
{
|
|
|
|
using underlying_t = std::underlying_type_t<node_type>;
|
2020-01-22 21:29:46 +00:00
|
|
|
const auto str = impl::node_type_friendly_names[static_cast<underlying_t>(rhs)];
|
2020-01-07 15:52:50 +00:00
|
|
|
if constexpr (std::is_same_v<CHAR, char>)
|
2020-01-22 21:29:46 +00:00
|
|
|
return lhs << str;
|
|
|
|
else
|
2020-01-07 15:52:50 +00:00
|
|
|
{
|
2020-01-22 21:29:46 +00:00
|
|
|
if constexpr (sizeof(CHAR) == 1)
|
|
|
|
return lhs << std::basic_string_view<CHAR>{ reinterpret_cast<const CHAR*>(str.data()), str.length() };
|
|
|
|
else
|
|
|
|
return lhs << str.data();
|
2020-01-07 15:52:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
2020-02-22 14:10:32 +00:00
|
|
|
TOML_END
|