00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
00044 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 #include <ctype.h>
00260 #include <stddef.h>
00261 #include <stdlib.h>
00262 #include <stdio.h>
00263 #include <string.h>
00264 #ifndef _WIN32_WCE
00265 # include <sys/types.h>
00266 # include <sys/stat.h>
00267 #endif // !_WIN32_WCE
00268
00269 #if defined __APPLE__
00270 # include <AvailabilityMacros.h>
00271 # include <TargetConditionals.h>
00272 #endif
00273
00274 #include <algorithm>
00275 #include <iostream>
00276 #include <sstream>
00277 #include <string>
00278 #include <utility>
00279 #include <vector>
00280
00281 #include "gtest/internal/gtest-port-arch.h"
00282 #include "gtest/internal/custom/gtest-port.h"
00283
00284 #if !defined(GTEST_DEV_EMAIL_)
00285 # define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
00286 # define GTEST_FLAG_PREFIX_ "gtest_"
00287 # define GTEST_FLAG_PREFIX_DASH_ "gtest-"
00288 # define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
00289 # define GTEST_NAME_ "Google Test"
00290 # define GTEST_PROJECT_URL_ "http://code.google.com/p/googletest/"
00291 #endif // !defined(GTEST_DEV_EMAIL_)
00292
00293 #if !defined(GTEST_INIT_GOOGLE_TEST_NAME_)
00294 # define GTEST_INIT_GOOGLE_TEST_NAME_ "testing::InitGoogleTest"
00295 #endif // !defined(GTEST_INIT_GOOGLE_TEST_NAME_)
00296
00297
00298 #ifdef __GNUC__
00299
00300 # define GTEST_GCC_VER_ \
00301 (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
00302 #endif // __GNUC__
00303
00304
00305
00306
00307
00308
00309 #if _MSC_VER >= 1500
00310 # define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) \
00311 __pragma(warning(push)) \
00312 __pragma(warning(disable: warnings))
00313 # define GTEST_DISABLE_MSC_WARNINGS_POP_() \
00314 __pragma(warning(pop))
00315 #else
00316
00317 # define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
00318 # define GTEST_DISABLE_MSC_WARNINGS_POP_()
00319 #endif
00320
00321 #ifndef GTEST_LANG_CXX11
00322
00323
00324
00325
00326 # if __GXX_EXPERIMENTAL_CXX0X__ || __cplusplus >= 201103L
00327
00328 # define GTEST_LANG_CXX11 1
00329 # else
00330 # define GTEST_LANG_CXX11 0
00331 # endif
00332 #endif
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 #if GTEST_LANG_CXX11 && \
00344 (!defined(__GLIBCXX__) || ( \
00345 __GLIBCXX__ >= 20110325ul && \
00346 \
00347 __GLIBCXX__ != 20110416ul && \
00348 __GLIBCXX__ != 20120313ul && \
00349 __GLIBCXX__ != 20110428ul && \
00350 __GLIBCXX__ != 20120702ul))
00351 # define GTEST_STDLIB_CXX11 1
00352 #endif
00353
00354
00355 #if GTEST_STDLIB_CXX11
00356 # define GTEST_HAS_STD_BEGIN_AND_END_ 1
00357 # define GTEST_HAS_STD_FORWARD_LIST_ 1
00358 # define GTEST_HAS_STD_FUNCTION_ 1
00359 # define GTEST_HAS_STD_INITIALIZER_LIST_ 1
00360 # define GTEST_HAS_STD_MOVE_ 1
00361 # define GTEST_HAS_STD_SHARED_PTR_ 1
00362 # define GTEST_HAS_STD_TYPE_TRAITS_ 1
00363 # define GTEST_HAS_STD_UNIQUE_PTR_ 1
00364 #endif
00365
00366
00367
00368 #if GTEST_LANG_CXX11
00369 # define GTEST_HAS_STD_TUPLE_ 1
00370 # if defined(__clang__)
00371
00372 # if defined(__has_include) && !__has_include(<tuple>)
00373 # undef GTEST_HAS_STD_TUPLE_
00374 # endif
00375 # elif defined(_MSC_VER)
00376
00377 # if defined(_CPPLIB_VER) && _CPPLIB_VER < 520
00378 # undef GTEST_HAS_STD_TUPLE_
00379 # endif
00380 # elif defined(__GLIBCXX__)
00381
00382
00383
00384 # if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 2)
00385 # undef GTEST_HAS_STD_TUPLE_
00386 # endif
00387 # endif
00388 #endif
00389
00390
00391
00392
00393 #if GTEST_OS_WINDOWS
00394 # if !GTEST_OS_WINDOWS_MOBILE
00395 # include <direct.h>
00396 # include <io.h>
00397 # endif
00398
00399
00400
00401
00402 struct _RTL_CRITICAL_SECTION;
00403 #else
00404
00405
00406
00407 # include <unistd.h>
00408 # include <strings.h>
00409 #endif // GTEST_OS_WINDOWS
00410
00411 #if GTEST_OS_LINUX_ANDROID
00412
00413 # include <android/api-level.h>
00414 #endif
00415
00416
00417 #ifndef GTEST_HAS_POSIX_RE
00418 # if GTEST_OS_LINUX_ANDROID
00419
00420 # define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9)
00421 # else
00422 # define GTEST_HAS_POSIX_RE (!GTEST_OS_WINDOWS)
00423 # endif
00424 #endif
00425
00426 #if GTEST_USES_PCRE
00427
00428
00429 #elif GTEST_HAS_POSIX_RE
00430
00431
00432
00433
00434
00435 # include <regex.h>
00436
00437 # define GTEST_USES_POSIX_RE 1
00438
00439 #elif GTEST_OS_WINDOWS
00440
00441
00442
00443 # define GTEST_USES_SIMPLE_RE 1
00444
00445 #else
00446
00447
00448
00449 # define GTEST_USES_SIMPLE_RE 1
00450
00451 #endif // GTEST_USES_PCRE
00452
00453 #ifndef GTEST_HAS_EXCEPTIONS
00454
00455
00456 # if defined(_MSC_VER) || defined(__BORLANDC__)
00457
00458
00459
00460 # ifndef _HAS_EXCEPTIONS
00461 # define _HAS_EXCEPTIONS 1
00462 # endif // _HAS_EXCEPTIONS
00463 # define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
00464 # elif defined(__clang__)
00465
00466
00467
00468
00469
00470
00471
00472 # define GTEST_HAS_EXCEPTIONS (__EXCEPTIONS && __has_feature(cxx_exceptions))
00473 # elif defined(__GNUC__) && __EXCEPTIONS
00474
00475 # define GTEST_HAS_EXCEPTIONS 1
00476 # elif defined(__SUNPRO_CC)
00477
00478
00479
00480 # define GTEST_HAS_EXCEPTIONS 1
00481 # elif defined(__IBMCPP__) && __EXCEPTIONS
00482
00483 # define GTEST_HAS_EXCEPTIONS 1
00484 # elif defined(__HP_aCC)
00485
00486
00487 # define GTEST_HAS_EXCEPTIONS 1
00488 # else
00489
00490
00491 # define GTEST_HAS_EXCEPTIONS 0
00492 # endif // defined(_MSC_VER) || defined(__BORLANDC__)
00493 #endif // GTEST_HAS_EXCEPTIONS
00494
00495 #if !defined(GTEST_HAS_STD_STRING)
00496
00497
00498 # define GTEST_HAS_STD_STRING 1
00499 #elif !GTEST_HAS_STD_STRING
00500
00501 # error "Google Test cannot be used where ::std::string isn't available."
00502 #endif // !defined(GTEST_HAS_STD_STRING)
00503
00504 #ifndef GTEST_HAS_GLOBAL_STRING
00505
00506
00507
00508 # define GTEST_HAS_GLOBAL_STRING 0
00509
00510 #endif // GTEST_HAS_GLOBAL_STRING
00511
00512 #ifndef GTEST_HAS_STD_WSTRING
00513
00514
00515
00516
00517
00518
00519
00520
00521 # define GTEST_HAS_STD_WSTRING \
00522 (!(GTEST_OS_LINUX_ANDROID || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS))
00523
00524 #endif // GTEST_HAS_STD_WSTRING
00525
00526 #ifndef GTEST_HAS_GLOBAL_WSTRING
00527
00528
00529 # define GTEST_HAS_GLOBAL_WSTRING \
00530 (GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING)
00531 #endif // GTEST_HAS_GLOBAL_WSTRING
00532
00533
00534 #ifndef GTEST_HAS_RTTI
00535
00536
00537
00538 # ifdef _MSC_VER
00539
00540 # ifdef _CPPRTTI // MSVC defines this macro iff RTTI is enabled.
00541 # define GTEST_HAS_RTTI 1
00542 # else
00543 # define GTEST_HAS_RTTI 0
00544 # endif
00545
00546
00547 # elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40302)
00548
00549 # ifdef __GXX_RTTI
00550
00551
00552
00553
00554 # if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR) && \
00555 !defined(__EXCEPTIONS)
00556 # define GTEST_HAS_RTTI 0
00557 # else
00558 # define GTEST_HAS_RTTI 1
00559 # endif // GTEST_OS_LINUX_ANDROID && __STLPORT_MAJOR && !__EXCEPTIONS
00560 # else
00561 # define GTEST_HAS_RTTI 0
00562 # endif // __GXX_RTTI
00563
00564
00565
00566
00567 # elif defined(__clang__)
00568
00569 # define GTEST_HAS_RTTI __has_feature(cxx_rtti)
00570
00571
00572
00573 # elif defined(__IBMCPP__) && (__IBMCPP__ >= 900)
00574
00575 # ifdef __RTTI_ALL__
00576 # define GTEST_HAS_RTTI 1
00577 # else
00578 # define GTEST_HAS_RTTI 0
00579 # endif
00580
00581 # else
00582
00583
00584 # define GTEST_HAS_RTTI 1
00585
00586 # endif // _MSC_VER
00587
00588 #endif // GTEST_HAS_RTTI
00589
00590
00591
00592 #if GTEST_HAS_RTTI
00593 # include <typeinfo>
00594 #endif
00595
00596
00597 #ifndef GTEST_HAS_PTHREAD
00598
00599
00600
00601
00602
00603 # define GTEST_HAS_PTHREAD (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_HPUX \
00604 || GTEST_OS_QNX || GTEST_OS_FREEBSD || GTEST_OS_NACL)
00605 #endif // GTEST_HAS_PTHREAD
00606
00607 #if GTEST_HAS_PTHREAD
00608
00609
00610 # include <pthread.h>
00611
00612
00613 # include <time.h>
00614 #endif
00615
00616
00617
00618 #if !defined(GTEST_HAS_HASH_MAP_)
00619 # if _MSC_VER
00620 # define GTEST_HAS_HASH_MAP_ 1 // Indicates that hash_map is available.
00621 # define GTEST_HAS_HASH_SET_ 1 // Indicates that hash_set is available.
00622 # endif // _MSC_VER
00623 #endif // !defined(GTEST_HAS_HASH_MAP_)
00624
00625
00626
00627
00628 #ifndef GTEST_HAS_TR1_TUPLE
00629 # if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR)
00630
00631 # define GTEST_HAS_TR1_TUPLE 0
00632 # else
00633
00634 # define GTEST_HAS_TR1_TUPLE 1
00635 # endif
00636 #endif // GTEST_HAS_TR1_TUPLE
00637
00638
00639
00640 #ifndef GTEST_USE_OWN_TR1_TUPLE
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653 # if (defined(__GNUC__) && !defined(__CUDACC__) && (GTEST_GCC_VER_ >= 40000) \
00654 && !GTEST_OS_QNX && !defined(_LIBCPP_VERSION)) || _MSC_VER >= 1600
00655 # define GTEST_ENV_HAS_TR1_TUPLE_ 1
00656 # endif
00657
00658
00659
00660
00661 # if GTEST_LANG_CXX11 && (!defined(__GLIBCXX__) || __GLIBCXX__ > 20110325)
00662 # define GTEST_ENV_HAS_STD_TUPLE_ 1
00663 # endif
00664
00665 # if GTEST_ENV_HAS_TR1_TUPLE_ || GTEST_ENV_HAS_STD_TUPLE_
00666 # define GTEST_USE_OWN_TR1_TUPLE 0
00667 # else
00668 # define GTEST_USE_OWN_TR1_TUPLE 1
00669 # endif
00670
00671 #endif // GTEST_USE_OWN_TR1_TUPLE
00672
00673
00674
00675
00676 #if GTEST_HAS_STD_TUPLE_
00677 # include <tuple>
00678 # define GTEST_TUPLE_NAMESPACE_ ::std
00679 #endif // GTEST_HAS_STD_TUPLE_
00680
00681
00682
00683 #if GTEST_HAS_TR1_TUPLE
00684 # ifndef GTEST_TUPLE_NAMESPACE_
00685 # define GTEST_TUPLE_NAMESPACE_ ::std::tr1
00686 # endif // GTEST_TUPLE_NAMESPACE_
00687
00688 # if GTEST_USE_OWN_TR1_TUPLE
00689 # include "gtest/internal/gtest-tuple.h"
00690 # elif GTEST_ENV_HAS_STD_TUPLE_
00691 # include <tuple>
00692
00693
00694
00695
00696 namespace std {
00697 namespace tr1 {
00698 using ::std::get;
00699 using ::std::make_tuple;
00700 using ::std::tuple;
00701 using ::std::tuple_element;
00702 using ::std::tuple_size;
00703 }
00704 }
00705
00706 # elif GTEST_OS_SYMBIAN
00707
00708
00709
00710
00711
00712
00713 # ifdef BOOST_HAS_TR1_TUPLE
00714 # undef BOOST_HAS_TR1_TUPLE
00715 # endif // BOOST_HAS_TR1_TUPLE
00716
00717
00718
00719 # define BOOST_TR1_DETAIL_CONFIG_HPP_INCLUDED
00720 # include <tuple>
00721
00722 # elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000)
00723
00724
00725
00726 # if !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
00727
00728
00729
00730
00731
00732 # define _TR1_FUNCTIONAL 1
00733 # include <tr1/tuple>
00734 # undef _TR1_FUNCTIONAL // Allows the user to #include
00735
00736 # else
00737 # include <tr1/tuple>
00738 # endif // !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
00739
00740 # else
00741
00742
00743 # include <tuple>
00744 # endif // GTEST_USE_OWN_TR1_TUPLE
00745
00746 #endif // GTEST_HAS_TR1_TUPLE
00747
00748
00749
00750
00751
00752 #ifndef GTEST_HAS_CLONE
00753
00754
00755 # if GTEST_OS_LINUX && !defined(__ia64__)
00756 # if GTEST_OS_LINUX_ANDROID
00757
00758 # if defined(__arm__) && __ANDROID_API__ >= 9
00759 # define GTEST_HAS_CLONE 1
00760 # else
00761 # define GTEST_HAS_CLONE 0
00762 # endif
00763 # else
00764 # define GTEST_HAS_CLONE 1
00765 # endif
00766 # else
00767 # define GTEST_HAS_CLONE 0
00768 # endif // GTEST_OS_LINUX && !defined(__ia64__)
00769
00770 #endif // GTEST_HAS_CLONE
00771
00772
00773
00774 #ifndef GTEST_HAS_STREAM_REDIRECTION
00775
00776
00777 # if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN || \
00778 GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT
00779 # define GTEST_HAS_STREAM_REDIRECTION 0
00780 # else
00781 # define GTEST_HAS_STREAM_REDIRECTION 1
00782 # endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN
00783 #endif // GTEST_HAS_STREAM_REDIRECTION
00784
00785
00786
00787
00788
00789 #if (GTEST_OS_LINUX || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \
00790 (GTEST_OS_MAC && !GTEST_OS_IOS) || \
00791 (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER >= 1400) || \
00792 GTEST_OS_WINDOWS_MINGW || GTEST_OS_AIX || GTEST_OS_HPUX || \
00793 GTEST_OS_OPENBSD || GTEST_OS_QNX || GTEST_OS_FREEBSD)
00794 # define GTEST_HAS_DEATH_TEST 1
00795 #endif
00796
00797
00798
00799
00800 #define GTEST_HAS_PARAM_TEST 1
00801
00802
00803
00804
00805
00806 #if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__SUNPRO_CC) || \
00807 defined(__IBMCPP__) || defined(__HP_aCC)
00808 # define GTEST_HAS_TYPED_TEST 1
00809 # define GTEST_HAS_TYPED_TEST_P 1
00810 #endif
00811
00812
00813
00814
00815
00816 #if GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE && !defined(__SUNPRO_CC)
00817 # define GTEST_HAS_COMBINE 1
00818 #endif
00819
00820
00821 #define GTEST_WIDE_STRING_USES_UTF16_ \
00822 (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN || GTEST_OS_AIX)
00823
00824
00825 #if GTEST_OS_LINUX
00826 # define GTEST_CAN_STREAM_RESULTS_ 1
00827 #endif
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839 #ifdef __INTEL_COMPILER
00840 # define GTEST_AMBIGUOUS_ELSE_BLOCKER_
00841 #else
00842 # define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: default: // NOLINT
00843 #endif
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856 #if defined(__GNUC__) && !defined(COMPILER_ICC)
00857 # define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
00858 #elif defined(__clang__)
00859 # if __has_attribute(unused)
00860 # define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
00861 # endif
00862 #endif
00863 #ifndef GTEST_ATTRIBUTE_UNUSED_
00864 # define GTEST_ATTRIBUTE_UNUSED_
00865 #endif
00866
00867
00868
00869 #define GTEST_DISALLOW_ASSIGN_(type)\
00870 void operator=(type const &)
00871
00872
00873
00874 #define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\
00875 type(type const &);\
00876 GTEST_DISALLOW_ASSIGN_(type)
00877
00878
00879
00880
00881
00882
00883 #if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC)
00884 # define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))
00885 #else
00886 # define GTEST_MUST_USE_RESULT_
00887 #endif // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897 # define GTEST_INTENTIONAL_CONST_COND_PUSH_() \
00898 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127)
00899 # define GTEST_INTENTIONAL_CONST_COND_POP_() \
00900 GTEST_DISABLE_MSC_WARNINGS_POP_()
00901
00902
00903
00904
00905 #ifndef GTEST_HAS_SEH
00906
00907
00908 # if defined(_MSC_VER) || defined(__BORLANDC__)
00909
00910 # define GTEST_HAS_SEH 1
00911 # else
00912
00913 # define GTEST_HAS_SEH 0
00914 # endif
00915
00916 #define GTEST_IS_THREADSAFE \
00917 (GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ \
00918 || (GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT) \
00919 || GTEST_HAS_PTHREAD)
00920
00921 #endif // GTEST_HAS_SEH
00922
00923 #ifdef _MSC_VER
00924 # if GTEST_LINKED_AS_SHARED_LIBRARY
00925 # define GTEST_API_ __declspec(dllimport)
00926 # elif GTEST_CREATE_SHARED_LIBRARY
00927 # define GTEST_API_ __declspec(dllexport)
00928 # endif
00929 #elif __GNUC__ >= 4 || defined(__clang__)
00930 # define GTEST_API_ __attribute__((visibility ("default")))
00931 #endif // _MSC_VER
00932
00933 #ifndef GTEST_API_
00934 # define GTEST_API_
00935 #endif
00936
00937 #ifdef __GNUC__
00938
00939 # define GTEST_NO_INLINE_ __attribute__((noinline))
00940 #else
00941 # define GTEST_NO_INLINE_
00942 #endif
00943
00944
00945 #if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION)
00946 # define GTEST_HAS_CXXABI_H_ 1
00947 #else
00948 # define GTEST_HAS_CXXABI_H_ 0
00949 #endif
00950
00951
00952
00953 #if defined(__clang__)
00954 # if __has_feature(memory_sanitizer)
00955 # define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ \
00956 __attribute__((no_sanitize_memory))
00957 # else
00958 # define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
00959 # endif // __has_feature(memory_sanitizer)
00960 #else
00961 # define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
00962 #endif // __clang__
00963
00964
00965 #if defined(__clang__)
00966 # if __has_feature(address_sanitizer)
00967 # define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ \
00968 __attribute__((no_sanitize_address))
00969 # else
00970 # define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
00971 # endif // __has_feature(address_sanitizer)
00972 #else
00973 # define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
00974 #endif // __clang__
00975
00976
00977 #if defined(__clang__)
00978 # if __has_feature(thread_sanitizer)
00979 # define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ \
00980 __attribute__((no_sanitize_thread))
00981 # else
00982 # define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
00983 # endif // __has_feature(thread_sanitizer)
00984 #else
00985 # define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
00986 #endif // __clang__
00987
00988 namespace testing {
00989
00990 class Message;
00991
00992 #if defined(GTEST_TUPLE_NAMESPACE_)
00993
00994
00995
00996 using GTEST_TUPLE_NAMESPACE_::get;
00997 using GTEST_TUPLE_NAMESPACE_::make_tuple;
00998 using GTEST_TUPLE_NAMESPACE_::tuple;
00999 using GTEST_TUPLE_NAMESPACE_::tuple_size;
01000 using GTEST_TUPLE_NAMESPACE_::tuple_element;
01001 #endif // defined(GTEST_TUPLE_NAMESPACE_)
01002
01003 namespace internal {
01004
01005
01006
01007
01008 class Secret;
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025 #if GTEST_LANG_CXX11
01026 # define GTEST_COMPILE_ASSERT_(expr, msg) static_assert(expr, #msg)
01027 #else // !GTEST_LANG_CXX11
01028 template <bool>
01029 struct CompileAssert {
01030 };
01031
01032 # define GTEST_COMPILE_ASSERT_(expr, msg) \
01033 typedef ::testing::internal::CompileAssert<(static_cast<bool>(expr))> \
01034 msg[static_cast<bool>(expr) ? 1 : -1] GTEST_ATTRIBUTE_UNUSED_
01035 #endif // !GTEST_LANG_CXX11
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083 template <typename T1, typename T2>
01084 struct StaticAssertTypeEqHelper;
01085
01086 template <typename T>
01087 struct StaticAssertTypeEqHelper<T, T> {
01088 enum { value = true };
01089 };
01090
01091
01092 #define GTEST_ARRAY_SIZE_(array) (sizeof(array) / sizeof(array[0]))
01093
01094 #if GTEST_HAS_GLOBAL_STRING
01095 typedef ::string string;
01096 #else
01097 typedef ::std::string string;
01098 #endif // GTEST_HAS_GLOBAL_STRING
01099
01100 #if GTEST_HAS_GLOBAL_WSTRING
01101 typedef ::wstring wstring;
01102 #elif GTEST_HAS_STD_WSTRING
01103 typedef ::std::wstring wstring;
01104 #endif // GTEST_HAS_GLOBAL_WSTRING
01105
01106
01107
01108 GTEST_API_ bool IsTrue(bool condition);
01109
01110
01111
01112
01113
01114 template <typename T>
01115 class scoped_ptr {
01116 public:
01117 typedef T element_type;
01118
01119 explicit scoped_ptr(T* p = NULL) : ptr_(p) {}
01120 ~scoped_ptr() { reset(); }
01121
01122 T& operator*() const { return *ptr_; }
01123 T* operator->() const { return ptr_; }
01124 T* get() const { return ptr_; }
01125
01126 T* release() {
01127 T* const ptr = ptr_;
01128 ptr_ = NULL;
01129 return ptr;
01130 }
01131
01132 void reset(T* p = NULL) {
01133 if (p != ptr_) {
01134 if (IsTrue(sizeof(T) > 0)) {
01135 delete ptr_;
01136 }
01137 ptr_ = p;
01138 }
01139 }
01140
01141 friend void swap(scoped_ptr& a, scoped_ptr& b) {
01142 using std::swap;
01143 swap(a.ptr_, b.ptr_);
01144 }
01145
01146 private:
01147 T* ptr_;
01148
01149 GTEST_DISALLOW_COPY_AND_ASSIGN_(scoped_ptr);
01150 };
01151
01152
01153
01154
01155
01156 class GTEST_API_ RE {
01157 public:
01158
01159
01160 RE(const RE& other) { Init(other.pattern()); }
01161
01162
01163 RE(const ::std::string& regex) { Init(regex.c_str()); }
01164
01165 #if GTEST_HAS_GLOBAL_STRING
01166
01167 RE(const ::string& regex) { Init(regex.c_str()); }
01168
01169 #endif // GTEST_HAS_GLOBAL_STRING
01170
01171 RE(const char* regex) { Init(regex); }
01172 ~RE();
01173
01174
01175 const char* pattern() const { return pattern_; }
01176
01177
01178
01179
01180
01181
01182
01183
01184 static bool FullMatch(const ::std::string& str, const RE& re) {
01185 return FullMatch(str.c_str(), re);
01186 }
01187 static bool PartialMatch(const ::std::string& str, const RE& re) {
01188 return PartialMatch(str.c_str(), re);
01189 }
01190
01191 #if GTEST_HAS_GLOBAL_STRING
01192
01193 static bool FullMatch(const ::string& str, const RE& re) {
01194 return FullMatch(str.c_str(), re);
01195 }
01196 static bool PartialMatch(const ::string& str, const RE& re) {
01197 return PartialMatch(str.c_str(), re);
01198 }
01199
01200 #endif // GTEST_HAS_GLOBAL_STRING
01201
01202 static bool FullMatch(const char* str, const RE& re);
01203 static bool PartialMatch(const char* str, const RE& re);
01204
01205 private:
01206 void Init(const char* regex);
01207
01208
01209
01210
01211 const char* pattern_;
01212 bool is_valid_;
01213
01214 #if GTEST_USES_POSIX_RE
01215
01216 regex_t full_regex_;
01217 regex_t partial_regex_;
01218
01219 #else // GTEST_USES_SIMPLE_RE
01220
01221 const char* full_pattern_;
01222
01223 #endif
01224
01225 GTEST_DISALLOW_ASSIGN_(RE);
01226 };
01227
01228
01229
01230 GTEST_API_ ::std::string FormatFileLocation(const char* file, int line);
01231
01232
01233
01234
01235 GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file,
01236 int line);
01237
01238
01239
01240
01241
01242
01243
01244 enum GTestLogSeverity {
01245 GTEST_INFO,
01246 GTEST_WARNING,
01247 GTEST_ERROR,
01248 GTEST_FATAL
01249 };
01250
01251
01252
01253
01254 class GTEST_API_ GTestLog {
01255 public:
01256 GTestLog(GTestLogSeverity severity, const char* file, int line);
01257
01258
01259 ~GTestLog();
01260
01261 ::std::ostream& GetStream() { return ::std::cerr; }
01262
01263 private:
01264 const GTestLogSeverity severity_;
01265
01266 GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog);
01267 };
01268
01269 #if !defined(GTEST_LOG_)
01270
01271 # define GTEST_LOG_(severity) \
01272 ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
01273 __FILE__, __LINE__).GetStream()
01274
01275 inline void LogToStderr() {}
01276 inline void FlushInfoLog() { fflush(NULL); }
01277
01278 #endif // !defined(GTEST_LOG_)
01279
01280 #if !defined(GTEST_CHECK_)
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295 # define GTEST_CHECK_(condition) \
01296 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
01297 if (::testing::internal::IsTrue(condition)) \
01298 ; \
01299 else \
01300 GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
01301 #endif // !defined(GTEST_CHECK_)
01302
01303
01304
01305
01306
01307
01308 #define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \
01309 if (const int gtest_error = (posix_call)) \
01310 GTEST_LOG_(FATAL) << #posix_call << "failed with error " \
01311 << gtest_error
01312
01313 #if GTEST_HAS_STD_MOVE_
01314 using std::move;
01315 #else // GTEST_HAS_STD_MOVE_
01316 template <typename T>
01317 const T& move(const T& t) {
01318 return t;
01319 }
01320 #endif // GTEST_HAS_STD_MOVE_
01321
01322
01323
01324
01325
01326
01327
01328
01329
01330
01331
01332
01333
01334
01335
01336
01337
01338
01339
01340
01341
01342 template<typename To>
01343 inline To ImplicitCast_(To x) { return x; }
01344
01345
01346
01347
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365
01366 template<typename To, typename From>
01367 inline To DownCast_(From* f) {
01368
01369
01370
01371
01372 GTEST_INTENTIONAL_CONST_COND_PUSH_()
01373 if (false) {
01374 GTEST_INTENTIONAL_CONST_COND_POP_()
01375 const To to = NULL;
01376 ::testing::internal::ImplicitCast_<From*>(to);
01377 }
01378
01379 #if GTEST_HAS_RTTI
01380
01381 GTEST_CHECK_(f == NULL || dynamic_cast<To>(f) != NULL);
01382 #endif
01383 return static_cast<To>(f);
01384 }
01385
01386
01387
01388
01389
01390
01391 template <class Derived, class Base>
01392 Derived* CheckedDowncastToActualType(Base* base) {
01393 #if GTEST_HAS_RTTI
01394 GTEST_CHECK_(typeid(*base) == typeid(Derived));
01395 #endif
01396
01397 #if GTEST_HAS_DOWNCAST_
01398 return ::down_cast<Derived*>(base);
01399 #elif GTEST_HAS_RTTI
01400 return dynamic_cast<Derived*>(base);
01401 #else
01402 return static_cast<Derived*>(base);
01403 #endif
01404 }
01405
01406 #if GTEST_HAS_STREAM_REDIRECTION
01407
01408
01409
01410
01411
01412
01413
01414 GTEST_API_ void CaptureStdout();
01415 GTEST_API_ std::string GetCapturedStdout();
01416 GTEST_API_ void CaptureStderr();
01417 GTEST_API_ std::string GetCapturedStderr();
01418
01419 #endif // GTEST_HAS_STREAM_REDIRECTION
01420
01421
01422 GTEST_API_ std::string TempDir();
01423
01424
01425 GTEST_API_ size_t GetFileSize(FILE* file);
01426
01427
01428 GTEST_API_ std::string ReadEntireFile(FILE* file);
01429
01430
01431 GTEST_API_ const ::std::vector<testing::internal::string>& GetArgvs();
01432
01433 #if GTEST_HAS_DEATH_TEST
01434
01435 const ::std::vector<testing::internal::string>& GetInjectableArgvs();
01436 void SetInjectableArgvs(const ::std::vector<testing::internal::string>*
01437 new_argvs);
01438
01439
01440 #endif // GTEST_HAS_DEATH_TEST
01441
01442
01443 #if GTEST_IS_THREADSAFE
01444 # if GTEST_HAS_PTHREAD
01445
01446
01447
01448 inline void SleepMilliseconds(int n) {
01449 const timespec time = {
01450 0,
01451 n * 1000L * 1000L,
01452 };
01453 nanosleep(&time, NULL);
01454 }
01455 # endif // GTEST_HAS_PTHREAD
01456
01457 # if GTEST_HAS_NOTIFICATION_
01458
01459
01460
01461 # elif GTEST_HAS_PTHREAD
01462
01463
01464
01465
01466
01467
01468 class Notification {
01469 public:
01470 Notification() : notified_(false) {
01471 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));
01472 }
01473 ~Notification() {
01474 pthread_mutex_destroy(&mutex_);
01475 }
01476
01477
01478
01479 void Notify() {
01480 pthread_mutex_lock(&mutex_);
01481 notified_ = true;
01482 pthread_mutex_unlock(&mutex_);
01483 }
01484
01485
01486
01487 void WaitForNotification() {
01488 for (;;) {
01489 pthread_mutex_lock(&mutex_);
01490 const bool notified = notified_;
01491 pthread_mutex_unlock(&mutex_);
01492 if (notified)
01493 break;
01494 SleepMilliseconds(10);
01495 }
01496 }
01497
01498 private:
01499 pthread_mutex_t mutex_;
01500 bool notified_;
01501
01502 GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
01503 };
01504
01505 # elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
01506
01507 GTEST_API_ void SleepMilliseconds(int n);
01508
01509
01510
01511 class GTEST_API_ AutoHandle {
01512 public:
01513
01514
01515
01516
01517
01518 typedef void* Handle;
01519 AutoHandle();
01520 explicit AutoHandle(Handle handle);
01521
01522 ~AutoHandle();
01523
01524 Handle Get() const;
01525 void Reset();
01526 void Reset(Handle handle);
01527
01528 private:
01529
01530 bool IsCloseable() const;
01531
01532 Handle handle_;
01533
01534 GTEST_DISALLOW_COPY_AND_ASSIGN_(AutoHandle);
01535 };
01536
01537
01538
01539
01540
01541
01542
01543 class GTEST_API_ Notification {
01544 public:
01545 Notification();
01546 void Notify();
01547 void WaitForNotification();
01548
01549 private:
01550 AutoHandle event_;
01551
01552 GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
01553 };
01554 # endif // GTEST_HAS_NOTIFICATION_
01555
01556
01557
01558
01559 # if GTEST_HAS_PTHREAD && !GTEST_OS_WINDOWS_MINGW
01560
01561
01562
01563
01564
01565
01566 class ThreadWithParamBase {
01567 public:
01568 virtual ~ThreadWithParamBase() {}
01569 virtual void Run() = 0;
01570 };
01571
01572
01573
01574
01575
01576
01577
01578 extern "C" inline void* ThreadFuncWithCLinkage(void* thread) {
01579 static_cast<ThreadWithParamBase*>(thread)->Run();
01580 return NULL;
01581 }
01582
01583
01584
01585
01586
01587
01588
01589
01590
01591
01592
01593
01594
01595 template <typename T>
01596 class ThreadWithParam : public ThreadWithParamBase {
01597 public:
01598 typedef void UserThreadFunc(T);
01599
01600 ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start)
01601 : func_(func),
01602 param_(param),
01603 thread_can_start_(thread_can_start),
01604 finished_(false) {
01605 ThreadWithParamBase* const base = this;
01606
01607
01608 GTEST_CHECK_POSIX_SUCCESS_(
01609 pthread_create(&thread_, 0, &ThreadFuncWithCLinkage, base));
01610 }
01611 ~ThreadWithParam() { Join(); }
01612
01613 void Join() {
01614 if (!finished_) {
01615 GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, 0));
01616 finished_ = true;
01617 }
01618 }
01619
01620 virtual void Run() {
01621 if (thread_can_start_ != NULL)
01622 thread_can_start_->WaitForNotification();
01623 func_(param_);
01624 }
01625
01626 private:
01627 UserThreadFunc* const func_;
01628 const T param_;
01629
01630
01631 Notification* const thread_can_start_;
01632 bool finished_;
01633 pthread_t thread_;
01634
01635 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam);
01636 };
01637 # endif // !GTEST_OS_WINDOWS && GTEST_HAS_PTHREAD ||
01638
01639
01640 # if GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
01641
01642
01643
01644 # elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
01645
01646
01647
01648
01649
01650
01651
01652
01653
01654
01655
01656
01657
01658
01659
01660 class GTEST_API_ Mutex {
01661 public:
01662 enum MutexType { kStatic = 0, kDynamic = 1 };
01663
01664
01665
01666 enum StaticConstructorSelector { kStaticMutex = 0 };
01667
01668
01669
01670
01671 explicit Mutex(StaticConstructorSelector ) {}
01672
01673 Mutex();
01674 ~Mutex();
01675
01676 void Lock();
01677
01678 void Unlock();
01679
01680
01681
01682 void AssertHeld();
01683
01684 private:
01685
01686 void ThreadSafeLazyInit();
01687
01688
01689
01690 unsigned int owner_thread_id_;
01691
01692
01693
01694 MutexType type_;
01695 long critical_section_init_phase_;
01696 _RTL_CRITICAL_SECTION* critical_section_;
01697
01698 GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex);
01699 };
01700
01701 # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
01702 extern ::testing::internal::Mutex mutex
01703
01704 # define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
01705 ::testing::internal::Mutex mutex(::testing::internal::Mutex::kStaticMutex)
01706
01707
01708
01709
01710
01711
01712 class GTestMutexLock {
01713 public:
01714 explicit GTestMutexLock(Mutex* mutex)
01715 : mutex_(mutex) { mutex_->Lock(); }
01716
01717 ~GTestMutexLock() { mutex_->Unlock(); }
01718
01719 private:
01720 Mutex* const mutex_;
01721
01722 GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock);
01723 };
01724
01725 typedef GTestMutexLock MutexLock;
01726
01727
01728
01729 class ThreadLocalValueHolderBase {
01730 public:
01731 virtual ~ThreadLocalValueHolderBase() {}
01732 };
01733
01734
01735
01736 class ThreadLocalBase {
01737 public:
01738
01739
01740
01741
01742 virtual ThreadLocalValueHolderBase* NewValueForCurrentThread() const = 0;
01743
01744 protected:
01745 ThreadLocalBase() {}
01746 virtual ~ThreadLocalBase() {}
01747
01748 private:
01749 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocalBase);
01750 };
01751
01752
01753
01754
01755 class GTEST_API_ ThreadLocalRegistry {
01756 public:
01757
01758
01759 static ThreadLocalValueHolderBase* GetValueOnCurrentThread(
01760 const ThreadLocalBase* thread_local_instance);
01761
01762
01763 static void OnThreadLocalDestroyed(
01764 const ThreadLocalBase* thread_local_instance);
01765 };
01766
01767 class GTEST_API_ ThreadWithParamBase {
01768 public:
01769 void Join();
01770
01771 protected:
01772 class Runnable {
01773 public:
01774 virtual ~Runnable() {}
01775 virtual void Run() = 0;
01776 };
01777
01778 ThreadWithParamBase(Runnable *runnable, Notification* thread_can_start);
01779 virtual ~ThreadWithParamBase();
01780
01781 private:
01782 AutoHandle thread_;
01783 };
01784
01785
01786 template <typename T>
01787 class ThreadWithParam : public ThreadWithParamBase {
01788 public:
01789 typedef void UserThreadFunc(T);
01790
01791 ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start)
01792 : ThreadWithParamBase(new RunnableImpl(func, param), thread_can_start) {
01793 }
01794 virtual ~ThreadWithParam() {}
01795
01796 private:
01797 class RunnableImpl : public Runnable {
01798 public:
01799 RunnableImpl(UserThreadFunc* func, T param)
01800 : func_(func),
01801 param_(param) {
01802 }
01803 virtual ~RunnableImpl() {}
01804 virtual void Run() {
01805 func_(param_);
01806 }
01807
01808 private:
01809 UserThreadFunc* const func_;
01810 const T param_;
01811
01812 GTEST_DISALLOW_COPY_AND_ASSIGN_(RunnableImpl);
01813 };
01814
01815 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam);
01816 };
01817
01818
01819
01820
01821
01822
01823
01824
01825
01826
01827
01828
01829
01830
01831
01832
01833
01834
01835
01836
01837
01838
01839
01840
01841
01842
01843
01844
01845 template <typename T>
01846 class ThreadLocal : public ThreadLocalBase {
01847 public:
01848 ThreadLocal() : default_factory_(new DefaultValueHolderFactory()) {}
01849 explicit ThreadLocal(const T& value)
01850 : default_factory_(new InstanceValueHolderFactory(value)) {}
01851
01852 ~ThreadLocal() { ThreadLocalRegistry::OnThreadLocalDestroyed(this); }
01853
01854 T* pointer() { return GetOrCreateValue(); }
01855 const T* pointer() const { return GetOrCreateValue(); }
01856 const T& get() const { return *pointer(); }
01857 void set(const T& value) { *pointer() = value; }
01858
01859 private:
01860
01861
01862 class ValueHolder : public ThreadLocalValueHolderBase {
01863 public:
01864 ValueHolder() : value_() {}
01865 explicit ValueHolder(const T& value) : value_(value) {}
01866
01867 T* pointer() { return &value_; }
01868
01869 private:
01870 T value_;
01871 GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder);
01872 };
01873
01874
01875 T* GetOrCreateValue() const {
01876 return static_cast<ValueHolder*>(
01877 ThreadLocalRegistry::GetValueOnCurrentThread(this))->pointer();
01878 }
01879
01880 virtual ThreadLocalValueHolderBase* NewValueForCurrentThread() const {
01881 return default_factory_->MakeNewHolder();
01882 }
01883
01884 class ValueHolderFactory {
01885 public:
01886 ValueHolderFactory() {}
01887 virtual ~ValueHolderFactory() {}
01888 virtual ValueHolder* MakeNewHolder() const = 0;
01889
01890 private:
01891 GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolderFactory);
01892 };
01893
01894 class DefaultValueHolderFactory : public ValueHolderFactory {
01895 public:
01896 DefaultValueHolderFactory() {}
01897 virtual ValueHolder* MakeNewHolder() const { return new ValueHolder(); }
01898
01899 private:
01900 GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultValueHolderFactory);
01901 };
01902
01903 class InstanceValueHolderFactory : public ValueHolderFactory {
01904 public:
01905 explicit InstanceValueHolderFactory(const T& value) : value_(value) {}
01906 virtual ValueHolder* MakeNewHolder() const {
01907 return new ValueHolder(value_);
01908 }
01909
01910 private:
01911 const T value_;
01912
01913 GTEST_DISALLOW_COPY_AND_ASSIGN_(InstanceValueHolderFactory);
01914 };
01915
01916 scoped_ptr<ValueHolderFactory> default_factory_;
01917
01918 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal);
01919 };
01920
01921 # elif GTEST_HAS_PTHREAD
01922
01923
01924 class MutexBase {
01925 public:
01926
01927 void Lock() {
01928 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_));
01929 owner_ = pthread_self();
01930 has_owner_ = true;
01931 }
01932
01933
01934 void Unlock() {
01935
01936
01937
01938
01939 has_owner_ = false;
01940 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_));
01941 }
01942
01943
01944
01945 void AssertHeld() const {
01946 GTEST_CHECK_(has_owner_ && pthread_equal(owner_, pthread_self()))
01947 << "The current thread is not holding the mutex @" << this;
01948 }
01949
01950
01951
01952
01953
01954
01955 public:
01956 pthread_mutex_t mutex_;
01957
01958
01959
01960
01961
01962
01963 bool has_owner_;
01964 pthread_t owner_;
01965 };
01966
01967
01968 # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
01969 extern ::testing::internal::MutexBase mutex
01970
01971
01972
01973
01974
01975
01976
01977 # define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
01978 ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, false }
01979
01980
01981
01982 class Mutex : public MutexBase {
01983 public:
01984 Mutex() {
01985 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));
01986 has_owner_ = false;
01987 }
01988 ~Mutex() {
01989 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_));
01990 }
01991
01992 private:
01993 GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex);
01994 };
01995
01996
01997
01998
01999
02000
02001 class GTestMutexLock {
02002 public:
02003 explicit GTestMutexLock(MutexBase* mutex)
02004 : mutex_(mutex) { mutex_->Lock(); }
02005
02006 ~GTestMutexLock() { mutex_->Unlock(); }
02007
02008 private:
02009 MutexBase* const mutex_;
02010
02011 GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock);
02012 };
02013
02014 typedef GTestMutexLock MutexLock;
02015
02016
02017
02018
02019
02020
02021
02022 class ThreadLocalValueHolderBase {
02023 public:
02024 virtual ~ThreadLocalValueHolderBase() {}
02025 };
02026
02027
02028
02029 extern "C" inline void DeleteThreadLocalValue(void* value_holder) {
02030 delete static_cast<ThreadLocalValueHolderBase*>(value_holder);
02031 }
02032
02033
02034 template <typename T>
02035 class ThreadLocal {
02036 public:
02037 ThreadLocal()
02038 : key_(CreateKey()), default_factory_(new DefaultValueHolderFactory()) {}
02039 explicit ThreadLocal(const T& value)
02040 : key_(CreateKey()),
02041 default_factory_(new InstanceValueHolderFactory(value)) {}
02042
02043 ~ThreadLocal() {
02044
02045 DeleteThreadLocalValue(pthread_getspecific(key_));
02046
02047
02048
02049 GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_));
02050 }
02051
02052 T* pointer() { return GetOrCreateValue(); }
02053 const T* pointer() const { return GetOrCreateValue(); }
02054 const T& get() const { return *pointer(); }
02055 void set(const T& value) { *pointer() = value; }
02056
02057 private:
02058
02059 class ValueHolder : public ThreadLocalValueHolderBase {
02060 public:
02061 ValueHolder() : value_() {}
02062 explicit ValueHolder(const T& value) : value_(value) {}
02063
02064 T* pointer() { return &value_; }
02065
02066 private:
02067 T value_;
02068 GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder);
02069 };
02070
02071 static pthread_key_t CreateKey() {
02072 pthread_key_t key;
02073
02074
02075 GTEST_CHECK_POSIX_SUCCESS_(
02076 pthread_key_create(&key, &DeleteThreadLocalValue));
02077 return key;
02078 }
02079
02080 T* GetOrCreateValue() const {
02081 ThreadLocalValueHolderBase* const holder =
02082 static_cast<ThreadLocalValueHolderBase*>(pthread_getspecific(key_));
02083 if (holder != NULL) {
02084 return CheckedDowncastToActualType<ValueHolder>(holder)->pointer();
02085 }
02086
02087 ValueHolder* const new_holder = default_factory_->MakeNewHolder();
02088 ThreadLocalValueHolderBase* const holder_base = new_holder;
02089 GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base));
02090 return new_holder->pointer();
02091 }
02092
02093 class ValueHolderFactory {
02094 public:
02095 ValueHolderFactory() {}
02096 virtual ~ValueHolderFactory() {}
02097 virtual ValueHolder* MakeNewHolder() const = 0;
02098
02099 private:
02100 GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolderFactory);
02101 };
02102
02103 class DefaultValueHolderFactory : public ValueHolderFactory {
02104 public:
02105 DefaultValueHolderFactory() {}
02106 virtual ValueHolder* MakeNewHolder() const { return new ValueHolder(); }
02107
02108 private:
02109 GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultValueHolderFactory);
02110 };
02111
02112 class InstanceValueHolderFactory : public ValueHolderFactory {
02113 public:
02114 explicit InstanceValueHolderFactory(const T& value) : value_(value) {}
02115 virtual ValueHolder* MakeNewHolder() const {
02116 return new ValueHolder(value_);
02117 }
02118
02119 private:
02120 const T value_;
02121
02122 GTEST_DISALLOW_COPY_AND_ASSIGN_(InstanceValueHolderFactory);
02123 };
02124
02125
02126 const pthread_key_t key_;
02127 scoped_ptr<ValueHolderFactory> default_factory_;
02128
02129 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal);
02130 };
02131
02132 # endif // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
02133
02134 #else // GTEST_IS_THREADSAFE
02135
02136
02137
02138
02139
02140
02141 class Mutex {
02142 public:
02143 Mutex() {}
02144 void Lock() {}
02145 void Unlock() {}
02146 void AssertHeld() const {}
02147 };
02148
02149 # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
02150 extern ::testing::internal::Mutex mutex
02151
02152 # define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex
02153
02154
02155
02156
02157
02158
02159 class GTestMutexLock {
02160 public:
02161 explicit GTestMutexLock(Mutex*) {}
02162 };
02163
02164 typedef GTestMutexLock MutexLock;
02165
02166 template <typename T>
02167 class ThreadLocal {
02168 public:
02169 ThreadLocal() : value_() {}
02170 explicit ThreadLocal(const T& value) : value_(value) {}
02171 T* pointer() { return &value_; }
02172 const T* pointer() const { return &value_; }
02173 const T& get() const { return value_; }
02174 void set(const T& value) { value_ = value; }
02175 private:
02176 T value_;
02177 };
02178
02179 #endif // GTEST_IS_THREADSAFE
02180
02181
02182
02183 GTEST_API_ size_t GetThreadCount();
02184
02185
02186
02187
02188
02189
02190
02191 #if defined(__SYMBIAN32__) || defined(__IBMCPP__) || defined(__SUNPRO_CC)
02192
02193
02194 # define GTEST_ELLIPSIS_NEEDS_POD_ 1
02195 #else
02196 # define GTEST_CAN_COMPARE_NULL 1
02197 #endif
02198
02199
02200
02201
02202
02203 #if defined(__SYMBIAN32__) || defined(__IBMCPP__)
02204 # define GTEST_NEEDS_IS_POINTER_ 1
02205 #endif
02206
02207 template <bool bool_value>
02208 struct bool_constant {
02209 typedef bool_constant<bool_value> type;
02210 static const bool value = bool_value;
02211 };
02212 template <bool bool_value> const bool bool_constant<bool_value>::value;
02213
02214 typedef bool_constant<false> false_type;
02215 typedef bool_constant<true> true_type;
02216
02217 template <typename T>
02218 struct is_pointer : public false_type {};
02219
02220 template <typename T>
02221 struct is_pointer<T*> : public true_type {};
02222
02223 template <typename Iterator>
02224 struct IteratorTraits {
02225 typedef typename Iterator::value_type value_type;
02226 };
02227
02228 template <typename T>
02229 struct IteratorTraits<T*> {
02230 typedef T value_type;
02231 };
02232
02233 template <typename T>
02234 struct IteratorTraits<const T*> {
02235 typedef T value_type;
02236 };
02237
02238 #if GTEST_OS_WINDOWS
02239 # define GTEST_PATH_SEP_ "\\"
02240 # define GTEST_HAS_ALT_PATH_SEP_ 1
02241
02242 typedef __int64 BiggestInt;
02243 #else
02244 # define GTEST_PATH_SEP_ "/"
02245 # define GTEST_HAS_ALT_PATH_SEP_ 0
02246 typedef long long BiggestInt;
02247 #endif // GTEST_OS_WINDOWS
02248
02249
02250
02251
02252
02253
02254
02255
02256 inline bool IsAlpha(char ch) {
02257 return isalpha(static_cast<unsigned char>(ch)) != 0;
02258 }
02259 inline bool IsAlNum(char ch) {
02260 return isalnum(static_cast<unsigned char>(ch)) != 0;
02261 }
02262 inline bool IsDigit(char ch) {
02263 return isdigit(static_cast<unsigned char>(ch)) != 0;
02264 }
02265 inline bool IsLower(char ch) {
02266 return islower(static_cast<unsigned char>(ch)) != 0;
02267 }
02268 inline bool IsSpace(char ch) {
02269 return isspace(static_cast<unsigned char>(ch)) != 0;
02270 }
02271 inline bool IsUpper(char ch) {
02272 return isupper(static_cast<unsigned char>(ch)) != 0;
02273 }
02274 inline bool IsXDigit(char ch) {
02275 return isxdigit(static_cast<unsigned char>(ch)) != 0;
02276 }
02277 inline bool IsXDigit(wchar_t ch) {
02278 const unsigned char low_byte = static_cast<unsigned char>(ch);
02279 return ch == low_byte && isxdigit(low_byte) != 0;
02280 }
02281
02282 inline char ToLower(char ch) {
02283 return static_cast<char>(tolower(static_cast<unsigned char>(ch)));
02284 }
02285 inline char ToUpper(char ch) {
02286 return static_cast<char>(toupper(static_cast<unsigned char>(ch)));
02287 }
02288
02289 inline std::string StripTrailingSpaces(std::string str) {
02290 std::string::iterator it = str.end();
02291 while (it != str.begin() && IsSpace(*--it))
02292 it = str.erase(it);
02293 return str;
02294 }
02295
02296
02297
02298
02299
02300
02301
02302 namespace posix {
02303
02304
02305
02306 #if GTEST_OS_WINDOWS
02307
02308 typedef struct _stat StatStruct;
02309
02310 # ifdef __BORLANDC__
02311 inline int IsATTY(int fd) { return isatty(fd); }
02312 inline int StrCaseCmp(const char* s1, const char* s2) {
02313 return stricmp(s1, s2);
02314 }
02315 inline char* StrDup(const char* src) { return strdup(src); }
02316 # else // !__BORLANDC__
02317 # if GTEST_OS_WINDOWS_MOBILE
02318 inline int IsATTY(int ) { return 0; }
02319 # else
02320 inline int IsATTY(int fd) { return _isatty(fd); }
02321 # endif // GTEST_OS_WINDOWS_MOBILE
02322 inline int StrCaseCmp(const char* s1, const char* s2) {
02323 return _stricmp(s1, s2);
02324 }
02325 inline char* StrDup(const char* src) { return _strdup(src); }
02326 # endif // __BORLANDC__
02327
02328 # if GTEST_OS_WINDOWS_MOBILE
02329 inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
02330
02331
02332 # else
02333 inline int FileNo(FILE* file) { return _fileno(file); }
02334 inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
02335 inline int RmDir(const char* dir) { return _rmdir(dir); }
02336 inline bool IsDir(const StatStruct& st) {
02337 return (_S_IFDIR & st.st_mode) != 0;
02338 }
02339 # endif // GTEST_OS_WINDOWS_MOBILE
02340
02341 #else
02342
02343 typedef struct stat StatStruct;
02344
02345 inline int FileNo(FILE* file) { return fileno(file); }
02346 inline int IsATTY(int fd) { return isatty(fd); }
02347 inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
02348 inline int StrCaseCmp(const char* s1, const char* s2) {
02349 return strcasecmp(s1, s2);
02350 }
02351 inline char* StrDup(const char* src) { return strdup(src); }
02352 inline int RmDir(const char* dir) { return rmdir(dir); }
02353 inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
02354
02355 #endif // GTEST_OS_WINDOWS
02356
02357
02358
02359 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996 )
02360
02361 inline const char* StrNCpy(char* dest, const char* src, size_t n) {
02362 return strncpy(dest, src, n);
02363 }
02364
02365
02366
02367
02368
02369 #if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
02370 inline int ChDir(const char* dir) { return chdir(dir); }
02371 #endif
02372 inline FILE* FOpen(const char* path, const char* mode) {
02373 return fopen(path, mode);
02374 }
02375 #if !GTEST_OS_WINDOWS_MOBILE
02376 inline FILE *FReopen(const char* path, const char* mode, FILE* stream) {
02377 return freopen(path, mode, stream);
02378 }
02379 inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
02380 #endif
02381 inline int FClose(FILE* fp) { return fclose(fp); }
02382 #if !GTEST_OS_WINDOWS_MOBILE
02383 inline int Read(int fd, void* buf, unsigned int count) {
02384 return static_cast<int>(read(fd, buf, count));
02385 }
02386 inline int Write(int fd, const void* buf, unsigned int count) {
02387 return static_cast<int>(write(fd, buf, count));
02388 }
02389 inline int Close(int fd) { return close(fd); }
02390 inline const char* StrError(int errnum) { return strerror(errnum); }
02391 #endif
02392 inline const char* GetEnv(const char* name) {
02393 #if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE | GTEST_OS_WINDOWS_RT
02394
02395 static_cast<void>(name);
02396 return NULL;
02397 #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
02398
02399
02400 const char* const env = getenv(name);
02401 return (env != NULL && env[0] != '\0') ? env : NULL;
02402 #else
02403 return getenv(name);
02404 #endif
02405 }
02406
02407 GTEST_DISABLE_MSC_WARNINGS_POP_()
02408
02409 #if GTEST_OS_WINDOWS_MOBILE
02410
02411
02412
02413 void Abort();
02414 #else
02415 inline void Abort() { abort(); }
02416 #endif // GTEST_OS_WINDOWS_MOBILE
02417
02418 }
02419
02420
02421
02422
02423
02424
02425 #if _MSC_VER >= 1400 && !GTEST_OS_WINDOWS_MOBILE
02426
02427 # define GTEST_SNPRINTF_(buffer, size, format, ...) \
02428 _snprintf_s(buffer, size, size, format, __VA_ARGS__)
02429 #elif defined(_MSC_VER)
02430
02431
02432 # define GTEST_SNPRINTF_ _snprintf
02433 #else
02434 # define GTEST_SNPRINTF_ snprintf
02435 #endif
02436
02437
02438
02439
02440
02441
02442
02443
02444 const BiggestInt kMaxBiggestInt =
02445 ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1));
02446
02447
02448
02449
02450
02451
02452
02453
02454
02455
02456
02457
02458
02459
02460
02461
02462
02463
02464
02465 template <size_t size>
02466 class TypeWithSize {
02467 public:
02468
02469
02470 typedef void UInt;
02471 };
02472
02473
02474 template <>
02475 class TypeWithSize<4> {
02476 public:
02477
02478
02479
02480
02481 typedef int Int;
02482 typedef unsigned int UInt;
02483 };
02484
02485
02486 template <>
02487 class TypeWithSize<8> {
02488 public:
02489 #if GTEST_OS_WINDOWS
02490 typedef __int64 Int;
02491 typedef unsigned __int64 UInt;
02492 #else
02493 typedef long long Int;
02494 typedef unsigned long long UInt;
02495 #endif // GTEST_OS_WINDOWS
02496 };
02497
02498
02499 typedef TypeWithSize<4>::Int Int32;
02500 typedef TypeWithSize<4>::UInt UInt32;
02501 typedef TypeWithSize<8>::Int Int64;
02502 typedef TypeWithSize<8>::UInt UInt64;
02503 typedef TypeWithSize<8>::Int TimeInMillis;
02504
02505
02506
02507
02508 #if !defined(GTEST_FLAG)
02509 # define GTEST_FLAG(name) FLAGS_gtest_##name
02510 #endif // !defined(GTEST_FLAG)
02511
02512 #if !defined(GTEST_USE_OWN_FLAGFILE_FLAG_)
02513 # define GTEST_USE_OWN_FLAGFILE_FLAG_ 1
02514 #endif // !defined(GTEST_USE_OWN_FLAGFILE_FLAG_)
02515
02516 #if !defined(GTEST_DECLARE_bool_)
02517 # define GTEST_FLAG_SAVER_ ::testing::internal::GTestFlagSaver
02518
02519
02520 # define GTEST_DECLARE_bool_(name) GTEST_API_ extern bool GTEST_FLAG(name)
02521 # define GTEST_DECLARE_int32_(name) \
02522 GTEST_API_ extern ::testing::internal::Int32 GTEST_FLAG(name)
02523 #define GTEST_DECLARE_string_(name) \
02524 GTEST_API_ extern ::std::string GTEST_FLAG(name)
02525
02526
02527 #define GTEST_DEFINE_bool_(name, default_val, doc) \
02528 GTEST_API_ bool GTEST_FLAG(name) = (default_val)
02529 #define GTEST_DEFINE_int32_(name, default_val, doc) \
02530 GTEST_API_ ::testing::internal::Int32 GTEST_FLAG(name) = (default_val)
02531 #define GTEST_DEFINE_string_(name, default_val, doc) \
02532 GTEST_API_ ::std::string GTEST_FLAG(name) = (default_val)
02533
02534 #endif // !defined(GTEST_DECLARE_bool_)
02535
02536
02537 #if !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)
02538 # define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
02539 # define GTEST_LOCK_EXCLUDED_(locks)
02540 #endif // !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)
02541
02542
02543
02544
02545
02546
02547
02548 bool ParseInt32(const Message& src_text, const char* str, Int32* value);
02549
02550
02551
02552 bool BoolFromGTestEnv(const char* flag, bool default_val);
02553 GTEST_API_ Int32 Int32FromGTestEnv(const char* flag, Int32 default_val);
02554 const char* StringFromGTestEnv(const char* flag, const char* default_val);
02555
02556 }
02557 }
02558
02559 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
02560