Replace as_const/void_t/no_fn type traits with their c++17 variants

webrtc own implementaions are no longer needed since webrtc uses c++17

Bug: None
Change-Id: I38bb295334182b73d333a453001d256e6df172d9
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/270924
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37716}
This commit is contained in:
Danil Chapovalov 2022-08-08 18:43:35 +02:00 committed by WebRTC LUCI CQ
parent 52701781b9
commit 9ee752aefb
6 changed files with 10 additions and 147 deletions

View file

@ -10,14 +10,11 @@ import("../../webrtc.gni")
rtc_library("flat_containers_internal") { rtc_library("flat_containers_internal") {
sources = [ sources = [
"as_const.h",
"flat_tree.cc", "flat_tree.cc",
"flat_tree.h", "flat_tree.h",
"identity.h", "identity.h",
"invoke.h", "invoke.h",
"move_only_int.h", "move_only_int.h",
"not_fn.h",
"void_t.h",
] ]
deps = [ deps = [
"..:checks", "..:checks",

View file

@ -1,32 +0,0 @@
/*
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
// This implementation is borrowed from Chromium.
#ifndef RTC_BASE_CONTAINERS_AS_CONST_H_
#define RTC_BASE_CONTAINERS_AS_CONST_H_
#include <type_traits>
namespace webrtc {
// C++14 implementation of C++17's std::as_const():
// https://en.cppreference.com/w/cpp/utility/as_const
template <typename T>
constexpr std::add_const_t<T>& as_const(T& t) noexcept {
return t;
}
template <typename T>
void as_const(const T&& t) = delete;
} // namespace webrtc
#endif // RTC_BASE_CONTAINERS_AS_CONST_H_

View file

@ -14,6 +14,7 @@
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <type_traits>
#include <vector> #include <vector>
#include "rtc_base/containers/move_only_int.h" #include "rtc_base/containers/move_only_int.h"
@ -220,7 +221,7 @@ TEST(FlatMap, AtFunction) {
EXPECT_EQ("b", m.at(2)); EXPECT_EQ("b", m.at(2));
// Const reference works. // Const reference works.
const std::string& const_ref = webrtc::as_const(m).at(1); const std::string& const_ref = std::as_const(m).at(1);
EXPECT_EQ("a", const_ref); EXPECT_EQ("a", const_ref);
// Reference works, can operate on the string. // Reference works, can operate on the string.
@ -234,7 +235,7 @@ TEST(FlatMap, AtFunction) {
// Heterogeneous look-up works. // Heterogeneous look-up works.
flat_map<std::string, int> m2 = {{"a", 1}, {"b", 2}}; flat_map<std::string, int> m2 = {{"a", 1}, {"b", 2}};
EXPECT_EQ(1, m2.at(absl::string_view("a"))); EXPECT_EQ(1, m2.at(absl::string_view("a")));
EXPECT_EQ(2, webrtc::as_const(m2).at(absl::string_view("b"))); EXPECT_EQ(2, std::as_const(m2).at(absl::string_view("b")));
} }
// insert_or_assign(K&&, M&&) // insert_or_assign(K&&, M&&)

View file

@ -21,9 +21,6 @@
#include "absl/algorithm/container.h" #include "absl/algorithm/container.h"
#include "rtc_base/checks.h" #include "rtc_base/checks.h"
#include "rtc_base/containers/as_const.h"
#include "rtc_base/containers/not_fn.h"
#include "rtc_base/containers/void_t.h"
#include "rtc_base/system/no_unique_address.h" #include "rtc_base/system/no_unique_address.h"
namespace webrtc { namespace webrtc {
@ -44,7 +41,7 @@ constexpr bool is_sorted_and_unique(const Range& range, Comp comp) {
// Being unique implies that there are no adjacent elements that // Being unique implies that there are no adjacent elements that
// compare equal. So this checks that each element is strictly less // compare equal. So this checks that each element is strictly less
// than the element after it. // than the element after it.
return absl::c_adjacent_find(range, webrtc::not_fn(comp)) == std::end(range); return absl::c_adjacent_find(range, std::not_fn(comp)) == std::end(range);
} }
// This is a convenience trait inheriting from std::true_type if Iterator is at // This is a convenience trait inheriting from std::true_type if Iterator is at
@ -58,7 +55,7 @@ using is_multipass =
template <typename T, typename = void> template <typename T, typename = void>
struct IsTransparentCompare : std::false_type {}; struct IsTransparentCompare : std::false_type {};
template <typename T> template <typename T>
struct IsTransparentCompare<T, void_t<typename T::is_transparent>> struct IsTransparentCompare<T, std::void_t<typename T::is_transparent>>
: std::true_type {}; : std::true_type {};
// Helper inspired by C++20's std::to_array to convert a C-style array to a // Helper inspired by C++20's std::to_array to convert a C-style array to a
@ -543,7 +540,7 @@ class flat_tree {
std::stable_sort(first, last, value_comp()); std::stable_sort(first, last, value_comp());
// lhs is already <= rhs due to sort, therefore !(lhs < rhs) <=> lhs == rhs. // lhs is already <= rhs due to sort, therefore !(lhs < rhs) <=> lhs == rhs.
auto equal_comp = webrtc::not_fn(value_comp()); auto equal_comp = std::not_fn(value_comp());
erase(std::unique(first, last, equal_comp), last); erase(std::unique(first, last, equal_comp), last);
} }
@ -946,7 +943,7 @@ template <class Key, class GetKeyFromValue, class KeyCompare, class Container>
template <typename K> template <typename K>
auto flat_tree<Key, GetKeyFromValue, KeyCompare, Container>::find(const K& key) auto flat_tree<Key, GetKeyFromValue, KeyCompare, Container>::find(const K& key)
-> iterator { -> iterator {
return const_cast_it(webrtc::as_const(*this).find(key)); return const_cast_it(std::as_const(*this).find(key));
} }
template <class Key, class GetKeyFromValue, class KeyCompare, class Container> template <class Key, class GetKeyFromValue, class KeyCompare, class Container>
@ -969,7 +966,7 @@ template <class Key, class GetKeyFromValue, class KeyCompare, class Container>
template <typename K> template <typename K>
auto flat_tree<Key, GetKeyFromValue, KeyCompare, Container>::equal_range( auto flat_tree<Key, GetKeyFromValue, KeyCompare, Container>::equal_range(
const K& key) -> std::pair<iterator, iterator> { const K& key) -> std::pair<iterator, iterator> {
auto res = webrtc::as_const(*this).equal_range(key); auto res = std::as_const(*this).equal_range(key);
return {const_cast_it(res.first), const_cast_it(res.second)}; return {const_cast_it(res.first), const_cast_it(res.second)};
} }
@ -990,7 +987,7 @@ template <class Key, class GetKeyFromValue, class KeyCompare, class Container>
template <typename K> template <typename K>
auto flat_tree<Key, GetKeyFromValue, KeyCompare, Container>::lower_bound( auto flat_tree<Key, GetKeyFromValue, KeyCompare, Container>::lower_bound(
const K& key) -> iterator { const K& key) -> iterator {
return const_cast_it(webrtc::as_const(*this).lower_bound(key)); return const_cast_it(std::as_const(*this).lower_bound(key));
} }
template <class Key, class GetKeyFromValue, class KeyCompare, class Container> template <class Key, class GetKeyFromValue, class KeyCompare, class Container>
@ -1011,7 +1008,7 @@ template <class Key, class GetKeyFromValue, class KeyCompare, class Container>
template <typename K> template <typename K>
auto flat_tree<Key, GetKeyFromValue, KeyCompare, Container>::upper_bound( auto flat_tree<Key, GetKeyFromValue, KeyCompare, Container>::upper_bound(
const K& key) -> iterator { const K& key) -> iterator {
return const_cast_it(webrtc::as_const(*this).upper_bound(key)); return const_cast_it(std::as_const(*this).upper_bound(key));
} }
template <class Key, class GetKeyFromValue, class KeyCompare, class Container> template <class Key, class GetKeyFromValue, class KeyCompare, class Container>

View file

@ -1,64 +0,0 @@
/*
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
// This implementation is borrowed from Chromium.
#ifndef RTC_BASE_CONTAINERS_NOT_FN_H_
#define RTC_BASE_CONTAINERS_NOT_FN_H_
#include <type_traits>
#include <utility>
#include "rtc_base/containers/invoke.h"
namespace webrtc {
namespace not_fn_internal {
template <typename F>
struct NotFnImpl {
F f;
template <typename... Args>
constexpr decltype(auto) operator()(Args&&... args) & noexcept {
return !webrtc::invoke(f, std::forward<Args>(args)...);
}
template <typename... Args>
constexpr decltype(auto) operator()(Args&&... args) const& noexcept {
return !webrtc::invoke(f, std::forward<Args>(args)...);
}
template <typename... Args>
constexpr decltype(auto) operator()(Args&&... args) && noexcept {
return !webrtc::invoke(std::move(f), std::forward<Args>(args)...);
}
template <typename... Args>
constexpr decltype(auto) operator()(Args&&... args) const&& noexcept {
return !webrtc::invoke(std::move(f), std::forward<Args>(args)...);
}
};
} // namespace not_fn_internal
// Implementation of C++17's std::not_fn.
//
// Reference:
// - https://en.cppreference.com/w/cpp/utility/functional/not_fn
// - https://wg21.link/func.not.fn
template <typename F>
constexpr not_fn_internal::NotFnImpl<std::decay_t<F>> not_fn(F&& f) {
return {std::forward<F>(f)};
}
} // namespace webrtc
#endif // RTC_BASE_CONTAINERS_NOT_FN_H_

View file

@ -1,36 +0,0 @@
/*
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
// This implementation is borrowed from Chromium.
#ifndef RTC_BASE_CONTAINERS_VOID_T_H_
#define RTC_BASE_CONTAINERS_VOID_T_H_
namespace webrtc {
namespace void_t_internal {
// Implementation detail of webrtc::void_t below.
template <typename...>
struct make_void {
using type = void;
};
} // namespace void_t_internal
// webrtc::void_t is an implementation of std::void_t from C++17.
//
// We use `webrtc::void_t_internal::make_void` as a helper struct to avoid a
// C++14 defect:
// http://en.cppreference.com/w/cpp/types/void_t
// http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1558
template <typename... Ts>
using void_t = typename ::webrtc::void_t_internal::make_void<Ts...>::type;
} // namespace webrtc
#endif // RTC_BASE_CONTAINERS_VOID_T_H_