OSDN Git Service

2010-10-28 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / stl_pair.h
1 // Pair implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 /*
27  *
28  * Copyright (c) 1994
29  * Hewlett-Packard Company
30  *
31  * Permission to use, copy, modify, distribute and sell this software
32  * and its documentation for any purpose is hereby granted without fee,
33  * provided that the above copyright notice appear in all copies and
34  * that both that copyright notice and this permission notice appear
35  * in supporting documentation.  Hewlett-Packard Company makes no
36  * representations about the suitability of this software for any
37  * purpose.  It is provided "as is" without express or implied warranty.
38  *
39  *
40  * Copyright (c) 1996,1997
41  * Silicon Graphics Computer Systems, Inc.
42  *
43  * Permission to use, copy, modify, distribute and sell this software
44  * and its documentation for any purpose is hereby granted without fee,
45  * provided that the above copyright notice appear in all copies and
46  * that both that copyright notice and this permission notice appear
47  * in supporting documentation.  Silicon Graphics makes no
48  * representations about the suitability of this software for any
49  * purpose.  It is provided "as is" without express or implied warranty.
50  */
51
52 /** @file stl_pair.h
53  *  This is an internal header file, included by other library headers.
54  *  You should not attempt to use it directly.
55  */
56
57 #ifndef _STL_PAIR_H
58 #define _STL_PAIR_H 1
59
60 #include <bits/move.h> // for std::move / std::forward, and std::swap
61
62 #ifdef __GXX_EXPERIMENTAL_CXX0X__
63 #include <type_traits> // for std::__decay_and_strip too
64 #endif
65
66 _GLIBCXX_BEGIN_NAMESPACE(std)
67
68 #ifdef __GXX_EXPERIMENTAL_CXX0X__
69   struct piecewise_construct_t { };
70
71   static const piecewise_construct_t piecewise_construct
72     = piecewise_construct_t();
73
74   // forward declarations
75   template<typename...>
76     class tuple;
77
78   template<int...>
79     struct _Index_tuple;
80 #endif
81
82   /// pair holds two objects of arbitrary type.
83   template<class _T1, class _T2>
84     struct pair
85     {
86       typedef _T1 first_type;    ///<  @c first_type is the first bound type
87       typedef _T2 second_type;   ///<  @c second_type is the second bound type
88
89       _T1 first;                 ///< @c first is a copy of the first object
90       _T2 second;                ///< @c second is a copy of the second object
91
92       // _GLIBCXX_RESOLVE_LIB_DEFECTS
93       // 265.  std::pair::pair() effects overly restrictive
94       /** The default constructor creates @c first and @c second using their
95        *  respective default constructors.  */
96       pair()
97       : first(), second() { }
98
99       /** Two objects may be passed to a @c pair constructor to be copied.  */
100       pair(const _T1& __a, const _T2& __b)
101       : first(__a), second(__b) { }
102
103 #ifdef __GXX_EXPERIMENTAL_CXX0X__
104       pair(const pair&) = default;
105
106       // DR 811.
107       template<class _U1, class = typename
108                std::enable_if<std::is_convertible<_U1, _T1>::value>::type>
109         pair(_U1&& __x, const _T2& __y)
110         : first(std::forward<_U1>(__x)),
111           second(__y) { }
112
113       template<class _U2, class = typename
114                std::enable_if<std::is_convertible<_U2, _T2>::value>::type>
115         pair(const _T1& __x, _U2&& __y)
116         : first(__x),
117           second(std::forward<_U2>(__y)) { }
118
119       template<class _U1, class _U2, class = typename
120                std::enable_if<std::is_convertible<_U1, _T1>::value
121                               && std::is_convertible<_U2, _T2>::value>::type>
122         pair(_U1&& __x, _U2&& __y)
123         : first(std::forward<_U1>(__x)),
124           second(std::forward<_U2>(__y)) { }
125
126       template<class... _Args1, class... _Args2>
127         pair(piecewise_construct_t,
128              tuple<_Args1...> __first_args,
129              tuple<_Args2...> __second_args)
130         : first(__cons<first_type>(std::move(__first_args))),
131           second(__cons<second_type>(std::move(__second_args))) { }
132 #endif
133
134       /** There is also a templated copy ctor for the @c pair class itself.  */
135       template<class _U1, class _U2>
136         pair(const pair<_U1, _U2>& __p)
137         : first(__p.first),
138           second(__p.second) { }
139
140 #ifdef __GXX_EXPERIMENTAL_CXX0X__
141       template<class _U1, class _U2>
142         pair(pair<_U1, _U2>&& __p)
143         : first(std::forward<_U1>(__p.first)),
144           second(std::forward<_U2>(__p.second)) { }
145
146       pair&
147       operator=(pair&& __p)
148       { 
149         first = std::move(__p.first);
150         second = std::move(__p.second);
151         return *this;
152       }
153
154       template<class _U1, class _U2>
155         pair&
156         operator=(const pair<_U1, _U2>& __p)
157         {
158           first = __p.first;
159           second = __p.second;
160           return *this;
161         }
162
163       template<class _U1, class _U2>
164         pair&
165         operator=(pair<_U1, _U2>&& __p)
166         {
167           first = std::move(__p.first);
168           second = std::move(__p.second);
169           return *this;
170         }
171
172       void
173       swap(pair& __p)
174       {
175         using std::swap;
176         swap(first, __p.first);
177         swap(second, __p.second);       
178       }
179
180     private:
181       template<typename _Tp, typename... _Args>
182         static _Tp
183         __cons(tuple<_Args...>&&);
184
185       template<typename _Tp, typename... _Args, int... _Indexes>
186         static _Tp
187         __do_cons(tuple<_Args...>&&, const _Index_tuple<_Indexes...>&);
188 #endif
189     };
190
191   /// Two pairs of the same type are equal iff their members are equal.
192   template<class _T1, class _T2>
193     inline bool
194     operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
195     { return __x.first == __y.first && __x.second == __y.second; }
196
197   /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html>
198   template<class _T1, class _T2>
199     inline bool
200     operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
201     { return __x.first < __y.first
202              || (!(__y.first < __x.first) && __x.second < __y.second); }
203
204   /// Uses @c operator== to find the result.
205   template<class _T1, class _T2>
206     inline bool
207     operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
208     { return !(__x == __y); }
209
210   /// Uses @c operator< to find the result.
211   template<class _T1, class _T2>
212     inline bool
213     operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
214     { return __y < __x; }
215
216   /// Uses @c operator< to find the result.
217   template<class _T1, class _T2>
218     inline bool
219     operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
220     { return !(__y < __x); }
221
222   /// Uses @c operator< to find the result.
223   template<class _T1, class _T2>
224     inline bool
225     operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
226     { return !(__x < __y); }
227
228 #ifdef __GXX_EXPERIMENTAL_CXX0X__
229   /// See std::pair::swap().
230   // Note:  no std::swap overloads in C++03 mode, this has performance
231   //        implications, see, eg, libstdc++/38466.
232   template<class _T1, class _T2>
233     inline void
234     swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
235     { __x.swap(__y); }
236 #endif
237
238   /**
239    *  @brief A convenience wrapper for creating a pair from two objects.
240    *  @param  x  The first object.
241    *  @param  y  The second object.
242    *  @return   A newly-constructed pair<> object of the appropriate type.
243    *
244    *  The standard requires that the objects be passed by reference-to-const,
245    *  but LWG issue #181 says they should be passed by const value.  We follow
246    *  the LWG by default.
247    */
248   // _GLIBCXX_RESOLVE_LIB_DEFECTS
249   // 181.  make_pair() unintended behavior
250 #ifndef __GXX_EXPERIMENTAL_CXX0X__
251   template<class _T1, class _T2>
252     inline pair<_T1, _T2>
253     make_pair(_T1 __x, _T2 __y)
254     { return pair<_T1, _T2>(__x, __y); }
255 #else
256   // NB: DR 706.
257   template<class _T1, class _T2>
258     inline pair<typename __decay_and_strip<_T1>::__type,
259                 typename __decay_and_strip<_T2>::__type>
260     make_pair(_T1&& __x, _T2&& __y)
261     {
262       return pair<typename __decay_and_strip<_T1>::__type,
263                   typename __decay_and_strip<_T2>::__type>
264         (std::forward<_T1>(__x), std::forward<_T2>(__y));
265     }
266 #endif
267
268 _GLIBCXX_END_NAMESPACE
269
270 #endif /* _STL_PAIR_H */