2018-12-13 11:44:10 +00:00
|
|
|
// Copyright Toru Niina 2017.
|
|
|
|
// Distributed under the MIT License.
|
2019-03-16 05:19:47 +00:00
|
|
|
#ifndef TOML11_UTILITY_HPP
|
|
|
|
#define TOML11_UTILITY_HPP
|
2017-04-21 04:14:53 +00:00
|
|
|
#include <memory>
|
2018-12-02 14:05:15 +00:00
|
|
|
#include <sstream>
|
2020-06-27 15:58:20 +00:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "traits.hpp"
|
2021-12-17 13:29:57 +00:00
|
|
|
#include "version.hpp"
|
2017-04-21 04:14:53 +00:00
|
|
|
|
2021-12-17 13:29:57 +00:00
|
|
|
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201402L
|
2019-03-18 07:28:27 +00:00
|
|
|
# define TOML11_MARK_AS_DEPRECATED(msg) [[deprecated(msg)]]
|
2019-03-15 05:29:32 +00:00
|
|
|
#elif defined(__GNUC__)
|
2019-03-18 07:28:27 +00:00
|
|
|
# define TOML11_MARK_AS_DEPRECATED(msg) __attribute__((deprecated(msg)))
|
2019-03-15 05:29:32 +00:00
|
|
|
#elif defined(_MSC_VER)
|
2019-03-18 07:28:27 +00:00
|
|
|
# define TOML11_MARK_AS_DEPRECATED(msg) __declspec(deprecated(msg))
|
2019-03-15 05:29:32 +00:00
|
|
|
#else
|
|
|
|
# define TOML11_MARK_AS_DEPRECATED
|
|
|
|
#endif
|
|
|
|
|
2017-04-21 04:14:53 +00:00
|
|
|
namespace toml
|
|
|
|
{
|
|
|
|
|
2021-12-17 13:29:57 +00:00
|
|
|
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201402L
|
2020-01-20 03:18:05 +00:00
|
|
|
|
|
|
|
using std::make_unique;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-04-21 04:14:53 +00:00
|
|
|
template<typename T, typename ... Ts>
|
|
|
|
inline std::unique_ptr<T> make_unique(Ts&& ... args)
|
|
|
|
{
|
|
|
|
return std::unique_ptr<T>(new T(std::forward<Ts>(args)...));
|
|
|
|
}
|
|
|
|
|
2021-12-17 13:29:57 +00:00
|
|
|
#endif // TOML11_CPLUSPLUS_STANDARD_VERSION >= 2014
|
2020-01-20 03:18:05 +00:00
|
|
|
|
2017-05-06 14:40:57 +00:00
|
|
|
namespace detail
|
|
|
|
{
|
2020-03-27 09:05:31 +00:00
|
|
|
template<typename Container>
|
|
|
|
void try_reserve_impl(Container& container, std::size_t N, std::true_type)
|
2017-05-06 14:40:57 +00:00
|
|
|
{
|
2020-03-27 09:05:31 +00:00
|
|
|
container.reserve(N);
|
|
|
|
return;
|
2017-05-06 14:40:57 +00:00
|
|
|
}
|
2020-03-27 09:05:31 +00:00
|
|
|
template<typename Container>
|
|
|
|
void try_reserve_impl(Container&, std::size_t, std::false_type) noexcept
|
2017-05-06 14:40:57 +00:00
|
|
|
{
|
2020-03-27 09:05:31 +00:00
|
|
|
return;
|
2017-05-06 14:40:57 +00:00
|
|
|
}
|
2018-12-02 14:05:15 +00:00
|
|
|
} // detail
|
2017-05-06 14:40:57 +00:00
|
|
|
|
2020-03-27 09:05:31 +00:00
|
|
|
template<typename Container>
|
|
|
|
void try_reserve(Container& container, std::size_t N)
|
2017-05-06 14:40:57 +00:00
|
|
|
{
|
2020-03-27 09:05:31 +00:00
|
|
|
if(N <= container.size()) {return;}
|
|
|
|
detail::try_reserve_impl(container, N, detail::has_reserve_method<Container>{});
|
|
|
|
return;
|
2017-05-06 14:40:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-02 14:05:15 +00:00
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
inline std::string concat_to_string_impl(std::ostringstream& oss)
|
|
|
|
{
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
template<typename T, typename ... Ts>
|
|
|
|
std::string concat_to_string_impl(std::ostringstream& oss, T&& head, Ts&& ... tail)
|
|
|
|
{
|
|
|
|
oss << std::forward<T>(head);
|
|
|
|
return concat_to_string_impl(oss, std::forward<Ts>(tail) ... );
|
|
|
|
}
|
|
|
|
} // detail
|
|
|
|
|
|
|
|
template<typename ... Ts>
|
|
|
|
std::string concat_to_string(Ts&& ... args)
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << std::boolalpha << std::fixed;
|
|
|
|
return detail::concat_to_string_impl(oss, std::forward<Ts>(args) ...);
|
|
|
|
}
|
|
|
|
|
2020-01-12 14:07:17 +00:00
|
|
|
template<typename T>
|
|
|
|
T from_string(const std::string& str, T opt)
|
2018-12-08 11:18:50 +00:00
|
|
|
{
|
2020-01-12 14:07:17 +00:00
|
|
|
T v(opt);
|
2018-12-08 11:18:50 +00:00
|
|
|
std::istringstream iss(str);
|
|
|
|
iss >> v;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2021-04-14 02:21:48 +00:00
|
|
|
namespace detail
|
|
|
|
{
|
2021-12-17 13:29:57 +00:00
|
|
|
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201402L
|
2021-04-14 02:21:48 +00:00
|
|
|
template<typename T>
|
|
|
|
decltype(auto) last_one(T&& tail) noexcept
|
|
|
|
{
|
|
|
|
return std::forward<T>(tail);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename ... Ts>
|
|
|
|
decltype(auto) last_one(T&& /*head*/, Ts&& ... tail) noexcept
|
|
|
|
{
|
|
|
|
return last_one(std::forward<Ts>(tail)...);
|
|
|
|
}
|
|
|
|
#else // C++11
|
|
|
|
// The following code
|
|
|
|
// ```cpp
|
|
|
|
// 1 | template<typename T, typename ... Ts>
|
|
|
|
// 2 | auto last_one(T&& /*head*/, Ts&& ... tail)
|
|
|
|
// 3 | -> decltype(last_one(std::forward<Ts>(tail)...))
|
|
|
|
// 4 | {
|
|
|
|
// 5 | return last_one(std::forward<Ts>(tail)...);
|
|
|
|
// 6 | }
|
|
|
|
// ```
|
|
|
|
// does not work because the function `last_one(...)` is not yet defined at
|
|
|
|
// line #3, so `decltype()` cannot deduce the type returned from `last_one`.
|
|
|
|
// So we need to determine return type in a different way, like a meta func.
|
|
|
|
|
|
|
|
template<typename T, typename ... Ts>
|
2021-04-14 04:09:25 +00:00
|
|
|
struct last_one_in_pack
|
2021-04-14 02:21:48 +00:00
|
|
|
{
|
|
|
|
using type = typename last_one_in_pack<Ts...>::type;
|
|
|
|
};
|
|
|
|
template<typename T>
|
|
|
|
struct last_one_in_pack<T>
|
|
|
|
{
|
|
|
|
using type = T;
|
|
|
|
};
|
|
|
|
template<typename ... Ts>
|
|
|
|
using last_one_in_pack_t = typename last_one_in_pack<Ts...>::type;
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T&& last_one(T&& tail) noexcept
|
|
|
|
{
|
|
|
|
return std::forward<T>(tail);
|
|
|
|
}
|
|
|
|
template<typename T, typename ... Ts>
|
2021-04-14 04:09:25 +00:00
|
|
|
enable_if_t<(sizeof...(Ts) > 0), last_one_in_pack_t<Ts&& ...>>
|
2021-04-14 02:21:48 +00:00
|
|
|
last_one(T&& /*head*/, Ts&& ... tail)
|
|
|
|
{
|
|
|
|
return last_one(std::forward<Ts>(tail)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
} // detail
|
|
|
|
|
2017-04-21 04:14:53 +00:00
|
|
|
}// toml
|
|
|
|
#endif // TOML11_UTILITY
|