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