From 9b52dc013183cf3a1c030fc906b94d3616923bd3 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 27 Mar 2020 18:05:31 +0900 Subject: [PATCH] feat: remove resize and add try_reserve --- toml/utility.hpp | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/toml/utility.hpp b/toml/utility.hpp index bac1249..c287b8e 100644 --- a/toml/utility.hpp +++ b/toml/utility.hpp @@ -36,30 +36,25 @@ inline std::unique_ptr make_unique(Ts&& ... args) namespace detail { - -template -inline void resize_impl(T& container, std::size_t N, std::true_type) +template +void try_reserve_impl(Container& container, std::size_t N, std::true_type) { - container.resize(N); - return ; + container.reserve(N); + return; } - -template -inline void resize_impl(T& container, std::size_t N, std::false_type) +template +void try_reserve_impl(Container&, std::size_t, std::false_type) noexcept { - if(container.size() >= N) {return;} - - throw std::invalid_argument("not resizable type"); + return; } - } // detail -template -inline void resize(T& container, std::size_t N) +template +void try_reserve(Container& container, std::size_t N) { - if(container.size() == N) {return;} - - return detail::resize_impl(container, N, detail::has_resize_method()); + if(N <= container.size()) {return;} + detail::try_reserve_impl(container, N, detail::has_reserve_method{}); + return; } namespace detail