NeBuild dev
Loading...
Searching...
No Matches
make_node.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
8#include "header_start.hpp"
9
12{
13 template <typename T>
15 TOML_ATTR(returns_nonnull)
16 auto* make_node_impl_specialized(T && val, [[maybe_unused]] value_flags flags)
17 {
18 using unwrapped_type = unwrap_node<remove_cvref<T>>;
19 static_assert(!std::is_same_v<unwrapped_type, node>);
20 static_assert(!is_node_view<unwrapped_type>);
21
22 // arrays + tables - invoke copy/move ctor
23 if constexpr (is_one_of<unwrapped_type, array, table>)
24 {
25 return new unwrapped_type(static_cast<T&&>(val));
26 }
27
28 // values
29 else
30 {
31 using native_type = native_type_of<unwrapped_type>;
32 using value_type = value<native_type>;
33
34 value_type* out;
35
36 // copy/move ctor
37 if constexpr (std::is_same_v<remove_cvref<T>, value_type>)
38 {
39 out = new value_type{ static_cast<T&&>(val), flags };
40 }
41
42 // creating from raw value
43 else
44 {
45 static_assert(!is_wide_string<T> || TOML_ENABLE_WINDOWS_COMPAT,
46 "Instantiating values from wide-character strings is only "
47 "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
48
49 if constexpr (!is_losslessly_convertible_to_native<unwrapped_type>)
50 {
51 if constexpr (std::is_same_v<native_type, int64_t>)
52 static_assert(always_false<T>,
53 "Integral value initializers must be losslessly convertible to int64_t");
54 else if constexpr (std::is_same_v<native_type, double>)
55 static_assert(always_false<T>,
56 "Floating-point value initializers must be losslessly convertible to double");
57 else
58 static_assert(
59 always_false<T>,
60 "Value initializers must be losslessly convertible to one of the TOML value types");
61 }
62
63 if constexpr (is_wide_string<T>)
64 {
65#if TOML_ENABLE_WINDOWS_COMPAT
66 out = new value_type{ narrow(static_cast<T&&>(val)) };
67#else
68 static_assert(always_false<T>, "Evaluated unreachable branch!");
69#endif
70 }
71 else
72 out = new value_type{ static_cast<T&&>(val) };
73
74 if (flags != preserve_source_value_flags)
75 out->flags(flags);
76 }
77
78 return out;
79 }
80 }
81
82 template <typename T>
84 auto* make_node_impl(T && val, value_flags flags = preserve_source_value_flags)
85 {
86 using unwrapped_type = unwrap_node<remove_cvref<T>>;
87 if constexpr (std::is_same_v<unwrapped_type, node> || is_node_view<unwrapped_type>)
88 {
89 if constexpr (is_node_view<unwrapped_type>)
90 {
91 if (!val)
92 return static_cast<toml::node*>(nullptr);
93 }
94
95 return static_cast<T&&>(val).visit(
96 [flags](auto&& concrete) {
97 return static_cast<toml::node*>(
98 make_node_impl_specialized(static_cast<decltype(concrete)&&>(concrete), flags));
99 });
100 }
101 else
102 return make_node_impl_specialized(static_cast<T&&>(val), flags);
103 }
104
105 template <typename T>
107 auto* make_node_impl(inserter<T> && val, value_flags flags = preserve_source_value_flags)
108 {
109 return make_node_impl(static_cast<T&&>(val.value), flags);
110 }
111
112 template <typename T, bool = (is_node<T> || is_node_view<T> || is_value<T> || can_partially_represent_native<T>)>
113 struct inserted_type_of_
114 {
115 using type = std::remove_pointer_t<decltype(make_node_impl(std::declval<T>()))>;
116 };
117
118 template <typename T>
119 struct inserted_type_of_<inserter<T>, false>
120 {
121 using type = typename inserted_type_of_<remove_cvref<T>>::type;
122 };
123
124 template <typename T>
125 struct inserted_type_of_<T, false>
126 {
127 using type = void;
128 };
129
130 template <typename T>
132 node_ptr make_node(T && val, value_flags flags = preserve_source_value_flags)
133 {
134 return node_ptr{ make_node_impl(static_cast<T&&>(val), flags) };
135 }
136
137 template <typename... T>
138 struct emplaced_type_of_
139 {
140 using type = void;
141 };
142
143 template <typename T>
144 struct emplaced_type_of_<T>
145 {
146 using type = std::conditional_t<is_one_of<T, node, node_view<node>, node_view<const node>>,
147 void,
148 typename inserted_type_of_<T>::type>;
149 };
150
151 template <typename T>
152 struct emplaced_type_of_<inserter<T>>
153 {
154 using type = typename emplaced_type_of_<remove_cvref<T>>::type;
155 };
156
157 template <typename... T>
158 using emplaced_type_of = typename emplaced_type_of_<remove_cvref<T>...>::type;
159}
162
164{
177 template <typename T>
178 using inserted_type_of = POXY_IMPLEMENTATION_DETAIL(typename impl::inserted_type_of_<impl::remove_cvref<T>>::type);
179}
181
182#include "header_end.hpp"
enum TOML_OPEN_FLAGS_ENUM value_flags
Metadata associated with TOML values.
Definition forward_declarations.hpp:272
constexpr value_flags preserve_source_value_flags
Special #toml::value_flags constant used for array + table insert functions to specify that any value...
Definition forward_declarations.hpp:290
#define TOML_ENABLE_WINDOWS_COMPAT
Enables the use of wide strings (wchar_t, std::wstring) in various places throughout the library when...
Definition preprocessor.hpp:1074
TOML_NAMESPACE_START
Definition make_node.hpp:164
TOML_NAMESPACE_END
Definition make_node.hpp:180
#define POXY_IMPLEMENTATION_DETAIL(...)
Definition preprocessor.hpp:633
#define TOML_NODISCARD
Definition preprocessor.hpp:439
#define TOML_ATTR(...)
Definition preprocessor.hpp:316
#define TOML_IMPL_NAMESPACE_END
Definition preprocessor.hpp:1334
#define TOML_IMPL_NAMESPACE_START
Definition preprocessor.hpp:1333
Helper class for suppressing move-construction in single-argument array constructors.
Definition forward_declarations.hpp:366