blob: 21599bf8ed92194240e976269bfde719a9060e2f [file] [log] [blame]
[email protected]b38d3572011-02-15 01:27:381// 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]59eff912011-02-18 23:29:318#ifndef BASE_CALLBACK_INTERNAL_H_
9#define BASE_CALLBACK_INTERNAL_H_
[email protected]b38d3572011-02-15 01:27:3810#pragma once
11
[email protected]2edc2862011-04-04 18:04:3712#include <stddef.h>
13
[email protected]0bea7252011-08-05 15:34:0014#include "base/base_export.h"
[email protected]3b63f8f42011-03-28 01:54:1515#include "base/memory/ref_counted.h"
[email protected]206a2ae82011-12-22 21:12:5816#include "base/memory/scoped_ptr.h"
[email protected]b38d3572011-02-15 01:27:3817
18namespace base {
19namespace internal {
20
[email protected]7296f2762011-11-21 19:23:4421// BindStateBase is used to provide an opaque handle that the Callback
[email protected]b38d3572011-02-15 01:27:3822// 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]7296f2762011-11-21 19:23:4427class BindStateBase : public RefCountedThreadSafe<BindStateBase> {
[email protected]b38d3572011-02-15 01:27:3828 protected:
[email protected]7296f2762011-11-21 19:23:4429 friend class RefCountedThreadSafe<BindStateBase>;
30 virtual ~BindStateBase() {}
[email protected]b38d3572011-02-15 01:27:3831};
32
[email protected]59eff912011-02-18 23:29:3133// Holds the Callback methods that don't require specialization to reduce
34// template bloat.
[email protected]0bea7252011-08-05 15:34:0035class BASE_EXPORT CallbackBase {
[email protected]59eff912011-02-18 23:29:3136 public:
37 // Returns true if Callback is null (doesn't refer to anything).
38 bool is_null() const;
39
[email protected]1c35a482011-10-25 23:19:5140 // Returns the Callback into an uninitialized state.
[email protected]59eff912011-02-18 23:29:3141 void Reset();
42
[email protected]481915a772011-09-10 03:14:3543 protected:
[email protected]59eff912011-02-18 23:29:3144 // 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]1c35a482011-10-25 23:19:5150 // Returns true if this callback equals |other|. |other| may be null.
51 bool Equals(const CallbackBase& other) const;
52
[email protected]e24f8762011-12-20 00:10:0453 // 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]59eff912011-02-18 23:29:3158
[email protected]1c35a482011-10-25 23:19:5159 // Force the destructor to be instantiated inside this translation unit so
[email protected]59eff912011-02-18 23:29:3160 // that our subclasses will not get inlined versions. Avoids more template
61 // bloat.
62 ~CallbackBase();
63
[email protected]7296f2762011-11-21 19:23:4464 scoped_refptr<BindStateBase> bind_state_;
[email protected]59eff912011-02-18 23:29:3165 InvokeFuncStorage polymorphic_invoke_;
66};
67
[email protected]c18b1052011-03-24 02:02:1768// 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.
79template <typename T>
[email protected]7296f2762011-11-21 19:23:4480struct CallbackParamTraits {
[email protected]c18b1052011-03-24 02:02:1781 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.
90template <typename T>
[email protected]7296f2762011-11-21 19:23:4491struct CallbackParamTraits<T&> {
[email protected]c18b1052011-03-24 02:02:1792 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.
101template <typename T, size_t n>
[email protected]7296f2762011-11-21 19:23:44102struct CallbackParamTraits<T[n]> {
[email protected]c18b1052011-03-24 02:02:17103 typedef const T* ForwardType;
104 typedef const T* StorageType;
105};
106
[email protected]7296f2762011-11-21 19:23:44107// See comment for CallbackParamTraits<T[n]>.
[email protected]c18b1052011-03-24 02:02:17108template <typename T>
[email protected]7296f2762011-11-21 19:23:44109struct CallbackParamTraits<T[]> {
[email protected]c18b1052011-03-24 02:02:17110 typedef const T* ForwardType;
111 typedef const T* StorageType;
112};
113
[email protected]206a2ae82011-12-22 21:12:58114// 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.
131template <typename T>
132struct CallbackParamTraits<scoped_ptr<T> > {
133 typedef scoped_ptr<T> ForwardType;
134 typedef scoped_ptr<T> StorageType;
135};
136
137template <typename T>
138struct CallbackParamTraits<scoped_array<T> > {
139 typedef scoped_array<T> ForwardType;
140 typedef scoped_array<T> StorageType;
141};
142
143template <typename T>
144struct 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.
160template <typename T>
161T& CallbackForward(T& t) { return t; }
162
163template <typename T>
164scoped_ptr<T> CallbackForward(scoped_ptr<T>& p) { return p.Pass(); }
165
166template <typename T>
167scoped_ptr<T> CallbackForward(scoped_array<T>& p) { return p.Pass(); }
168
169template <typename T>
170scoped_ptr<T> CallbackForward(scoped_ptr_malloc<T>& p) { return p.Pass(); }
171
[email protected]b38d3572011-02-15 01:27:38172} // namespace internal
173} // namespace base
174
[email protected]59eff912011-02-18 23:29:31175#endif // BASE_CALLBACK_INTERNAL_H_