|
||||
File indexing completed on 2024-11-15 09:01:03
0001 // Copyright 2019 The Abseil Authors. 0002 // 0003 // Licensed under the Apache License, Version 2.0 (the "License"); 0004 // you may not use this file except in compliance with the License. 0005 // You may obtain a copy of the License at 0006 // 0007 // https://www.apache.org/licenses/LICENSE-2.0 0008 // 0009 // Unless required by applicable law or agreed to in writing, software 0010 // distributed under the License is distributed on an "AS IS" BASIS, 0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0012 // See the License for the specific language governing permissions and 0013 // limitations under the License. 0014 // 0015 // ----------------------------------------------------------------------------- 0016 // File: function_ref.h 0017 // ----------------------------------------------------------------------------- 0018 // 0019 // This header file defines the `absl::FunctionRef` type for holding a 0020 // non-owning reference to an object of any invocable type. This function 0021 // reference is typically most useful as a type-erased argument type for 0022 // accepting function types that neither take ownership nor copy the type; using 0023 // the reference type in this case avoids a copy and an allocation. Best 0024 // practices of other non-owning reference-like objects (such as 0025 // `absl::string_view`) apply here. 0026 // 0027 // An `absl::FunctionRef` is similar in usage to a `std::function` but has the 0028 // following differences: 0029 // 0030 // * It doesn't own the underlying object. 0031 // * It doesn't have a null or empty state. 0032 // * It never performs deep copies or allocations. 0033 // * It's much faster and cheaper to construct. 0034 // * It's trivially copyable and destructable. 0035 // 0036 // Generally, `absl::FunctionRef` should not be used as a return value, data 0037 // member, or to initialize a `std::function`. Such usages will often lead to 0038 // problematic lifetime issues. Once you convert something to an 0039 // `absl::FunctionRef` you cannot make a deep copy later. 0040 // 0041 // This class is suitable for use wherever a "const std::function<>&" 0042 // would be used without making a copy. ForEach functions and other versions of 0043 // the visitor pattern are a good example of when this class should be used. 0044 // 0045 // This class is trivial to copy and should be passed by value. 0046 #ifndef ABSL_FUNCTIONAL_FUNCTION_REF_H_ 0047 #define ABSL_FUNCTIONAL_FUNCTION_REF_H_ 0048 0049 #include <cassert> 0050 #include <functional> 0051 #include <type_traits> 0052 0053 #include "absl/base/attributes.h" 0054 #include "absl/functional/internal/function_ref.h" 0055 #include "absl/meta/type_traits.h" 0056 0057 namespace absl { 0058 ABSL_NAMESPACE_BEGIN 0059 0060 // FunctionRef 0061 // 0062 // Dummy class declaration to allow the partial specialization based on function 0063 // types below. 0064 template <typename T> 0065 class FunctionRef; 0066 0067 // FunctionRef 0068 // 0069 // An `absl::FunctionRef` is a lightweight wrapper to any invocable object with 0070 // a compatible signature. Generally, an `absl::FunctionRef` should only be used 0071 // as an argument type and should be preferred as an argument over a const 0072 // reference to a `std::function`. `absl::FunctionRef` itself does not allocate, 0073 // although the wrapped invocable may. 0074 // 0075 // Example: 0076 // 0077 // // The following function takes a function callback by const reference 0078 // bool Visitor(const std::function<void(my_proto&, 0079 // absl::string_view)>& callback); 0080 // 0081 // // Assuming that the function is not stored or otherwise copied, it can be 0082 // // replaced by an `absl::FunctionRef`: 0083 // bool Visitor(absl::FunctionRef<void(my_proto&, absl::string_view)> 0084 // callback); 0085 // 0086 // Note: the assignment operator within an `absl::FunctionRef` is intentionally 0087 // deleted to prevent misuse; because the `absl::FunctionRef` does not own the 0088 // underlying type, assignment likely indicates misuse. 0089 template <typename R, typename... Args> 0090 class FunctionRef<R(Args...)> { 0091 private: 0092 // Used to disable constructors for objects that are not compatible with the 0093 // signature of this FunctionRef. 0094 template <typename F, 0095 typename FR = absl::base_internal::invoke_result_t<F, Args&&...>> 0096 using EnableIfCompatible = 0097 typename std::enable_if<std::is_void<R>::value || 0098 std::is_convertible<FR, R>::value>::type; 0099 0100 public: 0101 // Constructs a FunctionRef from any invocable type. 0102 template <typename F, typename = EnableIfCompatible<const F&>> 0103 // NOLINTNEXTLINE(runtime/explicit) 0104 FunctionRef(const F& f ABSL_ATTRIBUTE_LIFETIME_BOUND) 0105 : invoker_(&absl::functional_internal::InvokeObject<F, R, Args...>) { 0106 absl::functional_internal::AssertNonNull(f); 0107 ptr_.obj = &f; 0108 } 0109 0110 // Overload for function pointers. This eliminates a level of indirection that 0111 // would happen if the above overload was used (it lets us store the pointer 0112 // instead of a pointer to a pointer). 0113 // 0114 // This overload is also used for references to functions, since references to 0115 // functions can decay to function pointers implicitly. 0116 template < 0117 typename F, typename = EnableIfCompatible<F*>, 0118 absl::functional_internal::EnableIf<absl::is_function<F>::value> = 0> 0119 FunctionRef(F* f) // NOLINT(runtime/explicit) 0120 : invoker_(&absl::functional_internal::InvokeFunction<F*, R, Args...>) { 0121 assert(f != nullptr); 0122 ptr_.fun = reinterpret_cast<decltype(ptr_.fun)>(f); 0123 } 0124 0125 // To help prevent subtle lifetime bugs, FunctionRef is not assignable. 0126 // Typically, it should only be used as an argument type. 0127 FunctionRef& operator=(const FunctionRef& rhs) = delete; 0128 FunctionRef(const FunctionRef& rhs) = default; 0129 0130 // Call the underlying object. 0131 R operator()(Args... args) const { 0132 return invoker_(ptr_, std::forward<Args>(args)...); 0133 } 0134 0135 private: 0136 absl::functional_internal::VoidPtr ptr_; 0137 absl::functional_internal::Invoker<R, Args...> invoker_; 0138 }; 0139 0140 // Allow const qualified function signatures. Since FunctionRef requires 0141 // constness anyway we can just make this a no-op. 0142 template <typename R, typename... Args> 0143 class FunctionRef<R(Args...) const> : public FunctionRef<R(Args...)> { 0144 public: 0145 using FunctionRef<R(Args...)>::FunctionRef; 0146 }; 0147 0148 ABSL_NAMESPACE_END 0149 } // namespace absl 0150 0151 #endif // ABSL_FUNCTIONAL_FUNCTION_REF_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |