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