From 2d9bbd30fcac9a774b366238de71b1dcc4eb55f7 Mon Sep 17 00:00:00 2001 From: Adam Lugowski Date: Thu, 14 Dec 2023 00:47:27 -0800 Subject: [PATCH] Separate out variant_policy --- CMakeLists.txt | 2 +- include/poolstl/execution | 47 ------------------ include/poolstl/poolstl.hpp | 1 - .../{seq_fwd.hpp => variant_policy.hpp} | 48 ++++++++++++++++++- tests/supplement_test.cpp | 17 +++++-- tools/amalgamate_header.hpp | 4 ++ 6 files changed, 63 insertions(+), 56 deletions(-) rename include/poolstl/{seq_fwd.hpp => variant_policy.hpp} (74%) diff --git a/CMakeLists.txt b/CMakeLists.txt index bf3c940..a03f915 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,7 @@ set(HEADER_FILES include/poolstl/algorithm include/poolstl/execution include/poolstl/numeric - include/poolstl/seq_fwd.hpp + include/poolstl/variant_policy.hpp include/poolstl/iota_iter.hpp include/poolstl/internal/utils.hpp include/poolstl/internal/ttp_impl.hpp diff --git a/include/poolstl/execution b/include/poolstl/execution index 0697c52..db5f6cd 100644 --- a/include/poolstl/execution +++ b/include/poolstl/execution @@ -15,10 +15,6 @@ #include "internal/task_thread_pool.hpp" #include "internal/utils.hpp" -#if POOLSTL_HAVE_CXX17 -#include -#endif - namespace poolstl { namespace ttp = task_thread_pool; @@ -124,27 +120,6 @@ namespace poolstl { bool par_ok = true; }; - -#if POOLSTL_HAVE_CXX17 - /** - * A policy that allows selecting a policy at runtime. - * - * @tparam Variant std::variant<> of policy options. - */ - template - struct variant_policy { - explicit variant_policy(const Variant& policy): var(policy) {} - Variant var; - }; - - namespace internal { - using poolstl_policy_variant = std::variant< - poolstl::execution::parallel_policy, - poolstl::execution::sequenced_policy>; - } - -#endif - /** * Choose parallel or sequential at runtime. * @@ -180,9 +155,6 @@ namespace poolstl { using execution::seq; using execution::par; using execution::par_if; -#if POOLSTL_HAVE_CXX17 - using execution::variant_policy; -#endif namespace internal { /** @@ -223,25 +195,6 @@ namespace poolstl { template using is_pure_threads_policy = std::is_same::type>::type>; - -#if POOLSTL_HAVE_CXX17 - /** - * Helper for enable_if_poolstl_variant - */ - template struct is_poolstl_variant_policy : std::false_type {}; - template struct is_poolstl_variant_policy< - ::poolstl::execution::variant_policy> :std::true_type {}; - - /** - * To enable/disable variant_policy (for par_if) overload resolution - */ - template - using enable_if_poolstl_variant = - typename std::enable_if< - is_poolstl_variant_policy< - typename std::remove_cv::type>::type>::value, - Tp>::type; -#endif } } diff --git a/include/poolstl/poolstl.hpp b/include/poolstl/poolstl.hpp index 4382384..70bd308 100644 --- a/include/poolstl/poolstl.hpp +++ b/include/poolstl/poolstl.hpp @@ -15,7 +15,6 @@ #include "execution" #include "algorithm" #include "numeric" -#include "seq_fwd.hpp" // Note that iota_iter.hpp is self-contained in its own right. #include "iota_iter.hpp" diff --git a/include/poolstl/seq_fwd.hpp b/include/poolstl/variant_policy.hpp similarity index 74% rename from include/poolstl/seq_fwd.hpp rename to include/poolstl/variant_policy.hpp index b08a5ca..bdd2e10 100644 --- a/include/poolstl/seq_fwd.hpp +++ b/include/poolstl/variant_policy.hpp @@ -3,11 +3,55 @@ // the BSD 2-clause license, the MIT license, or at your choosing the BSL-1.0 license found in the LICENSE.*.txt files. // SPDX-License-Identifier: BSD-2-Clause OR MIT OR BSL-1.0 -#ifndef POOLSTL_SEQ_FWD_HPP -#define POOLSTL_SEQ_FWD_HPP +#ifndef POOLSTL_VARIANT_POLICY_HPP +#define POOLSTL_VARIANT_POLICY_HPP #include "execution" +#include + +namespace poolstl { + namespace execution { + /** + * A policy that allows selecting a policy at runtime. + * + * @tparam Variant std::variant<> of policy options. + */ + template + struct variant_policy { + explicit variant_policy(const Variant& policy): var(policy) {} + Variant var; + }; + + namespace internal { + using poolstl_policy_variant = std::variant< + poolstl::execution::parallel_policy, + poolstl::execution::sequenced_policy>; + } + } + + using execution::variant_policy; + + namespace internal { + /** + * Helper for enable_if_poolstl_variant + */ + template struct is_poolstl_variant_policy : std::false_type {}; + template struct is_poolstl_variant_policy< + ::poolstl::execution::variant_policy> :std::true_type {}; + + /** + * To enable/disable variant_policy (for par_if) overload resolution + */ + template + using enable_if_poolstl_variant = + typename std::enable_if< + is_poolstl_variant_policy< + typename std::remove_cv::type>::type>::value, + Tp>::type; + } +} + /* * Forward poolstl::seq to the native sequential (no policy) method. */ diff --git a/tests/supplement_test.cpp b/tests/supplement_test.cpp index ee06135..93ea5e3 100644 --- a/tests/supplement_test.cpp +++ b/tests/supplement_test.cpp @@ -4,7 +4,7 @@ // SPDX-License-Identifier: BSD-2-Clause OR MIT OR BSL-1.0 #include -#include +#include #include // The header defines compiler-provided execution policies, but is not always present. @@ -21,6 +21,10 @@ #define POOLSTL_STD_SUPPLEMENT #include +#ifndef POOLSTL_AMALGAM +// Test poolstl::variant_policy. +// Not bundled with the amalgam. +#include using std_policy_variant = std::variant; /** @@ -33,6 +37,7 @@ poolstl::variant_policy std_par_if(bool call_par) { return poolstl::variant_policy(std_policy_variant(std::execution::seq)); } } +#endif int main() { if (std::is_same_v) { @@ -45,24 +50,26 @@ int main() { std::for_each(std::execution::seq, v.cbegin(), v.cend(), [](int x) { std::cout << x; }); - std::cout << std::endl; + std::cout << " std::execution::seq" << std::endl; std::for_each(std::execution::par, v.cbegin(), v.cend(), [](int x) { std::cout << x; }); - std::cout << std::endl; + std::cout << " std::execution::par" << std::endl; std::for_each(std::execution::par_unseq, v.cbegin(), v.cend(), [](int x) { std::cout << x; }); - std::cout << std::endl; + std::cout << " std::execution::par_unseq" << std::endl; +#ifndef POOLSTL_AMALGAM for (bool is_parallel : {true, false}) { std::for_each(std_par_if(is_parallel), v.cbegin(), v.cend(), [](int x) { std::cout << x; }); - std::cout << std::endl; + std::cout << " std_par_if(" << std::to_string(is_parallel) << ")" << std::endl; } +#endif return 0; } diff --git a/tools/amalgamate_header.hpp b/tools/amalgamate_header.hpp index 032eaf5..9ba21fd 100644 --- a/tools/amalgamate_header.hpp +++ b/tools/amalgamate_header.hpp @@ -83,3 +83,7 @@ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ + +#ifndef POOLSTL_AMALGAM +#define POOLSTL_AMALGAM +#endif