gmock-port.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2008, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // Low-level types and utilities for porting Google Mock to various
  30. // platforms. All macros ending with _ and symbols defined in an
  31. // internal namespace are subject to change without notice. Code
  32. // outside Google Mock MUST NOT USE THEM DIRECTLY. Macros that don't
  33. // end with _ are part of Google Mock's public API and can be used by
  34. // code outside Google Mock.
  35. // IWYU pragma: private, include "gmock/gmock.h"
  36. // IWYU pragma: friend gmock/.*
  37. #ifndef GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_PORT_H_
  38. #define GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_PORT_H_
  39. #include <assert.h>
  40. #include <stdlib.h>
  41. #include <cstdint>
  42. #include <iostream>
  43. // Most of the utilities needed for porting Google Mock are also
  44. // required for Google Test and are defined in gtest-port.h.
  45. //
  46. // Note to maintainers: to reduce code duplication, prefer adding
  47. // portability utilities to Google Test's gtest-port.h instead of
  48. // here, as Google Mock depends on Google Test. Only add a utility
  49. // here if it's truly specific to Google Mock.
  50. #include "gmock/internal/custom/gmock-port.h"
  51. #include "gtest/internal/gtest-port.h"
  52. #if defined(GTEST_HAS_ABSL) && !defined(GTEST_NO_ABSL_FLAGS)
  53. #include "absl/flags/declare.h"
  54. #include "absl/flags/flag.h"
  55. #endif
  56. // For MS Visual C++, check the compiler version. At least VS 2015 is
  57. // required to compile Google Mock.
  58. #if defined(_MSC_VER) && _MSC_VER < 1900
  59. #error "At least Visual C++ 2015 (14.0) is required to compile Google Mock."
  60. #endif
  61. // Macro for referencing flags. This is public as we want the user to
  62. // use this syntax to reference Google Mock flags.
  63. #define GMOCK_FLAG_NAME_(name) gmock_##name
  64. #define GMOCK_FLAG(name) FLAGS_gmock_##name
  65. // Pick a command line flags implementation.
  66. #if defined(GTEST_HAS_ABSL) && !defined(GTEST_NO_ABSL_FLAGS)
  67. // Macros for defining flags.
  68. #define GMOCK_DEFINE_bool_(name, default_val, doc) \
  69. ABSL_FLAG(bool, GMOCK_FLAG_NAME_(name), default_val, doc)
  70. #define GMOCK_DEFINE_int32_(name, default_val, doc) \
  71. ABSL_FLAG(int32_t, GMOCK_FLAG_NAME_(name), default_val, doc)
  72. #define GMOCK_DEFINE_string_(name, default_val, doc) \
  73. ABSL_FLAG(std::string, GMOCK_FLAG_NAME_(name), default_val, doc)
  74. // Macros for declaring flags.
  75. #define GMOCK_DECLARE_bool_(name) \
  76. ABSL_DECLARE_FLAG(bool, GMOCK_FLAG_NAME_(name))
  77. #define GMOCK_DECLARE_int32_(name) \
  78. ABSL_DECLARE_FLAG(int32_t, GMOCK_FLAG_NAME_(name))
  79. #define GMOCK_DECLARE_string_(name) \
  80. ABSL_DECLARE_FLAG(std::string, GMOCK_FLAG_NAME_(name))
  81. #define GMOCK_FLAG_GET(name) ::absl::GetFlag(GMOCK_FLAG(name))
  82. #define GMOCK_FLAG_SET(name, value) \
  83. (void)(::absl::SetFlag(&GMOCK_FLAG(name), value))
  84. #else // defined(GTEST_HAS_ABSL) && !defined(GTEST_NO_ABSL_FLAGS)
  85. // Macros for defining flags.
  86. #define GMOCK_DEFINE_bool_(name, default_val, doc) \
  87. namespace testing { \
  88. GTEST_API_ bool GMOCK_FLAG(name) = (default_val); \
  89. } \
  90. static_assert(true, "no-op to require trailing semicolon")
  91. #define GMOCK_DEFINE_int32_(name, default_val, doc) \
  92. namespace testing { \
  93. GTEST_API_ int32_t GMOCK_FLAG(name) = (default_val); \
  94. } \
  95. static_assert(true, "no-op to require trailing semicolon")
  96. #define GMOCK_DEFINE_string_(name, default_val, doc) \
  97. namespace testing { \
  98. GTEST_API_ ::std::string GMOCK_FLAG(name) = (default_val); \
  99. } \
  100. static_assert(true, "no-op to require trailing semicolon")
  101. // Macros for declaring flags.
  102. #define GMOCK_DECLARE_bool_(name) \
  103. namespace testing { \
  104. GTEST_API_ extern bool GMOCK_FLAG(name); \
  105. } \
  106. static_assert(true, "no-op to require trailing semicolon")
  107. #define GMOCK_DECLARE_int32_(name) \
  108. namespace testing { \
  109. GTEST_API_ extern int32_t GMOCK_FLAG(name); \
  110. } \
  111. static_assert(true, "no-op to require trailing semicolon")
  112. #define GMOCK_DECLARE_string_(name) \
  113. namespace testing { \
  114. GTEST_API_ extern ::std::string GMOCK_FLAG(name); \
  115. } \
  116. static_assert(true, "no-op to require trailing semicolon")
  117. #define GMOCK_FLAG_GET(name) ::testing::GMOCK_FLAG(name)
  118. #define GMOCK_FLAG_SET(name, value) (void)(::testing::GMOCK_FLAG(name) = value)
  119. #endif // defined(GTEST_HAS_ABSL) && !defined(GTEST_NO_ABSL_FLAGS)
  120. #endif // GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_PORT_H_