-
Notifications
You must be signed in to change notification settings - Fork 16
Add support for other types than just vector #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| #pragma once | ||
|
|
||
| #include "method_tests.h" | ||
| #include <iterator> | ||
| #include <utility> | ||
|
|
||
| namespace kdalgorithms { | ||
| namespace detail { | ||
| // similar to std::inserter, but calls insert with just one argument - required for QSet in Qt5 | ||
| template <typename T> | ||
| class single_arg_inserter | ||
| { | ||
| public: | ||
| using value_type = typename T::value_type; | ||
| single_arg_inserter(T &set) | ||
| : m_set(set) | ||
| { | ||
| } | ||
|
|
||
| single_arg_inserter &operator++() { return *this; } | ||
| single_arg_inserter &operator*() { return *this; } | ||
| single_arg_inserter &operator=(const value_type &value) | ||
| { | ||
| m_set.insert(value); | ||
| return *this; | ||
| } | ||
|
|
||
| private: | ||
| T &m_set; | ||
| }; | ||
|
|
||
| template <class Container, typename T> | ||
| using has_push_back = decltype(std::declval<Container &>().push_back(std::declval<T>())); | ||
|
|
||
| template <class Container, typename T> | ||
| using has_insert = decltype(std::declval<Container &>().insert(std::declval<T>())); | ||
|
|
||
| template <typename Container> | ||
| auto insert_wrapper( | ||
| Container &c, | ||
| std::enable_if_t< | ||
| detail::is_detected_v<has_push_back, Container, typename Container::value_type>, int> = | ||
| 0) | ||
| { | ||
| return std::back_inserter(c); | ||
| } | ||
|
|
||
| template <typename Container> | ||
| auto insert_wrapper( | ||
| Container &c, | ||
| std::enable_if_t< | ||
| detail::is_detected_v<has_insert, Container, typename Container::value_type>, int> = 0) | ||
| { | ||
| return single_arg_inserter<Container>(c); | ||
| } | ||
|
|
||
| // similar to std::inserter, but for QMap/QHash which usually accept a value in their operator= | ||
| template <typename T> | ||
| class qmap_inserter | ||
| { | ||
| public: | ||
| qmap_inserter(T &map) | ||
| : m_map(map) | ||
| { | ||
| } | ||
|
|
||
| qmap_inserter &operator++() { return *this; } | ||
| qmap_inserter &operator*() { return *this; } | ||
| qmap_inserter & | ||
| operator=(const std::pair<typename T::key_type, typename T::mapped_type> &pair) | ||
| { | ||
| m_map.insert(pair.first, pair.second); | ||
| return *this; | ||
| } | ||
|
|
||
| private: | ||
| T &m_map; | ||
| }; | ||
|
|
||
| template <typename Container> | ||
| auto insert_wrapper(Container &c, | ||
| std::enable_if_t<detail::has_keyValueBegin_v<Container>, int> = 0) | ||
| { | ||
| return qmap_inserter<Container>(c); | ||
| } | ||
|
|
||
| } // namespace detail | ||
| } // namespace kdalgorithms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #pragma once | ||
|
|
||
| #include <type_traits> | ||
|
|
||
| // Inspired from std::experimental::is_detected | ||
| // See https://en.cppreference.com/w/cpp/experimental/is_detected | ||
|
|
||
| namespace kdalgorithms { | ||
| namespace detail { | ||
| template <class AlwaysVoid, template <class...> class, typename... Args> | ||
| struct detector : std::false_type | ||
| { | ||
| }; | ||
|
|
||
| template <template <class...> class Op, typename... Args> | ||
| struct detector<std::void_t<Op<Args...>>, Op, Args...> : std::true_type | ||
| { | ||
| }; | ||
|
|
||
| template <template <class...> class Op, typename... Args> | ||
| using is_detected = detector<void, Op, Args...>; | ||
|
|
||
| template <template <class...> class Op, typename... Args> | ||
| static constexpr auto is_detected_v = detector<void, Op, Args...>::value; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #pragma once | ||
|
|
||
| #include "is_detected.h" | ||
| namespace kdalgorithms { | ||
|
|
||
| namespace detail { | ||
| namespace tests { | ||
| template <typename Container> | ||
| using has_reserve = decltype(std::declval<Container>().reserve(42)); | ||
|
|
||
| template <typename Container> | ||
| using has_keyValueBegin = decltype(std::declval<Container &>().keyValueBegin()); | ||
|
|
||
| template <typename Container> | ||
| using has_keyValueBegin = decltype(std::declval<Container &>().keyValueBegin()); | ||
| } | ||
|
|
||
| template <typename Container> | ||
| constexpr bool has_reserve_method_v = detail::is_detected_v<tests::has_reserve, Container>; | ||
|
|
||
| template <typename Container> | ||
| using has_keyValueBegin = detail::is_detected<tests::has_keyValueBegin, Container>; | ||
|
|
||
| template <typename Container> | ||
| constexpr bool has_keyValueBegin_v = detail::is_detected_v<tests::has_keyValueBegin, Container>; | ||
|
|
||
| } // namespace detail | ||
| } // namespace kdalgorithms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.