1f4a2713aSLionel Sambuc // Copyright 2005, Google Inc.
2f4a2713aSLionel Sambuc // All rights reserved.
3f4a2713aSLionel Sambuc //
4f4a2713aSLionel Sambuc // Redistribution and use in source and binary forms, with or without
5f4a2713aSLionel Sambuc // modification, are permitted provided that the following conditions are
6f4a2713aSLionel Sambuc // met:
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //     * Redistributions of source code must retain the above copyright
9f4a2713aSLionel Sambuc // notice, this list of conditions and the following disclaimer.
10f4a2713aSLionel Sambuc //     * Redistributions in binary form must reproduce the above
11f4a2713aSLionel Sambuc // copyright notice, this list of conditions and the following disclaimer
12f4a2713aSLionel Sambuc // in the documentation and/or other materials provided with the
13f4a2713aSLionel Sambuc // distribution.
14f4a2713aSLionel Sambuc //     * Neither the name of Google Inc. nor the names of its
15f4a2713aSLionel Sambuc // contributors may be used to endorse or promote products derived from
16f4a2713aSLionel Sambuc // this software without specific prior written permission.
17f4a2713aSLionel Sambuc //
18f4a2713aSLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19f4a2713aSLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20f4a2713aSLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21f4a2713aSLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22f4a2713aSLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23f4a2713aSLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24f4a2713aSLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25f4a2713aSLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26f4a2713aSLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27f4a2713aSLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28f4a2713aSLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29f4a2713aSLionel Sambuc //
30f4a2713aSLionel Sambuc // Authors: wan@google.com (Zhanyong Wan)
31f4a2713aSLionel Sambuc //
32f4a2713aSLionel Sambuc // Low-level types and utilities for porting Google Test to various
33f4a2713aSLionel Sambuc // platforms.  They are subject to change without notice.  DO NOT USE
34f4a2713aSLionel Sambuc // THEM IN USER CODE.
35f4a2713aSLionel Sambuc 
36f4a2713aSLionel Sambuc #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
37f4a2713aSLionel Sambuc #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
38f4a2713aSLionel Sambuc 
39f4a2713aSLionel Sambuc // The user can define the following macros in the build script to
40f4a2713aSLionel Sambuc // control Google Test's behavior.  If the user doesn't define a macro
41f4a2713aSLionel Sambuc // in this list, Google Test will define it.
42f4a2713aSLionel Sambuc //
43f4a2713aSLionel Sambuc //   GTEST_HAS_CLONE          - Define it to 1/0 to indicate that clone(2)
44f4a2713aSLionel Sambuc //                              is/isn't available.
45f4a2713aSLionel Sambuc //   GTEST_HAS_EXCEPTIONS     - Define it to 1/0 to indicate that exceptions
46f4a2713aSLionel Sambuc //                              are enabled.
47f4a2713aSLionel Sambuc //   GTEST_HAS_GLOBAL_STRING  - Define it to 1/0 to indicate that ::string
48f4a2713aSLionel Sambuc //                              is/isn't available (some systems define
49f4a2713aSLionel Sambuc //                              ::string, which is different to std::string).
50f4a2713aSLionel Sambuc //   GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::string
51f4a2713aSLionel Sambuc //                              is/isn't available (some systems define
52f4a2713aSLionel Sambuc //                              ::wstring, which is different to std::wstring).
53f4a2713aSLionel Sambuc //   GTEST_HAS_POSIX_RE       - Define it to 1/0 to indicate that POSIX regular
54f4a2713aSLionel Sambuc //                              expressions are/aren't available.
55f4a2713aSLionel Sambuc //   GTEST_HAS_PTHREAD        - Define it to 1/0 to indicate that <pthread.h>
56f4a2713aSLionel Sambuc //                              is/isn't available.
57f4a2713aSLionel Sambuc //   GTEST_HAS_RTTI           - Define it to 1/0 to indicate that RTTI is/isn't
58f4a2713aSLionel Sambuc //                              enabled.
59f4a2713aSLionel Sambuc //   GTEST_HAS_STD_WSTRING    - Define it to 1/0 to indicate that
60f4a2713aSLionel Sambuc //                              std::wstring does/doesn't work (Google Test can
61f4a2713aSLionel Sambuc //                              be used where std::wstring is unavailable).
62f4a2713aSLionel Sambuc //   GTEST_HAS_TR1_TUPLE      - Define it to 1/0 to indicate tr1::tuple
63f4a2713aSLionel Sambuc //                              is/isn't available.
64f4a2713aSLionel Sambuc //   GTEST_HAS_SEH            - Define it to 1/0 to indicate whether the
65f4a2713aSLionel Sambuc //                              compiler supports Microsoft's "Structured
66f4a2713aSLionel Sambuc //                              Exception Handling".
67f4a2713aSLionel Sambuc //   GTEST_HAS_STREAM_REDIRECTION
68f4a2713aSLionel Sambuc //                            - Define it to 1/0 to indicate whether the
69f4a2713aSLionel Sambuc //                              platform supports I/O stream redirection using
70f4a2713aSLionel Sambuc //                              dup() and dup2().
71f4a2713aSLionel Sambuc //   GTEST_USE_OWN_TR1_TUPLE  - Define it to 1/0 to indicate whether Google
72f4a2713aSLionel Sambuc //                              Test's own tr1 tuple implementation should be
73f4a2713aSLionel Sambuc //                              used.  Unused when the user sets
74f4a2713aSLionel Sambuc //                              GTEST_HAS_TR1_TUPLE to 0.
75f4a2713aSLionel Sambuc //   GTEST_LINKED_AS_SHARED_LIBRARY
76f4a2713aSLionel Sambuc //                            - Define to 1 when compiling tests that use
77f4a2713aSLionel Sambuc //                              Google Test as a shared library (known as
78f4a2713aSLionel Sambuc //                              DLL on Windows).
79f4a2713aSLionel Sambuc //   GTEST_CREATE_SHARED_LIBRARY
80f4a2713aSLionel Sambuc //                            - Define to 1 when compiling Google Test itself
81f4a2713aSLionel Sambuc //                              as a shared library.
82f4a2713aSLionel Sambuc 
83f4a2713aSLionel Sambuc // This header defines the following utilities:
84f4a2713aSLionel Sambuc //
85f4a2713aSLionel Sambuc // Macros indicating the current platform (defined to 1 if compiled on
86f4a2713aSLionel Sambuc // the given platform; otherwise undefined):
87f4a2713aSLionel Sambuc //   GTEST_OS_AIX      - IBM AIX
88f4a2713aSLionel Sambuc //   GTEST_OS_CYGWIN   - Cygwin
89*0a6a1f1dSLionel Sambuc //   GTEST_OS_FREEBSD  - FreeBSD
90f4a2713aSLionel Sambuc //   GTEST_OS_HAIKU    - Haiku
91f4a2713aSLionel Sambuc //   GTEST_OS_HPUX     - HP-UX
92f4a2713aSLionel Sambuc //   GTEST_OS_LINUX    - Linux
93f4a2713aSLionel Sambuc //     GTEST_OS_LINUX_ANDROID - Google Android
94f4a2713aSLionel Sambuc //   GTEST_OS_MAC      - Mac OS X
95f4a2713aSLionel Sambuc //   GTEST_OS_NACL     - Google Native Client (NaCl)
96f4a2713aSLionel Sambuc //   GTEST_OS_SOLARIS  - Sun Solaris
97f4a2713aSLionel Sambuc //   GTEST_OS_SYMBIAN  - Symbian
98f4a2713aSLionel Sambuc //   GTEST_OS_WINDOWS  - Windows (Desktop, MinGW, or Mobile)
99f4a2713aSLionel Sambuc //     GTEST_OS_WINDOWS_DESKTOP  - Windows Desktop
100f4a2713aSLionel Sambuc //     GTEST_OS_WINDOWS_MINGW    - MinGW
101f4a2713aSLionel Sambuc //     GTEST_OS_WINDOWS_MOBILE   - Windows Mobile
102f4a2713aSLionel Sambuc //   GTEST_OS_ZOS      - z/OS
103f4a2713aSLionel Sambuc //
104f4a2713aSLionel Sambuc // Among the platforms, Cygwin, Linux, Max OS X, and Windows have the
105f4a2713aSLionel Sambuc // most stable support.  Since core members of the Google Test project
106f4a2713aSLionel Sambuc // don't have access to other platforms, support for them may be less
107f4a2713aSLionel Sambuc // stable.  If you notice any problems on your platform, please notify
108f4a2713aSLionel Sambuc // googletestframework@googlegroups.com (patches for fixing them are
109f4a2713aSLionel Sambuc // even more welcome!).
110f4a2713aSLionel Sambuc //
111f4a2713aSLionel Sambuc // Note that it is possible that none of the GTEST_OS_* macros are defined.
112f4a2713aSLionel Sambuc //
113f4a2713aSLionel Sambuc // Macros indicating available Google Test features (defined to 1 if
114f4a2713aSLionel Sambuc // the corresponding feature is supported; otherwise undefined):
115f4a2713aSLionel Sambuc //   GTEST_HAS_COMBINE      - the Combine() function (for value-parameterized
116f4a2713aSLionel Sambuc //                            tests)
117f4a2713aSLionel Sambuc //   GTEST_HAS_DEATH_TEST   - death tests
118f4a2713aSLionel Sambuc //   GTEST_HAS_PARAM_TEST   - value-parameterized tests
119f4a2713aSLionel Sambuc //   GTEST_HAS_TYPED_TEST   - typed tests
120f4a2713aSLionel Sambuc //   GTEST_HAS_TYPED_TEST_P - type-parameterized tests
121f4a2713aSLionel Sambuc //   GTEST_USES_POSIX_RE    - enhanced POSIX regex is used. Do not confuse with
122f4a2713aSLionel Sambuc //                            GTEST_HAS_POSIX_RE (see above) which users can
123f4a2713aSLionel Sambuc //                            define themselves.
124f4a2713aSLionel Sambuc //   GTEST_USES_SIMPLE_RE   - our own simple regex is used;
125f4a2713aSLionel Sambuc //                            the above two are mutually exclusive.
126f4a2713aSLionel Sambuc //   GTEST_CAN_COMPARE_NULL - accepts untyped NULL in EXPECT_EQ().
127f4a2713aSLionel Sambuc //
128f4a2713aSLionel Sambuc // Macros for basic C++ coding:
129f4a2713aSLionel Sambuc //   GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.
130f4a2713aSLionel Sambuc //   GTEST_ATTRIBUTE_UNUSED_  - declares that a class' instances or a
131f4a2713aSLionel Sambuc //                              variable don't have to be used.
132f4a2713aSLionel Sambuc //   GTEST_DISALLOW_ASSIGN_   - disables operator=.
133f4a2713aSLionel Sambuc //   GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=.
134f4a2713aSLionel Sambuc //   GTEST_MUST_USE_RESULT_   - declares that a function's result must be used.
135f4a2713aSLionel Sambuc //
136f4a2713aSLionel Sambuc // Synchronization:
137f4a2713aSLionel Sambuc //   Mutex, MutexLock, ThreadLocal, GetThreadCount()
138f4a2713aSLionel Sambuc //                  - synchronization primitives.
139f4a2713aSLionel Sambuc //   GTEST_IS_THREADSAFE - defined to 1 to indicate that the above
140f4a2713aSLionel Sambuc //                         synchronization primitives have real implementations
141f4a2713aSLionel Sambuc //                         and Google Test is thread-safe; or 0 otherwise.
142f4a2713aSLionel Sambuc //
143f4a2713aSLionel Sambuc // Template meta programming:
144f4a2713aSLionel Sambuc //   is_pointer     - as in TR1; needed on Symbian and IBM XL C/C++ only.
145f4a2713aSLionel Sambuc //   IteratorTraits - partial implementation of std::iterator_traits, which
146f4a2713aSLionel Sambuc //                    is not available in libCstd when compiled with Sun C++.
147f4a2713aSLionel Sambuc //
148f4a2713aSLionel Sambuc // Smart pointers:
149f4a2713aSLionel Sambuc //   scoped_ptr     - as in TR2.
150f4a2713aSLionel Sambuc //
151f4a2713aSLionel Sambuc // Regular expressions:
152f4a2713aSLionel Sambuc //   RE             - a simple regular expression class using the POSIX
153f4a2713aSLionel Sambuc //                    Extended Regular Expression syntax on UNIX-like
154f4a2713aSLionel Sambuc //                    platforms, or a reduced regular exception syntax on
155f4a2713aSLionel Sambuc //                    other platforms, including Windows.
156f4a2713aSLionel Sambuc //
157f4a2713aSLionel Sambuc // Logging:
158f4a2713aSLionel Sambuc //   GTEST_LOG_()   - logs messages at the specified severity level.
159f4a2713aSLionel Sambuc //   LogToStderr()  - directs all log messages to stderr.
160f4a2713aSLionel Sambuc //   FlushInfoLog() - flushes informational log messages.
161f4a2713aSLionel Sambuc //
162f4a2713aSLionel Sambuc // Stdout and stderr capturing:
163f4a2713aSLionel Sambuc //   CaptureStdout()     - starts capturing stdout.
164f4a2713aSLionel Sambuc //   GetCapturedStdout() - stops capturing stdout and returns the captured
165f4a2713aSLionel Sambuc //                         string.
166f4a2713aSLionel Sambuc //   CaptureStderr()     - starts capturing stderr.
167f4a2713aSLionel Sambuc //   GetCapturedStderr() - stops capturing stderr and returns the captured
168f4a2713aSLionel Sambuc //                         string.
169f4a2713aSLionel Sambuc //
170f4a2713aSLionel Sambuc // Integer types:
171f4a2713aSLionel Sambuc //   TypeWithSize   - maps an integer to a int type.
172f4a2713aSLionel Sambuc //   Int32, UInt32, Int64, UInt64, TimeInMillis
173f4a2713aSLionel Sambuc //                  - integers of known sizes.
174f4a2713aSLionel Sambuc //   BiggestInt     - the biggest signed integer type.
175f4a2713aSLionel Sambuc //
176f4a2713aSLionel Sambuc // Command-line utilities:
177f4a2713aSLionel Sambuc //   GTEST_FLAG()       - references a flag.
178f4a2713aSLionel Sambuc //   GTEST_DECLARE_*()  - declares a flag.
179f4a2713aSLionel Sambuc //   GTEST_DEFINE_*()   - defines a flag.
180f4a2713aSLionel Sambuc //   GetArgvs()         - returns the command line as a vector of strings.
181f4a2713aSLionel Sambuc //
182f4a2713aSLionel Sambuc // Environment variable utilities:
183f4a2713aSLionel Sambuc //   GetEnv()             - gets the value of an environment variable.
184f4a2713aSLionel Sambuc //   BoolFromGTestEnv()   - parses a bool environment variable.
185f4a2713aSLionel Sambuc //   Int32FromGTestEnv()  - parses an Int32 environment variable.
186f4a2713aSLionel Sambuc //   StringFromGTestEnv() - parses a string environment variable.
187f4a2713aSLionel Sambuc 
188f4a2713aSLionel Sambuc #include <ctype.h>   // for isspace, etc
189f4a2713aSLionel Sambuc #include <stddef.h>  // for ptrdiff_t
190f4a2713aSLionel Sambuc #include <stdlib.h>
191f4a2713aSLionel Sambuc #include <stdio.h>
192f4a2713aSLionel Sambuc #include <string.h>
193f4a2713aSLionel Sambuc #ifndef _WIN32_WCE
194f4a2713aSLionel Sambuc # include <sys/types.h>
195f4a2713aSLionel Sambuc # include <sys/stat.h>
196f4a2713aSLionel Sambuc #endif  // !_WIN32_WCE
197f4a2713aSLionel Sambuc 
198f4a2713aSLionel Sambuc #include <iostream>  // NOLINT
199f4a2713aSLionel Sambuc #include <sstream>  // NOLINT
200f4a2713aSLionel Sambuc #include <string>  // NOLINT
201f4a2713aSLionel Sambuc 
202f4a2713aSLionel Sambuc #define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
203f4a2713aSLionel Sambuc #define GTEST_FLAG_PREFIX_ "gtest_"
204f4a2713aSLionel Sambuc #define GTEST_FLAG_PREFIX_DASH_ "gtest-"
205f4a2713aSLionel Sambuc #define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
206f4a2713aSLionel Sambuc #define GTEST_NAME_ "Google Test"
207f4a2713aSLionel Sambuc #define GTEST_PROJECT_URL_ "http://code.google.com/p/googletest/"
208f4a2713aSLionel Sambuc 
209f4a2713aSLionel Sambuc // Determines the version of gcc that is used to compile this.
210f4a2713aSLionel Sambuc #ifdef __GNUC__
211f4a2713aSLionel Sambuc // 40302 means version 4.3.2.
212f4a2713aSLionel Sambuc # define GTEST_GCC_VER_ \
213f4a2713aSLionel Sambuc     (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
214f4a2713aSLionel Sambuc #endif  // __GNUC__
215f4a2713aSLionel Sambuc 
216f4a2713aSLionel Sambuc // Determines the platform on which Google Test is compiled.
217f4a2713aSLionel Sambuc #ifdef __CYGWIN__
218f4a2713aSLionel Sambuc # define GTEST_OS_CYGWIN 1
219f4a2713aSLionel Sambuc #elif defined __SYMBIAN32__
220f4a2713aSLionel Sambuc # define GTEST_OS_SYMBIAN 1
221f4a2713aSLionel Sambuc #elif defined _WIN32
222f4a2713aSLionel Sambuc # define GTEST_OS_WINDOWS 1
223f4a2713aSLionel Sambuc # ifdef _WIN32_WCE
224f4a2713aSLionel Sambuc #  define GTEST_OS_WINDOWS_MOBILE 1
225f4a2713aSLionel Sambuc # elif defined(__MINGW__) || defined(__MINGW32__)
226f4a2713aSLionel Sambuc #  define GTEST_OS_WINDOWS_MINGW 1
227f4a2713aSLionel Sambuc # else
228f4a2713aSLionel Sambuc #  define GTEST_OS_WINDOWS_DESKTOP 1
229f4a2713aSLionel Sambuc # endif  // _WIN32_WCE
230f4a2713aSLionel Sambuc #elif defined __APPLE__
231f4a2713aSLionel Sambuc # define GTEST_OS_MAC 1
232*0a6a1f1dSLionel Sambuc #elif defined __FreeBSD__
233*0a6a1f1dSLionel Sambuc # define GTEST_OS_FREEBSD 1
234f4a2713aSLionel Sambuc #elif defined __linux__
235f4a2713aSLionel Sambuc # define GTEST_OS_LINUX 1
236f4a2713aSLionel Sambuc # if defined(ANDROID) || defined(__ANDROID__)
237f4a2713aSLionel Sambuc #  define GTEST_OS_LINUX_ANDROID 1
238f4a2713aSLionel Sambuc # endif  // ANDROID
239f4a2713aSLionel Sambuc #elif defined __MVS__
240f4a2713aSLionel Sambuc # define GTEST_OS_ZOS 1
241f4a2713aSLionel Sambuc #elif defined(__sun) && defined(__SVR4)
242f4a2713aSLionel Sambuc # define GTEST_OS_SOLARIS 1
243f4a2713aSLionel Sambuc #elif defined(_AIX)
244f4a2713aSLionel Sambuc # define GTEST_OS_AIX 1
245f4a2713aSLionel Sambuc #elif defined(__hpux)
246f4a2713aSLionel Sambuc # define GTEST_OS_HPUX 1
247f4a2713aSLionel Sambuc #elif defined __native_client__
248f4a2713aSLionel Sambuc # define GTEST_OS_NACL 1
249f4a2713aSLionel Sambuc #elif defined(__HAIKU__)
250f4a2713aSLionel Sambuc # define GTEST_OS_HAIKU 1
251f4a2713aSLionel Sambuc #endif  // __CYGWIN__
252f4a2713aSLionel Sambuc 
253f4a2713aSLionel Sambuc // Brings in definitions for functions used in the testing::internal::posix
254f4a2713aSLionel Sambuc // namespace (read, write, close, chdir, isatty, stat). We do not currently
255f4a2713aSLionel Sambuc // use them on Windows Mobile.
256f4a2713aSLionel Sambuc #if !GTEST_OS_WINDOWS
257f4a2713aSLionel Sambuc // This assumes that non-Windows OSes provide unistd.h. For OSes where this
258f4a2713aSLionel Sambuc // is not the case, we need to include headers that provide the functions
259f4a2713aSLionel Sambuc // mentioned above.
260f4a2713aSLionel Sambuc # include <unistd.h>
261f4a2713aSLionel Sambuc # if !GTEST_OS_NACL
262f4a2713aSLionel Sambuc // TODO(vladl@google.com): Remove this condition when Native Client SDK adds
263f4a2713aSLionel Sambuc // strings.h (tracked in
264f4a2713aSLionel Sambuc // http://code.google.com/p/nativeclient/issues/detail?id=1175).
265f4a2713aSLionel Sambuc #  include <strings.h>  // Native Client doesn't provide strings.h.
266f4a2713aSLionel Sambuc # endif
267f4a2713aSLionel Sambuc #elif !GTEST_OS_WINDOWS_MOBILE
268f4a2713aSLionel Sambuc # include <direct.h>
269f4a2713aSLionel Sambuc # include <io.h>
270f4a2713aSLionel Sambuc #endif
271f4a2713aSLionel Sambuc 
272f4a2713aSLionel Sambuc // Defines this to true iff Google Test can use POSIX regular expressions.
273f4a2713aSLionel Sambuc #ifndef GTEST_HAS_POSIX_RE
274f4a2713aSLionel Sambuc # define GTEST_HAS_POSIX_RE (!GTEST_OS_WINDOWS)
275f4a2713aSLionel Sambuc #endif
276f4a2713aSLionel Sambuc 
277f4a2713aSLionel Sambuc #if GTEST_HAS_POSIX_RE
278f4a2713aSLionel Sambuc 
279f4a2713aSLionel Sambuc // On some platforms, <regex.h> needs someone to define size_t, and
280f4a2713aSLionel Sambuc // won't compile otherwise.  We can #include it here as we already
281f4a2713aSLionel Sambuc // included <stdlib.h>, which is guaranteed to define size_t through
282f4a2713aSLionel Sambuc // <stddef.h>.
283f4a2713aSLionel Sambuc # include <regex.h>  // NOLINT
284f4a2713aSLionel Sambuc 
285f4a2713aSLionel Sambuc # define GTEST_USES_POSIX_RE 1
286f4a2713aSLionel Sambuc 
287f4a2713aSLionel Sambuc #elif GTEST_OS_WINDOWS
288f4a2713aSLionel Sambuc 
289f4a2713aSLionel Sambuc // <regex.h> is not available on Windows.  Use our own simple regex
290f4a2713aSLionel Sambuc // implementation instead.
291f4a2713aSLionel Sambuc # define GTEST_USES_SIMPLE_RE 1
292f4a2713aSLionel Sambuc 
293f4a2713aSLionel Sambuc #else
294f4a2713aSLionel Sambuc 
295f4a2713aSLionel Sambuc // <regex.h> may not be available on this platform.  Use our own
296f4a2713aSLionel Sambuc // simple regex implementation instead.
297f4a2713aSLionel Sambuc # define GTEST_USES_SIMPLE_RE 1
298f4a2713aSLionel Sambuc 
299f4a2713aSLionel Sambuc #endif  // GTEST_HAS_POSIX_RE
300f4a2713aSLionel Sambuc 
301f4a2713aSLionel Sambuc #ifndef GTEST_HAS_EXCEPTIONS
302f4a2713aSLionel Sambuc // The user didn't tell us whether exceptions are enabled, so we need
303f4a2713aSLionel Sambuc // to figure it out.
304f4a2713aSLionel Sambuc # if defined(_MSC_VER) || defined(__BORLANDC__)
305f4a2713aSLionel Sambuc // MSVC's and C++Builder's implementations of the STL use the _HAS_EXCEPTIONS
306f4a2713aSLionel Sambuc // macro to enable exceptions, so we'll do the same.
307f4a2713aSLionel Sambuc // Assumes that exceptions are enabled by default.
308f4a2713aSLionel Sambuc #  ifndef _HAS_EXCEPTIONS
309f4a2713aSLionel Sambuc #   define _HAS_EXCEPTIONS 1
310f4a2713aSLionel Sambuc #  endif  // _HAS_EXCEPTIONS
311f4a2713aSLionel Sambuc #  define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
312f4a2713aSLionel Sambuc # elif defined(__GNUC__) && __EXCEPTIONS
313f4a2713aSLionel Sambuc // gcc defines __EXCEPTIONS to 1 iff exceptions are enabled.
314f4a2713aSLionel Sambuc #  define GTEST_HAS_EXCEPTIONS 1
315f4a2713aSLionel Sambuc # elif defined(__SUNPRO_CC)
316f4a2713aSLionel Sambuc // Sun Pro CC supports exceptions.  However, there is no compile-time way of
317f4a2713aSLionel Sambuc // detecting whether they are enabled or not.  Therefore, we assume that
318f4a2713aSLionel Sambuc // they are enabled unless the user tells us otherwise.
319f4a2713aSLionel Sambuc #  define GTEST_HAS_EXCEPTIONS 1
320f4a2713aSLionel Sambuc # elif defined(__IBMCPP__) && __EXCEPTIONS
321f4a2713aSLionel Sambuc // xlC defines __EXCEPTIONS to 1 iff exceptions are enabled.
322f4a2713aSLionel Sambuc #  define GTEST_HAS_EXCEPTIONS 1
323f4a2713aSLionel Sambuc # elif defined(__HP_aCC)
324f4a2713aSLionel Sambuc // Exception handling is in effect by default in HP aCC compiler. It has to
325f4a2713aSLionel Sambuc // be turned of by +noeh compiler option if desired.
326f4a2713aSLionel Sambuc #  define GTEST_HAS_EXCEPTIONS 1
327f4a2713aSLionel Sambuc # else
328f4a2713aSLionel Sambuc // For other compilers, we assume exceptions are disabled to be
329f4a2713aSLionel Sambuc // conservative.
330f4a2713aSLionel Sambuc #  define GTEST_HAS_EXCEPTIONS 0
331f4a2713aSLionel Sambuc # endif  // defined(_MSC_VER) || defined(__BORLANDC__)
332f4a2713aSLionel Sambuc #endif  // GTEST_HAS_EXCEPTIONS
333f4a2713aSLionel Sambuc 
334f4a2713aSLionel Sambuc #if !defined(GTEST_HAS_STD_STRING)
335f4a2713aSLionel Sambuc // Even though we don't use this macro any longer, we keep it in case
336f4a2713aSLionel Sambuc // some clients still depend on it.
337f4a2713aSLionel Sambuc # define GTEST_HAS_STD_STRING 1
338f4a2713aSLionel Sambuc #elif !GTEST_HAS_STD_STRING
339f4a2713aSLionel Sambuc // The user told us that ::std::string isn't available.
340f4a2713aSLionel Sambuc # error "Google Test cannot be used where ::std::string isn't available."
341f4a2713aSLionel Sambuc #endif  // !defined(GTEST_HAS_STD_STRING)
342f4a2713aSLionel Sambuc 
343f4a2713aSLionel Sambuc #ifndef GTEST_HAS_GLOBAL_STRING
344f4a2713aSLionel Sambuc // The user didn't tell us whether ::string is available, so we need
345f4a2713aSLionel Sambuc // to figure it out.
346f4a2713aSLionel Sambuc 
347f4a2713aSLionel Sambuc # define GTEST_HAS_GLOBAL_STRING 0
348f4a2713aSLionel Sambuc 
349f4a2713aSLionel Sambuc #endif  // GTEST_HAS_GLOBAL_STRING
350f4a2713aSLionel Sambuc 
351f4a2713aSLionel Sambuc #ifndef GTEST_HAS_STD_WSTRING
352f4a2713aSLionel Sambuc // The user didn't tell us whether ::std::wstring is available, so we need
353f4a2713aSLionel Sambuc // to figure it out.
354f4a2713aSLionel Sambuc // TODO(wan@google.com): uses autoconf to detect whether ::std::wstring
355f4a2713aSLionel Sambuc //   is available.
356f4a2713aSLionel Sambuc 
357f4a2713aSLionel Sambuc // Cygwin 1.7 and below doesn't support ::std::wstring.
358f4a2713aSLionel Sambuc // Solaris' libc++ doesn't support it either.  Android has
359f4a2713aSLionel Sambuc // no support for it at least as recent as Froyo (2.2).
360f4a2713aSLionel Sambuc // Minix currently doesn't support it either.
361f4a2713aSLionel Sambuc # define GTEST_HAS_STD_WSTRING \
362f4a2713aSLionel Sambuc     (!(GTEST_OS_LINUX_ANDROID || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || GTEST_OS_HAIKU || defined(_MINIX)))
363f4a2713aSLionel Sambuc 
364f4a2713aSLionel Sambuc #endif  // GTEST_HAS_STD_WSTRING
365f4a2713aSLionel Sambuc 
366f4a2713aSLionel Sambuc #ifndef GTEST_HAS_GLOBAL_WSTRING
367f4a2713aSLionel Sambuc // The user didn't tell us whether ::wstring is available, so we need
368f4a2713aSLionel Sambuc // to figure it out.
369f4a2713aSLionel Sambuc # define GTEST_HAS_GLOBAL_WSTRING \
370f4a2713aSLionel Sambuc     (GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING)
371f4a2713aSLionel Sambuc #endif  // GTEST_HAS_GLOBAL_WSTRING
372f4a2713aSLionel Sambuc 
373f4a2713aSLionel Sambuc // Determines whether RTTI is available.
374f4a2713aSLionel Sambuc #ifndef GTEST_HAS_RTTI
375f4a2713aSLionel Sambuc // The user didn't tell us whether RTTI is enabled, so we need to
376f4a2713aSLionel Sambuc // figure it out.
377f4a2713aSLionel Sambuc 
378f4a2713aSLionel Sambuc # ifdef _MSC_VER
379f4a2713aSLionel Sambuc 
380f4a2713aSLionel Sambuc #  ifdef _CPPRTTI  // MSVC defines this macro iff RTTI is enabled.
381f4a2713aSLionel Sambuc #   define GTEST_HAS_RTTI 1
382f4a2713aSLionel Sambuc #  else
383f4a2713aSLionel Sambuc #   define GTEST_HAS_RTTI 0
384f4a2713aSLionel Sambuc #  endif
385f4a2713aSLionel Sambuc 
386f4a2713aSLionel Sambuc // Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled.
387f4a2713aSLionel Sambuc # elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40302)
388f4a2713aSLionel Sambuc 
389f4a2713aSLionel Sambuc #  ifdef __GXX_RTTI
390f4a2713aSLionel Sambuc #   define GTEST_HAS_RTTI 1
391f4a2713aSLionel Sambuc #  else
392f4a2713aSLionel Sambuc #   define GTEST_HAS_RTTI 0
393f4a2713aSLionel Sambuc #  endif  // __GXX_RTTI
394f4a2713aSLionel Sambuc 
395f4a2713aSLionel Sambuc // Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if
396f4a2713aSLionel Sambuc // both the typeid and dynamic_cast features are present.
397f4a2713aSLionel Sambuc # elif defined(__IBMCPP__) && (__IBMCPP__ >= 900)
398f4a2713aSLionel Sambuc 
399f4a2713aSLionel Sambuc #  ifdef __RTTI_ALL__
400f4a2713aSLionel Sambuc #   define GTEST_HAS_RTTI 1
401f4a2713aSLionel Sambuc #  else
402f4a2713aSLionel Sambuc #   define GTEST_HAS_RTTI 0
403f4a2713aSLionel Sambuc #  endif
404f4a2713aSLionel Sambuc 
405f4a2713aSLionel Sambuc # else
406f4a2713aSLionel Sambuc 
407f4a2713aSLionel Sambuc // For all other compilers, we assume RTTI is enabled.
408f4a2713aSLionel Sambuc #  define GTEST_HAS_RTTI 1
409f4a2713aSLionel Sambuc 
410f4a2713aSLionel Sambuc # endif  // _MSC_VER
411f4a2713aSLionel Sambuc 
412f4a2713aSLionel Sambuc #endif  // GTEST_HAS_RTTI
413f4a2713aSLionel Sambuc 
414f4a2713aSLionel Sambuc // It's this header's responsibility to #include <typeinfo> when RTTI
415f4a2713aSLionel Sambuc // is enabled.
416f4a2713aSLionel Sambuc #if GTEST_HAS_RTTI
417f4a2713aSLionel Sambuc # include <typeinfo>
418f4a2713aSLionel Sambuc #endif
419f4a2713aSLionel Sambuc 
420f4a2713aSLionel Sambuc // Determines whether Google Test can use the pthreads library.
421f4a2713aSLionel Sambuc #ifndef GTEST_HAS_PTHREAD
422f4a2713aSLionel Sambuc // The user didn't tell us explicitly, so we assume pthreads support is
423f4a2713aSLionel Sambuc // available on Linux and Mac.
424f4a2713aSLionel Sambuc //
425f4a2713aSLionel Sambuc // To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0
426f4a2713aSLionel Sambuc // to your compiler flags.
427*0a6a1f1dSLionel Sambuc # define GTEST_HAS_PTHREAD (GTEST_OS_LINUX || GTEST_OS_MAC || \
428*0a6a1f1dSLionel Sambuc           GTEST_OS_HPUX || GTEST_OS_FREEBSD)
429f4a2713aSLionel Sambuc #endif  // GTEST_HAS_PTHREAD
430f4a2713aSLionel Sambuc 
431f4a2713aSLionel Sambuc #if GTEST_HAS_PTHREAD
432f4a2713aSLionel Sambuc // gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is
433f4a2713aSLionel Sambuc // true.
434f4a2713aSLionel Sambuc # include <pthread.h>  // NOLINT
435f4a2713aSLionel Sambuc 
436f4a2713aSLionel Sambuc // For timespec and nanosleep, used below.
437f4a2713aSLionel Sambuc # include <time.h>  // NOLINT
438f4a2713aSLionel Sambuc #endif
439f4a2713aSLionel Sambuc 
440f4a2713aSLionel Sambuc // Determines whether Google Test can use tr1/tuple.  You can define
441f4a2713aSLionel Sambuc // this macro to 0 to prevent Google Test from using tuple (any
442f4a2713aSLionel Sambuc // feature depending on tuple with be disabled in this mode).
443f4a2713aSLionel Sambuc #ifndef GTEST_HAS_TR1_TUPLE
444f4a2713aSLionel Sambuc // The user didn't tell us not to do it, so we assume it's OK.
445f4a2713aSLionel Sambuc # define GTEST_HAS_TR1_TUPLE 1
446f4a2713aSLionel Sambuc #endif  // GTEST_HAS_TR1_TUPLE
447f4a2713aSLionel Sambuc 
448f4a2713aSLionel Sambuc // Determines whether Google Test's own tr1 tuple implementation
449f4a2713aSLionel Sambuc // should be used.
450f4a2713aSLionel Sambuc #ifndef GTEST_USE_OWN_TR1_TUPLE
451f4a2713aSLionel Sambuc // The user didn't tell us, so we need to figure it out.
452f4a2713aSLionel Sambuc 
453f4a2713aSLionel Sambuc // We use our own TR1 tuple if we aren't sure the user has an
454f4a2713aSLionel Sambuc // implementation of it already.  At this time, GCC 4.0.0+ and MSVC
455f4a2713aSLionel Sambuc // 2010 are the only mainstream compilers that come with a TR1 tuple
456f4a2713aSLionel Sambuc // implementation.  NVIDIA's CUDA NVCC compiler pretends to be GCC by
457f4a2713aSLionel Sambuc // defining __GNUC__ and friends, but cannot compile GCC's tuple
458f4a2713aSLionel Sambuc // implementation.  MSVC 2008 (9.0) provides TR1 tuple in a 323 MB
459f4a2713aSLionel Sambuc // Feature Pack download, which we cannot assume the user has.
460f4a2713aSLionel Sambuc # if (defined(__GNUC__) && !(defined(__CUDACC__) || defined(__clang__)) \
461f4a2713aSLionel Sambuc                         && (GTEST_GCC_VER_ >= 40000)) \
462f4a2713aSLionel Sambuc     || _MSC_VER >= 1600
463f4a2713aSLionel Sambuc #  define GTEST_USE_OWN_TR1_TUPLE 0
464f4a2713aSLionel Sambuc # else
465f4a2713aSLionel Sambuc #  define GTEST_USE_OWN_TR1_TUPLE 1
466f4a2713aSLionel Sambuc # endif
467f4a2713aSLionel Sambuc 
468f4a2713aSLionel Sambuc #endif  // GTEST_USE_OWN_TR1_TUPLE
469f4a2713aSLionel Sambuc 
470f4a2713aSLionel Sambuc // To avoid conditional compilation everywhere, we make it
471f4a2713aSLionel Sambuc // gtest-port.h's responsibility to #include the header implementing
472f4a2713aSLionel Sambuc // tr1/tuple.
473f4a2713aSLionel Sambuc #if GTEST_HAS_TR1_TUPLE
474f4a2713aSLionel Sambuc 
475f4a2713aSLionel Sambuc # if GTEST_USE_OWN_TR1_TUPLE
476f4a2713aSLionel Sambuc #  include "gtest/internal/gtest-tuple.h"
477f4a2713aSLionel Sambuc # elif GTEST_OS_SYMBIAN
478f4a2713aSLionel Sambuc 
479f4a2713aSLionel Sambuc // On Symbian, BOOST_HAS_TR1_TUPLE causes Boost's TR1 tuple library to
480f4a2713aSLionel Sambuc // use STLport's tuple implementation, which unfortunately doesn't
481f4a2713aSLionel Sambuc // work as the copy of STLport distributed with Symbian is incomplete.
482f4a2713aSLionel Sambuc // By making sure BOOST_HAS_TR1_TUPLE is undefined, we force Boost to
483f4a2713aSLionel Sambuc // use its own tuple implementation.
484f4a2713aSLionel Sambuc #  ifdef BOOST_HAS_TR1_TUPLE
485f4a2713aSLionel Sambuc #   undef BOOST_HAS_TR1_TUPLE
486f4a2713aSLionel Sambuc #  endif  // BOOST_HAS_TR1_TUPLE
487f4a2713aSLionel Sambuc 
488f4a2713aSLionel Sambuc // This prevents <boost/tr1/detail/config.hpp>, which defines
489f4a2713aSLionel Sambuc // BOOST_HAS_TR1_TUPLE, from being #included by Boost's <tuple>.
490f4a2713aSLionel Sambuc #  define BOOST_TR1_DETAIL_CONFIG_HPP_INCLUDED
491f4a2713aSLionel Sambuc #  include <tuple>
492f4a2713aSLionel Sambuc 
493f4a2713aSLionel Sambuc # elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000)
494f4a2713aSLionel Sambuc // GCC 4.0+ implements tr1/tuple in the <tr1/tuple> header.  This does
495f4a2713aSLionel Sambuc // not conform to the TR1 spec, which requires the header to be <tuple>.
496f4a2713aSLionel Sambuc 
497f4a2713aSLionel Sambuc #  if !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
498f4a2713aSLionel Sambuc // Until version 4.3.2, gcc has a bug that causes <tr1/functional>,
499f4a2713aSLionel Sambuc // which is #included by <tr1/tuple>, to not compile when RTTI is
500f4a2713aSLionel Sambuc // disabled.  _TR1_FUNCTIONAL is the header guard for
501f4a2713aSLionel Sambuc // <tr1/functional>.  Hence the following #define is a hack to prevent
502f4a2713aSLionel Sambuc // <tr1/functional> from being included.
503f4a2713aSLionel Sambuc #   define _TR1_FUNCTIONAL 1
504f4a2713aSLionel Sambuc #   include <tr1/tuple>
505f4a2713aSLionel Sambuc #   undef _TR1_FUNCTIONAL  // Allows the user to #include
506f4a2713aSLionel Sambuc                         // <tr1/functional> if he chooses to.
507f4a2713aSLionel Sambuc #  else
508f4a2713aSLionel Sambuc #   include <tr1/tuple>  // NOLINT
509f4a2713aSLionel Sambuc #  endif  // !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
510f4a2713aSLionel Sambuc 
511f4a2713aSLionel Sambuc # else
512f4a2713aSLionel Sambuc // If the compiler is not GCC 4.0+, we assume the user is using a
513f4a2713aSLionel Sambuc // spec-conforming TR1 implementation.
514f4a2713aSLionel Sambuc #  include <tuple>  // NOLINT
515f4a2713aSLionel Sambuc # endif  // GTEST_USE_OWN_TR1_TUPLE
516f4a2713aSLionel Sambuc 
517f4a2713aSLionel Sambuc #endif  // GTEST_HAS_TR1_TUPLE
518f4a2713aSLionel Sambuc 
519f4a2713aSLionel Sambuc // Determines whether clone(2) is supported.
520f4a2713aSLionel Sambuc // Usually it will only be available on Linux, excluding
521f4a2713aSLionel Sambuc // Linux on the Itanium architecture.
522f4a2713aSLionel Sambuc // Also see http://linux.die.net/man/2/clone.
523f4a2713aSLionel Sambuc #ifndef GTEST_HAS_CLONE
524f4a2713aSLionel Sambuc // The user didn't tell us, so we need to figure it out.
525f4a2713aSLionel Sambuc 
526f4a2713aSLionel Sambuc # if GTEST_OS_LINUX && !defined(__ia64__)
527f4a2713aSLionel Sambuc #  define GTEST_HAS_CLONE 1
528f4a2713aSLionel Sambuc # else
529f4a2713aSLionel Sambuc #  define GTEST_HAS_CLONE 0
530f4a2713aSLionel Sambuc # endif  // GTEST_OS_LINUX && !defined(__ia64__)
531f4a2713aSLionel Sambuc 
532f4a2713aSLionel Sambuc #endif  // GTEST_HAS_CLONE
533f4a2713aSLionel Sambuc 
534f4a2713aSLionel Sambuc // Determines whether to support stream redirection. This is used to test
535f4a2713aSLionel Sambuc // output correctness and to implement death tests.
536f4a2713aSLionel Sambuc #ifndef GTEST_HAS_STREAM_REDIRECTION
537f4a2713aSLionel Sambuc // By default, we assume that stream redirection is supported on all
538f4a2713aSLionel Sambuc // platforms except known mobile ones.
539f4a2713aSLionel Sambuc # if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN
540f4a2713aSLionel Sambuc #  define GTEST_HAS_STREAM_REDIRECTION 0
541f4a2713aSLionel Sambuc # else
542f4a2713aSLionel Sambuc #  define GTEST_HAS_STREAM_REDIRECTION 1
543f4a2713aSLionel Sambuc # endif  // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN
544f4a2713aSLionel Sambuc #endif  // GTEST_HAS_STREAM_REDIRECTION
545f4a2713aSLionel Sambuc 
546f4a2713aSLionel Sambuc // Determines whether to support death tests.
547f4a2713aSLionel Sambuc // Google Test does not support death tests for VC 7.1 and earlier as
548f4a2713aSLionel Sambuc // abort() in a VC 7.1 application compiled as GUI in debug config
549f4a2713aSLionel Sambuc // pops up a dialog window that cannot be suppressed programmatically.
550f4a2713aSLionel Sambuc #if (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \
551f4a2713aSLionel Sambuc      (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER >= 1400) || \
552*0a6a1f1dSLionel Sambuc      GTEST_OS_WINDOWS_MINGW || GTEST_OS_AIX || \
553*0a6a1f1dSLionel Sambuc      GTEST_OS_HPUX || GTEST_OS_FREEBSD)
554f4a2713aSLionel Sambuc # define GTEST_HAS_DEATH_TEST 1
555f4a2713aSLionel Sambuc # include <vector>  // NOLINT
556f4a2713aSLionel Sambuc #endif
557f4a2713aSLionel Sambuc 
558f4a2713aSLionel Sambuc // We don't support MSVC 7.1 with exceptions disabled now.  Therefore
559f4a2713aSLionel Sambuc // all the compilers we care about are adequate for supporting
560f4a2713aSLionel Sambuc // value-parameterized tests.
561f4a2713aSLionel Sambuc #define GTEST_HAS_PARAM_TEST 1
562f4a2713aSLionel Sambuc 
563f4a2713aSLionel Sambuc // Determines whether to support type-driven tests.
564f4a2713aSLionel Sambuc 
565f4a2713aSLionel Sambuc // Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0,
566f4a2713aSLionel Sambuc // Sun Pro CC, IBM Visual Age, and HP aCC support.
567f4a2713aSLionel Sambuc #if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__SUNPRO_CC) || \
568f4a2713aSLionel Sambuc     defined(__IBMCPP__) || defined(__HP_aCC)
569f4a2713aSLionel Sambuc # define GTEST_HAS_TYPED_TEST 1
570f4a2713aSLionel Sambuc # define GTEST_HAS_TYPED_TEST_P 1
571f4a2713aSLionel Sambuc #endif
572f4a2713aSLionel Sambuc 
573f4a2713aSLionel Sambuc // Determines whether to support Combine(). This only makes sense when
574f4a2713aSLionel Sambuc // value-parameterized tests are enabled.  The implementation doesn't
575f4a2713aSLionel Sambuc // work on Sun Studio since it doesn't understand templated conversion
576f4a2713aSLionel Sambuc // operators.
577f4a2713aSLionel Sambuc #if GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE && !defined(__SUNPRO_CC)
578f4a2713aSLionel Sambuc # define GTEST_HAS_COMBINE 1
579f4a2713aSLionel Sambuc #endif
580f4a2713aSLionel Sambuc 
581f4a2713aSLionel Sambuc // Determines whether the system compiler uses UTF-16 for encoding wide strings.
582f4a2713aSLionel Sambuc #define GTEST_WIDE_STRING_USES_UTF16_ \
583f4a2713aSLionel Sambuc     (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN || GTEST_OS_AIX)
584f4a2713aSLionel Sambuc 
585f4a2713aSLionel Sambuc // Determines whether test results can be streamed to a socket.
586f4a2713aSLionel Sambuc #if GTEST_OS_LINUX
587f4a2713aSLionel Sambuc # define GTEST_CAN_STREAM_RESULTS_ 1
588f4a2713aSLionel Sambuc #endif
589f4a2713aSLionel Sambuc 
590f4a2713aSLionel Sambuc // Defines some utility macros.
591f4a2713aSLionel Sambuc 
592f4a2713aSLionel Sambuc // The GNU compiler emits a warning if nested "if" statements are followed by
593f4a2713aSLionel Sambuc // an "else" statement and braces are not used to explicitly disambiguate the
594f4a2713aSLionel Sambuc // "else" binding.  This leads to problems with code like:
595f4a2713aSLionel Sambuc //
596f4a2713aSLionel Sambuc //   if (gate)
597f4a2713aSLionel Sambuc //     ASSERT_*(condition) << "Some message";
598f4a2713aSLionel Sambuc //
599f4a2713aSLionel Sambuc // The "switch (0) case 0:" idiom is used to suppress this.
600f4a2713aSLionel Sambuc #ifdef __INTEL_COMPILER
601f4a2713aSLionel Sambuc # define GTEST_AMBIGUOUS_ELSE_BLOCKER_
602f4a2713aSLionel Sambuc #else
603f4a2713aSLionel Sambuc # define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: default:  // NOLINT
604f4a2713aSLionel Sambuc #endif
605f4a2713aSLionel Sambuc 
606f4a2713aSLionel Sambuc // Use this annotation at the end of a struct/class definition to
607f4a2713aSLionel Sambuc // prevent the compiler from optimizing away instances that are never
608f4a2713aSLionel Sambuc // used.  This is useful when all interesting logic happens inside the
609f4a2713aSLionel Sambuc // c'tor and / or d'tor.  Example:
610f4a2713aSLionel Sambuc //
611f4a2713aSLionel Sambuc //   struct Foo {
612f4a2713aSLionel Sambuc //     Foo() { ... }
613f4a2713aSLionel Sambuc //   } GTEST_ATTRIBUTE_UNUSED_;
614f4a2713aSLionel Sambuc //
615f4a2713aSLionel Sambuc // Also use it after a variable or parameter declaration to tell the
616f4a2713aSLionel Sambuc // compiler the variable/parameter does not have to be used.
617f4a2713aSLionel Sambuc #if defined(__GNUC__) && !defined(COMPILER_ICC)
618f4a2713aSLionel Sambuc # define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
619f4a2713aSLionel Sambuc #else
620f4a2713aSLionel Sambuc # define GTEST_ATTRIBUTE_UNUSED_
621f4a2713aSLionel Sambuc #endif
622f4a2713aSLionel Sambuc 
623f4a2713aSLionel Sambuc // A macro to disallow operator=
624f4a2713aSLionel Sambuc // This should be used in the private: declarations for a class.
625f4a2713aSLionel Sambuc #define GTEST_DISALLOW_ASSIGN_(type)\
626f4a2713aSLionel Sambuc   void operator=(type const &)
627f4a2713aSLionel Sambuc 
628f4a2713aSLionel Sambuc // A macro to disallow copy constructor and operator=
629f4a2713aSLionel Sambuc // This should be used in the private: declarations for a class.
630f4a2713aSLionel Sambuc #define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\
631f4a2713aSLionel Sambuc   type(type const &);\
632f4a2713aSLionel Sambuc   GTEST_DISALLOW_ASSIGN_(type)
633f4a2713aSLionel Sambuc 
634f4a2713aSLionel Sambuc // Tell the compiler to warn about unused return values for functions declared
635f4a2713aSLionel Sambuc // with this macro.  The macro should be used on function declarations
636f4a2713aSLionel Sambuc // following the argument list:
637f4a2713aSLionel Sambuc //
638f4a2713aSLionel Sambuc //   Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_;
639f4a2713aSLionel Sambuc #if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC)
640f4a2713aSLionel Sambuc # define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))
641f4a2713aSLionel Sambuc #else
642f4a2713aSLionel Sambuc # define GTEST_MUST_USE_RESULT_
643f4a2713aSLionel Sambuc #endif  // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC
644f4a2713aSLionel Sambuc 
645f4a2713aSLionel Sambuc // Determine whether the compiler supports Microsoft's Structured Exception
646f4a2713aSLionel Sambuc // Handling.  This is supported by several Windows compilers but generally
647f4a2713aSLionel Sambuc // does not exist on any other system.
648f4a2713aSLionel Sambuc #ifndef GTEST_HAS_SEH
649f4a2713aSLionel Sambuc // The user didn't tell us, so we need to figure it out.
650f4a2713aSLionel Sambuc 
651f4a2713aSLionel Sambuc # if defined(_MSC_VER) || defined(__BORLANDC__)
652f4a2713aSLionel Sambuc // These two compilers are known to support SEH.
653f4a2713aSLionel Sambuc #  define GTEST_HAS_SEH 1
654f4a2713aSLionel Sambuc # else
655f4a2713aSLionel Sambuc // Assume no SEH.
656f4a2713aSLionel Sambuc #  define GTEST_HAS_SEH 0
657f4a2713aSLionel Sambuc # endif
658f4a2713aSLionel Sambuc 
659f4a2713aSLionel Sambuc #endif  // GTEST_HAS_SEH
660f4a2713aSLionel Sambuc 
661f4a2713aSLionel Sambuc #ifdef _MSC_VER
662f4a2713aSLionel Sambuc 
663f4a2713aSLionel Sambuc # if GTEST_LINKED_AS_SHARED_LIBRARY
664f4a2713aSLionel Sambuc #  define GTEST_API_ __declspec(dllimport)
665f4a2713aSLionel Sambuc # elif GTEST_CREATE_SHARED_LIBRARY
666f4a2713aSLionel Sambuc #  define GTEST_API_ __declspec(dllexport)
667f4a2713aSLionel Sambuc # endif
668f4a2713aSLionel Sambuc 
669f4a2713aSLionel Sambuc #endif  // _MSC_VER
670f4a2713aSLionel Sambuc 
671f4a2713aSLionel Sambuc #ifndef GTEST_API_
672f4a2713aSLionel Sambuc # define GTEST_API_
673f4a2713aSLionel Sambuc #endif
674f4a2713aSLionel Sambuc 
675f4a2713aSLionel Sambuc #ifdef __GNUC__
676f4a2713aSLionel Sambuc // Ask the compiler to never inline a given function.
677f4a2713aSLionel Sambuc # define GTEST_NO_INLINE_ __attribute__((noinline))
678f4a2713aSLionel Sambuc #else
679f4a2713aSLionel Sambuc # define GTEST_NO_INLINE_
680f4a2713aSLionel Sambuc #endif
681f4a2713aSLionel Sambuc 
682f4a2713aSLionel Sambuc namespace testing {
683f4a2713aSLionel Sambuc 
684f4a2713aSLionel Sambuc class Message;
685f4a2713aSLionel Sambuc 
686f4a2713aSLionel Sambuc namespace internal {
687f4a2713aSLionel Sambuc 
688f4a2713aSLionel Sambuc class String;
689f4a2713aSLionel Sambuc 
690f4a2713aSLionel Sambuc // The GTEST_COMPILE_ASSERT_ macro can be used to verify that a compile time
691f4a2713aSLionel Sambuc // expression is true. For example, you could use it to verify the
692f4a2713aSLionel Sambuc // size of a static array:
693f4a2713aSLionel Sambuc //
694f4a2713aSLionel Sambuc //   GTEST_COMPILE_ASSERT_(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
695f4a2713aSLionel Sambuc //                         content_type_names_incorrect_size);
696f4a2713aSLionel Sambuc //
697f4a2713aSLionel Sambuc // or to make sure a struct is smaller than a certain size:
698f4a2713aSLionel Sambuc //
699f4a2713aSLionel Sambuc //   GTEST_COMPILE_ASSERT_(sizeof(foo) < 128, foo_too_large);
700f4a2713aSLionel Sambuc //
701f4a2713aSLionel Sambuc // The second argument to the macro is the name of the variable. If
702f4a2713aSLionel Sambuc // the expression is false, most compilers will issue a warning/error
703f4a2713aSLionel Sambuc // containing the name of the variable.
704f4a2713aSLionel Sambuc 
705f4a2713aSLionel Sambuc template <bool>
706f4a2713aSLionel Sambuc struct CompileAssert {
707f4a2713aSLionel Sambuc };
708f4a2713aSLionel Sambuc 
709f4a2713aSLionel Sambuc #define GTEST_COMPILE_ASSERT_(expr, msg) \
710f4a2713aSLionel Sambuc   typedef ::testing::internal::CompileAssert<(bool(expr))> \
711f4a2713aSLionel Sambuc       msg[bool(expr) ? 1 : -1]
712f4a2713aSLionel Sambuc 
713f4a2713aSLionel Sambuc // Implementation details of GTEST_COMPILE_ASSERT_:
714f4a2713aSLionel Sambuc //
715f4a2713aSLionel Sambuc // - GTEST_COMPILE_ASSERT_ works by defining an array type that has -1
716f4a2713aSLionel Sambuc //   elements (and thus is invalid) when the expression is false.
717f4a2713aSLionel Sambuc //
718f4a2713aSLionel Sambuc // - The simpler definition
719f4a2713aSLionel Sambuc //
720f4a2713aSLionel Sambuc //    #define GTEST_COMPILE_ASSERT_(expr, msg) typedef char msg[(expr) ? 1 : -1]
721f4a2713aSLionel Sambuc //
722f4a2713aSLionel Sambuc //   does not work, as gcc supports variable-length arrays whose sizes
723f4a2713aSLionel Sambuc //   are determined at run-time (this is gcc's extension and not part
724f4a2713aSLionel Sambuc //   of the C++ standard).  As a result, gcc fails to reject the
725f4a2713aSLionel Sambuc //   following code with the simple definition:
726f4a2713aSLionel Sambuc //
727f4a2713aSLionel Sambuc //     int foo;
728f4a2713aSLionel Sambuc //     GTEST_COMPILE_ASSERT_(foo, msg); // not supposed to compile as foo is
729f4a2713aSLionel Sambuc //                                      // not a compile-time constant.
730f4a2713aSLionel Sambuc //
731f4a2713aSLionel Sambuc // - By using the type CompileAssert<(bool(expr))>, we ensures that
732f4a2713aSLionel Sambuc //   expr is a compile-time constant.  (Template arguments must be
733f4a2713aSLionel Sambuc //   determined at compile-time.)
734f4a2713aSLionel Sambuc //
735f4a2713aSLionel Sambuc // - The outter parentheses in CompileAssert<(bool(expr))> are necessary
736f4a2713aSLionel Sambuc //   to work around a bug in gcc 3.4.4 and 4.0.1.  If we had written
737f4a2713aSLionel Sambuc //
738f4a2713aSLionel Sambuc //     CompileAssert<bool(expr)>
739f4a2713aSLionel Sambuc //
740f4a2713aSLionel Sambuc //   instead, these compilers will refuse to compile
741f4a2713aSLionel Sambuc //
742f4a2713aSLionel Sambuc //     GTEST_COMPILE_ASSERT_(5 > 0, some_message);
743f4a2713aSLionel Sambuc //
744f4a2713aSLionel Sambuc //   (They seem to think the ">" in "5 > 0" marks the end of the
745f4a2713aSLionel Sambuc //   template argument list.)
746f4a2713aSLionel Sambuc //
747f4a2713aSLionel Sambuc // - The array size is (bool(expr) ? 1 : -1), instead of simply
748f4a2713aSLionel Sambuc //
749f4a2713aSLionel Sambuc //     ((expr) ? 1 : -1).
750f4a2713aSLionel Sambuc //
751f4a2713aSLionel Sambuc //   This is to avoid running into a bug in MS VC 7.1, which
752f4a2713aSLionel Sambuc //   causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
753f4a2713aSLionel Sambuc 
754f4a2713aSLionel Sambuc // StaticAssertTypeEqHelper is used by StaticAssertTypeEq defined in gtest.h.
755f4a2713aSLionel Sambuc //
756f4a2713aSLionel Sambuc // This template is declared, but intentionally undefined.
757f4a2713aSLionel Sambuc template <typename T1, typename T2>
758f4a2713aSLionel Sambuc struct StaticAssertTypeEqHelper;
759f4a2713aSLionel Sambuc 
760f4a2713aSLionel Sambuc template <typename T>
761f4a2713aSLionel Sambuc struct StaticAssertTypeEqHelper<T, T> {};
762f4a2713aSLionel Sambuc 
763f4a2713aSLionel Sambuc #if GTEST_HAS_GLOBAL_STRING
764f4a2713aSLionel Sambuc typedef ::string string;
765f4a2713aSLionel Sambuc #else
766f4a2713aSLionel Sambuc typedef ::std::string string;
767f4a2713aSLionel Sambuc #endif  // GTEST_HAS_GLOBAL_STRING
768f4a2713aSLionel Sambuc 
769f4a2713aSLionel Sambuc #if GTEST_HAS_GLOBAL_WSTRING
770f4a2713aSLionel Sambuc typedef ::wstring wstring;
771f4a2713aSLionel Sambuc #elif GTEST_HAS_STD_WSTRING
772f4a2713aSLionel Sambuc typedef ::std::wstring wstring;
773f4a2713aSLionel Sambuc #endif  // GTEST_HAS_GLOBAL_WSTRING
774f4a2713aSLionel Sambuc 
775f4a2713aSLionel Sambuc // A helper for suppressing warnings on constant condition.  It just
776f4a2713aSLionel Sambuc // returns 'condition'.
777f4a2713aSLionel Sambuc GTEST_API_ bool IsTrue(bool condition);
778f4a2713aSLionel Sambuc 
779f4a2713aSLionel Sambuc // Defines scoped_ptr.
780f4a2713aSLionel Sambuc 
781f4a2713aSLionel Sambuc // This implementation of scoped_ptr is PARTIAL - it only contains
782f4a2713aSLionel Sambuc // enough stuff to satisfy Google Test's need.
783f4a2713aSLionel Sambuc template <typename T>
784f4a2713aSLionel Sambuc class scoped_ptr {
785f4a2713aSLionel Sambuc  public:
786f4a2713aSLionel Sambuc   typedef T element_type;
787f4a2713aSLionel Sambuc 
788f4a2713aSLionel Sambuc   explicit scoped_ptr(T* p = NULL) : ptr_(p) {}
789f4a2713aSLionel Sambuc   ~scoped_ptr() { reset(); }
790f4a2713aSLionel Sambuc 
791f4a2713aSLionel Sambuc   T& operator*() const { return *ptr_; }
792f4a2713aSLionel Sambuc   T* operator->() const { return ptr_; }
793f4a2713aSLionel Sambuc   T* get() const { return ptr_; }
794f4a2713aSLionel Sambuc 
795f4a2713aSLionel Sambuc   T* release() {
796f4a2713aSLionel Sambuc     T* const ptr = ptr_;
797f4a2713aSLionel Sambuc     ptr_ = NULL;
798f4a2713aSLionel Sambuc     return ptr;
799f4a2713aSLionel Sambuc   }
800f4a2713aSLionel Sambuc 
801f4a2713aSLionel Sambuc   void reset(T* p = NULL) {
802f4a2713aSLionel Sambuc     if (p != ptr_) {
803f4a2713aSLionel Sambuc       if (IsTrue(sizeof(T) > 0)) {  // Makes sure T is a complete type.
804f4a2713aSLionel Sambuc         delete ptr_;
805f4a2713aSLionel Sambuc       }
806f4a2713aSLionel Sambuc       ptr_ = p;
807f4a2713aSLionel Sambuc     }
808f4a2713aSLionel Sambuc   }
809f4a2713aSLionel Sambuc  private:
810f4a2713aSLionel Sambuc   T* ptr_;
811f4a2713aSLionel Sambuc 
812f4a2713aSLionel Sambuc   GTEST_DISALLOW_COPY_AND_ASSIGN_(scoped_ptr);
813f4a2713aSLionel Sambuc };
814f4a2713aSLionel Sambuc 
815f4a2713aSLionel Sambuc // Defines RE.
816f4a2713aSLionel Sambuc 
817f4a2713aSLionel Sambuc // A simple C++ wrapper for <regex.h>.  It uses the POSIX Extended
818f4a2713aSLionel Sambuc // Regular Expression syntax.
819f4a2713aSLionel Sambuc class GTEST_API_ RE {
820f4a2713aSLionel Sambuc  public:
821f4a2713aSLionel Sambuc   // A copy constructor is required by the Standard to initialize object
822f4a2713aSLionel Sambuc   // references from r-values.
823f4a2713aSLionel Sambuc   RE(const RE& other) { Init(other.pattern()); }
824f4a2713aSLionel Sambuc 
825f4a2713aSLionel Sambuc   // Constructs an RE from a string.
826f4a2713aSLionel Sambuc   RE(const ::std::string& regex) { Init(regex.c_str()); }  // NOLINT
827f4a2713aSLionel Sambuc 
828f4a2713aSLionel Sambuc #if GTEST_HAS_GLOBAL_STRING
829f4a2713aSLionel Sambuc 
830f4a2713aSLionel Sambuc   RE(const ::string& regex) { Init(regex.c_str()); }  // NOLINT
831f4a2713aSLionel Sambuc 
832f4a2713aSLionel Sambuc #endif  // GTEST_HAS_GLOBAL_STRING
833f4a2713aSLionel Sambuc 
834f4a2713aSLionel Sambuc   RE(const char* regex) { Init(regex); }  // NOLINT
835f4a2713aSLionel Sambuc   ~RE();
836f4a2713aSLionel Sambuc 
837f4a2713aSLionel Sambuc   // Returns the string representation of the regex.
838f4a2713aSLionel Sambuc   const char* pattern() const { return pattern_; }
839f4a2713aSLionel Sambuc 
840f4a2713aSLionel Sambuc   // FullMatch(str, re) returns true iff regular expression re matches
841f4a2713aSLionel Sambuc   // the entire str.
842f4a2713aSLionel Sambuc   // PartialMatch(str, re) returns true iff regular expression re
843f4a2713aSLionel Sambuc   // matches a substring of str (including str itself).
844f4a2713aSLionel Sambuc   //
845f4a2713aSLionel Sambuc   // TODO(wan@google.com): make FullMatch() and PartialMatch() work
846f4a2713aSLionel Sambuc   // when str contains NUL characters.
847f4a2713aSLionel Sambuc   static bool FullMatch(const ::std::string& str, const RE& re) {
848f4a2713aSLionel Sambuc     return FullMatch(str.c_str(), re);
849f4a2713aSLionel Sambuc   }
850f4a2713aSLionel Sambuc   static bool PartialMatch(const ::std::string& str, const RE& re) {
851f4a2713aSLionel Sambuc     return PartialMatch(str.c_str(), re);
852f4a2713aSLionel Sambuc   }
853f4a2713aSLionel Sambuc 
854f4a2713aSLionel Sambuc #if GTEST_HAS_GLOBAL_STRING
855f4a2713aSLionel Sambuc 
856f4a2713aSLionel Sambuc   static bool FullMatch(const ::string& str, const RE& re) {
857f4a2713aSLionel Sambuc     return FullMatch(str.c_str(), re);
858f4a2713aSLionel Sambuc   }
859f4a2713aSLionel Sambuc   static bool PartialMatch(const ::string& str, const RE& re) {
860f4a2713aSLionel Sambuc     return PartialMatch(str.c_str(), re);
861f4a2713aSLionel Sambuc   }
862f4a2713aSLionel Sambuc 
863f4a2713aSLionel Sambuc #endif  // GTEST_HAS_GLOBAL_STRING
864f4a2713aSLionel Sambuc 
865f4a2713aSLionel Sambuc   static bool FullMatch(const char* str, const RE& re);
866f4a2713aSLionel Sambuc   static bool PartialMatch(const char* str, const RE& re);
867f4a2713aSLionel Sambuc 
868f4a2713aSLionel Sambuc  private:
869f4a2713aSLionel Sambuc   void Init(const char* regex);
870f4a2713aSLionel Sambuc 
871f4a2713aSLionel Sambuc   // We use a const char* instead of a string, as Google Test may be used
872f4a2713aSLionel Sambuc   // where string is not available.  We also do not use Google Test's own
873f4a2713aSLionel Sambuc   // String type here, in order to simplify dependencies between the
874f4a2713aSLionel Sambuc   // files.
875f4a2713aSLionel Sambuc   const char* pattern_;
876f4a2713aSLionel Sambuc   bool is_valid_;
877f4a2713aSLionel Sambuc 
878f4a2713aSLionel Sambuc #if GTEST_USES_POSIX_RE
879f4a2713aSLionel Sambuc 
880f4a2713aSLionel Sambuc   regex_t full_regex_;     // For FullMatch().
881f4a2713aSLionel Sambuc   regex_t partial_regex_;  // For PartialMatch().
882f4a2713aSLionel Sambuc 
883f4a2713aSLionel Sambuc #else  // GTEST_USES_SIMPLE_RE
884f4a2713aSLionel Sambuc 
885f4a2713aSLionel Sambuc   const char* full_pattern_;  // For FullMatch();
886f4a2713aSLionel Sambuc 
887f4a2713aSLionel Sambuc #endif
888f4a2713aSLionel Sambuc 
889f4a2713aSLionel Sambuc   GTEST_DISALLOW_ASSIGN_(RE);
890f4a2713aSLionel Sambuc };
891f4a2713aSLionel Sambuc 
892f4a2713aSLionel Sambuc // Formats a source file path and a line number as they would appear
893f4a2713aSLionel Sambuc // in an error message from the compiler used to compile this code.
894f4a2713aSLionel Sambuc GTEST_API_ ::std::string FormatFileLocation(const char* file, int line);
895f4a2713aSLionel Sambuc 
896f4a2713aSLionel Sambuc // Formats a file location for compiler-independent XML output.
897f4a2713aSLionel Sambuc // Although this function is not platform dependent, we put it next to
898f4a2713aSLionel Sambuc // FormatFileLocation in order to contrast the two functions.
899f4a2713aSLionel Sambuc GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file,
900f4a2713aSLionel Sambuc                                                                int line);
901f4a2713aSLionel Sambuc 
902f4a2713aSLionel Sambuc // Defines logging utilities:
903f4a2713aSLionel Sambuc //   GTEST_LOG_(severity) - logs messages at the specified severity level. The
904f4a2713aSLionel Sambuc //                          message itself is streamed into the macro.
905f4a2713aSLionel Sambuc //   LogToStderr()  - directs all log messages to stderr.
906f4a2713aSLionel Sambuc //   FlushInfoLog() - flushes informational log messages.
907f4a2713aSLionel Sambuc 
908f4a2713aSLionel Sambuc enum GTestLogSeverity {
909f4a2713aSLionel Sambuc   GTEST_INFO,
910f4a2713aSLionel Sambuc   GTEST_WARNING,
911f4a2713aSLionel Sambuc   GTEST_ERROR,
912f4a2713aSLionel Sambuc   GTEST_FATAL
913f4a2713aSLionel Sambuc };
914f4a2713aSLionel Sambuc 
915f4a2713aSLionel Sambuc // Formats log entry severity, provides a stream object for streaming the
916f4a2713aSLionel Sambuc // log message, and terminates the message with a newline when going out of
917f4a2713aSLionel Sambuc // scope.
918f4a2713aSLionel Sambuc class GTEST_API_ GTestLog {
919f4a2713aSLionel Sambuc  public:
920f4a2713aSLionel Sambuc   GTestLog(GTestLogSeverity severity, const char* file, int line);
921f4a2713aSLionel Sambuc 
922f4a2713aSLionel Sambuc   // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
923f4a2713aSLionel Sambuc   ~GTestLog();
924f4a2713aSLionel Sambuc 
925f4a2713aSLionel Sambuc   ::std::ostream& GetStream() { return ::std::cerr; }
926f4a2713aSLionel Sambuc 
927f4a2713aSLionel Sambuc  private:
928f4a2713aSLionel Sambuc   const GTestLogSeverity severity_;
929f4a2713aSLionel Sambuc 
930f4a2713aSLionel Sambuc   GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog);
931f4a2713aSLionel Sambuc };
932f4a2713aSLionel Sambuc 
933f4a2713aSLionel Sambuc #define GTEST_LOG_(severity) \
934f4a2713aSLionel Sambuc     ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
935f4a2713aSLionel Sambuc                                   __FILE__, __LINE__).GetStream()
936f4a2713aSLionel Sambuc 
937f4a2713aSLionel Sambuc inline void LogToStderr() {}
938f4a2713aSLionel Sambuc inline void FlushInfoLog() { fflush(NULL); }
939f4a2713aSLionel Sambuc 
940f4a2713aSLionel Sambuc // INTERNAL IMPLEMENTATION - DO NOT USE.
941f4a2713aSLionel Sambuc //
942f4a2713aSLionel Sambuc // GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition
943f4a2713aSLionel Sambuc // is not satisfied.
944f4a2713aSLionel Sambuc //  Synopsys:
945f4a2713aSLionel Sambuc //    GTEST_CHECK_(boolean_condition);
946f4a2713aSLionel Sambuc //     or
947f4a2713aSLionel Sambuc //    GTEST_CHECK_(boolean_condition) << "Additional message";
948f4a2713aSLionel Sambuc //
949f4a2713aSLionel Sambuc //    This checks the condition and if the condition is not satisfied
950f4a2713aSLionel Sambuc //    it prints message about the condition violation, including the
951f4a2713aSLionel Sambuc //    condition itself, plus additional message streamed into it, if any,
952f4a2713aSLionel Sambuc //    and then it aborts the program. It aborts the program irrespective of
953f4a2713aSLionel Sambuc //    whether it is built in the debug mode or not.
954f4a2713aSLionel Sambuc #define GTEST_CHECK_(condition) \
955f4a2713aSLionel Sambuc     GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
956f4a2713aSLionel Sambuc     if (::testing::internal::IsTrue(condition)) \
957f4a2713aSLionel Sambuc       ; \
958f4a2713aSLionel Sambuc     else \
959f4a2713aSLionel Sambuc       GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
960f4a2713aSLionel Sambuc 
961f4a2713aSLionel Sambuc // An all-mode assert to verify that the given POSIX-style function
962f4a2713aSLionel Sambuc // call returns 0 (indicating success).  Known limitation: this
963f4a2713aSLionel Sambuc // doesn't expand to a balanced 'if' statement, so enclose the macro
964f4a2713aSLionel Sambuc // in {} if you need to use it as the only statement in an 'if'
965f4a2713aSLionel Sambuc // branch.
966f4a2713aSLionel Sambuc #define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \
967f4a2713aSLionel Sambuc   if (const int gtest_error = (posix_call)) \
968f4a2713aSLionel Sambuc     GTEST_LOG_(FATAL) << #posix_call << "failed with error " \
969f4a2713aSLionel Sambuc                       << gtest_error
970f4a2713aSLionel Sambuc 
971f4a2713aSLionel Sambuc // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
972f4a2713aSLionel Sambuc //
973f4a2713aSLionel Sambuc // Use ImplicitCast_ as a safe version of static_cast for upcasting in
974f4a2713aSLionel Sambuc // the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a
975f4a2713aSLionel Sambuc // const Foo*).  When you use ImplicitCast_, the compiler checks that
976f4a2713aSLionel Sambuc // the cast is safe.  Such explicit ImplicitCast_s are necessary in
977f4a2713aSLionel Sambuc // surprisingly many situations where C++ demands an exact type match
978f4a2713aSLionel Sambuc // instead of an argument type convertable to a target type.
979f4a2713aSLionel Sambuc //
980f4a2713aSLionel Sambuc // The syntax for using ImplicitCast_ is the same as for static_cast:
981f4a2713aSLionel Sambuc //
982f4a2713aSLionel Sambuc //   ImplicitCast_<ToType>(expr)
983f4a2713aSLionel Sambuc //
984f4a2713aSLionel Sambuc // ImplicitCast_ would have been part of the C++ standard library,
985f4a2713aSLionel Sambuc // but the proposal was submitted too late.  It will probably make
986f4a2713aSLionel Sambuc // its way into the language in the future.
987f4a2713aSLionel Sambuc //
988f4a2713aSLionel Sambuc // This relatively ugly name is intentional. It prevents clashes with
989f4a2713aSLionel Sambuc // similar functions users may have (e.g., implicit_cast). The internal
990f4a2713aSLionel Sambuc // namespace alone is not enough because the function can be found by ADL.
991f4a2713aSLionel Sambuc template<typename To>
992f4a2713aSLionel Sambuc inline To ImplicitCast_(To x) { return x; }
993f4a2713aSLionel Sambuc 
994f4a2713aSLionel Sambuc // When you upcast (that is, cast a pointer from type Foo to type
995f4a2713aSLionel Sambuc // SuperclassOfFoo), it's fine to use ImplicitCast_<>, since upcasts
996f4a2713aSLionel Sambuc // always succeed.  When you downcast (that is, cast a pointer from
997f4a2713aSLionel Sambuc // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
998f4a2713aSLionel Sambuc // how do you know the pointer is really of type SubclassOfFoo?  It
999f4a2713aSLionel Sambuc // could be a bare Foo, or of type DifferentSubclassOfFoo.  Thus,
1000f4a2713aSLionel Sambuc // when you downcast, you should use this macro.  In debug mode, we
1001f4a2713aSLionel Sambuc // use dynamic_cast<> to double-check the downcast is legal (we die
1002f4a2713aSLionel Sambuc // if it's not).  In normal mode, we do the efficient static_cast<>
1003f4a2713aSLionel Sambuc // instead.  Thus, it's important to test in debug mode to make sure
1004f4a2713aSLionel Sambuc // the cast is legal!
1005f4a2713aSLionel Sambuc //    This is the only place in the code we should use dynamic_cast<>.
1006f4a2713aSLionel Sambuc // In particular, you SHOULDN'T be using dynamic_cast<> in order to
1007f4a2713aSLionel Sambuc // do RTTI (eg code like this:
1008f4a2713aSLionel Sambuc //    if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
1009f4a2713aSLionel Sambuc //    if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
1010f4a2713aSLionel Sambuc // You should design the code some other way not to need this.
1011f4a2713aSLionel Sambuc //
1012f4a2713aSLionel Sambuc // This relatively ugly name is intentional. It prevents clashes with
1013f4a2713aSLionel Sambuc // similar functions users may have (e.g., down_cast). The internal
1014f4a2713aSLionel Sambuc // namespace alone is not enough because the function can be found by ADL.
1015f4a2713aSLionel Sambuc template<typename To, typename From>  // use like this: DownCast_<T*>(foo);
1016f4a2713aSLionel Sambuc inline To DownCast_(From* f) {  // so we only accept pointers
1017f4a2713aSLionel Sambuc   // Ensures that To is a sub-type of From *.  This test is here only
1018f4a2713aSLionel Sambuc   // for compile-time type checking, and has no overhead in an
1019f4a2713aSLionel Sambuc   // optimized build at run-time, as it will be optimized away
1020f4a2713aSLionel Sambuc   // completely.
1021f4a2713aSLionel Sambuc   if (false) {
1022f4a2713aSLionel Sambuc     const To to = NULL;
1023f4a2713aSLionel Sambuc     ::testing::internal::ImplicitCast_<From*>(to);
1024f4a2713aSLionel Sambuc   }
1025f4a2713aSLionel Sambuc 
1026f4a2713aSLionel Sambuc #if GTEST_HAS_RTTI
1027f4a2713aSLionel Sambuc   // RTTI: debug mode only!
1028f4a2713aSLionel Sambuc   GTEST_CHECK_(f == NULL || dynamic_cast<To>(f) != NULL);
1029f4a2713aSLionel Sambuc #endif
1030f4a2713aSLionel Sambuc   return static_cast<To>(f);
1031f4a2713aSLionel Sambuc }
1032f4a2713aSLionel Sambuc 
1033f4a2713aSLionel Sambuc // Downcasts the pointer of type Base to Derived.
1034f4a2713aSLionel Sambuc // Derived must be a subclass of Base. The parameter MUST
1035f4a2713aSLionel Sambuc // point to a class of type Derived, not any subclass of it.
1036f4a2713aSLionel Sambuc // When RTTI is available, the function performs a runtime
1037f4a2713aSLionel Sambuc // check to enforce this.
1038f4a2713aSLionel Sambuc template <class Derived, class Base>
1039f4a2713aSLionel Sambuc Derived* CheckedDowncastToActualType(Base* base) {
1040f4a2713aSLionel Sambuc #if GTEST_HAS_RTTI
1041f4a2713aSLionel Sambuc   GTEST_CHECK_(typeid(*base) == typeid(Derived));
1042f4a2713aSLionel Sambuc   return dynamic_cast<Derived*>(base);  // NOLINT
1043f4a2713aSLionel Sambuc #else
1044f4a2713aSLionel Sambuc   return static_cast<Derived*>(base);  // Poor man's downcast.
1045f4a2713aSLionel Sambuc #endif
1046f4a2713aSLionel Sambuc }
1047f4a2713aSLionel Sambuc 
1048f4a2713aSLionel Sambuc #if GTEST_HAS_STREAM_REDIRECTION
1049f4a2713aSLionel Sambuc 
1050f4a2713aSLionel Sambuc // Defines the stderr capturer:
1051f4a2713aSLionel Sambuc //   CaptureStdout     - starts capturing stdout.
1052f4a2713aSLionel Sambuc //   GetCapturedStdout - stops capturing stdout and returns the captured string.
1053f4a2713aSLionel Sambuc //   CaptureStderr     - starts capturing stderr.
1054f4a2713aSLionel Sambuc //   GetCapturedStderr - stops capturing stderr and returns the captured string.
1055f4a2713aSLionel Sambuc //
1056f4a2713aSLionel Sambuc GTEST_API_ void CaptureStdout();
1057f4a2713aSLionel Sambuc GTEST_API_ String GetCapturedStdout();
1058f4a2713aSLionel Sambuc GTEST_API_ void CaptureStderr();
1059f4a2713aSLionel Sambuc GTEST_API_ String GetCapturedStderr();
1060f4a2713aSLionel Sambuc 
1061f4a2713aSLionel Sambuc #endif  // GTEST_HAS_STREAM_REDIRECTION
1062f4a2713aSLionel Sambuc 
1063f4a2713aSLionel Sambuc 
1064f4a2713aSLionel Sambuc #if GTEST_HAS_DEATH_TEST
1065f4a2713aSLionel Sambuc 
1066f4a2713aSLionel Sambuc // A copy of all command line arguments.  Set by InitGoogleTest().
1067f4a2713aSLionel Sambuc extern ::std::vector<String> g_argvs;
1068f4a2713aSLionel Sambuc 
1069f4a2713aSLionel Sambuc // GTEST_HAS_DEATH_TEST implies we have ::std::string.
1070f4a2713aSLionel Sambuc const ::std::vector<String>& GetArgvs();
1071f4a2713aSLionel Sambuc 
1072f4a2713aSLionel Sambuc #endif  // GTEST_HAS_DEATH_TEST
1073f4a2713aSLionel Sambuc 
1074f4a2713aSLionel Sambuc // Defines synchronization primitives.
1075f4a2713aSLionel Sambuc 
1076f4a2713aSLionel Sambuc #if GTEST_HAS_PTHREAD
1077f4a2713aSLionel Sambuc 
1078f4a2713aSLionel Sambuc // Sleeps for (roughly) n milli-seconds.  This function is only for
1079f4a2713aSLionel Sambuc // testing Google Test's own constructs.  Don't use it in user tests,
1080f4a2713aSLionel Sambuc // either directly or indirectly.
1081f4a2713aSLionel Sambuc inline void SleepMilliseconds(int n) {
1082f4a2713aSLionel Sambuc   const timespec time = {
1083f4a2713aSLionel Sambuc     0,                  // 0 seconds.
1084f4a2713aSLionel Sambuc     n * 1000L * 1000L,  // And n ms.
1085f4a2713aSLionel Sambuc   };
1086f4a2713aSLionel Sambuc   nanosleep(&time, NULL);
1087f4a2713aSLionel Sambuc }
1088f4a2713aSLionel Sambuc 
1089f4a2713aSLionel Sambuc // Allows a controller thread to pause execution of newly created
1090f4a2713aSLionel Sambuc // threads until notified.  Instances of this class must be created
1091f4a2713aSLionel Sambuc // and destroyed in the controller thread.
1092f4a2713aSLionel Sambuc //
1093f4a2713aSLionel Sambuc // This class is only for testing Google Test's own constructs. Do not
1094f4a2713aSLionel Sambuc // use it in user tests, either directly or indirectly.
1095f4a2713aSLionel Sambuc class Notification {
1096f4a2713aSLionel Sambuc  public:
1097f4a2713aSLionel Sambuc   Notification() : notified_(false) {}
1098f4a2713aSLionel Sambuc 
1099f4a2713aSLionel Sambuc   // Notifies all threads created with this notification to start. Must
1100f4a2713aSLionel Sambuc   // be called from the controller thread.
1101f4a2713aSLionel Sambuc   void Notify() { notified_ = true; }
1102f4a2713aSLionel Sambuc 
1103f4a2713aSLionel Sambuc   // Blocks until the controller thread notifies. Must be called from a test
1104f4a2713aSLionel Sambuc   // thread.
1105f4a2713aSLionel Sambuc   void WaitForNotification() {
1106f4a2713aSLionel Sambuc     while(!notified_) {
1107f4a2713aSLionel Sambuc       SleepMilliseconds(10);
1108f4a2713aSLionel Sambuc     }
1109f4a2713aSLionel Sambuc   }
1110f4a2713aSLionel Sambuc 
1111f4a2713aSLionel Sambuc  private:
1112f4a2713aSLionel Sambuc   volatile bool notified_;
1113f4a2713aSLionel Sambuc 
1114f4a2713aSLionel Sambuc   GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
1115f4a2713aSLionel Sambuc };
1116f4a2713aSLionel Sambuc 
1117f4a2713aSLionel Sambuc // As a C-function, ThreadFuncWithCLinkage cannot be templated itself.
1118f4a2713aSLionel Sambuc // Consequently, it cannot select a correct instantiation of ThreadWithParam
1119f4a2713aSLionel Sambuc // in order to call its Run(). Introducing ThreadWithParamBase as a
1120f4a2713aSLionel Sambuc // non-templated base class for ThreadWithParam allows us to bypass this
1121f4a2713aSLionel Sambuc // problem.
1122f4a2713aSLionel Sambuc class ThreadWithParamBase {
1123f4a2713aSLionel Sambuc  public:
1124f4a2713aSLionel Sambuc   virtual ~ThreadWithParamBase();
1125f4a2713aSLionel Sambuc   virtual void Run() = 0;
1126f4a2713aSLionel Sambuc };
1127f4a2713aSLionel Sambuc 
1128f4a2713aSLionel Sambuc // pthread_create() accepts a pointer to a function type with the C linkage.
1129f4a2713aSLionel Sambuc // According to the Standard (7.5/1), function types with different linkages
1130f4a2713aSLionel Sambuc // are different even if they are otherwise identical.  Some compilers (for
1131f4a2713aSLionel Sambuc // example, SunStudio) treat them as different types.  Since class methods
1132f4a2713aSLionel Sambuc // cannot be defined with C-linkage we need to define a free C-function to
1133f4a2713aSLionel Sambuc // pass into pthread_create().
1134f4a2713aSLionel Sambuc extern "C" inline void* ThreadFuncWithCLinkage(void* thread) {
1135f4a2713aSLionel Sambuc   static_cast<ThreadWithParamBase*>(thread)->Run();
1136f4a2713aSLionel Sambuc   return NULL;
1137f4a2713aSLionel Sambuc }
1138f4a2713aSLionel Sambuc 
1139f4a2713aSLionel Sambuc // Helper class for testing Google Test's multi-threading constructs.
1140f4a2713aSLionel Sambuc // To use it, write:
1141f4a2713aSLionel Sambuc //
1142f4a2713aSLionel Sambuc //   void ThreadFunc(int param) { /* Do things with param */ }
1143f4a2713aSLionel Sambuc //   Notification thread_can_start;
1144f4a2713aSLionel Sambuc //   ...
1145f4a2713aSLionel Sambuc //   // The thread_can_start parameter is optional; you can supply NULL.
1146f4a2713aSLionel Sambuc //   ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start);
1147f4a2713aSLionel Sambuc //   thread_can_start.Notify();
1148f4a2713aSLionel Sambuc //
1149f4a2713aSLionel Sambuc // These classes are only for testing Google Test's own constructs. Do
1150f4a2713aSLionel Sambuc // not use them in user tests, either directly or indirectly.
1151f4a2713aSLionel Sambuc template <typename T>
1152f4a2713aSLionel Sambuc class ThreadWithParam : public ThreadWithParamBase {
1153f4a2713aSLionel Sambuc  public:
1154f4a2713aSLionel Sambuc   typedef void (*UserThreadFunc)(T);
1155f4a2713aSLionel Sambuc 
1156f4a2713aSLionel Sambuc   ThreadWithParam(
1157f4a2713aSLionel Sambuc       UserThreadFunc func, T param, Notification* thread_can_start)
1158f4a2713aSLionel Sambuc       : func_(func),
1159f4a2713aSLionel Sambuc         param_(param),
1160f4a2713aSLionel Sambuc         thread_can_start_(thread_can_start),
1161f4a2713aSLionel Sambuc         finished_(false) {
1162f4a2713aSLionel Sambuc     ThreadWithParamBase* const base = this;
1163f4a2713aSLionel Sambuc     // The thread can be created only after all fields except thread_
1164f4a2713aSLionel Sambuc     // have been initialized.
1165f4a2713aSLionel Sambuc     GTEST_CHECK_POSIX_SUCCESS_(
1166f4a2713aSLionel Sambuc         pthread_create(&thread_, 0, &ThreadFuncWithCLinkage, base));
1167f4a2713aSLionel Sambuc   }
1168f4a2713aSLionel Sambuc   ~ThreadWithParam() { Join(); }
1169f4a2713aSLionel Sambuc 
1170f4a2713aSLionel Sambuc   void Join() {
1171f4a2713aSLionel Sambuc     if (!finished_) {
1172f4a2713aSLionel Sambuc       GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, 0));
1173f4a2713aSLionel Sambuc       finished_ = true;
1174f4a2713aSLionel Sambuc     }
1175f4a2713aSLionel Sambuc   }
1176f4a2713aSLionel Sambuc 
1177f4a2713aSLionel Sambuc   virtual void Run() {
1178f4a2713aSLionel Sambuc     if (thread_can_start_ != NULL)
1179f4a2713aSLionel Sambuc       thread_can_start_->WaitForNotification();
1180f4a2713aSLionel Sambuc     func_(param_);
1181f4a2713aSLionel Sambuc   }
1182f4a2713aSLionel Sambuc 
1183f4a2713aSLionel Sambuc  private:
1184f4a2713aSLionel Sambuc   const UserThreadFunc func_;  // User-supplied thread function.
1185f4a2713aSLionel Sambuc   const T param_;  // User-supplied parameter to the thread function.
1186f4a2713aSLionel Sambuc   // When non-NULL, used to block execution until the controller thread
1187f4a2713aSLionel Sambuc   // notifies.
1188f4a2713aSLionel Sambuc   Notification* const thread_can_start_;
1189f4a2713aSLionel Sambuc   bool finished_;  // true iff we know that the thread function has finished.
1190f4a2713aSLionel Sambuc   pthread_t thread_;  // The native thread object.
1191f4a2713aSLionel Sambuc 
1192f4a2713aSLionel Sambuc   GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam);
1193f4a2713aSLionel Sambuc };
1194f4a2713aSLionel Sambuc 
1195f4a2713aSLionel Sambuc // MutexBase and Mutex implement mutex on pthreads-based platforms. They
1196f4a2713aSLionel Sambuc // are used in conjunction with class MutexLock:
1197f4a2713aSLionel Sambuc //
1198f4a2713aSLionel Sambuc //   Mutex mutex;
1199f4a2713aSLionel Sambuc //   ...
1200f4a2713aSLionel Sambuc //   MutexLock lock(&mutex);  // Acquires the mutex and releases it at the end
1201f4a2713aSLionel Sambuc //                            // of the current scope.
1202f4a2713aSLionel Sambuc //
1203f4a2713aSLionel Sambuc // MutexBase implements behavior for both statically and dynamically
1204f4a2713aSLionel Sambuc // allocated mutexes.  Do not use MutexBase directly.  Instead, write
1205f4a2713aSLionel Sambuc // the following to define a static mutex:
1206f4a2713aSLionel Sambuc //
1207f4a2713aSLionel Sambuc //   GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex);
1208f4a2713aSLionel Sambuc //
1209f4a2713aSLionel Sambuc // You can forward declare a static mutex like this:
1210f4a2713aSLionel Sambuc //
1211f4a2713aSLionel Sambuc //   GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex);
1212f4a2713aSLionel Sambuc //
1213f4a2713aSLionel Sambuc // To create a dynamic mutex, just define an object of type Mutex.
1214f4a2713aSLionel Sambuc class MutexBase {
1215f4a2713aSLionel Sambuc  public:
1216f4a2713aSLionel Sambuc   // Acquires this mutex.
1217f4a2713aSLionel Sambuc   void Lock() {
1218f4a2713aSLionel Sambuc     GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_));
1219f4a2713aSLionel Sambuc     owner_ = pthread_self();
1220f4a2713aSLionel Sambuc   }
1221f4a2713aSLionel Sambuc 
1222f4a2713aSLionel Sambuc   // Releases this mutex.
1223f4a2713aSLionel Sambuc   void Unlock() {
1224f4a2713aSLionel Sambuc     // We don't protect writing to owner_ here, as it's the caller's
1225f4a2713aSLionel Sambuc     // responsibility to ensure that the current thread holds the
1226f4a2713aSLionel Sambuc     // mutex when this is called.
1227f4a2713aSLionel Sambuc     owner_ = 0;
1228f4a2713aSLionel Sambuc     GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_));
1229f4a2713aSLionel Sambuc   }
1230f4a2713aSLionel Sambuc 
1231f4a2713aSLionel Sambuc   // Does nothing if the current thread holds the mutex. Otherwise, crashes
1232f4a2713aSLionel Sambuc   // with high probability.
1233f4a2713aSLionel Sambuc   void AssertHeld() const {
1234f4a2713aSLionel Sambuc     GTEST_CHECK_(owner_ == pthread_self())
1235f4a2713aSLionel Sambuc         << "The current thread is not holding the mutex @" << this;
1236f4a2713aSLionel Sambuc   }
1237f4a2713aSLionel Sambuc 
1238f4a2713aSLionel Sambuc   // A static mutex may be used before main() is entered.  It may even
1239f4a2713aSLionel Sambuc   // be used before the dynamic initialization stage.  Therefore we
1240f4a2713aSLionel Sambuc   // must be able to initialize a static mutex object at link time.
1241f4a2713aSLionel Sambuc   // This means MutexBase has to be a POD and its member variables
1242f4a2713aSLionel Sambuc   // have to be public.
1243f4a2713aSLionel Sambuc  public:
1244f4a2713aSLionel Sambuc   pthread_mutex_t mutex_;  // The underlying pthread mutex.
1245f4a2713aSLionel Sambuc   pthread_t owner_;  // The thread holding the mutex; 0 means no one holds it.
1246f4a2713aSLionel Sambuc };
1247f4a2713aSLionel Sambuc 
1248f4a2713aSLionel Sambuc // Forward-declares a static mutex.
1249f4a2713aSLionel Sambuc # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1250f4a2713aSLionel Sambuc     extern ::testing::internal::MutexBase mutex
1251f4a2713aSLionel Sambuc 
1252f4a2713aSLionel Sambuc // Defines and statically (i.e. at link time) initializes a static mutex.
1253f4a2713aSLionel Sambuc # define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1254f4a2713aSLionel Sambuc     ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, 0 }
1255f4a2713aSLionel Sambuc 
1256f4a2713aSLionel Sambuc // The Mutex class can only be used for mutexes created at runtime. It
1257f4a2713aSLionel Sambuc // shares its API with MutexBase otherwise.
1258f4a2713aSLionel Sambuc class Mutex : public MutexBase {
1259f4a2713aSLionel Sambuc  public:
1260f4a2713aSLionel Sambuc   Mutex() {
1261f4a2713aSLionel Sambuc     GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));
1262f4a2713aSLionel Sambuc     owner_ = 0;
1263f4a2713aSLionel Sambuc   }
1264f4a2713aSLionel Sambuc   ~Mutex() {
1265f4a2713aSLionel Sambuc     GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_));
1266f4a2713aSLionel Sambuc   }
1267f4a2713aSLionel Sambuc 
1268f4a2713aSLionel Sambuc  private:
1269f4a2713aSLionel Sambuc   GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex);
1270f4a2713aSLionel Sambuc };
1271f4a2713aSLionel Sambuc 
1272f4a2713aSLionel Sambuc // We cannot name this class MutexLock as the ctor declaration would
1273f4a2713aSLionel Sambuc // conflict with a macro named MutexLock, which is defined on some
1274f4a2713aSLionel Sambuc // platforms.  Hence the typedef trick below.
1275f4a2713aSLionel Sambuc class GTestMutexLock {
1276f4a2713aSLionel Sambuc  public:
1277f4a2713aSLionel Sambuc   explicit GTestMutexLock(MutexBase* mutex)
1278f4a2713aSLionel Sambuc       : mutex_(mutex) { mutex_->Lock(); }
1279f4a2713aSLionel Sambuc 
1280f4a2713aSLionel Sambuc   ~GTestMutexLock() { mutex_->Unlock(); }
1281f4a2713aSLionel Sambuc 
1282f4a2713aSLionel Sambuc  private:
1283f4a2713aSLionel Sambuc   MutexBase* const mutex_;
1284f4a2713aSLionel Sambuc 
1285f4a2713aSLionel Sambuc   GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock);
1286f4a2713aSLionel Sambuc };
1287f4a2713aSLionel Sambuc 
1288f4a2713aSLionel Sambuc typedef GTestMutexLock MutexLock;
1289f4a2713aSLionel Sambuc 
1290f4a2713aSLionel Sambuc // Helpers for ThreadLocal.
1291f4a2713aSLionel Sambuc 
1292f4a2713aSLionel Sambuc // pthread_key_create() requires DeleteThreadLocalValue() to have
1293f4a2713aSLionel Sambuc // C-linkage.  Therefore it cannot be templatized to access
1294f4a2713aSLionel Sambuc // ThreadLocal<T>.  Hence the need for class
1295f4a2713aSLionel Sambuc // ThreadLocalValueHolderBase.
1296f4a2713aSLionel Sambuc class ThreadLocalValueHolderBase {
1297f4a2713aSLionel Sambuc  public:
1298f4a2713aSLionel Sambuc   virtual ~ThreadLocalValueHolderBase();
1299f4a2713aSLionel Sambuc };
1300f4a2713aSLionel Sambuc 
1301f4a2713aSLionel Sambuc // Called by pthread to delete thread-local data stored by
1302f4a2713aSLionel Sambuc // pthread_setspecific().
1303f4a2713aSLionel Sambuc extern "C" inline void DeleteThreadLocalValue(void* value_holder) {
1304f4a2713aSLionel Sambuc   delete static_cast<ThreadLocalValueHolderBase*>(value_holder);
1305f4a2713aSLionel Sambuc }
1306f4a2713aSLionel Sambuc 
1307f4a2713aSLionel Sambuc // Implements thread-local storage on pthreads-based systems.
1308f4a2713aSLionel Sambuc //
1309f4a2713aSLionel Sambuc //   // Thread 1
1310f4a2713aSLionel Sambuc //   ThreadLocal<int> tl(100);  // 100 is the default value for each thread.
1311f4a2713aSLionel Sambuc //
1312f4a2713aSLionel Sambuc //   // Thread 2
1313f4a2713aSLionel Sambuc //   tl.set(150);  // Changes the value for thread 2 only.
1314f4a2713aSLionel Sambuc //   EXPECT_EQ(150, tl.get());
1315f4a2713aSLionel Sambuc //
1316f4a2713aSLionel Sambuc //   // Thread 1
1317f4a2713aSLionel Sambuc //   EXPECT_EQ(100, tl.get());  // In thread 1, tl has the original value.
1318f4a2713aSLionel Sambuc //   tl.set(200);
1319f4a2713aSLionel Sambuc //   EXPECT_EQ(200, tl.get());
1320f4a2713aSLionel Sambuc //
1321f4a2713aSLionel Sambuc // The template type argument T must have a public copy constructor.
1322f4a2713aSLionel Sambuc // In addition, the default ThreadLocal constructor requires T to have
1323f4a2713aSLionel Sambuc // a public default constructor.
1324f4a2713aSLionel Sambuc //
1325f4a2713aSLionel Sambuc // An object managed for a thread by a ThreadLocal instance is deleted
1326f4a2713aSLionel Sambuc // when the thread exits.  Or, if the ThreadLocal instance dies in
1327f4a2713aSLionel Sambuc // that thread, when the ThreadLocal dies.  It's the user's
1328f4a2713aSLionel Sambuc // responsibility to ensure that all other threads using a ThreadLocal
1329f4a2713aSLionel Sambuc // have exited when it dies, or the per-thread objects for those
1330f4a2713aSLionel Sambuc // threads will not be deleted.
1331f4a2713aSLionel Sambuc //
1332f4a2713aSLionel Sambuc // Google Test only uses global ThreadLocal objects.  That means they
1333f4a2713aSLionel Sambuc // will die after main() has returned.  Therefore, no per-thread
1334f4a2713aSLionel Sambuc // object managed by Google Test will be leaked as long as all threads
1335f4a2713aSLionel Sambuc // using Google Test have exited when main() returns.
1336f4a2713aSLionel Sambuc template <typename T>
1337f4a2713aSLionel Sambuc class ThreadLocal {
1338f4a2713aSLionel Sambuc  public:
1339f4a2713aSLionel Sambuc   ThreadLocal() : key_(CreateKey()),
1340f4a2713aSLionel Sambuc                   default_() {}
1341f4a2713aSLionel Sambuc   explicit ThreadLocal(const T& value) : key_(CreateKey()),
1342f4a2713aSLionel Sambuc                                          default_(value) {}
1343f4a2713aSLionel Sambuc 
1344f4a2713aSLionel Sambuc   ~ThreadLocal() {
1345f4a2713aSLionel Sambuc     // Destroys the managed object for the current thread, if any.
1346f4a2713aSLionel Sambuc     DeleteThreadLocalValue(pthread_getspecific(key_));
1347f4a2713aSLionel Sambuc 
1348f4a2713aSLionel Sambuc     // Releases resources associated with the key.  This will *not*
1349f4a2713aSLionel Sambuc     // delete managed objects for other threads.
1350f4a2713aSLionel Sambuc     GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_));
1351f4a2713aSLionel Sambuc   }
1352f4a2713aSLionel Sambuc 
1353f4a2713aSLionel Sambuc   T* pointer() { return GetOrCreateValue(); }
1354f4a2713aSLionel Sambuc   const T* pointer() const { return GetOrCreateValue(); }
1355f4a2713aSLionel Sambuc   const T& get() const { return *pointer(); }
1356f4a2713aSLionel Sambuc   void set(const T& value) { *pointer() = value; }
1357f4a2713aSLionel Sambuc 
1358f4a2713aSLionel Sambuc  private:
1359f4a2713aSLionel Sambuc   // Holds a value of type T.
1360f4a2713aSLionel Sambuc   class ValueHolder : public ThreadLocalValueHolderBase {
1361f4a2713aSLionel Sambuc    public:
1362f4a2713aSLionel Sambuc     explicit ValueHolder(const T& value) : value_(value) {}
1363f4a2713aSLionel Sambuc 
1364f4a2713aSLionel Sambuc     T* pointer() { return &value_; }
1365f4a2713aSLionel Sambuc 
1366f4a2713aSLionel Sambuc    private:
1367f4a2713aSLionel Sambuc     T value_;
1368f4a2713aSLionel Sambuc     GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder);
1369f4a2713aSLionel Sambuc   };
1370f4a2713aSLionel Sambuc 
1371f4a2713aSLionel Sambuc   static pthread_key_t CreateKey() {
1372f4a2713aSLionel Sambuc     pthread_key_t key;
1373f4a2713aSLionel Sambuc     // When a thread exits, DeleteThreadLocalValue() will be called on
1374f4a2713aSLionel Sambuc     // the object managed for that thread.
1375f4a2713aSLionel Sambuc     GTEST_CHECK_POSIX_SUCCESS_(
1376f4a2713aSLionel Sambuc         pthread_key_create(&key, &DeleteThreadLocalValue));
1377f4a2713aSLionel Sambuc     return key;
1378f4a2713aSLionel Sambuc   }
1379f4a2713aSLionel Sambuc 
1380f4a2713aSLionel Sambuc   T* GetOrCreateValue() const {
1381f4a2713aSLionel Sambuc     ThreadLocalValueHolderBase* const holder =
1382f4a2713aSLionel Sambuc         static_cast<ThreadLocalValueHolderBase*>(pthread_getspecific(key_));
1383f4a2713aSLionel Sambuc     if (holder != NULL) {
1384f4a2713aSLionel Sambuc       return CheckedDowncastToActualType<ValueHolder>(holder)->pointer();
1385f4a2713aSLionel Sambuc     }
1386f4a2713aSLionel Sambuc 
1387f4a2713aSLionel Sambuc     ValueHolder* const new_holder = new ValueHolder(default_);
1388f4a2713aSLionel Sambuc     ThreadLocalValueHolderBase* const holder_base = new_holder;
1389f4a2713aSLionel Sambuc     GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base));
1390f4a2713aSLionel Sambuc     return new_holder->pointer();
1391f4a2713aSLionel Sambuc   }
1392f4a2713aSLionel Sambuc 
1393f4a2713aSLionel Sambuc   // A key pthreads uses for looking up per-thread values.
1394f4a2713aSLionel Sambuc   const pthread_key_t key_;
1395f4a2713aSLionel Sambuc   const T default_;  // The default value for each thread.
1396f4a2713aSLionel Sambuc 
1397f4a2713aSLionel Sambuc   GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal);
1398f4a2713aSLionel Sambuc };
1399f4a2713aSLionel Sambuc 
1400f4a2713aSLionel Sambuc # define GTEST_IS_THREADSAFE 1
1401f4a2713aSLionel Sambuc 
1402f4a2713aSLionel Sambuc #else  // GTEST_HAS_PTHREAD
1403f4a2713aSLionel Sambuc 
1404f4a2713aSLionel Sambuc // A dummy implementation of synchronization primitives (mutex, lock,
1405f4a2713aSLionel Sambuc // and thread-local variable).  Necessary for compiling Google Test where
1406f4a2713aSLionel Sambuc // mutex is not supported - using Google Test in multiple threads is not
1407f4a2713aSLionel Sambuc // supported on such platforms.
1408f4a2713aSLionel Sambuc 
1409f4a2713aSLionel Sambuc class Mutex {
1410f4a2713aSLionel Sambuc  public:
1411f4a2713aSLionel Sambuc   Mutex() {}
1412f4a2713aSLionel Sambuc   void AssertHeld() const {}
1413f4a2713aSLionel Sambuc };
1414f4a2713aSLionel Sambuc 
1415f4a2713aSLionel Sambuc # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1416f4a2713aSLionel Sambuc   extern ::testing::internal::Mutex mutex
1417f4a2713aSLionel Sambuc 
1418f4a2713aSLionel Sambuc # define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex
1419f4a2713aSLionel Sambuc 
1420f4a2713aSLionel Sambuc class GTestMutexLock {
1421f4a2713aSLionel Sambuc  public:
1422f4a2713aSLionel Sambuc   explicit GTestMutexLock(Mutex*) {}  // NOLINT
1423f4a2713aSLionel Sambuc };
1424f4a2713aSLionel Sambuc 
1425f4a2713aSLionel Sambuc typedef GTestMutexLock MutexLock;
1426f4a2713aSLionel Sambuc 
1427f4a2713aSLionel Sambuc template <typename T>
1428f4a2713aSLionel Sambuc class ThreadLocal {
1429f4a2713aSLionel Sambuc  public:
1430f4a2713aSLionel Sambuc   ThreadLocal() : value_() {}
1431f4a2713aSLionel Sambuc   explicit ThreadLocal(const T& value) : value_(value) {}
1432f4a2713aSLionel Sambuc   T* pointer() { return &value_; }
1433f4a2713aSLionel Sambuc   const T* pointer() const { return &value_; }
1434f4a2713aSLionel Sambuc   const T& get() const { return value_; }
1435f4a2713aSLionel Sambuc   void set(const T& value) { value_ = value; }
1436f4a2713aSLionel Sambuc  private:
1437f4a2713aSLionel Sambuc   T value_;
1438f4a2713aSLionel Sambuc };
1439f4a2713aSLionel Sambuc 
1440f4a2713aSLionel Sambuc // The above synchronization primitives have dummy implementations.
1441f4a2713aSLionel Sambuc // Therefore Google Test is not thread-safe.
1442f4a2713aSLionel Sambuc # define GTEST_IS_THREADSAFE 0
1443f4a2713aSLionel Sambuc 
1444f4a2713aSLionel Sambuc #endif  // GTEST_HAS_PTHREAD
1445f4a2713aSLionel Sambuc 
1446f4a2713aSLionel Sambuc // Returns the number of threads running in the process, or 0 to indicate that
1447f4a2713aSLionel Sambuc // we cannot detect it.
1448f4a2713aSLionel Sambuc GTEST_API_ size_t GetThreadCount();
1449f4a2713aSLionel Sambuc 
1450f4a2713aSLionel Sambuc // Passing non-POD classes through ellipsis (...) crashes the ARM
1451f4a2713aSLionel Sambuc // compiler and generates a warning in Sun Studio.  The Nokia Symbian
1452f4a2713aSLionel Sambuc // and the IBM XL C/C++ compiler try to instantiate a copy constructor
1453f4a2713aSLionel Sambuc // for objects passed through ellipsis (...), failing for uncopyable
1454f4a2713aSLionel Sambuc // objects.  We define this to ensure that only POD is passed through
1455f4a2713aSLionel Sambuc // ellipsis on these systems.
1456f4a2713aSLionel Sambuc #if defined(__SYMBIAN32__) || defined(__IBMCPP__) || defined(__SUNPRO_CC)
1457f4a2713aSLionel Sambuc // We lose support for NULL detection where the compiler doesn't like
1458f4a2713aSLionel Sambuc // passing non-POD classes through ellipsis (...).
1459f4a2713aSLionel Sambuc # define GTEST_ELLIPSIS_NEEDS_POD_ 1
1460f4a2713aSLionel Sambuc #else
1461f4a2713aSLionel Sambuc # define GTEST_CAN_COMPARE_NULL 1
1462f4a2713aSLionel Sambuc #endif
1463f4a2713aSLionel Sambuc 
1464f4a2713aSLionel Sambuc // The Nokia Symbian and IBM XL C/C++ compilers cannot decide between
1465f4a2713aSLionel Sambuc // const T& and const T* in a function template.  These compilers
1466f4a2713aSLionel Sambuc // _can_ decide between class template specializations for T and T*,
1467f4a2713aSLionel Sambuc // so a tr1::type_traits-like is_pointer works.
1468f4a2713aSLionel Sambuc #if defined(__SYMBIAN32__) || defined(__IBMCPP__)
1469f4a2713aSLionel Sambuc # define GTEST_NEEDS_IS_POINTER_ 1
1470f4a2713aSLionel Sambuc #endif
1471f4a2713aSLionel Sambuc 
1472f4a2713aSLionel Sambuc template <bool bool_value>
1473f4a2713aSLionel Sambuc struct bool_constant {
1474f4a2713aSLionel Sambuc   typedef bool_constant<bool_value> type;
1475f4a2713aSLionel Sambuc   static const bool value = bool_value;
1476f4a2713aSLionel Sambuc };
1477f4a2713aSLionel Sambuc template <bool bool_value> const bool bool_constant<bool_value>::value;
1478f4a2713aSLionel Sambuc 
1479f4a2713aSLionel Sambuc typedef bool_constant<false> false_type;
1480f4a2713aSLionel Sambuc typedef bool_constant<true> true_type;
1481f4a2713aSLionel Sambuc 
1482f4a2713aSLionel Sambuc template <typename T>
1483f4a2713aSLionel Sambuc struct is_pointer : public false_type {};
1484f4a2713aSLionel Sambuc 
1485f4a2713aSLionel Sambuc template <typename T>
1486f4a2713aSLionel Sambuc struct is_pointer<T*> : public true_type {};
1487f4a2713aSLionel Sambuc 
1488f4a2713aSLionel Sambuc template <typename Iterator>
1489f4a2713aSLionel Sambuc struct IteratorTraits {
1490f4a2713aSLionel Sambuc   typedef typename Iterator::value_type value_type;
1491f4a2713aSLionel Sambuc };
1492f4a2713aSLionel Sambuc 
1493f4a2713aSLionel Sambuc template <typename T>
1494f4a2713aSLionel Sambuc struct IteratorTraits<T*> {
1495f4a2713aSLionel Sambuc   typedef T value_type;
1496f4a2713aSLionel Sambuc };
1497f4a2713aSLionel Sambuc 
1498f4a2713aSLionel Sambuc template <typename T>
1499f4a2713aSLionel Sambuc struct IteratorTraits<const T*> {
1500f4a2713aSLionel Sambuc   typedef T value_type;
1501f4a2713aSLionel Sambuc };
1502f4a2713aSLionel Sambuc 
1503f4a2713aSLionel Sambuc #if GTEST_OS_WINDOWS
1504f4a2713aSLionel Sambuc # define GTEST_PATH_SEP_ "\\"
1505f4a2713aSLionel Sambuc # define GTEST_HAS_ALT_PATH_SEP_ 1
1506f4a2713aSLionel Sambuc // The biggest signed integer type the compiler supports.
1507f4a2713aSLionel Sambuc typedef __int64 BiggestInt;
1508f4a2713aSLionel Sambuc #else
1509f4a2713aSLionel Sambuc # define GTEST_PATH_SEP_ "/"
1510f4a2713aSLionel Sambuc # define GTEST_HAS_ALT_PATH_SEP_ 0
1511f4a2713aSLionel Sambuc typedef long long BiggestInt;  // NOLINT
1512f4a2713aSLionel Sambuc #endif  // GTEST_OS_WINDOWS
1513f4a2713aSLionel Sambuc 
1514f4a2713aSLionel Sambuc // Utilities for char.
1515f4a2713aSLionel Sambuc 
1516f4a2713aSLionel Sambuc // isspace(int ch) and friends accept an unsigned char or EOF.  char
1517f4a2713aSLionel Sambuc // may be signed, depending on the compiler (or compiler flags).
1518f4a2713aSLionel Sambuc // Therefore we need to cast a char to unsigned char before calling
1519f4a2713aSLionel Sambuc // isspace(), etc.
1520f4a2713aSLionel Sambuc 
1521f4a2713aSLionel Sambuc inline bool IsAlpha(char ch) {
1522f4a2713aSLionel Sambuc   return isalpha(static_cast<unsigned char>(ch)) != 0;
1523f4a2713aSLionel Sambuc }
1524f4a2713aSLionel Sambuc inline bool IsAlNum(char ch) {
1525f4a2713aSLionel Sambuc   return isalnum(static_cast<unsigned char>(ch)) != 0;
1526f4a2713aSLionel Sambuc }
1527f4a2713aSLionel Sambuc inline bool IsDigit(char ch) {
1528f4a2713aSLionel Sambuc   return isdigit(static_cast<unsigned char>(ch)) != 0;
1529f4a2713aSLionel Sambuc }
1530f4a2713aSLionel Sambuc inline bool IsLower(char ch) {
1531f4a2713aSLionel Sambuc   return islower(static_cast<unsigned char>(ch)) != 0;
1532f4a2713aSLionel Sambuc }
1533f4a2713aSLionel Sambuc inline bool IsSpace(char ch) {
1534f4a2713aSLionel Sambuc   return isspace(static_cast<unsigned char>(ch)) != 0;
1535f4a2713aSLionel Sambuc }
1536f4a2713aSLionel Sambuc inline bool IsUpper(char ch) {
1537f4a2713aSLionel Sambuc   return isupper(static_cast<unsigned char>(ch)) != 0;
1538f4a2713aSLionel Sambuc }
1539f4a2713aSLionel Sambuc inline bool IsXDigit(char ch) {
1540f4a2713aSLionel Sambuc   return isxdigit(static_cast<unsigned char>(ch)) != 0;
1541f4a2713aSLionel Sambuc }
1542f4a2713aSLionel Sambuc 
1543f4a2713aSLionel Sambuc inline char ToLower(char ch) {
1544f4a2713aSLionel Sambuc   return static_cast<char>(tolower(static_cast<unsigned char>(ch)));
1545f4a2713aSLionel Sambuc }
1546f4a2713aSLionel Sambuc inline char ToUpper(char ch) {
1547f4a2713aSLionel Sambuc   return static_cast<char>(toupper(static_cast<unsigned char>(ch)));
1548f4a2713aSLionel Sambuc }
1549f4a2713aSLionel Sambuc 
1550f4a2713aSLionel Sambuc // The testing::internal::posix namespace holds wrappers for common
1551f4a2713aSLionel Sambuc // POSIX functions.  These wrappers hide the differences between
1552f4a2713aSLionel Sambuc // Windows/MSVC and POSIX systems.  Since some compilers define these
1553f4a2713aSLionel Sambuc // standard functions as macros, the wrapper cannot have the same name
1554f4a2713aSLionel Sambuc // as the wrapped function.
1555f4a2713aSLionel Sambuc 
1556f4a2713aSLionel Sambuc namespace posix {
1557f4a2713aSLionel Sambuc 
1558f4a2713aSLionel Sambuc // Functions with a different name on Windows.
1559f4a2713aSLionel Sambuc 
1560f4a2713aSLionel Sambuc #if GTEST_OS_WINDOWS
1561f4a2713aSLionel Sambuc 
1562f4a2713aSLionel Sambuc typedef struct _stat StatStruct;
1563f4a2713aSLionel Sambuc 
1564f4a2713aSLionel Sambuc # ifdef __BORLANDC__
1565f4a2713aSLionel Sambuc inline int IsATTY(int fd) { return isatty(fd); }
1566f4a2713aSLionel Sambuc inline int StrCaseCmp(const char* s1, const char* s2) {
1567f4a2713aSLionel Sambuc   return stricmp(s1, s2);
1568f4a2713aSLionel Sambuc }
1569f4a2713aSLionel Sambuc inline char* StrDup(const char* src) { return strdup(src); }
1570f4a2713aSLionel Sambuc # else  // !__BORLANDC__
1571f4a2713aSLionel Sambuc #  if GTEST_OS_WINDOWS_MOBILE
1572f4a2713aSLionel Sambuc inline int IsATTY(int /* fd */) { return 0; }
1573f4a2713aSLionel Sambuc #  else
1574f4a2713aSLionel Sambuc inline int IsATTY(int fd) { return _isatty(fd); }
1575f4a2713aSLionel Sambuc #  endif  // GTEST_OS_WINDOWS_MOBILE
1576f4a2713aSLionel Sambuc inline int StrCaseCmp(const char* s1, const char* s2) {
1577f4a2713aSLionel Sambuc   return _stricmp(s1, s2);
1578f4a2713aSLionel Sambuc }
1579f4a2713aSLionel Sambuc inline char* StrDup(const char* src) { return _strdup(src); }
1580f4a2713aSLionel Sambuc # endif  // __BORLANDC__
1581f4a2713aSLionel Sambuc 
1582f4a2713aSLionel Sambuc # if GTEST_OS_WINDOWS_MOBILE
1583f4a2713aSLionel Sambuc inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
1584f4a2713aSLionel Sambuc // Stat(), RmDir(), and IsDir() are not needed on Windows CE at this
1585f4a2713aSLionel Sambuc // time and thus not defined there.
1586f4a2713aSLionel Sambuc # else
1587f4a2713aSLionel Sambuc inline int FileNo(FILE* file) { return _fileno(file); }
1588f4a2713aSLionel Sambuc inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
1589f4a2713aSLionel Sambuc inline int RmDir(const char* dir) { return _rmdir(dir); }
1590f4a2713aSLionel Sambuc inline bool IsDir(const StatStruct& st) {
1591f4a2713aSLionel Sambuc   return (_S_IFDIR & st.st_mode) != 0;
1592f4a2713aSLionel Sambuc }
1593f4a2713aSLionel Sambuc # endif  // GTEST_OS_WINDOWS_MOBILE
1594f4a2713aSLionel Sambuc 
1595f4a2713aSLionel Sambuc #else
1596f4a2713aSLionel Sambuc 
1597f4a2713aSLionel Sambuc typedef struct stat StatStruct;
1598f4a2713aSLionel Sambuc 
1599f4a2713aSLionel Sambuc inline int FileNo(FILE* file) { return fileno(file); }
1600f4a2713aSLionel Sambuc inline int IsATTY(int fd) { return isatty(fd); }
1601f4a2713aSLionel Sambuc inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
1602f4a2713aSLionel Sambuc inline int StrCaseCmp(const char* s1, const char* s2) {
1603f4a2713aSLionel Sambuc   return strcasecmp(s1, s2);
1604f4a2713aSLionel Sambuc }
1605f4a2713aSLionel Sambuc inline char* StrDup(const char* src) { return strdup(src); }
1606f4a2713aSLionel Sambuc inline int RmDir(const char* dir) { return rmdir(dir); }
1607f4a2713aSLionel Sambuc inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
1608f4a2713aSLionel Sambuc 
1609f4a2713aSLionel Sambuc #endif  // GTEST_OS_WINDOWS
1610f4a2713aSLionel Sambuc 
1611f4a2713aSLionel Sambuc // Functions deprecated by MSVC 8.0.
1612f4a2713aSLionel Sambuc 
1613f4a2713aSLionel Sambuc #ifdef _MSC_VER
1614f4a2713aSLionel Sambuc // Temporarily disable warning 4996 (deprecated function).
1615f4a2713aSLionel Sambuc # pragma warning(push)
1616f4a2713aSLionel Sambuc # pragma warning(disable:4996)
1617f4a2713aSLionel Sambuc #endif
1618f4a2713aSLionel Sambuc 
1619f4a2713aSLionel Sambuc inline const char* StrNCpy(char* dest, const char* src, size_t n) {
1620f4a2713aSLionel Sambuc   return strncpy(dest, src, n);
1621f4a2713aSLionel Sambuc }
1622f4a2713aSLionel Sambuc 
1623f4a2713aSLionel Sambuc // ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and
1624f4a2713aSLionel Sambuc // StrError() aren't needed on Windows CE at this time and thus not
1625f4a2713aSLionel Sambuc // defined there.
1626f4a2713aSLionel Sambuc 
1627f4a2713aSLionel Sambuc #if !GTEST_OS_WINDOWS_MOBILE
1628f4a2713aSLionel Sambuc inline int ChDir(const char* dir) { return chdir(dir); }
1629f4a2713aSLionel Sambuc #endif
1630f4a2713aSLionel Sambuc inline FILE* FOpen(const char* path, const char* mode) {
1631f4a2713aSLionel Sambuc   return fopen(path, mode);
1632f4a2713aSLionel Sambuc }
1633f4a2713aSLionel Sambuc #if !GTEST_OS_WINDOWS_MOBILE
1634f4a2713aSLionel Sambuc inline FILE *FReopen(const char* path, const char* mode, FILE* stream) {
1635f4a2713aSLionel Sambuc   return freopen(path, mode, stream);
1636f4a2713aSLionel Sambuc }
1637f4a2713aSLionel Sambuc inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
1638f4a2713aSLionel Sambuc #endif
1639f4a2713aSLionel Sambuc inline int FClose(FILE* fp) { return fclose(fp); }
1640f4a2713aSLionel Sambuc #if !GTEST_OS_WINDOWS_MOBILE
1641f4a2713aSLionel Sambuc inline int Read(int fd, void* buf, unsigned int count) {
1642f4a2713aSLionel Sambuc   return static_cast<int>(read(fd, buf, count));
1643f4a2713aSLionel Sambuc }
1644f4a2713aSLionel Sambuc inline int Write(int fd, const void* buf, unsigned int count) {
1645f4a2713aSLionel Sambuc   return static_cast<int>(write(fd, buf, count));
1646f4a2713aSLionel Sambuc }
1647f4a2713aSLionel Sambuc inline int Close(int fd) { return close(fd); }
1648f4a2713aSLionel Sambuc inline const char* StrError(int errnum) { return strerror(errnum); }
1649f4a2713aSLionel Sambuc #endif
1650f4a2713aSLionel Sambuc inline const char* GetEnv(const char* name) {
1651f4a2713aSLionel Sambuc #if GTEST_OS_WINDOWS_MOBILE
1652f4a2713aSLionel Sambuc   // We are on Windows CE, which has no environment variables.
1653f4a2713aSLionel Sambuc   return NULL;
1654f4a2713aSLionel Sambuc #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
1655f4a2713aSLionel Sambuc   // Environment variables which we programmatically clear will be set to the
1656f4a2713aSLionel Sambuc   // empty string rather than unset (NULL).  Handle that case.
1657f4a2713aSLionel Sambuc   const char* const env = getenv(name);
1658f4a2713aSLionel Sambuc   return (env != NULL && env[0] != '\0') ? env : NULL;
1659f4a2713aSLionel Sambuc #else
1660f4a2713aSLionel Sambuc   return getenv(name);
1661f4a2713aSLionel Sambuc #endif
1662f4a2713aSLionel Sambuc }
1663f4a2713aSLionel Sambuc 
1664f4a2713aSLionel Sambuc #ifdef _MSC_VER
1665f4a2713aSLionel Sambuc # pragma warning(pop)  // Restores the warning state.
1666f4a2713aSLionel Sambuc #endif
1667f4a2713aSLionel Sambuc 
1668f4a2713aSLionel Sambuc #if GTEST_OS_WINDOWS_MOBILE
1669f4a2713aSLionel Sambuc // Windows CE has no C library. The abort() function is used in
1670f4a2713aSLionel Sambuc // several places in Google Test. This implementation provides a reasonable
1671f4a2713aSLionel Sambuc // imitation of standard behaviour.
1672f4a2713aSLionel Sambuc void Abort();
1673f4a2713aSLionel Sambuc #else
1674f4a2713aSLionel Sambuc inline void Abort() { abort(); }
1675f4a2713aSLionel Sambuc #endif  // GTEST_OS_WINDOWS_MOBILE
1676f4a2713aSLionel Sambuc 
1677f4a2713aSLionel Sambuc }  // namespace posix
1678f4a2713aSLionel Sambuc 
1679f4a2713aSLionel Sambuc // The maximum number a BiggestInt can represent.  This definition
1680f4a2713aSLionel Sambuc // works no matter BiggestInt is represented in one's complement or
1681f4a2713aSLionel Sambuc // two's complement.
1682f4a2713aSLionel Sambuc //
1683f4a2713aSLionel Sambuc // We cannot rely on numeric_limits in STL, as __int64 and long long
1684f4a2713aSLionel Sambuc // are not part of standard C++ and numeric_limits doesn't need to be
1685f4a2713aSLionel Sambuc // defined for them.
1686f4a2713aSLionel Sambuc const BiggestInt kMaxBiggestInt =
1687f4a2713aSLionel Sambuc     ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1));
1688f4a2713aSLionel Sambuc 
1689f4a2713aSLionel Sambuc // This template class serves as a compile-time function from size to
1690f4a2713aSLionel Sambuc // type.  It maps a size in bytes to a primitive type with that
1691f4a2713aSLionel Sambuc // size. e.g.
1692f4a2713aSLionel Sambuc //
1693f4a2713aSLionel Sambuc //   TypeWithSize<4>::UInt
1694f4a2713aSLionel Sambuc //
1695f4a2713aSLionel Sambuc // is typedef-ed to be unsigned int (unsigned integer made up of 4
1696f4a2713aSLionel Sambuc // bytes).
1697f4a2713aSLionel Sambuc //
1698f4a2713aSLionel Sambuc // Such functionality should belong to STL, but I cannot find it
1699f4a2713aSLionel Sambuc // there.
1700f4a2713aSLionel Sambuc //
1701f4a2713aSLionel Sambuc // Google Test uses this class in the implementation of floating-point
1702f4a2713aSLionel Sambuc // comparison.
1703f4a2713aSLionel Sambuc //
1704f4a2713aSLionel Sambuc // For now it only handles UInt (unsigned int) as that's all Google Test
1705f4a2713aSLionel Sambuc // needs.  Other types can be easily added in the future if need
1706f4a2713aSLionel Sambuc // arises.
1707f4a2713aSLionel Sambuc template <size_t size>
1708f4a2713aSLionel Sambuc class TypeWithSize {
1709f4a2713aSLionel Sambuc  public:
1710f4a2713aSLionel Sambuc   // This prevents the user from using TypeWithSize<N> with incorrect
1711f4a2713aSLionel Sambuc   // values of N.
1712f4a2713aSLionel Sambuc   typedef void UInt;
1713f4a2713aSLionel Sambuc };
1714f4a2713aSLionel Sambuc 
1715f4a2713aSLionel Sambuc // The specialization for size 4.
1716f4a2713aSLionel Sambuc template <>
1717f4a2713aSLionel Sambuc class TypeWithSize<4> {
1718f4a2713aSLionel Sambuc  public:
1719f4a2713aSLionel Sambuc   // unsigned int has size 4 in both gcc and MSVC.
1720f4a2713aSLionel Sambuc   //
1721f4a2713aSLionel Sambuc   // As base/basictypes.h doesn't compile on Windows, we cannot use
1722f4a2713aSLionel Sambuc   // uint32, uint64, and etc here.
1723f4a2713aSLionel Sambuc   typedef int Int;
1724f4a2713aSLionel Sambuc   typedef unsigned int UInt;
1725f4a2713aSLionel Sambuc };
1726f4a2713aSLionel Sambuc 
1727f4a2713aSLionel Sambuc // The specialization for size 8.
1728f4a2713aSLionel Sambuc template <>
1729f4a2713aSLionel Sambuc class TypeWithSize<8> {
1730f4a2713aSLionel Sambuc  public:
1731f4a2713aSLionel Sambuc 
1732f4a2713aSLionel Sambuc #if GTEST_OS_WINDOWS
1733f4a2713aSLionel Sambuc   typedef __int64 Int;
1734f4a2713aSLionel Sambuc   typedef unsigned __int64 UInt;
1735f4a2713aSLionel Sambuc #else
1736f4a2713aSLionel Sambuc   typedef long long Int;  // NOLINT
1737f4a2713aSLionel Sambuc   typedef unsigned long long UInt;  // NOLINT
1738f4a2713aSLionel Sambuc #endif  // GTEST_OS_WINDOWS
1739f4a2713aSLionel Sambuc };
1740f4a2713aSLionel Sambuc 
1741f4a2713aSLionel Sambuc // Integer types of known sizes.
1742f4a2713aSLionel Sambuc typedef TypeWithSize<4>::Int Int32;
1743f4a2713aSLionel Sambuc typedef TypeWithSize<4>::UInt UInt32;
1744f4a2713aSLionel Sambuc typedef TypeWithSize<8>::Int Int64;
1745f4a2713aSLionel Sambuc typedef TypeWithSize<8>::UInt UInt64;
1746f4a2713aSLionel Sambuc typedef TypeWithSize<8>::Int TimeInMillis;  // Represents time in milliseconds.
1747f4a2713aSLionel Sambuc 
1748f4a2713aSLionel Sambuc // Utilities for command line flags and environment variables.
1749f4a2713aSLionel Sambuc 
1750f4a2713aSLionel Sambuc // Macro for referencing flags.
1751f4a2713aSLionel Sambuc #define GTEST_FLAG(name) FLAGS_gtest_##name
1752f4a2713aSLionel Sambuc 
1753f4a2713aSLionel Sambuc // Macros for declaring flags.
1754f4a2713aSLionel Sambuc #define GTEST_DECLARE_bool_(name) GTEST_API_ extern bool GTEST_FLAG(name)
1755f4a2713aSLionel Sambuc #define GTEST_DECLARE_int32_(name) \
1756f4a2713aSLionel Sambuc     GTEST_API_ extern ::testing::internal::Int32 GTEST_FLAG(name)
1757f4a2713aSLionel Sambuc #define GTEST_DECLARE_string_(name) \
1758f4a2713aSLionel Sambuc     GTEST_API_ extern ::testing::internal::String GTEST_FLAG(name)
1759f4a2713aSLionel Sambuc 
1760f4a2713aSLionel Sambuc // Macros for defining flags.
1761f4a2713aSLionel Sambuc #define GTEST_DEFINE_bool_(name, default_val, doc) \
1762f4a2713aSLionel Sambuc     GTEST_API_ bool GTEST_FLAG(name) = (default_val)
1763f4a2713aSLionel Sambuc #define GTEST_DEFINE_int32_(name, default_val, doc) \
1764f4a2713aSLionel Sambuc     GTEST_API_ ::testing::internal::Int32 GTEST_FLAG(name) = (default_val)
1765f4a2713aSLionel Sambuc #define GTEST_DEFINE_string_(name, default_val, doc) \
1766f4a2713aSLionel Sambuc     GTEST_API_ ::testing::internal::String GTEST_FLAG(name) = (default_val)
1767f4a2713aSLionel Sambuc 
1768f4a2713aSLionel Sambuc // Parses 'str' for a 32-bit signed integer.  If successful, writes the result
1769f4a2713aSLionel Sambuc // to *value and returns true; otherwise leaves *value unchanged and returns
1770f4a2713aSLionel Sambuc // false.
1771f4a2713aSLionel Sambuc // TODO(chandlerc): Find a better way to refactor flag and environment parsing
1772f4a2713aSLionel Sambuc // out of both gtest-port.cc and gtest.cc to avoid exporting this utility
1773f4a2713aSLionel Sambuc // function.
1774f4a2713aSLionel Sambuc bool ParseInt32(const Message& src_text, const char* str, Int32* value);
1775f4a2713aSLionel Sambuc 
1776f4a2713aSLionel Sambuc // Parses a bool/Int32/string from the environment variable
1777f4a2713aSLionel Sambuc // corresponding to the given Google Test flag.
1778f4a2713aSLionel Sambuc bool BoolFromGTestEnv(const char* flag, bool default_val);
1779f4a2713aSLionel Sambuc GTEST_API_ Int32 Int32FromGTestEnv(const char* flag, Int32 default_val);
1780f4a2713aSLionel Sambuc const char* StringFromGTestEnv(const char* flag, const char* default_val);
1781f4a2713aSLionel Sambuc 
1782f4a2713aSLionel Sambuc }  // namespace internal
1783f4a2713aSLionel Sambuc }  // namespace testing
1784f4a2713aSLionel Sambuc 
1785f4a2713aSLionel Sambuc #endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
1786