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 |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 50 | // tr1/tuple. gmock-port.h does this via gtest-port.h, which is |
| 51 | // guaranteed to pull in the tuple header. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 52 | |
zhanyong.wan | 652540a | 2009-02-23 23:37:29 +0000 | [diff] [blame] | 53 | #if GTEST_OS_LINUX |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 54 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 55 | #endif // GTEST_OS_LINUX |
| 56 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 57 | namespace testing { |
| 58 | namespace internal { |
| 59 | |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 60 | // For MS Visual C++, check the compiler version. At least VS 2003 is |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 61 | // required to compile Google Mock. |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 62 | #if defined(_MSC_VER) && _MSC_VER < 1310 |
| 63 | #error "At least Visual C++ 2003 (7.1) is required to compile Google Mock." |
| 64 | #endif |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 65 | |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 66 | // Use implicit_cast as a safe version of static_cast for upcasting in |
| 67 | // the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a |
| 68 | // const Foo*). When you use implicit_cast, the compiler checks that |
| 69 | // the cast is safe. Such explicit implicit_casts are necessary in |
| 70 | // surprisingly many situations where C++ demands an exact type match |
| 71 | // instead of an argument type convertable to a target type. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 72 | // |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 73 | // The syntax for using implicit_cast is the same as for static_cast: |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 74 | // |
| 75 | // implicit_cast<ToType>(expr) |
| 76 | // |
| 77 | // implicit_cast would have been part of the C++ standard library, |
| 78 | // but the proposal was submitted too late. It will probably make |
| 79 | // its way into the language in the future. |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 80 | template<typename To> |
| 81 | inline To implicit_cast(To x) { return x; } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 82 | |
| 83 | // When you upcast (that is, cast a pointer from type Foo to type |
| 84 | // SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts |
| 85 | // always succeed. When you downcast (that is, cast a pointer from |
| 86 | // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because |
| 87 | // how do you know the pointer is really of type SubclassOfFoo? It |
| 88 | // could be a bare Foo, or of type DifferentSubclassOfFoo. Thus, |
| 89 | // when you downcast, you should use this macro. In debug mode, we |
| 90 | // use dynamic_cast<> to double-check the downcast is legal (we die |
| 91 | // if it's not). In normal mode, we do the efficient static_cast<> |
| 92 | // instead. Thus, it's important to test in debug mode to make sure |
| 93 | // the cast is legal! |
| 94 | // This is the only place in the code we should use dynamic_cast<>. |
| 95 | // In particular, you SHOULDN'T be using dynamic_cast<> in order to |
| 96 | // do RTTI (eg code like this: |
| 97 | // if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo); |
| 98 | // if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo); |
| 99 | // You should design the code some other way not to need this. |
| 100 | template<typename To, typename From> // use like this: down_cast<T*>(foo); |
| 101 | inline To down_cast(From* f) { // so we only accept pointers |
| 102 | // Ensures that To is a sub-type of From *. This test is here only |
| 103 | // for compile-time type checking, and has no overhead in an |
| 104 | // optimized build at run-time, as it will be optimized away |
| 105 | // completely. |
| 106 | if (false) { |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 107 | const To to = NULL; |
zhanyong.wan | f6d6a22 | 2009-12-01 19:42:25 +0000 | [diff] [blame] | 108 | ::testing::internal::implicit_cast<From*>(to); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 109 | } |
| 110 | |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 111 | #if GTEST_HAS_RTTI |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 112 | assert(f == NULL || dynamic_cast<To>(f) != NULL); // RTTI: debug mode only! |
zhanyong.wan | c6a4123 | 2009-05-13 23:38:40 +0000 | [diff] [blame] | 113 | #endif |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 114 | return static_cast<To>(f); |
| 115 | } |
| 116 | |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 117 | // The GMOCK_COMPILE_ASSERT_ macro can be used to verify that a compile time |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 118 | // expression is true. For example, you could use it to verify the |
| 119 | // size of a static array: |
| 120 | // |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 121 | // GMOCK_COMPILE_ASSERT_(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES, |
| 122 | // content_type_names_incorrect_size); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 123 | // |
| 124 | // or to make sure a struct is smaller than a certain size: |
| 125 | // |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 126 | // GMOCK_COMPILE_ASSERT_(sizeof(foo) < 128, foo_too_large); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 127 | // |
| 128 | // The second argument to the macro is the name of the variable. If |
| 129 | // the expression is false, most compilers will issue a warning/error |
| 130 | // containing the name of the variable. |
| 131 | |
| 132 | template <bool> |
| 133 | struct CompileAssert { |
| 134 | }; |
| 135 | |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 136 | #define GMOCK_COMPILE_ASSERT_(expr, msg) \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 137 | typedef ::testing::internal::CompileAssert<(bool(expr))> \ |
| 138 | msg[bool(expr) ? 1 : -1] |
| 139 | |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 140 | // Implementation details of GMOCK_COMPILE_ASSERT_: |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 141 | // |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 142 | // - GMOCK_COMPILE_ASSERT_ works by defining an array type that has -1 |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 143 | // elements (and thus is invalid) when the expression is false. |
| 144 | // |
| 145 | // - The simpler definition |
| 146 | // |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 147 | // #define GMOCK_COMPILE_ASSERT_(expr, msg) typedef char msg[(expr) ? 1 : -1] |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 148 | // |
| 149 | // does not work, as gcc supports variable-length arrays whose sizes |
| 150 | // are determined at run-time (this is gcc's extension and not part |
| 151 | // of the C++ standard). As a result, gcc fails to reject the |
| 152 | // following code with the simple definition: |
| 153 | // |
| 154 | // int foo; |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 155 | // GMOCK_COMPILE_ASSERT_(foo, msg); // not supposed to compile as foo is |
| 156 | // // not a compile-time constant. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 157 | // |
| 158 | // - By using the type CompileAssert<(bool(expr))>, we ensures that |
| 159 | // expr is a compile-time constant. (Template arguments must be |
| 160 | // determined at compile-time.) |
| 161 | // |
| 162 | // - The outter parentheses in CompileAssert<(bool(expr))> are necessary |
| 163 | // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written |
| 164 | // |
| 165 | // CompileAssert<bool(expr)> |
| 166 | // |
| 167 | // instead, these compilers will refuse to compile |
| 168 | // |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 169 | // GMOCK_COMPILE_ASSERT_(5 > 0, some_message); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 170 | // |
| 171 | // (They seem to think the ">" in "5 > 0" marks the end of the |
| 172 | // template argument list.) |
| 173 | // |
| 174 | // - The array size is (bool(expr) ? 1 : -1), instead of simply |
| 175 | // |
| 176 | // ((expr) ? 1 : -1). |
| 177 | // |
| 178 | // This is to avoid running into a bug in MS VC 7.1, which |
| 179 | // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1. |
| 180 | |
| 181 | #if GTEST_HAS_GLOBAL_STRING |
| 182 | typedef ::string string; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 183 | #else |
zhanyong.wan | 39bf784 | 2009-12-16 23:36:08 +0000 | [diff] [blame] | 184 | typedef ::std::string string; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 185 | #endif // GTEST_HAS_GLOBAL_STRING |
| 186 | |
| 187 | #if GTEST_HAS_GLOBAL_WSTRING |
| 188 | typedef ::wstring wstring; |
| 189 | #elif GTEST_HAS_STD_WSTRING |
| 190 | typedef ::std::wstring wstring; |
| 191 | #endif // GTEST_HAS_GLOBAL_WSTRING |
| 192 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 193 | } // namespace internal |
| 194 | } // namespace testing |
| 195 | |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 196 | // Macro for referencing flags. This is public as we want the user to |
| 197 | // use this syntax to reference Google Mock flags. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 198 | #define GMOCK_FLAG(name) FLAGS_gmock_##name |
| 199 | |
| 200 | // Macros for declaring flags. |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 201 | #define GMOCK_DECLARE_bool_(name) extern bool GMOCK_FLAG(name) |
| 202 | #define GMOCK_DECLARE_int32_(name) \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 203 | extern ::testing::internal::Int32 GMOCK_FLAG(name) |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 204 | #define GMOCK_DECLARE_string_(name) \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 205 | extern ::testing::internal::String GMOCK_FLAG(name) |
| 206 | |
| 207 | // Macros for defining flags. |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 208 | #define GMOCK_DEFINE_bool_(name, default_val, doc) \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 209 | bool GMOCK_FLAG(name) = (default_val) |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 210 | #define GMOCK_DEFINE_int32_(name, default_val, doc) \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 211 | ::testing::internal::Int32 GMOCK_FLAG(name) = (default_val) |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 212 | #define GMOCK_DEFINE_string_(name, default_val, doc) \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 213 | ::testing::internal::String GMOCK_FLAG(name) = (default_val) |
| 214 | |
| 215 | #endif // GMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_PORT_H_ |