Skip to content

Commit

Permalink
Separate out variant_policy
Browse files Browse the repository at this point in the history
  • Loading branch information
alugowski committed Dec 14, 2023
1 parent cddf3a9 commit 1bb7462
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 51 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 0 additions & 47 deletions include/poolstl/execution
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
#include "internal/task_thread_pool.hpp"
#include "internal/utils.hpp"

#if POOLSTL_HAVE_CXX17
#include <variant>
#endif

namespace poolstl {

namespace ttp = task_thread_pool;
Expand Down Expand Up @@ -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 <typename Variant>
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.
*
Expand Down Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -223,25 +195,6 @@ namespace poolstl {
template <class ExecPolicy>
using is_pure_threads_policy = std::is_same<poolstl::execution::pure_threads_policy,
typename std::remove_cv<typename std::remove_reference<ExecPolicy>::type>::type>;

#if POOLSTL_HAVE_CXX17
/**
* Helper for enable_if_poolstl_variant
*/
template <typename T> struct is_poolstl_variant_policy : std::false_type {};
template <typename V> struct is_poolstl_variant_policy<
::poolstl::execution::variant_policy<V>> :std::true_type {};

/**
* To enable/disable variant_policy (for par_if) overload resolution
*/
template <class ExecPolicy, class Tp>
using enable_if_poolstl_variant =
typename std::enable_if<
is_poolstl_variant_policy<
typename std::remove_cv<typename std::remove_reference<ExecPolicy>::type>::type>::value,
Tp>::type;
#endif
}
}

Expand Down
1 change: 0 additions & 1 deletion include/poolstl/poolstl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
48 changes: 46 additions & 2 deletions include/poolstl/seq_fwd.hpp → include/poolstl/variant_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <variant>

namespace poolstl {
namespace execution {
/**
* A policy that allows selecting a policy at runtime.
*
* @tparam Variant std::variant<> of policy options.
*/
template <typename Variant>
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 <typename T> struct is_poolstl_variant_policy : std::false_type {};
template <typename V> struct is_poolstl_variant_policy<
::poolstl::execution::variant_policy<V>> :std::true_type {};

/**
* To enable/disable variant_policy (for par_if) overload resolution
*/
template <class ExecPolicy, class Tp>
using enable_if_poolstl_variant =
typename std::enable_if<
is_poolstl_variant_policy<
typename std::remove_cv<typename std::remove_reference<ExecPolicy>::type>::type>::value,
Tp>::type;
}
}

/*
* Forward poolstl::seq to the native sequential (no policy) method.
*/
Expand Down
2 changes: 2 additions & 0 deletions tests/supplement_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#define POOLSTL_STD_SUPPLEMENT
#include <poolstl/poolstl.hpp>

// Test poolstl::variant_policy
#include <poolstl/variant_policy.hpp>
using std_policy_variant = std::variant<std::execution::parallel_policy, std::execution::sequenced_policy>;

/**
Expand Down

0 comments on commit 1bb7462

Please sign in to comment.