From 32dcc359189fd34d86face721b5b5d200f67958e Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Wed, 13 Feb 2019 13:34:03 +0900 Subject: [PATCH] move return_type_of_t from result to traits --- toml/result.hpp | 50 +++++++++++++++++++------------------------------ toml/traits.hpp | 28 ++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 32 deletions(-) diff --git a/toml/result.hpp b/toml/result.hpp index c174578..949b6f8 100644 --- a/toml/result.hpp +++ b/toml/result.hpp @@ -2,6 +2,7 @@ // Distributed under the MIT License. #ifndef TOML11_RESULT_H #define TOML11_RESULT_H +#include "traits.hpp" #include #include #include @@ -13,19 +14,6 @@ namespace toml { -#if __cplusplus >= 201703L - -template -using return_type_of_t = std::invoke_result_t; - -#else -// result_of is deprecated after C++17 -template -using return_type_of_t = typename std::result_of::type; - -#endif - - template struct success { @@ -441,21 +429,21 @@ struct result // F: T -> U // retval: result template - result, error_type> + result, error_type> map(F&& f) & { if(this->is_ok()){return ok(f(this->as_ok()));} return err(this->as_err()); } template - result, error_type> + result, error_type> map(F&& f) const& { if(this->is_ok()){return ok(f(this->as_ok()));} return err(this->as_err()); } template - result, error_type> + result, error_type> map(F&& f) && { if(this->is_ok()){return ok(f(std::move(this->as_ok())));} @@ -466,21 +454,21 @@ struct result // F: E -> F // retval: result template - result> + result> map_err(F&& f) & { if(this->is_err()){return err(f(this->as_err()));} return ok(this->as_ok()); } template - result> + result> map_err(F&& f) const& { if(this->is_err()){return err(f(this->as_err()));} return ok(this->as_ok()); } template - result> + result> map_err(F&& f) && { if(this->is_err()){return err(f(std::move(this->as_err())));} @@ -491,21 +479,21 @@ struct result // F: T -> U // retval: U template - return_type_of_t + detail::return_type_of_t map_or_else(F&& f, U&& opt) & { if(this->is_err()){return std::forward(opt);} return f(this->as_ok()); } template - return_type_of_t + detail::return_type_of_t map_or_else(F&& f, U&& opt) const& { if(this->is_err()){return std::forward(opt);} return f(this->as_ok()); } template - return_type_of_t + detail::return_type_of_t map_or_else(F&& f, U&& opt) && { if(this->is_err()){return std::forward(opt);} @@ -516,21 +504,21 @@ struct result // F: E -> U // retval: U template - return_type_of_t + detail::return_type_of_t map_err_or_else(F&& f, U&& opt) & { if(this->is_ok()){return std::forward(opt);} return f(this->as_err()); } template - return_type_of_t + detail::return_type_of_t map_err_or_else(F&& f, U&& opt) const& { if(this->is_ok()){return std::forward(opt);} return f(this->as_err()); } template - return_type_of_t + detail::return_type_of_t map_err_or_else(F&& f, U&& opt) && { if(this->is_ok()){return std::forward(opt);} @@ -542,21 +530,21 @@ struct result // toml::err(error_type) should be convertible to U. // normally, type U is another result and E is convertible to F template - return_type_of_t + detail::return_type_of_t and_then(F&& f) & { if(this->is_ok()){return f(this->as_ok());} return err(this->as_err()); } template - return_type_of_t + detail::return_type_of_t and_then(F&& f) const& { if(this->is_ok()){return f(this->as_ok());} return err(this->as_err()); } template - return_type_of_t + detail::return_type_of_t and_then(F&& f) && { if(this->is_ok()){return f(std::move(this->as_ok()));} @@ -568,21 +556,21 @@ struct result // toml::ok(value_type) should be convertible to U. // normally, type U is another result and T is convertible to S template - return_type_of_t + detail::return_type_of_t or_else(F&& f) & { if(this->is_err()){return f(this->as_err());} return ok(this->as_ok()); } template - return_type_of_t + detail::return_type_of_t or_else(F&& f) const& { if(this->is_err()){return f(this->as_err());} return ok(this->as_ok()); } template - return_type_of_t + detail::return_type_of_t or_else(F&& f) && { if(this->is_err()){return f(std::move(this->as_err()));} diff --git a/toml/traits.hpp b/toml/traits.hpp index fc4ef85..79bb8b2 100644 --- a/toml/traits.hpp +++ b/toml/traits.hpp @@ -15,6 +15,9 @@ namespace detail template using unwrap_t = typename std::decay::type; +// --------------------------------------------------------------------------- +// check whether type T is a kind of container/map class + struct has_iterator_impl { template static std::true_type check(typename T::iterator*); @@ -63,6 +66,9 @@ struct has_resize_method : decltype(has_resize_method_impl::check(nullptr)){} #undef decltype(...) #endif +// --------------------------------------------------------------------------- +// C++17 and/or/not + template struct conjunction : std::true_type{}; template struct conjunction : T{}; template @@ -80,6 +86,9 @@ struct disjunction : template struct negation : std::integral_constant(T::value)>{}; +// --------------------------------------------------------------------------- +// normal type checker + template struct is_std_pair : std::false_type{}; template struct is_std_pair> : std::true_type{}; @@ -92,7 +101,9 @@ template struct is_chrono_duration: std::false_type{}; template struct is_chrono_duration>: std::true_type{}; -// to use toml::get> in C++11 +// --------------------------------------------------------------------------- +// C++14 index_sequence + template struct index_sequence{}; template struct push_back_index_sequence{}; @@ -116,6 +127,21 @@ struct index_sequence_maker<0> template using make_index_sequence = typename index_sequence_maker::type; +// --------------------------------------------------------------------------- +// return_type_of_t + +#if __cplusplus >= 201703L + +template +using return_type_of_t = std::invoke_result_t; + +#else +// result_of is deprecated after C++17 +template +using return_type_of_t = typename std::result_of::type; + +#endif + }// detail }//toml #endif // TOML_TRAITS