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.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are
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
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.
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.
34 // Implements a subset of TR1 tuple needed by Google Test and Google Mock.
36 // GOOGLETEST_CM0001 DO NOT DELETE
38 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TUPLE_H_
39 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TUPLE_H_
41 #include <utility> // For ::std::pair.
43 // The compiler used in Symbian has a bug that prevents us from declaring the
44 // tuple template as a friend (it complains that tuple is redefined). This
45 // bypasses the bug by declaring the members that should otherwise be
47 // Sun Studio versions < 12 also have the above bug.
48 #if defined(__SYMBIAN32__) || (defined(__SUNPRO_CC) && __SUNPRO_CC < 0x590)
49 # define GTEST_DECLARE_TUPLE_AS_FRIEND_ public:
51 # define GTEST_DECLARE_TUPLE_AS_FRIEND_ \
52 template <GTEST_$(n)_TYPENAMES_(U)> friend class tuple; \
56 // Visual Studio 2010, 2012, and 2013 define symbols in std::tr1 that conflict
57 // with our own definitions. Therefore using our own tuple does not work on
59 #if defined(_MSC_VER) && _MSC_VER >= 1600 /* 1600 is Visual Studio 2010 */
60 # error "gtest's tuple doesn't compile on Visual Studio 2010 or later. \
61 GTEST_USE_OWN_TR1_TUPLE must be set to 0 on those compilers."
68 // GTEST_n_TUPLE_(T) is the type of an n-tuple.
69 #define GTEST_0_TUPLE_(T) tuple<>
74 #define GTEST_$(k)_TUPLE_(T) tuple<$for m, [[T##$m]]$for m2 [[, void]]>
78 // GTEST_n_TYPENAMES_(T) declares a list of n typenames.
82 #define GTEST_$(j)_TYPENAMES_(T) $for m, [[typename T##$m]]
87 // In theory, defining stuff in the ::std namespace is undefined
88 // behavior. We can do this as we are playing the role of a standard
93 template <$for i, [[typename T$i = void]]>
96 // Anything in namespace gtest_internal is Google Test's INTERNAL
97 // IMPLEMENTATION DETAIL and MUST NOT BE USED DIRECTLY in user code.
98 namespace gtest_internal {
100 // ByRef<T>::type is T if T is a reference; otherwise it's const T&.
101 template <typename T>
102 struct ByRef { typedef const T& type; }; // NOLINT
103 template <typename T>
104 struct ByRef<T&> { typedef T& type; }; // NOLINT
106 // A handy wrapper for ByRef.
107 #define GTEST_BY_REF_(T) typename ::std::tr1::gtest_internal::ByRef<T>::type
109 // AddRef<T>::type is T if T is a reference; otherwise it's T&. This
110 // is the same as tr1::add_reference<T>::type.
111 template <typename T>
112 struct AddRef { typedef T& type; }; // NOLINT
113 template <typename T>
114 struct AddRef<T&> { typedef T& type; }; // NOLINT
116 // A handy wrapper for AddRef.
117 #define GTEST_ADD_REF_(T) typename ::std::tr1::gtest_internal::AddRef<T>::type
119 // A helper for implementing get<k>().
120 template <int k> class Get;
122 // A helper for implementing tuple_element<k, T>. kIndexValid is true
123 // iff k < the number of fields in tuple type T.
124 template <bool kIndexValid, int kIndex, class Tuple>
129 template <GTEST_$(n)_TYPENAMES_(T)>
130 struct TupleElement<true, $i, GTEST_$(n)_TUPLE_(T) > {
136 } // namespace gtest_internal
142 tuple(const tuple& /* t */) {}
143 tuple& operator=(const tuple& /* t */) { return *this; }
149 template <GTEST_$(k)_TYPENAMES_(T)>
150 class $if k < n [[GTEST_$(k)_TUPLE_(T)]] $else [[tuple]] {
152 template <int k> friend class gtest_internal::Get;
154 tuple() : $for m, [[f$(m)_()]] {}
156 explicit tuple($for m, [[GTEST_BY_REF_(T$m) f$m]]) : [[]]
157 $for m, [[f$(m)_(f$m)]] {}
159 tuple(const tuple& t) : $for m, [[f$(m)_(t.f$(m)_)]] {}
161 template <GTEST_$(k)_TYPENAMES_(U)>
162 tuple(const GTEST_$(k)_TUPLE_(U)& t) : $for m, [[f$(m)_(t.f$(m)_)]] {}
165 template <typename U0, typename U1>
166 tuple(const ::std::pair<U0, U1>& p) : f0_(p.first), f1_(p.second) {}
170 tuple& operator=(const tuple& t) { return CopyFrom(t); }
172 template <GTEST_$(k)_TYPENAMES_(U)>
173 tuple& operator=(const GTEST_$(k)_TUPLE_(U)& t) {
178 template <typename U0, typename U1>
179 tuple& operator=(const ::std::pair<U0, U1>& p) {
187 GTEST_DECLARE_TUPLE_AS_FRIEND_
189 template <GTEST_$(k)_TYPENAMES_(U)>
190 tuple& CopyFrom(const GTEST_$(k)_TUPLE_(U)& t) {
208 // 6.1.3.2 Tuple creation functions.
210 // Known limitations: we don't support passing an
211 // std::tr1::reference_wrapper<T> to make_tuple(). And we don't
214 inline tuple<> make_tuple() { return tuple<>(); }
219 template <GTEST_$(k)_TYPENAMES_(T)>
220 inline GTEST_$(k)_TUPLE_(T) make_tuple($for m, [[const T$m& f$m]]) {
221 return GTEST_$(k)_TUPLE_(T)($for m, [[f$m]]);
226 // 6.1.3.3 Tuple helper classes.
228 template <typename Tuple> struct tuple_size;
232 template <GTEST_$(j)_TYPENAMES_(T)>
233 struct tuple_size<GTEST_$(j)_TUPLE_(T) > {
234 static const int value = $j;
239 template <int k, class Tuple>
240 struct tuple_element {
241 typedef typename gtest_internal::TupleElement<
242 k < (tuple_size<Tuple>::value), k, Tuple>::type type;
245 #define GTEST_TUPLE_ELEMENT_(k, Tuple) typename tuple_element<k, Tuple >::type
247 // 6.1.3.4 Element access.
249 namespace gtest_internal {
256 template <class Tuple>
257 static GTEST_ADD_REF_(GTEST_TUPLE_ELEMENT_($i, Tuple))
258 Field(Tuple& t) { return t.f$(i)_; } // NOLINT
260 template <class Tuple>
261 static GTEST_BY_REF_(GTEST_TUPLE_ELEMENT_($i, Tuple))
262 ConstField(const Tuple& t) { return t.f$(i)_; }
267 } // namespace gtest_internal
269 template <int k, GTEST_$(n)_TYPENAMES_(T)>
270 GTEST_ADD_REF_(GTEST_TUPLE_ELEMENT_(k, GTEST_$(n)_TUPLE_(T)))
271 get(GTEST_$(n)_TUPLE_(T)& t) {
272 return gtest_internal::Get<k>::Field(t);
275 template <int k, GTEST_$(n)_TYPENAMES_(T)>
276 GTEST_BY_REF_(GTEST_TUPLE_ELEMENT_(k, GTEST_$(n)_TUPLE_(T)))
277 get(const GTEST_$(n)_TUPLE_(T)& t) {
278 return gtest_internal::Get<k>::ConstField(t);
281 // 6.1.3.5 Relational operators
283 // We only implement == and !=, as we don't have a need for the rest yet.
285 namespace gtest_internal {
287 // SameSizeTuplePrefixComparator<k, k>::Eq(t1, t2) returns true if the
288 // first k fields of t1 equals the first k fields of t2.
289 // SameSizeTuplePrefixComparator(k1, k2) would be a compiler error if
291 template <int kSize1, int kSize2>
292 struct SameSizeTuplePrefixComparator;
295 struct SameSizeTuplePrefixComparator<0, 0> {
296 template <class Tuple1, class Tuple2>
297 static bool Eq(const Tuple1& /* t1 */, const Tuple2& /* t2 */) {
303 struct SameSizeTuplePrefixComparator<k, k> {
304 template <class Tuple1, class Tuple2>
305 static bool Eq(const Tuple1& t1, const Tuple2& t2) {
306 return SameSizeTuplePrefixComparator<k - 1, k - 1>::Eq(t1, t2) &&
307 ::std::tr1::get<k - 1>(t1) == ::std::tr1::get<k - 1>(t2);
311 } // namespace gtest_internal
313 template <GTEST_$(n)_TYPENAMES_(T), GTEST_$(n)_TYPENAMES_(U)>
314 inline bool operator==(const GTEST_$(n)_TUPLE_(T)& t,
315 const GTEST_$(n)_TUPLE_(U)& u) {
316 return gtest_internal::SameSizeTuplePrefixComparator<
317 tuple_size<GTEST_$(n)_TUPLE_(T) >::value,
318 tuple_size<GTEST_$(n)_TUPLE_(U) >::value>::Eq(t, u);
321 template <GTEST_$(n)_TYPENAMES_(T), GTEST_$(n)_TYPENAMES_(U)>
322 inline bool operator!=(const GTEST_$(n)_TUPLE_(T)& t,
323 const GTEST_$(n)_TUPLE_(U)& u) { return !(t == u); }
333 #undef GTEST_$(j)_TUPLE_
339 #undef GTEST_$(j)_TYPENAMES_
343 #undef GTEST_DECLARE_TUPLE_AS_FRIEND_
345 #undef GTEST_ADD_REF_
346 #undef GTEST_TUPLE_ELEMENT_
348 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TUPLE_H_