[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // This file contains utility functions and classes that help the |
| 6 | // implementation, and management of the Callback objects. |
| 7 | |
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 8 | #ifndef BASE_CALLBACK_INTERNAL_H_ |
| 9 | #define BASE_CALLBACK_INTERNAL_H_ |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 10 | #pragma once |
| 11 | |
[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 12 | #include <stddef.h> |
| 13 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 14 | #include "base/base_export.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 16 | #include "base/memory/scoped_ptr.h" |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 17 | |
| 18 | namespace base { |
| 19 | namespace internal { |
| 20 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 21 | // BindStateBase is used to provide an opaque handle that the Callback |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 22 | // class can use to represent a function object with bound arguments. It |
| 23 | // behaves as an existential type that is used by a corresponding |
| 24 | // DoInvoke function to perform the function execution. This allows |
| 25 | // us to shield the Callback class from the types of the bound argument via |
| 26 | // "type erasure." |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 27 | class BindStateBase : public RefCountedThreadSafe<BindStateBase> { |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 28 | protected: |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 29 | friend class RefCountedThreadSafe<BindStateBase>; |
| 30 | virtual ~BindStateBase() {} |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 31 | }; |
| 32 | |
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 33 | // Holds the Callback methods that don't require specialization to reduce |
| 34 | // template bloat. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 35 | class BASE_EXPORT CallbackBase { |
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 36 | public: |
| 37 | // Returns true if Callback is null (doesn't refer to anything). |
| 38 | bool is_null() const; |
| 39 | |
[email protected] | 1c35a48 | 2011-10-25 23:19:51 | [diff] [blame] | 40 | // Returns the Callback into an uninitialized state. |
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 41 | void Reset(); |
| 42 | |
[email protected] | 481915a77 | 2011-09-10 03:14:35 | [diff] [blame] | 43 | protected: |
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 44 | // In C++, it is safe to cast function pointers to function pointers of |
| 45 | // another type. It is not okay to use void*. We create a InvokeFuncStorage |
| 46 | // that that can store our function pointer, and then cast it back to |
| 47 | // the original type on usage. |
| 48 | typedef void(*InvokeFuncStorage)(void); |
| 49 | |
[email protected] | 1c35a48 | 2011-10-25 23:19:51 | [diff] [blame] | 50 | // Returns true if this callback equals |other|. |other| may be null. |
| 51 | bool Equals(const CallbackBase& other) const; |
| 52 | |
[email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 53 | // Allow initializing of |bind_state_| via the constructor to avoid default |
| 54 | // initialization of the scoped_refptr. We do not also initialize |
| 55 | // |polymorphic_invoke_| here because doing a normal assignment in the |
| 56 | // derived Callback templates makes for much nicer compiler errors. |
| 57 | explicit CallbackBase(BindStateBase* bind_state); |
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 58 | |
[email protected] | 1c35a48 | 2011-10-25 23:19:51 | [diff] [blame] | 59 | // Force the destructor to be instantiated inside this translation unit so |
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 60 | // that our subclasses will not get inlined versions. Avoids more template |
| 61 | // bloat. |
| 62 | ~CallbackBase(); |
| 63 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 64 | scoped_refptr<BindStateBase> bind_state_; |
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 65 | InvokeFuncStorage polymorphic_invoke_; |
| 66 | }; |
| 67 | |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 68 | // This is a typetraits object that's used to take an argument type, and |
| 69 | // extract a suitable type for storing and forwarding arguments. |
| 70 | // |
| 71 | // In particular, it strips off references, and converts arrays to |
| 72 | // pointers for storage; and it avoids accidentally trying to create a |
| 73 | // "reference of a reference" if the argument is a reference type. |
| 74 | // |
| 75 | // This array type becomes an issue for storage because we are passing bound |
| 76 | // parameters by const reference. In this case, we end up passing an actual |
| 77 | // array type in the initializer list which C++ does not allow. This will |
| 78 | // break passing of C-string literals. |
| 79 | template <typename T> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 80 | struct CallbackParamTraits { |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 81 | typedef const T& ForwardType; |
| 82 | typedef T StorageType; |
| 83 | }; |
| 84 | |
| 85 | // The Storage should almost be impossible to trigger unless someone manually |
| 86 | // specifies type of the bind parameters. However, in case they do, |
| 87 | // this will guard against us accidentally storing a reference parameter. |
| 88 | // |
| 89 | // The ForwardType should only be used for unbound arguments. |
| 90 | template <typename T> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 91 | struct CallbackParamTraits<T&> { |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 92 | typedef T& ForwardType; |
| 93 | typedef T StorageType; |
| 94 | }; |
| 95 | |
| 96 | // Note that for array types, we implicitly add a const in the conversion. This |
| 97 | // means that it is not possible to bind array arguments to functions that take |
| 98 | // a non-const pointer. Trying to specialize the template based on a "const |
| 99 | // T[n]" does not seem to match correctly, so we are stuck with this |
| 100 | // restriction. |
| 101 | template <typename T, size_t n> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 102 | struct CallbackParamTraits<T[n]> { |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 103 | typedef const T* ForwardType; |
| 104 | typedef const T* StorageType; |
| 105 | }; |
| 106 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 107 | // See comment for CallbackParamTraits<T[n]>. |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 108 | template <typename T> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 109 | struct CallbackParamTraits<T[]> { |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 110 | typedef const T* ForwardType; |
| 111 | typedef const T* StorageType; |
| 112 | }; |
| 113 | |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 114 | // Parameter traits for movable-but-not-copyable scopers. |
| 115 | // |
| 116 | // Callback<>/Bind() understands movable-but-not-copyable semantics where |
| 117 | // the type cannot be copied but can still have its state destructively |
| 118 | // transferred (aka. moved) to another instance of the same type by calling a |
| 119 | // helper function. When used with Bind(), this signifies transferal of the |
| 120 | // object's state to the target function. |
| 121 | // |
| 122 | // For these types, the ForwardType must not be a const reference, or a |
| 123 | // reference. A const reference is inappropriate, and would break const |
| 124 | // correctness, because we are implementing a destructive move. A non-const |
| 125 | // reference cannot be used with temporaries which means the result of a |
| 126 | // function or a cast would not be usable with Callback<> or Bind(). |
| 127 | // |
| 128 | // TODO(ajwong): We might be able to use SFINAE to search for the existence of |
| 129 | // a Pass() function in the type and avoid the whitelist in CallbackParamTraits |
| 130 | // and CallbackForward. |
| 131 | template <typename T> |
| 132 | struct CallbackParamTraits<scoped_ptr<T> > { |
| 133 | typedef scoped_ptr<T> ForwardType; |
| 134 | typedef scoped_ptr<T> StorageType; |
| 135 | }; |
| 136 | |
| 137 | template <typename T> |
| 138 | struct CallbackParamTraits<scoped_array<T> > { |
| 139 | typedef scoped_array<T> ForwardType; |
| 140 | typedef scoped_array<T> StorageType; |
| 141 | }; |
| 142 | |
| 143 | template <typename T> |
| 144 | struct CallbackParamTraits<scoped_ptr_malloc<T> > { |
| 145 | typedef scoped_ptr_malloc<T> ForwardType; |
| 146 | typedef scoped_ptr_malloc<T> StorageType; |
| 147 | }; |
| 148 | |
| 149 | // CallbackForward() is a very limited simulation of C++11's std::forward() |
| 150 | // used by the Callback/Bind system for a set of movable-but-not-copyable |
| 151 | // types. It is needed because forwarding a movable-but-not-copyable |
| 152 | // argument to another function requires us to invoke the proper move |
| 153 | // operator to create a rvalue version of the type. The supported types are |
| 154 | // whitelisted below as overloads of the CallbackForward() function. The |
| 155 | // default template compiles out to be a no-op. |
| 156 | // |
| 157 | // In C++11, std::forward would replace all uses of this function. However, it |
| 158 | // is impossible to implement a general std::forward with C++11 due to a lack |
| 159 | // of rvalue references. |
| 160 | template <typename T> |
| 161 | T& CallbackForward(T& t) { return t; } |
| 162 | |
| 163 | template <typename T> |
| 164 | scoped_ptr<T> CallbackForward(scoped_ptr<T>& p) { return p.Pass(); } |
| 165 | |
| 166 | template <typename T> |
| 167 | scoped_ptr<T> CallbackForward(scoped_array<T>& p) { return p.Pass(); } |
| 168 | |
| 169 | template <typename T> |
| 170 | scoped_ptr<T> CallbackForward(scoped_ptr_malloc<T>& p) { return p.Pass(); } |
| 171 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 172 | } // namespace internal |
| 173 | } // namespace base |
| 174 | |
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 175 | #endif // BASE_CALLBACK_INTERNAL_H_ |