2021-10-24 22:04:23 +00:00
|
|
|
//# This file is a part of toml++ and is subject to the the terms of the MIT license.
|
|
|
|
//# Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
|
|
|
|
//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "forward_declarations.h"
|
|
|
|
#include "header_start.h"
|
|
|
|
|
|
|
|
/// \cond
|
|
|
|
TOML_IMPL_NAMESPACE_START
|
|
|
|
{
|
|
|
|
template <typename T>
|
|
|
|
TOML_NODISCARD
|
|
|
|
TOML_ATTR(returns_nonnull)
|
2021-11-10 09:17:15 +00:00
|
|
|
auto* make_node_impl_specialized(T && val, [[maybe_unused]] value_flags flags)
|
2021-10-24 22:04:23 +00:00
|
|
|
{
|
2021-10-29 08:12:41 +00:00
|
|
|
using unwrapped_type = unwrap_node<remove_cvref<T>>;
|
|
|
|
static_assert(!std::is_same_v<unwrapped_type, node>);
|
|
|
|
static_assert(!is_node_view<unwrapped_type>);
|
2021-10-24 22:04:23 +00:00
|
|
|
|
2021-10-29 08:12:41 +00:00
|
|
|
// arrays + tables - invoke copy/move ctor
|
|
|
|
if constexpr (is_one_of<unwrapped_type, array, table>)
|
2021-10-24 22:04:23 +00:00
|
|
|
{
|
2021-11-14 17:24:19 +00:00
|
|
|
return new unwrapped_type(static_cast<T&&>(val));
|
2021-10-24 22:04:23 +00:00
|
|
|
}
|
2021-10-29 08:12:41 +00:00
|
|
|
|
|
|
|
// values
|
2021-10-24 22:04:23 +00:00
|
|
|
else
|
|
|
|
{
|
2021-10-29 08:12:41 +00:00
|
|
|
using native_type = native_type_of<unwrapped_type>;
|
|
|
|
using value_type = value<native_type>;
|
|
|
|
|
|
|
|
value_type* out;
|
|
|
|
|
|
|
|
// copy/move ctor
|
|
|
|
if constexpr (std::is_same_v<remove_cvref<T>, value_type>)
|
2021-10-24 22:04:23 +00:00
|
|
|
{
|
2021-10-29 08:12:41 +00:00
|
|
|
out = new value_type{ static_cast<T&&>(val) };
|
|
|
|
}
|
|
|
|
|
|
|
|
// creating from raw value
|
|
|
|
else
|
|
|
|
{
|
2021-10-30 14:26:05 +00:00
|
|
|
static_assert(!is_wide_string<T> || TOML_ENABLE_WINDOWS_COMPAT,
|
2021-10-29 08:12:41 +00:00
|
|
|
"Instantiating values from wide-character strings is only "
|
2021-10-30 14:26:05 +00:00
|
|
|
"supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
|
2021-11-10 09:17:15 +00:00
|
|
|
|
|
|
|
if constexpr (!is_losslessly_convertible_to_native<unwrapped_type>)
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<native_type, int64_t>)
|
|
|
|
static_assert(dependent_false<T>,
|
|
|
|
"Integral value initializers must be losslessly convertible to int64_t");
|
|
|
|
else if constexpr (std::is_same_v<native_type, double>)
|
|
|
|
static_assert(dependent_false<T>,
|
|
|
|
"Floating-point value initializers must be losslessly convertible to double");
|
|
|
|
else
|
|
|
|
static_assert(
|
|
|
|
dependent_false<T>,
|
|
|
|
"Value initializers must be losslessly convertible to one of the TOML value types");
|
|
|
|
}
|
2021-10-29 08:12:41 +00:00
|
|
|
|
|
|
|
if constexpr (is_wide_string<T>)
|
|
|
|
{
|
2021-10-30 14:26:05 +00:00
|
|
|
#if TOML_ENABLE_WINDOWS_COMPAT
|
2021-10-29 08:12:41 +00:00
|
|
|
out = new value_type{ narrow(static_cast<T&&>(val)) };
|
2021-10-24 22:04:23 +00:00
|
|
|
#else
|
2021-10-29 08:12:41 +00:00
|
|
|
static_assert(dependent_false<T>, "Evaluated unreachable branch!");
|
2021-10-24 22:04:23 +00:00
|
|
|
#endif
|
2021-10-29 08:12:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
out = new value_type{ static_cast<T&&>(val) };
|
2021-10-29 13:28:04 +00:00
|
|
|
}
|
2021-10-29 08:12:41 +00:00
|
|
|
|
2021-10-29 13:28:04 +00:00
|
|
|
if (flags != preserve_source_value_flags)
|
2021-10-29 08:12:41 +00:00
|
|
|
out->flags(flags);
|
|
|
|
|
|
|
|
return out;
|
2021-10-24 22:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
TOML_NODISCARD
|
2021-11-10 09:17:15 +00:00
|
|
|
auto* make_node_impl(T && val, value_flags flags = preserve_source_value_flags)
|
2021-10-24 22:04:23 +00:00
|
|
|
{
|
2021-11-10 09:17:15 +00:00
|
|
|
using unwrapped_type = unwrap_node<remove_cvref<T>>;
|
|
|
|
if constexpr (std::is_same_v<unwrapped_type, node> || is_node_view<unwrapped_type>)
|
2021-10-24 22:04:23 +00:00
|
|
|
{
|
2021-11-10 09:17:15 +00:00
|
|
|
if constexpr (is_node_view<unwrapped_type>)
|
2021-10-24 22:04:23 +00:00
|
|
|
{
|
|
|
|
if (!val)
|
|
|
|
return static_cast<toml::node*>(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<T&&>(val).visit(
|
2021-10-29 08:12:41 +00:00
|
|
|
[flags](auto&& concrete) {
|
|
|
|
return static_cast<toml::node*>(
|
2021-11-10 09:17:15 +00:00
|
|
|
make_node_impl_specialized(static_cast<decltype(concrete)&&>(concrete), flags));
|
2021-10-24 22:04:23 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
2021-11-10 09:17:15 +00:00
|
|
|
return make_node_impl_specialized(static_cast<T&&>(val), flags);
|
2021-10-24 22:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
TOML_NODISCARD
|
2021-11-10 09:17:15 +00:00
|
|
|
auto* make_node_impl(inserter<T> && val, value_flags flags = preserve_source_value_flags)
|
2021-10-24 22:04:23 +00:00
|
|
|
{
|
2021-11-10 09:17:15 +00:00
|
|
|
return make_node_impl(static_cast<T&&>(val.value), flags);
|
2021-10-24 22:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, bool = (is_node<T> || is_node_view<T> || is_value<T> || can_partially_represent_native<T>)>
|
|
|
|
struct inserted_type_of_
|
|
|
|
{
|
2021-11-10 09:17:15 +00:00
|
|
|
using type = std::remove_pointer_t<decltype(make_node_impl(std::declval<T>()))>;
|
2021-10-24 22:04:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct inserted_type_of_<inserter<T>, false>
|
|
|
|
{
|
|
|
|
using type = typename inserted_type_of_<T>::type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct inserted_type_of_<T, false>
|
|
|
|
{
|
|
|
|
using type = void;
|
|
|
|
};
|
2021-11-10 09:17:15 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
TOML_NODISCARD
|
|
|
|
node_ptr make_node(T && val, value_flags flags = preserve_source_value_flags)
|
|
|
|
{
|
|
|
|
return node_ptr{ make_node_impl(static_cast<T&&>(val), flags) };
|
|
|
|
}
|
2021-10-24 22:04:23 +00:00
|
|
|
}
|
|
|
|
TOML_IMPL_NAMESPACE_END;
|
|
|
|
/// \endcond
|
|
|
|
|
|
|
|
TOML_NAMESPACE_START
|
|
|
|
{
|
|
|
|
/// \brief Metafunction for determining which node type would be constructed
|
|
|
|
/// if an object of this type was inserted into a toml::table or toml::array.
|
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// static_assert(std::is_same_v<toml::inserted_type_of<const char*>, toml::value<std::string>);
|
|
|
|
/// static_assert(std::is_same_v<toml::inserted_type_of<int>, toml::value<int64_t>);
|
|
|
|
/// static_assert(std::is_same_v<toml::inserted_type_of<float>, toml::value<double>);
|
|
|
|
/// static_assert(std::is_same_v<toml::inserted_type_of<bool>, toml::value<bool>);
|
|
|
|
/// \ecpp
|
|
|
|
///
|
|
|
|
/// \note This will return toml::node for nodes and node_views, even though a more specific node subclass
|
|
|
|
/// would actually be inserted. There is no way around this in a compile-time metafunction.
|
|
|
|
template <typename T>
|
2021-10-26 18:03:56 +00:00
|
|
|
using inserted_type_of = POXY_IMPLEMENTATION_DETAIL(typename impl::inserted_type_of_<impl::remove_cvref<T>>::type);
|
2021-10-24 22:04:23 +00:00
|
|
|
}
|
|
|
|
TOML_NAMESPACE_END;
|
|
|
|
|
|
|
|
#include "header_end.h"
|