Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: testing/gmock/include/gmock/internal/gmock-port.h

Issue 113807: Checkin a version of gmock, modified to use our boost_tuple in VS2005. (Closed)
Patch Set: Fix grammar issue. Created 11 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2008, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: vadimb@google.com (Vadim Berman)
31 //
32 // Low-level types and utilities for porting Google Mock to various
33 // platforms. They are subject to change without notice. DO NOT USE
34 // THEM IN USER CODE.
35
36 #ifndef GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_PORT_H_
37 #define GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_PORT_H_
38
39 #include <assert.h>
40 #include <stdlib.h>
41 #include <iostream>
42
43 // Most of the types needed for porting Google Mock are also required
44 // for Google Test and are defined in gtest-port.h.
45 #include <gtest/internal/gtest-linked_ptr.h>
46 #include <gtest/internal/gtest-port.h>
47
48 // To avoid conditional compilation everywhere, we make it
49 // gmock-port.h's responsibility to #include the header implementing
50 // tr1/tuple.
51 #if defined(__GNUC__) && GTEST_GCC_VER_ >= 40000
52 // GTEST_GCC_VER_ is defined in gtest-port.h and 40000 corresponds to
53 // version 4.0.0.
54 // GCC 4.0+ implements tr1/tuple in the <tr1/tuple> header. This does
55 // not conform to the TR1 spec, which requires the header to be <tuple>.
56 #include <tr1/tuple>
57 #elif defined(_MSC_VER) && _MSC_VER < 1500
58 // For Visual Studio older than 2008, we redirect directly to boost tuple
59 // searching from boost's root. This is to avoid extra dirtying of the
60 // compiler include paths.
61 #include "boost/tr1/tr1/tuple"
62 #else
63 // If the compiler is neither GCC 4.0+, nor Visual Studio 2008, we assume the
64 // user is using a spec-conforming TR1 implementation.
65 #include <tuple>
66 #endif // __GNUC__
67
68 #if GTEST_OS_LINUX
69
70 // On some platforms, <regex.h> needs someone to define size_t, and
71 // won't compile otherwise. We can #include it here as we already
72 // included <stdlib.h>, which is guaranteed to define size_t through
73 // <stddef.h>.
74 #include <regex.h> // NOLINT
75
76 // Defines this iff Google Mock uses the enhanced POSIX regular
77 // expression syntax. This is public as it affects how a user uses
78 // regular expression matchers.
79 #define GMOCK_USES_POSIX_RE 1
80
81 #endif // GTEST_OS_LINUX
82
83 #if defined(GMOCK_USES_PCRE) || defined(GMOCK_USES_POSIX_RE)
84 // Defines this iff regular expression matchers are supported. This
85 // is public as it tells a user whether he can use regular expression
86 // matchers.
87 #define GMOCK_HAS_REGEX 1
88 #endif // defined(GMOCK_USES_PCRE) || defined(GMOCK_USES_POSIX_RE)
89
90 namespace testing {
91 namespace internal {
92
93 // For Windows, check the compiler version. At least VS 2005 SP1 is
94 // required to compile Google Mock.
95 #if GTEST_OS_WINDOWS
96
97 #if _MSC_VER < 1400
98 #error "At least Visual Studio 2005 SP1 is required to compile Google Mock."
99 #elif _MSC_VER == 1400
100
101 // Unfortunately there is no unique _MSC_VER number for SP1. So for VS 2005
102 // we have to check if it has SP1 by checking whether a bug fixed in SP1
103 // is present. The bug in question is
104 // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?Feedback ID=101702
105 // where the compiler incorrectly reports sizeof(poiter to an array).
106
107 class TestForSP1 {
108 private: // GCC complains if x_ is used by sizeof before defining it.
109 static char x_[100];
110 // VS 2005 RTM incorrectly reports sizeof(&x) as 100, and that value
111 // is used to trigger 'invalid negative array size' error. If you
112 // see this error, upgrade to VS 2005 SP1 since Google Mock will not
113 // compile in VS 2005 RTM.
114 static char Google_Mock_requires_Visual_Studio_2005_SP1_or_later_to_compile_[
115 sizeof(&x_) != 100 ? 1 : -1];
116 };
117
118 #endif // _MSC_VER
119 #endif // GTEST_OS_WINDOWS
120
121 // Use implicit_cast as a safe version of static_cast or const_cast
122 // for upcasting in the type hierarchy (i.e. casting a pointer to Foo
123 // to a pointer to SuperclassOfFoo or casting a pointer to Foo to
124 // a const pointer to Foo).
125 // When you use implicit_cast, the compiler checks that the cast is safe.
126 // Such explicit implicit_casts are necessary in surprisingly many
127 // situations where C++ demands an exact type match instead of an
128 // argument type convertable to a target type.
129 //
130 // The From type can be inferred, so the preferred syntax for using
131 // implicit_cast is the same as for static_cast etc.:
132 //
133 // implicit_cast<ToType>(expr)
134 //
135 // implicit_cast would have been part of the C++ standard library,
136 // but the proposal was submitted too late. It will probably make
137 // its way into the language in the future.
138 template<typename To, typename From>
139 inline To implicit_cast(From const &f) {
140 return f;
141 }
142
143 // When you upcast (that is, cast a pointer from type Foo to type
144 // SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
145 // always succeed. When you downcast (that is, cast a pointer from
146 // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
147 // how do you know the pointer is really of type SubclassOfFoo? It
148 // could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
149 // when you downcast, you should use this macro. In debug mode, we
150 // use dynamic_cast<> to double-check the downcast is legal (we die
151 // if it's not). In normal mode, we do the efficient static_cast<>
152 // instead. Thus, it's important to test in debug mode to make sure
153 // the cast is legal!
154 // This is the only place in the code we should use dynamic_cast<>.
155 // In particular, you SHOULDN'T be using dynamic_cast<> in order to
156 // do RTTI (eg code like this:
157 // if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
158 // if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
159 // You should design the code some other way not to need this.
160 template<typename To, typename From> // use like this: down_cast<T*>(foo);
161 inline To down_cast(From* f) { // so we only accept pointers
162 // Ensures that To is a sub-type of From *. This test is here only
163 // for compile-time type checking, and has no overhead in an
164 // optimized build at run-time, as it will be optimized away
165 // completely.
166 if (false) {
167 implicit_cast<From*, To>(0);
168 }
169
170 #if GTEST_HAS_RTTI
171 assert(f == NULL || dynamic_cast<To>(f) != NULL); // RTTI: debug mode only!
172 #endif
173 return static_cast<To>(f);
174 }
175
176 // The GMOCK_COMPILE_ASSERT_ macro can be used to verify that a compile time
177 // expression is true. For example, you could use it to verify the
178 // size of a static array:
179 //
180 // GMOCK_COMPILE_ASSERT_(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
181 // content_type_names_incorrect_size);
182 //
183 // or to make sure a struct is smaller than a certain size:
184 //
185 // GMOCK_COMPILE_ASSERT_(sizeof(foo) < 128, foo_too_large);
186 //
187 // The second argument to the macro is the name of the variable. If
188 // the expression is false, most compilers will issue a warning/error
189 // containing the name of the variable.
190
191 template <bool>
192 struct CompileAssert {
193 };
194
195 #define GMOCK_COMPILE_ASSERT_(expr, msg) \
196 typedef ::testing::internal::CompileAssert<(bool(expr))> \
197 msg[bool(expr) ? 1 : -1]
198
199 // Implementation details of GMOCK_COMPILE_ASSERT_:
200 //
201 // - GMOCK_COMPILE_ASSERT_ works by defining an array type that has -1
202 // elements (and thus is invalid) when the expression is false.
203 //
204 // - The simpler definition
205 //
206 // #define GMOCK_COMPILE_ASSERT_(expr, msg) typedef char msg[(expr) ? 1 : -1]
207 //
208 // does not work, as gcc supports variable-length arrays whose sizes
209 // are determined at run-time (this is gcc's extension and not part
210 // of the C++ standard). As a result, gcc fails to reject the
211 // following code with the simple definition:
212 //
213 // int foo;
214 // GMOCK_COMPILE_ASSERT_(foo, msg); // not supposed to compile as foo is
215 // // not a compile-time constant.
216 //
217 // - By using the type CompileAssert<(bool(expr))>, we ensures that
218 // expr is a compile-time constant. (Template arguments must be
219 // determined at compile-time.)
220 //
221 // - The outter parentheses in CompileAssert<(bool(expr))> are necessary
222 // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written
223 //
224 // CompileAssert<bool(expr)>
225 //
226 // instead, these compilers will refuse to compile
227 //
228 // GMOCK_COMPILE_ASSERT_(5 > 0, some_message);
229 //
230 // (They seem to think the ">" in "5 > 0" marks the end of the
231 // template argument list.)
232 //
233 // - The array size is (bool(expr) ? 1 : -1), instead of simply
234 //
235 // ((expr) ? 1 : -1).
236 //
237 // This is to avoid running into a bug in MS VC 7.1, which
238 // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
239
240 #if GTEST_HAS_GLOBAL_STRING
241 typedef ::string string;
242 #elif GTEST_HAS_STD_STRING
243 typedef ::std::string string;
244 #else
245 #error "Google Mock requires ::std::string to compile."
246 #endif // GTEST_HAS_GLOBAL_STRING
247
248 #if GTEST_HAS_GLOBAL_WSTRING
249 typedef ::wstring wstring;
250 #elif GTEST_HAS_STD_WSTRING
251 typedef ::std::wstring wstring;
252 #endif // GTEST_HAS_GLOBAL_WSTRING
253
254 // Prints the file location in the format native to the compiler.
255 inline void FormatFileLocation(const char* file, int line, ::std::ostream* os) {
256 if (file == NULL)
257 file = "unknown file";
258 if (line < 0) {
259 *os << file << ":";
260 } else {
261 #if _MSC_VER
262 *os << file << "(" << line << "):";
263 #else
264 *os << file << ":" << line << ":";
265 #endif
266 }
267 }
268
269 // INTERNAL IMPLEMENTATION - DO NOT USE.
270 //
271 // GMOCK_CHECK_ is an all mode assert. It aborts the program if the condition
272 // is not satisfied.
273 // Synopsys:
274 // GMOCK_CHECK_(boolean_condition);
275 // or
276 // GMOCK_CHECK_(boolean_condition) << "Additional message";
277 //
278 // This checks the condition and if the condition is not satisfied
279 // it prints message about the condition violation, including the
280 // condition itself, plus additional message streamed into it, if any,
281 // and then it aborts the program. It aborts the program irrespective of
282 // whether it is built in the debug mode or not.
283
284 class GMockCheckProvider {
285 public:
286 GMockCheckProvider(const char* condition, const char* file, int line) {
287 FormatFileLocation(file, line, &::std::cerr);
288 ::std::cerr << " ERROR: Condition " << condition << " failed. ";
289 }
290 ~GMockCheckProvider() {
291 ::std::cerr << ::std::endl;
292 abort();
293 }
294 ::std::ostream& GetStream() { return ::std::cerr; }
295 };
296 #define GMOCK_CHECK_(condition) \
297 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
298 if (condition) \
299 ; \
300 else \
301 ::testing::internal::GMockCheckProvider(\
302 #condition, __FILE__, __LINE__).GetStream()
303
304 } // namespace internal
305 } // namespace testing
306
307 // Macro for referencing flags. This is public as we want the user to
308 // use this syntax to reference Google Mock flags.
309 #define GMOCK_FLAG(name) FLAGS_gmock_##name
310
311 // Macros for declaring flags.
312 #define GMOCK_DECLARE_bool_(name) extern bool GMOCK_FLAG(name)
313 #define GMOCK_DECLARE_int32_(name) \
314 extern ::testing::internal::Int32 GMOCK_FLAG(name)
315 #define GMOCK_DECLARE_string_(name) \
316 extern ::testing::internal::String GMOCK_FLAG(name)
317
318 // Macros for defining flags.
319 #define GMOCK_DEFINE_bool_(name, default_val, doc) \
320 bool GMOCK_FLAG(name) = (default_val)
321 #define GMOCK_DEFINE_int32_(name, default_val, doc) \
322 ::testing::internal::Int32 GMOCK_FLAG(name) = (default_val)
323 #define GMOCK_DEFINE_string_(name, default_val, doc) \
324 ::testing::internal::String GMOCK_FLAG(name) = (default_val)
325
326 #endif // GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_PORT_H_
OLDNEW
« no previous file with comments | « testing/gmock/include/gmock/internal/gmock-internal-utils.h ('k') | testing/gmock/include/gmock/internal/gmock-port.h.orig » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698