You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

348 lines
9.4 KiB

  1. $$ -*- mode: c++; -*-
  2. $var n = 10 $$ Maximum number of tuple fields we want to support.
  3. $$ This meta comment fixes auto-indentation in Emacs. }}
  4. // Copyright 2009 Google Inc.
  5. // All Rights Reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are
  9. // met:
  10. //
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Google Inc. nor the names of its
  18. // contributors may be used to endorse or promote products derived from
  19. // this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. // Author: wan@google.com (Zhanyong Wan)
  34. // Implements a subset of TR1 tuple needed by Google Test and Google Mock.
  35. #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TUPLE_H_
  36. #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TUPLE_H_
  37. #include <utility> // For ::std::pair.
  38. // The compiler used in Symbian has a bug that prevents us from declaring the
  39. // tuple template as a friend (it complains that tuple is redefined). This
  40. // hack bypasses the bug by declaring the members that should otherwise be
  41. // private as public.
  42. // Sun Studio versions < 12 also have the above bug.
  43. #if defined(__SYMBIAN32__) || (defined(__SUNPRO_CC) && __SUNPRO_CC < 0x590)
  44. # define GTEST_DECLARE_TUPLE_AS_FRIEND_ public:
  45. #else
  46. # define GTEST_DECLARE_TUPLE_AS_FRIEND_ \
  47. template <GTEST_$(n)_TYPENAMES_(U)> friend class tuple; \
  48. private:
  49. #endif
  50. // Visual Studio 2010, 2012, and 2013 define symbols in std::tr1 that conflict
  51. // with our own definitions. Therefore using our own tuple does not work on
  52. // those compilers.
  53. #if defined(_MSC_VER) && _MSC_VER >= 1600 /* 1600 is Visual Studio 2010 */
  54. # error "gtest's tuple doesn't compile on Visual Studio 2010 or later. \
  55. GTEST_USE_OWN_TR1_TUPLE must be set to 0 on those compilers."
  56. #endif
  57. $range i 0..n-1
  58. $range j 0..n
  59. $range k 1..n
  60. // GTEST_n_TUPLE_(T) is the type of an n-tuple.
  61. #define GTEST_0_TUPLE_(T) tuple<>
  62. $for k [[
  63. $range m 0..k-1
  64. $range m2 k..n-1
  65. #define GTEST_$(k)_TUPLE_(T) tuple<$for m, [[T##$m]]$for m2 [[, void]]>
  66. ]]
  67. // GTEST_n_TYPENAMES_(T) declares a list of n typenames.
  68. $for j [[
  69. $range m 0..j-1
  70. #define GTEST_$(j)_TYPENAMES_(T) $for m, [[typename T##$m]]
  71. ]]
  72. // In theory, defining stuff in the ::std namespace is undefined
  73. // behavior. We can do this as we are playing the role of a standard
  74. // library vendor.
  75. namespace std {
  76. namespace tr1 {
  77. template <$for i, [[typename T$i = void]]>
  78. class tuple;
  79. // Anything in namespace gtest_internal is Google Test's INTERNAL
  80. // IMPLEMENTATION DETAIL and MUST NOT BE USED DIRECTLY in user code.
  81. namespace gtest_internal {
  82. // ByRef<T>::type is T if T is a reference; otherwise it's const T&.
  83. template <typename T>
  84. struct ByRef { typedef const T& type; }; // NOLINT
  85. template <typename T>
  86. struct ByRef<T&> { typedef T& type; }; // NOLINT
  87. // A handy wrapper for ByRef.
  88. #define GTEST_BY_REF_(T) typename ::std::tr1::gtest_internal::ByRef<T>::type
  89. // AddRef<T>::type is T if T is a reference; otherwise it's T&. This
  90. // is the same as tr1::add_reference<T>::type.
  91. template <typename T>
  92. struct AddRef { typedef T& type; }; // NOLINT
  93. template <typename T>
  94. struct AddRef<T&> { typedef T& type; }; // NOLINT
  95. // A handy wrapper for AddRef.
  96. #define GTEST_ADD_REF_(T) typename ::std::tr1::gtest_internal::AddRef<T>::type
  97. // A helper for implementing get<k>().
  98. template <int k> class Get;
  99. // A helper for implementing tuple_element<k, T>. kIndexValid is true
  100. // iff k < the number of fields in tuple type T.
  101. template <bool kIndexValid, int kIndex, class Tuple>
  102. struct TupleElement;
  103. $for i [[
  104. template <GTEST_$(n)_TYPENAMES_(T)>
  105. struct TupleElement<true, $i, GTEST_$(n)_TUPLE_(T) > {
  106. typedef T$i type;
  107. };
  108. ]]
  109. } // namespace gtest_internal
  110. template <>
  111. class tuple<> {
  112. public:
  113. tuple() {}
  114. tuple(const tuple& /* t */) {}
  115. tuple& operator=(const tuple& /* t */) { return *this; }
  116. };
  117. $for k [[
  118. $range m 0..k-1
  119. template <GTEST_$(k)_TYPENAMES_(T)>
  120. class $if k < n [[GTEST_$(k)_TUPLE_(T)]] $else [[tuple]] {
  121. public:
  122. template <int k> friend class gtest_internal::Get;
  123. tuple() : $for m, [[f$(m)_()]] {}
  124. explicit tuple($for m, [[GTEST_BY_REF_(T$m) f$m]]) : [[]]
  125. $for m, [[f$(m)_(f$m)]] {}
  126. tuple(const tuple& t) : $for m, [[f$(m)_(t.f$(m)_)]] {}
  127. template <GTEST_$(k)_TYPENAMES_(U)>
  128. tuple(const GTEST_$(k)_TUPLE_(U)& t) : $for m, [[f$(m)_(t.f$(m)_)]] {}
  129. $if k == 2 [[
  130. template <typename U0, typename U1>
  131. tuple(const ::std::pair<U0, U1>& p) : f0_(p.first), f1_(p.second) {}
  132. ]]
  133. tuple& operator=(const tuple& t) { return CopyFrom(t); }
  134. template <GTEST_$(k)_TYPENAMES_(U)>
  135. tuple& operator=(const GTEST_$(k)_TUPLE_(U)& t) {
  136. return CopyFrom(t);
  137. }
  138. $if k == 2 [[
  139. template <typename U0, typename U1>
  140. tuple& operator=(const ::std::pair<U0, U1>& p) {
  141. f0_ = p.first;
  142. f1_ = p.second;
  143. return *this;
  144. }
  145. ]]
  146. GTEST_DECLARE_TUPLE_AS_FRIEND_
  147. template <GTEST_$(k)_TYPENAMES_(U)>
  148. tuple& CopyFrom(const GTEST_$(k)_TUPLE_(U)& t) {
  149. $for m [[
  150. f$(m)_ = t.f$(m)_;
  151. ]]
  152. return *this;
  153. }
  154. $for m [[
  155. T$m f$(m)_;
  156. ]]
  157. };
  158. ]]
  159. // 6.1.3.2 Tuple creation functions.
  160. // Known limitations: we don't support passing an
  161. // std::tr1::reference_wrapper<T> to make_tuple(). And we don't
  162. // implement tie().
  163. inline tuple<> make_tuple() { return tuple<>(); }
  164. $for k [[
  165. $range m 0..k-1
  166. template <GTEST_$(k)_TYPENAMES_(T)>
  167. inline GTEST_$(k)_TUPLE_(T) make_tuple($for m, [[const T$m& f$m]]) {
  168. return GTEST_$(k)_TUPLE_(T)($for m, [[f$m]]);
  169. }
  170. ]]
  171. // 6.1.3.3 Tuple helper classes.
  172. template <typename Tuple> struct tuple_size;
  173. $for j [[
  174. template <GTEST_$(j)_TYPENAMES_(T)>
  175. struct tuple_size<GTEST_$(j)_TUPLE_(T) > {
  176. static const int value = $j;
  177. };
  178. ]]
  179. template <int k, class Tuple>
  180. struct tuple_element {
  181. typedef typename gtest_internal::TupleElement<
  182. k < (tuple_size<Tuple>::value), k, Tuple>::type type;
  183. };
  184. #define GTEST_TUPLE_ELEMENT_(k, Tuple) typename tuple_element<k, Tuple >::type
  185. // 6.1.3.4 Element access.
  186. namespace gtest_internal {
  187. $for i [[
  188. template <>
  189. class Get<$i> {
  190. public:
  191. template <class Tuple>
  192. static GTEST_ADD_REF_(GTEST_TUPLE_ELEMENT_($i, Tuple))
  193. Field(Tuple& t) { return t.f$(i)_; } // NOLINT
  194. template <class Tuple>
  195. static GTEST_BY_REF_(GTEST_TUPLE_ELEMENT_($i, Tuple))
  196. ConstField(const Tuple& t) { return t.f$(i)_; }
  197. };
  198. ]]
  199. } // namespace gtest_internal
  200. template <int k, GTEST_$(n)_TYPENAMES_(T)>
  201. GTEST_ADD_REF_(GTEST_TUPLE_ELEMENT_(k, GTEST_$(n)_TUPLE_(T)))
  202. get(GTEST_$(n)_TUPLE_(T)& t) {
  203. return gtest_internal::Get<k>::Field(t);
  204. }
  205. template <int k, GTEST_$(n)_TYPENAMES_(T)>
  206. GTEST_BY_REF_(GTEST_TUPLE_ELEMENT_(k, GTEST_$(n)_TUPLE_(T)))
  207. get(const GTEST_$(n)_TUPLE_(T)& t) {
  208. return gtest_internal::Get<k>::ConstField(t);
  209. }
  210. // 6.1.3.5 Relational operators
  211. // We only implement == and !=, as we don't have a need for the rest yet.
  212. namespace gtest_internal {
  213. // SameSizeTuplePrefixComparator<k, k>::Eq(t1, t2) returns true if the
  214. // first k fields of t1 equals the first k fields of t2.
  215. // SameSizeTuplePrefixComparator(k1, k2) would be a compiler error if
  216. // k1 != k2.
  217. template <int kSize1, int kSize2>
  218. struct SameSizeTuplePrefixComparator;
  219. template <>
  220. struct SameSizeTuplePrefixComparator<0, 0> {
  221. template <class Tuple1, class Tuple2>
  222. static bool Eq(const Tuple1& /* t1 */, const Tuple2& /* t2 */) {
  223. return true;
  224. }
  225. };
  226. template <int k>
  227. struct SameSizeTuplePrefixComparator<k, k> {
  228. template <class Tuple1, class Tuple2>
  229. static bool Eq(const Tuple1& t1, const Tuple2& t2) {
  230. return SameSizeTuplePrefixComparator<k - 1, k - 1>::Eq(t1, t2) &&
  231. ::std::tr1::get<k - 1>(t1) == ::std::tr1::get<k - 1>(t2);
  232. }
  233. };
  234. } // namespace gtest_internal
  235. template <GTEST_$(n)_TYPENAMES_(T), GTEST_$(n)_TYPENAMES_(U)>
  236. inline bool operator==(const GTEST_$(n)_TUPLE_(T)& t,
  237. const GTEST_$(n)_TUPLE_(U)& u) {
  238. return gtest_internal::SameSizeTuplePrefixComparator<
  239. tuple_size<GTEST_$(n)_TUPLE_(T) >::value,
  240. tuple_size<GTEST_$(n)_TUPLE_(U) >::value>::Eq(t, u);
  241. }
  242. template <GTEST_$(n)_TYPENAMES_(T), GTEST_$(n)_TYPENAMES_(U)>
  243. inline bool operator!=(const GTEST_$(n)_TUPLE_(T)& t,
  244. const GTEST_$(n)_TUPLE_(U)& u) { return !(t == u); }
  245. // 6.1.4 Pairs.
  246. // Unimplemented.
  247. } // namespace tr1
  248. } // namespace std
  249. $for j [[
  250. #undef GTEST_$(j)_TUPLE_
  251. ]]
  252. $for j [[
  253. #undef GTEST_$(j)_TYPENAMES_
  254. ]]
  255. #undef GTEST_DECLARE_TUPLE_AS_FRIEND_
  256. #undef GTEST_BY_REF_
  257. #undef GTEST_ADD_REF_
  258. #undef GTEST_TUPLE_ELEMENT_
  259. #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TUPLE_H_