From e1611a07d8128b001ea882e6e395e6b7083d5df9 Mon Sep 17 00:00:00 2001 From: Steve Anton Date: Wed, 27 Nov 2019 10:53:45 -0800 Subject: [PATCH] Replace template_util.h with C++14 STL methods Bug: None Change-Id: Ib24889db4f452353afab816af4f9618b2767021f Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/160760 Commit-Queue: Steve Anton Reviewed-by: Karl Wiberg Cr-Commit-Position: refs/heads/master@{#29942} --- rtc_base/BUILD.gn | 1 - rtc_base/bind.h | 28 +++----- rtc_base/bind_unittest.cc | 13 ++-- rtc_base/template_util.h | 138 -------------------------------------- 4 files changed, 15 insertions(+), 165 deletions(-) delete mode 100644 rtc_base/template_util.h diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index 154f3ec7ce..d626adde12 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -104,7 +104,6 @@ rtc_library("rtc_base_approved") { "rate_tracker.cc", "rate_tracker.h", "swap_queue.h", - "template_util.h", "timestamp_aligner.cc", "timestamp_aligner.h", "trace_event.h", diff --git a/rtc_base/bind.h b/rtc_base/bind.h index 16ac556b46..b61d189f7a 100644 --- a/rtc_base/bind.h +++ b/rtc_base/bind.h @@ -65,7 +65,6 @@ #include #include "api/scoped_refptr.h" -#include "rtc_base/template_util.h" #define NONAME @@ -160,15 +159,12 @@ class MethodFunctor { MethodFunctor(MethodT method, ObjectT* object, Args... args) : method_(method), object_(object), args_(args...) {} R operator()() const { - return CallMethod(typename sequence_generator::type()); + return CallMethod(std::index_sequence_for()); } private: - // Use sequence_generator (see template_util.h) to expand a MethodFunctor - // with 2 arguments to (std::get<0>(args_), std::get<1>(args_)), for - // instance. - template - R CallMethod(sequence) const { + template + R CallMethod(std::index_sequence) const { return (object_->*method_)(std::get(args_)...); } @@ -185,15 +181,12 @@ class UnretainedMethodFunctor { Args... args) : method_(method), object_(object.get()), args_(args...) {} R operator()() const { - return CallMethod(typename sequence_generator::type()); + return CallMethod(std::index_sequence_for()); } private: - // Use sequence_generator (see template_util.h) to expand an - // UnretainedMethodFunctor with 2 arguments to (std::get<0>(args_), - // std::get<1>(args_)), for instance. - template - R CallMethod(sequence) const { + template + R CallMethod(std::index_sequence) const { return (object_->*method_)(std::get(args_)...); } @@ -208,15 +201,12 @@ class Functor { Functor(const FunctorT& functor, Args... args) : functor_(functor), args_(args...) {} R operator()() const { - return CallFunction(typename sequence_generator::type()); + return CallFunction(std::index_sequence_for()); } private: - // Use sequence_generator (see template_util.h) to expand a Functor - // with 2 arguments to (std::get<0>(args_), std::get<1>(args_)), for - // instance. - template - R CallFunction(sequence) const { + template + R CallFunction(std::index_sequence) const { return functor_(std::get(args_)...); } diff --git a/rtc_base/bind_unittest.cc b/rtc_base/bind_unittest.cc index a97d2bee6d..664cb54500 100644 --- a/rtc_base/bind_unittest.cc +++ b/rtc_base/bind_unittest.cc @@ -97,13 +97,12 @@ int Multiply(int a, int b) { // Try to catch any problem with scoped_refptr type deduction in rtc::Bind at // compile time. -#define EXPECT_IS_CAPTURED_AS_PTR(T) \ - static_assert(is_same::type, T*>::value, \ - "PointerTyp" \ - "e") -#define EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(T) \ - static_assert( \ - is_same::type, scoped_refptr>::value, \ +#define EXPECT_IS_CAPTURED_AS_PTR(T) \ + static_assert(std::is_same::type, T*>::value, \ + "PointerType") +#define EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(T) \ + static_assert( \ + std::is_same::type, scoped_refptr>::value, \ "PointerType") EXPECT_IS_CAPTURED_AS_PTR(void); diff --git a/rtc_base/template_util.h b/rtc_base/template_util.h deleted file mode 100644 index 3c04a860d8..0000000000 --- a/rtc_base/template_util.h +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Copyright (c) 2013 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. - */ - -// Borrowed from Chromium's src/base/template_util.h. - -#ifndef RTC_BASE_TEMPLATE_UTIL_H_ -#define RTC_BASE_TEMPLATE_UTIL_H_ - -#include // For size_t. - -namespace rtc { - -// Template definitions from tr1. - -template -struct integral_constant { - static const T value = v; - typedef T value_type; - typedef integral_constant type; -}; - -template -const T integral_constant::value; - -typedef integral_constant true_type; -typedef integral_constant false_type; - -template -struct is_pointer : false_type {}; -template -struct is_pointer : true_type {}; - -template -struct is_same : public false_type {}; -template -struct is_same : true_type {}; - -template -struct is_array : public false_type {}; -template -struct is_array : public true_type {}; -template -struct is_array : public true_type {}; - -template -struct is_non_const_reference : false_type {}; -template -struct is_non_const_reference : true_type {}; -template -struct is_non_const_reference : false_type {}; - -template -struct is_void : false_type {}; -template <> -struct is_void : true_type {}; - -// Helper useful for converting a tuple to variadic template function -// arguments. -// -// sequence_generator<3>::type will be sequence<0, 1, 2>. -template -struct sequence {}; -template -struct sequence_generator : sequence_generator {}; -template -struct sequence_generator<0, S...> { - typedef sequence type; -}; - -namespace internal { - -// Types YesType and NoType are guaranteed such that sizeof(YesType) < -// sizeof(NoType). -typedef char YesType; - -struct NoType { - YesType dummy[2]; -}; - -// This class is an implementation detail for is_convertible, and you -// don't need to know how it works to use is_convertible. For those -// who care: we declare two different functions, one whose argument is -// of type To and one with a variadic argument list. We give them -// return types of different size, so we can use sizeof to trick the -// compiler into telling us which function it would have chosen if we -// had called it with an argument of type From. See Alexandrescu's -// _Modern C++ Design_ for more details on this sort of trick. - -struct ConvertHelper { - template - static YesType Test(To); - - template - static NoType Test(...); - - template - static From& Create(); -}; - -// Used to determine if a type is a struct/union/class. Inspired by Boost's -// is_class type_trait implementation. -struct IsClassHelper { - template - static YesType Test(void (C::*)(void)); - - template - static NoType Test(...); -}; - -} // namespace internal - -// Inherits from true_type if From is convertible to To, false_type otherwise. -// -// Note that if the type is convertible, this will be a true_type REGARDLESS -// of whether or not the conversion would emit a warning. -template -struct is_convertible - : integral_constant( - internal::ConvertHelper::Create())) == - sizeof(internal::YesType)> {}; - -template -struct is_class - : integral_constant(0)) == - sizeof(internal::YesType)> {}; - -} // namespace rtc - -#endif // RTC_BASE_TEMPLATE_UTIL_H_