NeBuild dev
Loading...
Searching...
No Matches
toml_formatter.hpp
Go to the documentation of this file.
1//# This file is a part of toml++ and is subject to the the terms of the MIT license.
2//# Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
3//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
4// SPDX-License-Identifier: MIT
5#pragma once
6
7#include "preprocessor.hpp"
8#if TOML_ENABLE_FORMATTERS
9
10#include "std_vector.hpp"
11#include "formatter.hpp"
12#include "header_start.hpp"
13
15{
44 class TOML_EXPORTED_CLASS toml_formatter : impl::formatter
45 {
46 private:
48
49 using base = impl::formatter;
50 std::vector<const key*> key_path_;
51 bool pending_table_separator_ = false;
52
54 void print_pending_table_separator();
55
57 void print(const key&);
58
60 void print_inline(const toml::table&);
61
63 void print(const toml::array&);
64
66 void print(const toml::table&);
67
69 void print();
70
71 static constexpr impl::formatter_constants constants = { format_flags::none, // mandatory
72 format_flags::none, // ignored
73 "inf"sv,
74 "-inf"sv,
75 "nan"sv,
76 "true"sv,
77 "false"sv };
78
80
81 public:
83 static constexpr format_flags default_flags = constants.mandatory_flags //
84 | format_flags::allow_literal_strings //
85 | format_flags::allow_multi_line_strings //
86 | format_flags::allow_unicode_strings //
87 | format_flags::allow_real_tabs_in_strings //
88 | format_flags::allow_binary_integers //
89 | format_flags::allow_octal_integers //
90 | format_flags::allow_hexadecimal_integers //
91 | format_flags::indentation;
92
98 explicit toml_formatter(const toml::node& source, format_flags flags = default_flags) noexcept
99 : base{ &source, nullptr, constants, { flags, " "sv } }
100 {}
101
102#if TOML_DOXYGEN || (TOML_ENABLE_PARSER && !TOML_EXCEPTIONS)
103
127 explicit toml_formatter(const toml::parse_result& result, format_flags flags = default_flags) noexcept
128 : base{ nullptr, &result, constants, { flags, " "sv } }
129 {}
130
131#endif
132
134 friend std::ostream& operator<<(std::ostream& lhs, toml_formatter& rhs)
135 {
136 rhs.attach(lhs);
137 rhs.key_path_.clear();
138 rhs.print();
139 rhs.detach();
140 return lhs;
141 }
142
144 friend std::ostream& operator<<(std::ostream& lhs, toml_formatter&& rhs)
145 {
146 return lhs << rhs; // as lvalue
147 }
148 };
149}
151
152#include "header_end.hpp"
153#endif // TOML_ENABLE_FORMATTERS
enum TOML_CLOSED_FLAGS_ENUM format_flags
Format flags for modifying how TOML data is printed to streams.
Definition forward_declarations.hpp:297
std::basic_ostream< Char > & operator<<(std::basic_ostream< Char > &lhs, node_type rhs)
Pretty-prints the value of a node_type to a stream.
Definition forward_declarations.hpp:256
#define TOML_EXPORTED_CLASS
An 'export' annotation to add to classes. \detail Not defined by default.
Definition preprocessor.hpp:979
#define TOML_EXPORTED_MEMBER_FUNCTION
An 'export' annotation to add to non-static class member functions. \detail Not defined by default.
Definition preprocessor.hpp:982
#define TOML_NODISCARD_CTOR
Definition preprocessor.hpp:446
TOML_NAMESPACE_START
Definition toml_formatter.hpp:15
TOML_NAMESPACE_END
Definition toml_formatter.hpp:150