gtest-tuple.h.pump 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. // Implements a subset of TR1 tuple needed by Google Test and Google Mock.
  33. // GOOGLETEST_CM0001 DO NOT DELETE
  34. #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TUPLE_H_
  35. #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TUPLE_H_
  36. #include <utility> // For ::std::pair.
  37. // The compiler used in Symbian has a bug that prevents us from declaring the
  38. // tuple template as a friend (it complains that tuple is redefined). This
  39. // bypasses the bug by declaring the members that should otherwise be
  40. // private as public.
  41. // Sun Studio versions < 12 also have the above bug.
  42. #if defined(__SYMBIAN32__) || (defined(__SUNPRO_CC) && __SUNPRO_CC < 0x590)
  43. # define GTEST_DECLARE_TUPLE_AS_FRIEND_ public:
  44. #else
  45. # define GTEST_DECLARE_TUPLE_AS_FRIEND_ \
  46. template <GTEST_$(n)_TYPENAMES_(U)> friend class tuple; \
  47. private:
  48. #endif
  49. // Visual Studio 2010, 2012, and 2013 define symbols in std::tr1 that conflict
  50. // with our own definitions. Therefore using our own tuple does not work on
  51. // those compilers.
  52. #if defined(_MSC_VER) && _MSC_VER >= 1600 /* 1600 is Visual Studio 2010 */
  53. # error "gtest's tuple doesn't compile on Visual Studio 2010 or later. \
  54. GTEST_USE_OWN_TR1_TUPLE must be set to 0 on those compilers."
  55. #endif
  56. $range i 0..n-1
  57. $range j 0..n
  58. $range k 1..n
  59. // GTEST_n_TUPLE_(T) is the type of an n-tuple.
  60. #define GTEST_0_TUPLE_(T) tuple<>
  61. $for k [[
  62. $range m 0..k-1
  63. $range m2 k..n-1
  64. #define GTEST_$(k)_TUPLE_(T) tuple<$for m, [[T##$m]]$for m2 [[, void]]>
  65. ]]
  66. // GTEST_n_TYPENAMES_(T) declares a list of n typenames.
  67. $for j [[
  68. $range m 0..j-1
  69. #define GTEST_$(j)_TYPENAMES_(T) $for m, [[typename T##$m]]
  70. ]]
  71. // In theory, defining stuff in the ::std namespace is undefined
  72. // behavior. We can do this as we are playing the role of a standard
  73. // library vendor.
  74. namespace std {
  75. namespace tr1 {
  76. template <$for i, [[typename T$i = void]]>
  77. class tuple;
  78. // Anything in namespace gtest_internal is Google Test's INTERNAL
  79. // IMPLEMENTATION DETAIL and MUST NOT BE USED DIRECTLY in user code.
  80. namespace gtest_internal {
  81. // ByRef<T>::type is T if T is a reference; otherwise it's const T&.
  82. template <typename T>
  83. struct ByRef { typedef const T& type; }; // NOLINT
  84. template <typename T>
  85. struct ByRef<T&> { typedef T& type; }; // NOLINT
  86. // A handy wrapper for ByRef.
  87. #define GTEST_BY_REF_(T) typename ::std::tr1::gtest_internal::ByRef<T>::type
  88. // AddRef<T>::type is T if T is a reference; otherwise it's T&. This
  89. // is the same as tr1::add_reference<T>::type.
  90. template <typename T>
  91. struct AddRef { typedef T& type; }; // NOLINT
  92. template <typename T>
  93. struct AddRef<T&> { typedef T& type; }; // NOLINT
  94. // A handy wrapper for AddRef.
  95. #define GTEST_ADD_REF_(T) typename ::std::tr1::gtest_internal::AddRef<T>::type
  96. // A helper for implementing get<k>().
  97. template <int k> class Get;
  98. // A helper for implementing tuple_element<k, T>. kIndexValid is true
  99. // iff k < the number of fields in tuple type T.
  100. template <bool kIndexValid, int kIndex, class Tuple>
  101. struct TupleElement;
  102. $for i [[
  103. template <GTEST_$(n)_TYPENAMES_(T)>
  104. struct TupleElement<true, $i, GTEST_$(n)_TUPLE_(T) > {
  105. typedef T$i type;
  106. };
  107. ]]
  108. } // namespace gtest_internal
  109. template <>
  110. class tuple<> {
  111. public:
  112. tuple() {}
  113. tuple(const tuple& /* t */) {}
  114. tuple& operator=(const tuple& /* t */) { return *this; }
  115. };
  116. $for k [[
  117. $range m 0..k-1
  118. template <GTEST_$(k)_TYPENAMES_(T)>
  119. class $if k < n [[GTEST_$(k)_TUPLE_(T)]] $else [[tuple]] {
  120. public:
  121. template <int k> friend class gtest_internal::Get;
  122. tuple() : $for m, [[f$(m)_()]] {}
  123. explicit tuple($for m, [[GTEST_BY_REF_(T$m) f$m]]) : [[]]
  124. $for m, [[f$(m)_(f$m)]] {}
  125. tuple(const tuple& t) : $for m, [[f$(m)_(t.f$(m)_)]] {}
  126. template <GTEST_$(k)_TYPENAMES_(U)>
  127. tuple(const GTEST_$(k)_TUPLE_(U)& t) : $for m, [[f$(m)_(t.f$(m)_)]] {}
  128. $if k == 2 [[
  129. template <typename U0, typename U1>
  130. tuple(const ::std::pair<U0, U1>& p) : f0_(p.first), f1_(p.second) {}
  131. ]]
  132. tuple& operator=(const tuple& t) { return CopyFrom(t); }
  133. template <GTEST_$(k)_TYPENAMES_(U)>
  134. tuple& operator=(const GTEST_$(k)_TUPLE_(U)& t) {
  135. return CopyFrom(t);
  136. }
  137. $if k == 2 [[
  138. template <typename U0, typename U1>
  139. tuple& operator=(const ::std::pair<U0, U1>& p) {
  140. f0_ = p.first;
  141. f1_ = p.second;
  142. return *this;
  143. }
  144. ]]
  145. GTEST_DECLARE_TUPLE_AS_FRIEND_
  146. template <GTEST_$(k)_TYPENAMES_(U)>
  147. tuple& CopyFrom(const GTEST_$(k)_TUPLE_(U)& t) {
  148. $for m [[
  149. f$(m)_ = t.f$(m)_;
  150. ]]
  151. return *this;
  152. }
  153. $for m [[
  154. T$m f$(m)_;
  155. ]]
  156. };
  157. ]]
  158. // 6.1.3.2 Tuple creation functions.
  159. // Known limitations: we don't support passing an
  160. // std::tr1::reference_wrapper<T> to make_tuple(). And we don't
  161. // implement tie().
  162. inline tuple<> make_tuple() { return tuple<>(); }
  163. $for k [[
  164. $range m 0..k-1
  165. template <GTEST_$(k)_TYPENAMES_(T)>
  166. inline GTEST_$(k)_TUPLE_(T) make_tuple($for m, [[const T$m& f$m]]) {
  167. return GTEST_$(k)_TUPLE_(T)($for m, [[f$m]]);
  168. }
  169. ]]
  170. // 6.1.3.3 Tuple helper classes.
  171. template <typename Tuple> struct tuple_size;
  172. $for j [[
  173. template <GTEST_$(j)_TYPENAMES_(T)>
  174. struct tuple_size<GTEST_$(j)_TUPLE_(T) > {
  175. static const int value = $j;
  176. };
  177. ]]
  178. template <int k, class Tuple>
  179. struct tuple_element {
  180. typedef typename gtest_internal::TupleElement<
  181. k < (tuple_size<Tuple>::value), k, Tuple>::type type;
  182. };
  183. #define GTEST_TUPLE_ELEMENT_(k, Tuple) typename tuple_element<k, Tuple >::type
  184. // 6.1.3.4 Element access.
  185. namespace gtest_internal {
  186. $for i [[
  187. template <>
  188. class Get<$i> {
  189. public:
  190. template <class Tuple>
  191. static GTEST_ADD_REF_(GTEST_TUPLE_ELEMENT_($i, Tuple))
  192. Field(Tuple& t) { return t.f$(i)_; } // NOLINT
  193. template <class Tuple>
  194. static GTEST_BY_REF_(GTEST_TUPLE_ELEMENT_($i, Tuple))
  195. ConstField(const Tuple& t) { return t.f$(i)_; }
  196. };
  197. ]]
  198. } // namespace gtest_internal
  199. template <int k, GTEST_$(n)_TYPENAMES_(T)>
  200. GTEST_ADD_REF_(GTEST_TUPLE_ELEMENT_(k, GTEST_$(n)_TUPLE_(T)))
  201. get(GTEST_$(n)_TUPLE_(T)& t) {
  202. return gtest_internal::Get<k>::Field(t);
  203. }
  204. template <int k, GTEST_$(n)_TYPENAMES_(T)>
  205. GTEST_BY_REF_(GTEST_TUPLE_ELEMENT_(k, GTEST_$(n)_TUPLE_(T)))
  206. get(const GTEST_$(n)_TUPLE_(T)& t) {
  207. return gtest_internal::Get<k>::ConstField(t);
  208. }
  209. // 6.1.3.5 Relational operators
  210. // We only implement == and !=, as we don't have a need for the rest yet.
  211. namespace gtest_internal {
  212. // SameSizeTuplePrefixComparator<k, k>::Eq(t1, t2) returns true if the
  213. // first k fields of t1 equals the first k fields of t2.
  214. // SameSizeTuplePrefixComparator(k1, k2) would be a compiler error if
  215. // k1 != k2.
  216. template <int kSize1, int kSize2>
  217. struct SameSizeTuplePrefixComparator;
  218. template <>
  219. struct SameSizeTuplePrefixComparator<0, 0> {
  220. template <class Tuple1, class Tuple2>
  221. static bool Eq(const Tuple1& /* t1 */, const Tuple2& /* t2 */) {
  222. return true;
  223. }
  224. };
  225. template <int k>
  226. struct SameSizeTuplePrefixComparator<k, k> {
  227. template <class Tuple1, class Tuple2>
  228. static bool Eq(const Tuple1& t1, const Tuple2& t2) {
  229. return SameSizeTuplePrefixComparator<k - 1, k - 1>::Eq(t1, t2) &&
  230. ::std::tr1::get<k - 1>(t1) == ::std::tr1::get<k - 1>(t2);
  231. }
  232. };
  233. } // namespace gtest_internal
  234. template <GTEST_$(n)_TYPENAMES_(T), GTEST_$(n)_TYPENAMES_(U)>
  235. inline bool operator==(const GTEST_$(n)_TUPLE_(T)& t,
  236. const GTEST_$(n)_TUPLE_(U)& u) {
  237. return gtest_internal::SameSizeTuplePrefixComparator<
  238. tuple_size<GTEST_$(n)_TUPLE_(T) >::value,
  239. tuple_size<GTEST_$(n)_TUPLE_(U) >::value>::Eq(t, u);
  240. }
  241. template <GTEST_$(n)_TYPENAMES_(T), GTEST_$(n)_TYPENAMES_(U)>
  242. inline bool operator!=(const GTEST_$(n)_TUPLE_(T)& t,
  243. const GTEST_$(n)_TUPLE_(U)& u) { return !(t == u); }
  244. // 6.1.4 Pairs.
  245. // Unimplemented.
  246. } // namespace tr1
  247. } // namespace std
  248. $for j [[
  249. #undef GTEST_$(j)_TUPLE_
  250. ]]
  251. $for j [[
  252. #undef GTEST_$(j)_TYPENAMES_
  253. ]]
  254. #undef GTEST_DECLARE_TUPLE_AS_FRIEND_
  255. #undef GTEST_BY_REF_
  256. #undef GTEST_ADD_REF_
  257. #undef GTEST_TUPLE_ELEMENT_
  258. #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TUPLE_H_