function_view.h (4763B)
1 /* 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef API_FUNCTION_VIEW_H_ 12 #define API_FUNCTION_VIEW_H_ 13 14 #include <cstddef> 15 #include <type_traits> 16 #include <utility> 17 18 #include "rtc_base/checks.h" 19 20 // Just like std::function, FunctionView will wrap any callable and hide its 21 // actual type, exposing only its signature. But unlike std::function, 22 // FunctionView doesn't own its callable---it just points to it. Thus, it's a 23 // good choice mainly as a function argument when the callable argument will 24 // not be called again once the function has returned. 25 // 26 // Its constructors are implicit, so that callers won't have to convert lambdas 27 // and other callables to FunctionView<Blah(Blah, Blah)> explicitly. This is 28 // safe because FunctionView is only a reference to the real callable. 29 // 30 // Example use: 31 // 32 // void SomeFunction(webrtc::FunctionView<int(int)> index_transform); 33 // ... 34 // SomeFunction([](int i) { return 2 * i + 1; }); 35 // 36 // Note: FunctionView is tiny (essentially just two pointers) and trivially 37 // copyable, so it's probably cheaper to pass it by value than by const 38 // reference. 39 40 namespace webrtc { 41 42 template <typename T> 43 class FunctionView; // Undefined. 44 45 template <typename RetT, typename... ArgT> 46 class FunctionView<RetT(ArgT...)> final { 47 public: 48 // Constructor for lambdas and other callables; it accepts every type of 49 // argument except those noted in its enable_if call. 50 template < 51 typename F, 52 typename std::enable_if< 53 // Not for function pointers; we have another constructor for that 54 // below. 55 !std::is_function<typename std::remove_pointer< 56 typename std::remove_reference<F>::type>::type>::value && 57 58 // Not for nullptr; we have another constructor for that below. 59 !std::is_same<std::nullptr_t, 60 typename std::remove_cv<F>::type>::value && 61 62 // Not for FunctionView objects; we have another constructor for that 63 // (the implicitly declared copy constructor). 64 !std::is_same<FunctionView, 65 typename std::remove_cv<typename std::remove_reference< 66 F>::type>::type>::value>::type* = nullptr> 67 FunctionView(F&& f) 68 : call_(CallVoidPtr<typename std::remove_reference<F>::type>) { 69 f_.void_ptr = &f; 70 } 71 72 // Constructor that accepts function pointers. If the argument is null, the 73 // result is an empty FunctionView. 74 template < 75 typename F, 76 typename std::enable_if<std::is_function<typename std::remove_pointer< 77 typename std::remove_reference<F>::type>::type>::value>::type* = 78 nullptr> 79 FunctionView(F&& f) 80 : call_(f ? CallFunPtr<typename std::remove_pointer<F>::type> : nullptr) { 81 f_.fun_ptr = reinterpret_cast<void (*)()>(f); 82 } 83 84 // Constructor that accepts nullptr. It creates an empty FunctionView. 85 template <typename F, 86 typename std::enable_if<std::is_same< 87 std::nullptr_t, 88 typename std::remove_cv<F>::type>::value>::type* = nullptr> 89 FunctionView(F&& /* f */) : call_(nullptr) {} 90 91 // Default constructor. Creates an empty FunctionView. 92 FunctionView() : call_(nullptr) {} 93 94 RetT operator()(ArgT... args) const { 95 RTC_DCHECK(call_); 96 return call_(f_, std::forward<ArgT>(args)...); 97 } 98 99 // Returns true if we have a function, false if we don't (i.e., we're null). 100 explicit operator bool() const { return !!call_; } 101 102 private: 103 union VoidUnion { 104 void* void_ptr; 105 void (*fun_ptr)(); 106 }; 107 108 template <typename F> 109 static RetT CallVoidPtr(VoidUnion vu, ArgT... args) { 110 return (*static_cast<F*>(vu.void_ptr))(std::forward<ArgT>(args)...); 111 } 112 template <typename F> 113 static RetT CallFunPtr(VoidUnion vu, ArgT... args) { 114 return (reinterpret_cast<typename std::add_pointer<F>::type>(vu.fun_ptr))( 115 std::forward<ArgT>(args)...); 116 } 117 118 // A pointer to the callable thing, with type information erased. It's a 119 // union because we have to use separate types depending on if the callable 120 // thing is a function pointer or something else. 121 VoidUnion f_; 122 123 // Pointer to a dispatch function that knows the type of the callable thing 124 // that's stored in f_, and how to call it. A FunctionView object is empty 125 // (null) iff call_ is null. 126 RetT (*call_)(VoidUnion, ArgT...); 127 }; 128 129 } // namespace webrtc 130 131 132 #endif // API_FUNCTION_VIEW_H_