/firmware/tests/gtest-1.4.0/include/gtest/internal/gtest-tuple.h.pump

http://github.com/makerbot/G3Firmware · Unknown · 335 lines · 248 code · 87 blank · 0 comment · 0 complexity · 5ed64c2185f1592ea02b79f7e4cb7ec5 MD5 · raw file

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