// Copyright Toru Niina 2017. // Distributed under the MIT License. #ifndef TOML11_UTILITY_HPP #define TOML11_UTILITY_HPP #include #include #include #include "traits.hpp" #if __cplusplus >= 201402L # define TOML11_MARK_AS_DEPRECATED(msg) [[deprecated(msg)]] #elif defined(__GNUC__) # define TOML11_MARK_AS_DEPRECATED(msg) __attribute__((deprecated(msg))) #elif defined(_MSC_VER) # define TOML11_MARK_AS_DEPRECATED(msg) __declspec(deprecated(msg)) #else # define TOML11_MARK_AS_DEPRECATED #endif namespace toml { #if __cplusplus >= 201402L using std::make_unique; #else template inline std::unique_ptr make_unique(Ts&& ... args) { return std::unique_ptr(new T(std::forward(args)...)); } #endif // __cplusplus >= 2014 namespace detail { template void try_reserve_impl(Container& container, std::size_t N, std::true_type) { container.reserve(N); return; } template void try_reserve_impl(Container&, std::size_t, std::false_type) noexcept { return; } } // detail template void try_reserve(Container& container, std::size_t N) { if(N <= container.size()) {return;} detail::try_reserve_impl(container, N, detail::has_reserve_method{}); return; } namespace detail { inline std::string concat_to_string_impl(std::ostringstream& oss) { return oss.str(); } template std::string concat_to_string_impl(std::ostringstream& oss, T&& head, Ts&& ... tail) { oss << std::forward(head); return concat_to_string_impl(oss, std::forward(tail) ... ); } } // detail template std::string concat_to_string(Ts&& ... args) { std::ostringstream oss; oss << std::boolalpha << std::fixed; return detail::concat_to_string_impl(oss, std::forward(args) ...); } template T from_string(const std::string& str, T opt) { T v(opt); std::istringstream iss(str); iss >> v; return v; } namespace detail { #if __cplusplus >= 201402L template decltype(auto) last_one(T&& tail) noexcept { return std::forward(tail); } template decltype(auto) last_one(T&& /*head*/, Ts&& ... tail) noexcept { return last_one(std::forward(tail)...); } #else // C++11 // The following code // ```cpp // 1 | template // 2 | auto last_one(T&& /*head*/, Ts&& ... tail) // 3 | -> decltype(last_one(std::forward(tail)...)) // 4 | { // 5 | return last_one(std::forward(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 struct last_one_in_pack { using type = typename last_one_in_pack::type; }; template struct last_one_in_pack { using type = T; }; template using last_one_in_pack_t = typename last_one_in_pack::type; template T&& last_one(T&& tail) noexcept { return std::forward(tail); } template enable_if_t<(sizeof...(Ts) > 0), last_one_in_pack_t> last_one(T&& /*head*/, Ts&& ... tail) { return last_one(std::forward(tail)...); } #endif } // detail }// toml #endif // TOML11_UTILITY