diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-12 14:27:29 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-13 09:35:20 +0000 |
commit | c30a6232df03e1efbd9f3b226777b07e087a1122 (patch) | |
tree | e992f45784689f373bcc38d1b79a239ebe17ee23 /chromium/base/util/ranges/functional.h | |
parent | 7b5b123ac58f58ffde0f4f6e488bcd09aa4decd3 (diff) | |
download | qtwebengine-chromium-85-based.tar.gz |
BASELINE: Update Chromium to 85.0.4183.14085-based
Change-Id: Iaa42f4680837c57725b1344f108c0196741f6057
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/base/util/ranges/functional.h')
-rw-r--r-- | chromium/base/util/ranges/functional.h | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/chromium/base/util/ranges/functional.h b/chromium/base/util/ranges/functional.h new file mode 100644 index 00000000000..9d31a71a891 --- /dev/null +++ b/chromium/base/util/ranges/functional.h @@ -0,0 +1,71 @@ +// Copyright 2020 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_UTIL_RANGES_FUNCTIONAL_H_ +#define BASE_UTIL_RANGES_FUNCTIONAL_H_ + +#include <functional> +#include <type_traits> +#include <utility> + +namespace util { + +// Implementation of C++20's std::identity. +// +// Reference: +// - https://en.cppreference.com/w/cpp/utility/functional/identity +// - https://wg21.link/func.identity +struct identity { + template <typename T> + constexpr T&& operator()(T&& t) const noexcept { + return std::forward<T>(t); + } + + using is_transparent = void; +}; + +// Minimal implementation of C++17's std::invoke. Based on implementation +// referenced in original std::invoke proposal. +// +// Note: Unlike C++20's std::invoke this implementation is not constexpr. A +// constexpr version can be added in the future, but it won't be as concise, +// since std::mem_fn is not constexpr prior to C++20. +// +// References: +// - https://wg21.link/n4169#implementability +// - https://en.cppreference.com/w/cpp/utility/functional/invoke +// - https://wg21.link/func.invoke +template <typename Functor, + typename... Args, + std::enable_if_t< + std::is_member_pointer<std::decay_t<Functor>>::value>* = nullptr> +decltype(auto) invoke(Functor&& f, Args&&... args) { + return std::mem_fn(f)(std::forward<Args>(args)...); +} + +template <typename Functor, + typename... Args, + std::enable_if_t< + !std::is_member_pointer<std::decay_t<Functor>>::value>* = nullptr> +decltype(auto) invoke(Functor&& f, Args&&... args) { + return std::forward<Functor>(f)(std::forward<Args>(args)...); +} + +// Simplified implementations of C++20's std::ranges comparison function +// objects. As opposed to the std::ranges implementation, these versions do not +// constrain the passed-in types. +// +// Reference: https://wg21.link/range.cmp +namespace ranges { +using equal_to = std::equal_to<>; +using not_equal_to = std::not_equal_to<>; +using greater = std::greater<>; +using less = std::less<>; +using greater_equal = std::greater_equal<>; +using less_equal = std::less_equal<>; +} // namespace ranges + +} // namespace util + +#endif // BASE_UTIL_RANGES_FUNCTIONAL_H_ |