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