blob: 9ee8f728e3e85ba80c1c93f2c0fde1d6121629cb [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// 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
zhanyong.wanbf550852009-06-09 06:09:53 +000050// tr1/tuple. gmock-port.h does this via gtest-port.h, which is
51// guaranteed to pull in the tuple header.
shiqiane35fdd92008-12-10 05:08:54 +000052
zhanyong.wan652540a2009-02-23 23:37:29 +000053#if GTEST_OS_LINUX
shiqiane35fdd92008-12-10 05:08:54 +000054
55// On some platforms, <regex.h> needs someone to define size_t, and
56// won't compile otherwise. We can #include it here as we already
57// included <stdlib.h>, which is guaranteed to define size_t through
58// <stddef.h>.
59#include <regex.h> // NOLINT
60
61// Defines this iff Google Mock uses the enhanced POSIX regular
62// expression syntax. This is public as it affects how a user uses
63// regular expression matchers.
64#define GMOCK_USES_POSIX_RE 1
65
66#endif // GTEST_OS_LINUX
67
68#if defined(GMOCK_USES_PCRE) || defined(GMOCK_USES_POSIX_RE)
69// Defines this iff regular expression matchers are supported. This
70// is public as it tells a user whether he can use regular expression
71// matchers.
72#define GMOCK_HAS_REGEX 1
73#endif // defined(GMOCK_USES_PCRE) || defined(GMOCK_USES_POSIX_RE)
74
75namespace testing {
76namespace internal {
77
78// For Windows, check the compiler version. At least VS 2005 SP1 is
79// required to compile Google Mock.
zhanyong.wan652540a2009-02-23 23:37:29 +000080#if GTEST_OS_WINDOWS
shiqiane35fdd92008-12-10 05:08:54 +000081
82#if _MSC_VER < 1400
83#error "At least Visual Studio 2005 SP1 is required to compile Google Mock."
84#elif _MSC_VER == 1400
85
86// Unfortunately there is no unique _MSC_VER number for SP1. So for VS 2005
87// we have to check if it has SP1 by checking whether a bug fixed in SP1
88// is present. The bug in question is
89// http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=101702
90// where the compiler incorrectly reports sizeof(poiter to an array).
91
92class TestForSP1 {
93 private: // GCC complains if x_ is used by sizeof before defining it.
94 static char x_[100];
95 // VS 2005 RTM incorrectly reports sizeof(&x) as 100, and that value
96 // is used to trigger 'invalid negative array size' error. If you
97 // see this error, upgrade to VS 2005 SP1 since Google Mock will not
98 // compile in VS 2005 RTM.
99 static char Google_Mock_requires_Visual_Studio_2005_SP1_or_later_to_compile_[
100 sizeof(&x_) != 100 ? 1 : -1];
101};
102
103#endif // _MSC_VER
104#endif // GTEST_OS_WINDOWS
105
106// Use implicit_cast as a safe version of static_cast or const_cast
107// for upcasting in the type hierarchy (i.e. casting a pointer to Foo
108// to a pointer to SuperclassOfFoo or casting a pointer to Foo to
109// a const pointer to Foo).
110// When you use implicit_cast, the compiler checks that the cast is safe.
111// Such explicit implicit_casts are necessary in surprisingly many
112// situations where C++ demands an exact type match instead of an
113// argument type convertable to a target type.
114//
115// The From type can be inferred, so the preferred syntax for using
116// implicit_cast is the same as for static_cast etc.:
117//
118// implicit_cast<ToType>(expr)
119//
120// implicit_cast would have been part of the C++ standard library,
121// but the proposal was submitted too late. It will probably make
122// its way into the language in the future.
123template<typename To, typename From>
124inline To implicit_cast(From const &f) {
125 return f;
126}
127
128// When you upcast (that is, cast a pointer from type Foo to type
129// SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
130// always succeed. When you downcast (that is, cast a pointer from
131// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
132// how do you know the pointer is really of type SubclassOfFoo? It
133// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
134// when you downcast, you should use this macro. In debug mode, we
135// use dynamic_cast<> to double-check the downcast is legal (we die
136// if it's not). In normal mode, we do the efficient static_cast<>
137// instead. Thus, it's important to test in debug mode to make sure
138// the cast is legal!
139// This is the only place in the code we should use dynamic_cast<>.
140// In particular, you SHOULDN'T be using dynamic_cast<> in order to
141// do RTTI (eg code like this:
142// if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
143// if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
144// You should design the code some other way not to need this.
145template<typename To, typename From> // use like this: down_cast<T*>(foo);
146inline To down_cast(From* f) { // so we only accept pointers
147 // Ensures that To is a sub-type of From *. This test is here only
148 // for compile-time type checking, and has no overhead in an
149 // optimized build at run-time, as it will be optimized away
150 // completely.
151 if (false) {
152 implicit_cast<From*, To>(0);
153 }
154
zhanyong.wanc6a41232009-05-13 23:38:40 +0000155#if GTEST_HAS_RTTI
shiqiane35fdd92008-12-10 05:08:54 +0000156 assert(f == NULL || dynamic_cast<To>(f) != NULL); // RTTI: debug mode only!
zhanyong.wanc6a41232009-05-13 23:38:40 +0000157#endif
shiqiane35fdd92008-12-10 05:08:54 +0000158 return static_cast<To>(f);
159}
160
zhanyong.wane7bb5ed2009-05-05 23:14:47 +0000161// The GMOCK_COMPILE_ASSERT_ macro can be used to verify that a compile time
shiqiane35fdd92008-12-10 05:08:54 +0000162// expression is true. For example, you could use it to verify the
163// size of a static array:
164//
zhanyong.wane7bb5ed2009-05-05 23:14:47 +0000165// GMOCK_COMPILE_ASSERT_(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
166// content_type_names_incorrect_size);
shiqiane35fdd92008-12-10 05:08:54 +0000167//
168// or to make sure a struct is smaller than a certain size:
169//
zhanyong.wane7bb5ed2009-05-05 23:14:47 +0000170// GMOCK_COMPILE_ASSERT_(sizeof(foo) < 128, foo_too_large);
shiqiane35fdd92008-12-10 05:08:54 +0000171//
172// The second argument to the macro is the name of the variable. If
173// the expression is false, most compilers will issue a warning/error
174// containing the name of the variable.
175
176template <bool>
177struct CompileAssert {
178};
179
zhanyong.wane0d051e2009-02-19 00:33:37 +0000180#define GMOCK_COMPILE_ASSERT_(expr, msg) \
shiqiane35fdd92008-12-10 05:08:54 +0000181 typedef ::testing::internal::CompileAssert<(bool(expr))> \
182 msg[bool(expr) ? 1 : -1]
183
zhanyong.wane0d051e2009-02-19 00:33:37 +0000184// Implementation details of GMOCK_COMPILE_ASSERT_:
shiqiane35fdd92008-12-10 05:08:54 +0000185//
zhanyong.wane0d051e2009-02-19 00:33:37 +0000186// - GMOCK_COMPILE_ASSERT_ works by defining an array type that has -1
shiqiane35fdd92008-12-10 05:08:54 +0000187// elements (and thus is invalid) when the expression is false.
188//
189// - The simpler definition
190//
zhanyong.wane0d051e2009-02-19 00:33:37 +0000191// #define GMOCK_COMPILE_ASSERT_(expr, msg) typedef char msg[(expr) ? 1 : -1]
shiqiane35fdd92008-12-10 05:08:54 +0000192//
193// does not work, as gcc supports variable-length arrays whose sizes
194// are determined at run-time (this is gcc's extension and not part
195// of the C++ standard). As a result, gcc fails to reject the
196// following code with the simple definition:
197//
198// int foo;
zhanyong.wane0d051e2009-02-19 00:33:37 +0000199// GMOCK_COMPILE_ASSERT_(foo, msg); // not supposed to compile as foo is
200// // not a compile-time constant.
shiqiane35fdd92008-12-10 05:08:54 +0000201//
202// - By using the type CompileAssert<(bool(expr))>, we ensures that
203// expr is a compile-time constant. (Template arguments must be
204// determined at compile-time.)
205//
206// - The outter parentheses in CompileAssert<(bool(expr))> are necessary
207// to work around a bug in gcc 3.4.4 and 4.0.1. If we had written
208//
209// CompileAssert<bool(expr)>
210//
211// instead, these compilers will refuse to compile
212//
zhanyong.wane0d051e2009-02-19 00:33:37 +0000213// GMOCK_COMPILE_ASSERT_(5 > 0, some_message);
shiqiane35fdd92008-12-10 05:08:54 +0000214//
215// (They seem to think the ">" in "5 > 0" marks the end of the
216// template argument list.)
217//
218// - The array size is (bool(expr) ? 1 : -1), instead of simply
219//
220// ((expr) ? 1 : -1).
221//
222// This is to avoid running into a bug in MS VC 7.1, which
223// causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
224
225#if GTEST_HAS_GLOBAL_STRING
226typedef ::string string;
227#elif GTEST_HAS_STD_STRING
228typedef ::std::string string;
229#else
230#error "Google Mock requires ::std::string to compile."
231#endif // GTEST_HAS_GLOBAL_STRING
232
233#if GTEST_HAS_GLOBAL_WSTRING
234typedef ::wstring wstring;
235#elif GTEST_HAS_STD_WSTRING
236typedef ::std::wstring wstring;
237#endif // GTEST_HAS_GLOBAL_WSTRING
238
zhanyong.wandf35a762009-04-22 22:25:31 +0000239// Prints the file location in the format native to the compiler.
240inline void FormatFileLocation(const char* file, int line, ::std::ostream* os) {
241 if (file == NULL)
242 file = "unknown file";
243 if (line < 0) {
244 *os << file << ":";
245 } else {
246#if _MSC_VER
247 *os << file << "(" << line << "):";
248#else
249 *os << file << ":" << line << ":";
250#endif
251 }
252}
253
shiqiane35fdd92008-12-10 05:08:54 +0000254// INTERNAL IMPLEMENTATION - DO NOT USE.
255//
256// GMOCK_CHECK_ is an all mode assert. It aborts the program if the condition
257// is not satisfied.
258// Synopsys:
259// GMOCK_CHECK_(boolean_condition);
260// or
261// GMOCK_CHECK_(boolean_condition) << "Additional message";
262//
263// This checks the condition and if the condition is not satisfied
264// it prints message about the condition violation, including the
265// condition itself, plus additional message streamed into it, if any,
266// and then it aborts the program. It aborts the program irrespective of
267// whether it is built in the debug mode or not.
268
269class GMockCheckProvider {
270 public:
271 GMockCheckProvider(const char* condition, const char* file, int line) {
zhanyong.wandf35a762009-04-22 22:25:31 +0000272 FormatFileLocation(file, line, &::std::cerr);
shiqiane35fdd92008-12-10 05:08:54 +0000273 ::std::cerr << " ERROR: Condition " << condition << " failed. ";
274 }
275 ~GMockCheckProvider() {
276 ::std::cerr << ::std::endl;
277 abort();
278 }
shiqiane35fdd92008-12-10 05:08:54 +0000279 ::std::ostream& GetStream() { return ::std::cerr; }
280};
281#define GMOCK_CHECK_(condition) \
282 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
283 if (condition) \
284 ; \
285 else \
286 ::testing::internal::GMockCheckProvider(\
287 #condition, __FILE__, __LINE__).GetStream()
288
289} // namespace internal
290} // namespace testing
291
zhanyong.wane0d051e2009-02-19 00:33:37 +0000292// Macro for referencing flags. This is public as we want the user to
293// use this syntax to reference Google Mock flags.
shiqiane35fdd92008-12-10 05:08:54 +0000294#define GMOCK_FLAG(name) FLAGS_gmock_##name
295
296// Macros for declaring flags.
zhanyong.wane0d051e2009-02-19 00:33:37 +0000297#define GMOCK_DECLARE_bool_(name) extern bool GMOCK_FLAG(name)
298#define GMOCK_DECLARE_int32_(name) \
shiqiane35fdd92008-12-10 05:08:54 +0000299 extern ::testing::internal::Int32 GMOCK_FLAG(name)
zhanyong.wane0d051e2009-02-19 00:33:37 +0000300#define GMOCK_DECLARE_string_(name) \
shiqiane35fdd92008-12-10 05:08:54 +0000301 extern ::testing::internal::String GMOCK_FLAG(name)
302
303// Macros for defining flags.
zhanyong.wane0d051e2009-02-19 00:33:37 +0000304#define GMOCK_DEFINE_bool_(name, default_val, doc) \
shiqiane35fdd92008-12-10 05:08:54 +0000305 bool GMOCK_FLAG(name) = (default_val)
zhanyong.wane0d051e2009-02-19 00:33:37 +0000306#define GMOCK_DEFINE_int32_(name, default_val, doc) \
shiqiane35fdd92008-12-10 05:08:54 +0000307 ::testing::internal::Int32 GMOCK_FLAG(name) = (default_val)
zhanyong.wane0d051e2009-02-19 00:33:37 +0000308#define GMOCK_DEFINE_string_(name, default_val, doc) \
shiqiane35fdd92008-12-10 05:08:54 +0000309 ::testing::internal::String GMOCK_FLAG(name) = (default_val)
310
311#endif // GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_PORT_H_