Added
Link Here
|
1 |
--- 3rdparty/gmock/include/gmock/gmock-spec-builders.h.orig 2014-10-22 20:07:20 UTC |
2 |
+++ 3rdparty/gmock/include/gmock/gmock-spec-builders.h |
3 |
@@ -211,7 +211,7 @@ class GTEST_API_ UntypedFunctionMockerBase { |
4 |
// arguments. This function can be safely called from multiple |
5 |
// threads concurrently. The caller is responsible for deleting the |
6 |
// result. |
7 |
- const UntypedActionResultHolderBase* UntypedInvokeWith( |
8 |
+ UntypedActionResultHolderBase* UntypedInvokeWith( |
9 |
const void* untyped_args) |
10 |
GTEST_LOCK_EXCLUDED_(g_gmock_mutex); |
11 |
|
12 |
@@ -1289,6 +1289,58 @@ class MockSpec { |
13 |
GTEST_DISALLOW_ASSIGN_(MockSpec); |
14 |
}; // class MockSpec |
15 |
|
16 |
+// Wrapper type for generically holding an ordinary value or lvalue reference. |
17 |
+// If T is not a reference type, it must be copyable or movable. |
18 |
+// ReferenceOrValueWrapper<T> is movable, and will also be copyable unless |
19 |
+// T is a move-only value type (which means that it will always be copyable |
20 |
+// if the current platform does not support move semantics). |
21 |
+// |
22 |
+// The primary template defines handling for values, but function header |
23 |
+// comments describe the contract for the whole template (including |
24 |
+// specializations). |
25 |
+template <typename T> |
26 |
+class ReferenceOrValueWrapper { |
27 |
+ public: |
28 |
+ // Constructs a wrapper from the given value/reference. |
29 |
+ explicit ReferenceOrValueWrapper(T value) |
30 |
+ : value_(GTEST_MOVE_(value)) {} |
31 |
+ |
32 |
+ // Unwraps and returns the underlying value/reference, exactly as |
33 |
+ // originally passed. The behavior of calling this more than once on |
34 |
+ // the same object is unspecified. |
35 |
+ T Unwrap() { |
36 |
+ return GTEST_MOVE_(value_); |
37 |
+ } |
38 |
+ |
39 |
+ // Provides nondestructive access to the underlying value/reference. |
40 |
+ // Always returns a const reference (more precisely, |
41 |
+ // const RemoveReference<T>&). The behavior of calling this after |
42 |
+ // calling Unwrap on the same object is unspecified. |
43 |
+ const T& Peek() const { |
44 |
+ return value_; |
45 |
+ } |
46 |
+ |
47 |
+ private: |
48 |
+ T value_; |
49 |
+}; |
50 |
+ |
51 |
+// Specialization for lvalue reference types. See primary template |
52 |
+// for documentation. |
53 |
+template <typename T> |
54 |
+class ReferenceOrValueWrapper<T&> { |
55 |
+ public: |
56 |
+ // Workaround for debatable pass-by-reference lint warning (c-library-team |
57 |
+ // policy precludes NOLINT in this context) |
58 |
+ typedef T& reference; |
59 |
+ explicit ReferenceOrValueWrapper(reference ref) |
60 |
+ : value_ptr_(&ref) {} |
61 |
+ T& Unwrap() { return *value_ptr_; } |
62 |
+ const T& Peek() const { return *value_ptr_; } |
63 |
+ |
64 |
+ private: |
65 |
+ T* value_ptr_; |
66 |
+}; |
67 |
+ |
68 |
// MSVC warns about using 'this' in base member initializer list, so |
69 |
// we need to temporarily disable the warning. We have to do it for |
70 |
// the entire class to suppress the warning, even though it's about |
71 |
@@ -1320,23 +1372,16 @@ class UntypedActionResultHolderBase { |
72 |
template <typename T> |
73 |
class ActionResultHolder : public UntypedActionResultHolderBase { |
74 |
public: |
75 |
- explicit ActionResultHolder(T a_value) : value_(a_value) {} |
76 |
- |
77 |
- // The compiler-generated copy constructor and assignment operator |
78 |
- // are exactly what we need, so we don't need to define them. |
79 |
- |
80 |
- // Returns the held value and deletes this object. |
81 |
- T GetValueAndDelete() const { |
82 |
- T retval(value_); |
83 |
- delete this; |
84 |
- return retval; |
85 |
+ // Returns the held value. Must not be called more than once. |
86 |
+ T Unwrap() { |
87 |
+ return result_.Unwrap(); |
88 |
} |
89 |
|
90 |
// Prints the held value as an action's result to os. |
91 |
virtual void PrintAsActionResult(::std::ostream* os) const { |
92 |
*os << "\n Returns: "; |
93 |
// T may be a reference type, so we don't use UniversalPrint(). |
94 |
- UniversalPrinter<T>::Print(value_, os); |
95 |
+ UniversalPrinter<T>::Print(result_.Peek(), os); |
96 |
} |
97 |
|
98 |
// Performs the given mock function's default action and returns the |
99 |
@@ -1346,8 +1391,8 @@ class ActionResultHolder : public UntypedActionResultH |
100 |
const FunctionMockerBase<F>* func_mocker, |
101 |
const typename Function<F>::ArgumentTuple& args, |
102 |
const string& call_description) { |
103 |
- return new ActionResultHolder( |
104 |
- func_mocker->PerformDefaultAction(args, call_description)); |
105 |
+ return new ActionResultHolder(Wrapper( |
106 |
+ func_mocker->PerformDefaultAction(args, call_description))); |
107 |
} |
108 |
|
109 |
// Performs the given action and returns the result in a new-ed |
110 |
@@ -1356,42 +1401,52 @@ class ActionResultHolder : public UntypedActionResultH |
111 |
static ActionResultHolder* |
112 |
PerformAction(const Action<F>& action, |
113 |
const typename Function<F>::ArgumentTuple& args) { |
114 |
- return new ActionResultHolder(action.Perform(args)); |
115 |
+ return new ActionResultHolder(Wrapper(action.Perform(args))); |
116 |
} |
117 |
|
118 |
private: |
119 |
- T value_; |
120 |
+ typedef ReferenceOrValueWrapper<T> Wrapper; |
121 |
|
122 |
- // T could be a reference type, so = isn't supported. |
123 |
- GTEST_DISALLOW_ASSIGN_(ActionResultHolder); |
124 |
+ explicit ActionResultHolder(Wrapper result) |
125 |
+ : result_(GTEST_MOVE_(result)) {} |
126 |
+ |
127 |
+ Wrapper result_; |
128 |
+ |
129 |
+ GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionResultHolder); |
130 |
}; |
131 |
|
132 |
// Specialization for T = void. |
133 |
template <> |
134 |
class ActionResultHolder<void> : public UntypedActionResultHolderBase { |
135 |
public: |
136 |
- void GetValueAndDelete() const { delete this; } |
137 |
+ void Unwrap() { } |
138 |
|
139 |
virtual void PrintAsActionResult(::std::ostream* /* os */) const {} |
140 |
|
141 |
- // Performs the given mock function's default action and returns NULL; |
142 |
+ // Performs the given mock function's default action and returns ownership |
143 |
+ // of an empty ActionResultHolder*. |
144 |
template <typename F> |
145 |
static ActionResultHolder* PerformDefaultAction( |
146 |
const FunctionMockerBase<F>* func_mocker, |
147 |
const typename Function<F>::ArgumentTuple& args, |
148 |
const string& call_description) { |
149 |
func_mocker->PerformDefaultAction(args, call_description); |
150 |
- return NULL; |
151 |
+ return new ActionResultHolder; |
152 |
} |
153 |
|
154 |
- // Performs the given action and returns NULL. |
155 |
+ // Performs the given action and returns ownership of an empty |
156 |
+ // ActionResultHolder*. |
157 |
template <typename F> |
158 |
static ActionResultHolder* PerformAction( |
159 |
const Action<F>& action, |
160 |
const typename Function<F>::ArgumentTuple& args) { |
161 |
action.Perform(args); |
162 |
- return NULL; |
163 |
+ return new ActionResultHolder; |
164 |
} |
165 |
+ |
166 |
+ private: |
167 |
+ ActionResultHolder() {} |
168 |
+ GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionResultHolder); |
169 |
}; |
170 |
|
171 |
// The base of the function mocker class for the given function type. |
172 |
@@ -1526,8 +1581,9 @@ class FunctionMockerBase : public UntypedFunctionMocke |
173 |
// threads concurrently. |
174 |
Result InvokeWith(const ArgumentTuple& args) |
175 |
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { |
176 |
- return static_cast<const ResultHolder*>( |
177 |
- this->UntypedInvokeWith(&args))->GetValueAndDelete(); |
178 |
+ scoped_ptr<ResultHolder> holder( |
179 |
+ DownCast_<ResultHolder*>(this->UntypedInvokeWith(&args))); |
180 |
+ return holder->Unwrap(); |
181 |
} |
182 |
|
183 |
// Adds and returns a default action spec for this mock function. |