Move rtc::FunctionView to the public API

Bug: webrtc:10138
Change-Id: Icc25a2a277a9608701aaddd546882366739991ca
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/127898
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Artem Titov <titovartem@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#27227}
This commit is contained in:
Artem Titov 2019-03-21 14:37:36 +01:00 committed by Commit Bot
parent ab9735f6ca
commit 741daaf039
29 changed files with 175 additions and 133 deletions

View file

@ -399,6 +399,16 @@ rtc_source_set("neteq_simulator_api") {
]
}
rtc_source_set("function_view") {
visibility = [ "*" ]
sources = [
"function_view.h",
]
deps = [
"../rtc_base:checks",
]
}
if (rtc_include_tests) {
if (rtc_enable_protobuf) {
rtc_source_set("audioproc_f_api") {
@ -684,6 +694,7 @@ if (rtc_include_tests) {
sources = [
"array_view_unittest.cc",
"function_view_unittest.cc",
"rtc_error_unittest.cc",
"rtp_parameters_unittest.cc",
"test/loopback_media_transport_unittest.cc",
@ -691,6 +702,7 @@ if (rtc_include_tests) {
deps = [
":array_view",
":function_view",
":libjingle_peerconnection_api",
":loopback_media_transport",
"../rtc_base:checks",

130
api/function_view.h Normal file
View file

@ -0,0 +1,130 @@
/*
* Copyright 2016 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.
*/
#ifndef API_FUNCTION_VIEW_H_
#define API_FUNCTION_VIEW_H_
#include <type_traits>
#include <utility>
#include "rtc_base/checks.h"
// Just like std::function, FunctionView will wrap any callable and hide its
// actual type, exposing only its signature. But unlike std::function,
// FunctionView doesn't own its callable---it just points to it. Thus, it's a
// good choice mainly as a function argument when the callable argument will
// not be called again once the function has returned.
//
// Its constructors are implicit, so that callers won't have to convert lambdas
// and other callables to FunctionView<Blah(Blah, Blah)> explicitly. This is
// safe because FunctionView is only a reference to the real callable.
//
// Example use:
//
// void SomeFunction(rtc::FunctionView<int(int)> index_transform);
// ...
// SomeFunction([](int i) { return 2 * i + 1; });
//
// Note: FunctionView is tiny (essentially just two pointers) and trivially
// copyable, so it's probably cheaper to pass it by value than by const
// reference.
namespace rtc {
template <typename T>
class FunctionView; // Undefined.
template <typename RetT, typename... ArgT>
class FunctionView<RetT(ArgT...)> final {
public:
// Constructor for lambdas and other callables; it accepts every type of
// argument except those noted in its enable_if call.
template <
typename F,
typename std::enable_if<
// Not for function pointers; we have another constructor for that
// below.
!std::is_function<typename std::remove_pointer<
typename std::remove_reference<F>::type>::type>::value &&
// Not for nullptr; we have another constructor for that below.
!std::is_same<std::nullptr_t,
typename std::remove_cv<F>::type>::value &&
// Not for FunctionView objects; we have another constructor for that
// (the implicitly declared copy constructor).
!std::is_same<FunctionView,
typename std::remove_cv<typename std::remove_reference<
F>::type>::type>::value>::type* = nullptr>
FunctionView(F&& f)
: call_(CallVoidPtr<typename std::remove_reference<F>::type>) {
f_.void_ptr = &f;
}
// Constructor that accepts function pointers. If the argument is null, the
// result is an empty FunctionView.
template <
typename F,
typename std::enable_if<std::is_function<typename std::remove_pointer<
typename std::remove_reference<F>::type>::type>::value>::type* =
nullptr>
FunctionView(F&& f)
: call_(f ? CallFunPtr<typename std::remove_pointer<F>::type> : nullptr) {
f_.fun_ptr = reinterpret_cast<void (*)()>(f);
}
// Constructor that accepts nullptr. It creates an empty FunctionView.
template <typename F,
typename std::enable_if<std::is_same<
std::nullptr_t,
typename std::remove_cv<F>::type>::value>::type* = nullptr>
FunctionView(F&& f) : call_(nullptr) {}
// Default constructor. Creates an empty FunctionView.
FunctionView() : call_(nullptr) {}
RetT operator()(ArgT... args) const {
RTC_DCHECK(call_);
return call_(f_, std::forward<ArgT>(args)...);
}
// Returns true if we have a function, false if we don't (i.e., we're null).
explicit operator bool() const { return !!call_; }
private:
union VoidUnion {
void* void_ptr;
void (*fun_ptr)();
};
template <typename F>
static RetT CallVoidPtr(VoidUnion vu, ArgT... args) {
return (*static_cast<F*>(vu.void_ptr))(std::forward<ArgT>(args)...);
}
template <typename F>
static RetT CallFunPtr(VoidUnion vu, ArgT... args) {
return (reinterpret_cast<typename std::add_pointer<F>::type>(vu.fun_ptr))(
std::forward<ArgT>(args)...);
}
// A pointer to the callable thing, with type information erased. It's a
// union because we have to use separate types depending on if the callable
// thing is a function pointer or something else.
VoidUnion f_;
// Pointer to a dispatch function that knows the type of the callable thing
// that's stored in f_, and how to call it. A FunctionView object is empty
// (null) iff call_ is null.
RetT (*call_)(VoidUnion, ArgT...);
};
} // namespace rtc
#endif // API_FUNCTION_VIEW_H_

View file

@ -11,7 +11,7 @@
#include <memory>
#include <utility>
#include "rtc_base/function_view.h"
#include "api/function_view.h"
#include "test/gtest.h"
namespace rtc {
@ -97,8 +97,7 @@ TEST(FunctionViewTest, CopyConstructor) {
TEST(FunctionViewTest, MoveConstructorIsCopy) {
auto f17 = [] { return 17; };
rtc::FunctionView<int()> fv1(f17);
// NOLINTNEXTLINE(performance-move-const-arg)
rtc::FunctionView<int()> fv2(std::move(fv1));
rtc::FunctionView<int()> fv2(std::move(fv1)); // NOLINT
EXPECT_EQ(17, fv1());
EXPECT_EQ(17, fv2());
}
@ -122,7 +121,7 @@ TEST(FunctionViewTest, MoveAssignmentIsCopy) {
rtc::FunctionView<int()> fv2(f23);
EXPECT_EQ(17, fv1());
EXPECT_EQ(23, fv2());
fv2 = std::move(fv1); // NOLINT(performance-move-const-arg)
fv2 = std::move(fv1); // NOLINT
EXPECT_EQ(17, fv1());
EXPECT_EQ(17, fv2());
}

View file

@ -40,6 +40,7 @@ rtc_static_library("audio") {
deps = [
"../api:array_view",
"../api:call_api",
"../api:function_view",
"../api:libjingle_peerconnection_api",
"../api:rtp_headers",
"../api:scoped_refptr",

View file

@ -20,6 +20,7 @@
#include "api/audio_codecs/audio_format.h"
#include "api/call/transport.h"
#include "api/crypto/frame_encryptor_interface.h"
#include "api/function_view.h"
#include "audio/audio_state.h"
#include "audio/channel_send.h"
#include "audio/conversion.h"
@ -33,7 +34,6 @@
#include "modules/audio_processing/include/audio_processing.h"
#include "rtc_base/checks.h"
#include "rtc_base/event.h"
#include "rtc_base/function_view.h"
#include "rtc_base/logging.h"
#include "rtc_base/strings/audio_format_to_string.h"
#include "rtc_base/task_queue.h"

View file

@ -18,11 +18,11 @@
#include "api/audio/audio_frame.h"
#include "api/audio_codecs/audio_encoder.h"
#include "api/crypto/crypto_options.h"
#include "api/function_view.h"
#include "api/media_transport_interface.h"
#include "api/task_queue/task_queue_factory.h"
#include "modules/rtp_rtcp/include/rtp_rtcp.h"
#include "modules/rtp_rtcp/source/rtp_sender_audio.h"
#include "rtc_base/function_view.h"
namespace webrtc {

View file

@ -328,6 +328,7 @@ if (rtc_enable_protobuf) {
":rtc_event_log_impl_encoder",
":rtc_event_log_proto",
":rtc_stream_config",
"../api:function_view",
"../api:libjingle_peerconnection_api",
"../api:rtp_headers",
"../api/units:data_rate",

View file

@ -18,8 +18,8 @@
#include <vector>
#include "absl/memory/memory.h"
#include "api/function_view.h"
#include "rtc_base/checks.h"
#include "rtc_base/function_view.h"
namespace webrtc {

View file

@ -45,6 +45,7 @@ rtc_static_library("audio_coding") {
"..:module_api",
"..:module_api_public",
"../../api:array_view",
"../../api:function_view",
"../../api/audio:audio_frame_api",
"../../api/audio_codecs:audio_codecs_api",
"../../common_audio",

View file

@ -19,9 +19,9 @@
#include "absl/types/optional.h"
#include "api/audio_codecs/audio_decoder_factory.h"
#include "api/audio_codecs/audio_encoder.h"
#include "api/function_view.h"
#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
#include "modules/audio_coding/neteq/include/neteq.h"
#include "rtc_base/function_view.h"
#include "system_wrappers/include/clock.h"
namespace webrtc {

View file

@ -158,6 +158,7 @@ rtc_static_library("audio_processing") {
":noise_suppression_proxy",
"../..:webrtc_common",
"../../api:array_view",
"../../api:function_view",
"../../api/audio:aec3_config",
"../../api/audio:audio_frame_api",
"../../api/audio:echo_control",

View file

@ -35,6 +35,7 @@ rtc_source_set("rnn_vad") {
deps = [
"..:biquad_filter",
"../../../../api:array_view",
"../../../../api:function_view",
"../../../../common_audio/",
"../../../../rtc_base:checks",
"../../../../rtc_base:rtc_base_approved",

View file

@ -16,8 +16,8 @@
#include <complex>
#include "api/array_view.h"
#include "api/function_view.h"
#include "modules/audio_processing/agc2/rnn_vad/common.h"
#include "rtc_base/function_view.h"
namespace webrtc {
namespace rnn_vad {

View file

@ -15,6 +15,7 @@
#include <memory>
#include <vector>
#include "api/function_view.h"
#include "modules/audio_processing/audio_buffer.h"
#include "modules/audio_processing/include/aec_dump.h"
#include "modules/audio_processing/include/audio_processing.h"
@ -22,7 +23,6 @@
#include "modules/audio_processing/render_queue_item_verifier.h"
#include "modules/audio_processing/rms_level.h"
#include "rtc_base/critical_section.h"
#include "rtc_base/function_view.h"
#include "rtc_base/gtest_prod_util.h"
#include "rtc_base/ignore_wundef.h"
#include "rtc_base/swap_queue.h"

View file

@ -47,6 +47,7 @@ if (rtc_include_tests) {
sources = []
deps = [
"../../api:function_view",
"../../api:scoped_refptr",
"../../rtc_base:checks",
"//third_party/abseil-cpp/absl/memory",
@ -428,6 +429,7 @@ rtc_static_library("desktop_capture_generic") {
deps = [
":primitives",
"../../api:function_view",
"../../api:refcountedbase",
"../../api:scoped_refptr",
"../../rtc_base", # TODO(kjellander): Cleanup in bugs.webrtc.org/3806.

View file

@ -15,9 +15,9 @@
#include <X11/Xlib.h>
#include <stdint.h>
#include "api/function_view.h"
#include "modules/desktop_capture/desktop_geometry.h"
#include "modules/desktop_capture/linux/x_atom_cache.h"
#include "rtc_base/function_view.h"
namespace webrtc {

View file

@ -13,11 +13,11 @@
#include <ApplicationServices/ApplicationServices.h>
#include "api/function_view.h"
#include "modules/desktop_capture/desktop_capture_types.h"
#include "modules/desktop_capture/desktop_capturer.h"
#include "modules/desktop_capture/desktop_geometry.h"
#include "modules/desktop_capture/mac/desktop_configuration.h"
#include "rtc_base/function_view.h"
namespace webrtc {

View file

@ -14,8 +14,8 @@
#include <atomic>
#include "absl/memory/memory.h"
#include "api/function_view.h"
#include "rtc_base/checks.h"
#include "rtc_base/function_view.h"
#include "rtc_base/logging.h"
#include "rtc_base/platform_thread.h"
#include "rtc_base/random.h"

View file

@ -91,6 +91,7 @@ rtc_source_set("rtp_rtcp_format") {
"..:module_api_public",
"../..:webrtc_common",
"../../api:array_view",
"../../api:function_view",
"../../api:libjingle_peerconnection_api",
"../../api:rtp_headers",
"../../api/audio_codecs:audio_codecs_api",

View file

@ -15,8 +15,8 @@
#include <stdint.h>
#include "api/array_view.h"
#include "api/function_view.h"
#include "rtc_base/buffer.h"
#include "rtc_base/function_view.h"
namespace webrtc {
namespace rtcp {

View file

@ -355,6 +355,7 @@ if (rtc_include_tests) {
]
deps = [
":pc_test_utils",
"../api:function_view",
"../api:libjingle_peerconnection_api",
"../api:rtc_stats_api",
"../api:scoped_refptr",
@ -490,6 +491,7 @@ if (rtc_include_tests) {
"../api:create_peerconnection_factory",
"../api:fake_frame_decryptor",
"../api:fake_frame_encryptor",
"../api:function_view",
"../api:libjingle_logging_api",
"../api:libjingle_peerconnection_api",
"../api:loopback_media_transport",

View file

@ -16,11 +16,11 @@
#include <utility>
#include <vector>
#include "api/function_view.h"
#include "api/set_remote_description_observer_interface.h"
#include "pc/sdp_utils.h"
#include "pc/test/fake_video_track_source.h"
#include "rtc_base/checks.h"
#include "rtc_base/function_view.h"
#include "rtc_base/gunit.h"
#include "rtc_base/logging.h"
#include "rtc_base/ref_counted_object.h"

View file

@ -16,6 +16,7 @@
#include <vector>
#include "api/data_channel_interface.h"
#include "api/function_view.h"
#include "api/jsep.h"
#include "api/media_stream_interface.h"
#include "api/media_types.h"
@ -26,7 +27,6 @@
#include "api/scoped_refptr.h"
#include "api/stats/rtc_stats_report.h"
#include "pc/test/mock_peer_connection_observers.h"
#include "rtc_base/function_view.h"
namespace webrtc {

View file

@ -79,6 +79,7 @@ rtc_source_set("rtc_base_approved") {
":safe_minmax",
":type_traits",
"../api:array_view",
"../api:function_view",
"../api:scoped_refptr",
"../system_wrappers:field_trial",
"experiments:field_trial_parser",
@ -1209,7 +1210,6 @@ if (rtc_include_tests) {
"critical_section_unittest.cc",
"event_tracer_unittest.cc",
"event_unittest.cc",
"function_view_unittest.cc",
"logging_unittest.cc",
"numerics/histogram_percentile_counter_unittest.cc",
"numerics/mod_ops_unittest.cc",

View file

@ -11,120 +11,9 @@
#ifndef RTC_BASE_FUNCTION_VIEW_H_
#define RTC_BASE_FUNCTION_VIEW_H_
#include <type_traits>
#include <utility>
// This header is deprecated and will be removed. Please use the one,
// that is specified below instead.
#include "rtc_base/checks.h"
// Just like std::function, FunctionView will wrap any callable and hide its
// actual type, exposing only its signature. But unlike std::function,
// FunctionView doesn't own its callable---it just points to it. Thus, it's a
// good choice mainly as a function argument when the callable argument will
// not be called again once the function has returned.
//
// Its constructors are implicit, so that callers won't have to convert lambdas
// and other callables to FunctionView<Blah(Blah, Blah)> explicitly. This is
// safe because FunctionView is only a reference to the real callable.
//
// Example use:
//
// void SomeFunction(rtc::FunctionView<int(int)> index_transform);
// ...
// SomeFunction([](int i) { return 2 * i + 1; });
//
// Note: FunctionView is tiny (essentially just two pointers) and trivially
// copyable, so it's probably cheaper to pass it by value than by const
// reference.
namespace rtc {
template <typename T>
class FunctionView; // Undefined.
template <typename RetT, typename... ArgT>
class FunctionView<RetT(ArgT...)> final {
public:
// Constructor for lambdas and other callables; it accepts every type of
// argument except those noted in its enable_if call.
template <
typename F,
typename std::enable_if<
// Not for function pointers; we have another constructor for that
// below.
!std::is_function<typename std::remove_pointer<
typename std::remove_reference<F>::type>::type>::value &&
// Not for nullptr; we have another constructor for that below.
!std::is_same<std::nullptr_t,
typename std::remove_cv<F>::type>::value &&
// Not for FunctionView objects; we have another constructor for that
// (the implicitly declared copy constructor).
!std::is_same<FunctionView,
typename std::remove_cv<typename std::remove_reference<
F>::type>::type>::value>::type* = nullptr>
FunctionView(F&& f)
: call_(CallVoidPtr<typename std::remove_reference<F>::type>) {
f_.void_ptr = &f;
}
// Constructor that accepts function pointers. If the argument is null, the
// result is an empty FunctionView.
template <
typename F,
typename std::enable_if<std::is_function<typename std::remove_pointer<
typename std::remove_reference<F>::type>::type>::value>::type* =
nullptr>
FunctionView(F&& f)
: call_(f ? CallFunPtr<typename std::remove_pointer<F>::type> : nullptr) {
f_.fun_ptr = reinterpret_cast<void (*)()>(f);
}
// Constructor that accepts nullptr. It creates an empty FunctionView.
template <typename F,
typename std::enable_if<std::is_same<
std::nullptr_t,
typename std::remove_cv<F>::type>::value>::type* = nullptr>
FunctionView(F&& f) : call_(nullptr) {}
// Default constructor. Creates an empty FunctionView.
FunctionView() : call_(nullptr) {}
RetT operator()(ArgT... args) const {
RTC_DCHECK(call_);
return call_(f_, std::forward<ArgT>(args)...);
}
// Returns true if we have a function, false if we don't (i.e., we're null).
explicit operator bool() const { return !!call_; }
private:
union VoidUnion {
void* void_ptr;
void (*fun_ptr)();
};
template <typename F>
static RetT CallVoidPtr(VoidUnion vu, ArgT... args) {
return (*static_cast<F*>(vu.void_ptr))(std::forward<ArgT>(args)...);
}
template <typename F>
static RetT CallFunPtr(VoidUnion vu, ArgT... args) {
return (reinterpret_cast<typename std::add_pointer<F>::type>(vu.fun_ptr))(
std::forward<ArgT>(args)...);
}
// A pointer to the callable thing, with type information erased. It's a
// union because we have to use separate types depending on if the callable
// thing is a function pointer or something else.
VoidUnion f_;
// Pointer to a dispatch function that knows the type of the callable thing
// that's stored in f_, and how to call it. A FunctionView object is empty
// (null) iff call_ is null.
RetT (*call_)(VoidUnion, ArgT...);
};
} // namespace rtc
#include "api/function_view.h"
#endif // RTC_BASE_FUNCTION_VIEW_H_

View file

@ -299,6 +299,7 @@ if (!build_with_chromium) {
deps = [
":chart_proto",
"../:webrtc_common",
"../api:function_view",
# TODO(kwiberg): Remove this dependency.
"../api/audio_codecs:audio_codecs_api",

View file

@ -19,6 +19,7 @@
#include "absl/memory/memory.h"
#include "absl/strings/string_view.h"
#include "api/function_view.h"
#include "api/transport/field_trial_based_config.h"
#include "api/transport/goog_cc_factory.h"
#include "call/audio_receive_stream.h"
@ -54,7 +55,6 @@
#include "modules/rtp_rtcp/source/rtp_utility.h"
#include "rtc_base/checks.h"
#include "rtc_base/format_macros.h"
#include "rtc_base/function_view.h"
#include "rtc_base/logging.h"
#include "rtc_base/numerics/sequence_number_util.h"
#include "rtc_base/rate_statistics.h"

View file

@ -58,6 +58,7 @@ rtc_source_set("peer_connection_quality_test_fixture_api") {
":video_quality_analyzer_api",
"../../../../api:callfactory_api",
"../../../../api:fec_controller_api",
"../../../../api:function_view",
"../../../../api:libjingle_peerconnection_api",
"../../../../api:simulated_network_api",
"../../../../api/transport:network_control",

View file

@ -18,6 +18,7 @@
#include "api/async_resolver_factory.h"
#include "api/call/call_factory_interface.h"
#include "api/fec_controller.h"
#include "api/function_view.h"
#include "api/media_transport_interface.h"
#include "api/peer_connection_interface.h"
#include "api/test/simulated_network.h"
@ -27,7 +28,6 @@
#include "api/video_codecs/video_encoder.h"
#include "api/video_codecs/video_encoder_factory.h"
#include "logging/rtc_event_log/rtc_event_log_factory_interface.h"
#include "rtc_base/function_view.h"
#include "rtc_base/network.h"
#include "rtc_base/rtc_certificate_generator.h"
#include "rtc_base/ssl_certificate.h"