diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2022-09-16 10:26:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-16 10:26:57 +0200 |
commit | f399f6b9eb5e93dc21bc2c514a837410c9868574 (patch) | |
tree | 112ebeba17ee89a6c549077ec53b5b57a40c7c51 | |
parent | 3da74664cee04a25a86cab43f83037f79c1f70f9 (diff) | |
parent | b42c1df070cf5d0b4afca678eed639ad135b33c6 (diff) |
Merge pull request #24079 from vespa-engine/havardpe/start-using-concepts
start using concepts in vespalib
5 files changed, 22 insertions, 19 deletions
diff --git a/vespalib/src/tests/simple_thread_bundle/simple_thread_bundle_test.cpp b/vespalib/src/tests/simple_thread_bundle/simple_thread_bundle_test.cpp index d6e0e473e59..27e75315dfc 100644 --- a/vespalib/src/tests/simple_thread_bundle/simple_thread_bundle_test.cpp +++ b/vespalib/src/tests/simple_thread_bundle/simple_thread_bundle_test.cpp @@ -249,6 +249,9 @@ TEST_FF("require that various versions of run can be used to invoke targets", Si f2.check({4,4,4,4,4}); f1.run(f2.cnts); f2.check({5,5,5,5,5}); + std::initializer_list<std::reference_wrapper<Cnt>> list = {f2.cnts[0], f2.cnts[1], f2.cnts[2], f2.cnts[3], f2.cnts[4]}; + f1.run(list); + f2.check({6,6,6,6,6}); } TEST_MAIN() { TEST_RUN_ALL(); } diff --git a/vespalib/src/vespa/vespalib/util/rendezvous.h b/vespalib/src/vespa/vespalib/util/rendezvous.h index 6121b3f3dd6..2880f325d96 100644 --- a/vespalib/src/vespa/vespalib/util/rendezvous.h +++ b/vespalib/src/vespa/vespalib/util/rendezvous.h @@ -93,8 +93,7 @@ public: * @return output parameter for a single thread * @param input input parameter for a single thread **/ - template <bool ext_id = external_id> - typename std::enable_if<!ext_id,OUT>::type rendezvous(IN input); + OUT rendezvous(IN input) requires (!external_id); /** * Called by individual threads to synchronize execution and share @@ -107,8 +106,7 @@ public: * @param my_id participant id for this thread (must be in range and * not conflicting with other threads) **/ - template <bool ext_id = external_id> - typename std::enable_if<ext_id,OUT>::type rendezvous(IN input, size_t my_id); + OUT rendezvous(IN input, size_t my_id) requires (external_id); }; } // namespace vespalib diff --git a/vespalib/src/vespa/vespalib/util/rendezvous.hpp b/vespalib/src/vespa/vespalib/util/rendezvous.hpp index a22e05fac3f..4b3c6138428 100644 --- a/vespalib/src/vespa/vespalib/util/rendezvous.hpp +++ b/vespalib/src/vespa/vespalib/util/rendezvous.hpp @@ -59,12 +59,12 @@ template <typename IN, typename OUT, bool external_id> Rendezvous<IN, OUT, external_id>::~Rendezvous() = default; template <typename IN, typename OUT, bool external_id> -template <bool ext_id> -typename std::enable_if<!ext_id,OUT>::type +OUT Rendezvous<IN, OUT, external_id>::rendezvous(IN input) + requires (!external_id) { OUT ret{}; - static_assert(ext_id == external_id); + static_assert(!external_id); if (_size == 1) { meet_self(input, ret); } else { @@ -75,13 +75,13 @@ Rendezvous<IN, OUT, external_id>::rendezvous(IN input) } template <typename IN, typename OUT, bool external_id> -template <bool ext_id> -typename std::enable_if<ext_id,OUT>::type +OUT Rendezvous<IN, OUT, external_id>::rendezvous(IN input, size_t my_id) + requires (external_id) { OUT ret{}; assert(my_id < _size); - static_assert(ext_id == external_id); + static_assert(external_id); if (_size == 1) { meet_self(input, ret); } else { diff --git a/vespalib/src/vespa/vespalib/util/small_vector.h b/vespalib/src/vespa/vespalib/util/small_vector.h index cf9910e24b5..3e0e3cc7e4b 100644 --- a/vespalib/src/vespa/vespalib/util/small_vector.h +++ b/vespalib/src/vespa/vespalib/util/small_vector.h @@ -104,15 +104,15 @@ private: free(old_data); } } - template <typename InputIt> - void init(InputIt first, InputIt last, std::random_access_iterator_tag) { + template <std::random_access_iterator InputIt> + void init(InputIt first, InputIt last) { reserve(last - first); while (first != last) { small_vector::create_at((_data + _size++), *first++); } } - template <typename InputIt> - void init(InputIt first, InputIt last, std::input_iterator_tag) { + template <std::input_iterator InputIt> + void init(InputIt first, InputIt last) { while (first != last) { emplace_back(*first++); } @@ -137,10 +137,10 @@ public: small_vector::create_at((_data + _size++), value); } } - template <typename InputIt, std::enable_if_t<std::is_base_of_v<std::input_iterator_tag, typename std::iterator_traits<InputIt>::iterator_category>, bool> = true> + template <std::input_iterator InputIt> SmallVector(InputIt first, InputIt last) : SmallVector() { - init(first, last, typename std::iterator_traits<InputIt>::iterator_category()); + init(first, last); } SmallVector(SmallVector &&rhs) : SmallVector() { reserve(rhs._size); diff --git a/vespalib/src/vespa/vespalib/util/thread_bundle.h b/vespalib/src/vespa/vespalib/util/thread_bundle.h index 830d7c76e7c..cfdedb347c8 100644 --- a/vespalib/src/vespa/vespalib/util/thread_bundle.h +++ b/vespalib/src/vespa/vespalib/util/thread_bundle.h @@ -4,6 +4,7 @@ #include "runnable.h" #include <vector> +#include <ranges> namespace vespalib { @@ -46,10 +47,11 @@ struct ThreadBundle { } // convenience run wrapper - template <typename Item> - std::enable_if_t<!is_runnable_ptr<Item>(),void> run(std::vector<Item> &items) { + template <std::ranges::range List> + requires (!is_runnable_ptr<std::ranges::range_value_t<List>>()) + void run(List &items) { std::vector<Runnable*> targets; - targets.reserve(items.size()); + targets.reserve(std::ranges::size(items)); for (auto &item: items) { targets.push_back(resolve(item)); } |