OSDN Git Service

Add NIOS2 support. Code from SourceyG++.
[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   // forward declarations
72   template<typename...>
73     class tuple;
74
75   template<int...>
76     struct _Index_tuple;
77 #endif
78
79   /// pair holds two objects of arbitrary type.
80   template<class _T1, class _T2>
81     struct pair
82     {
83       typedef _T1 first_type;    ///<  @c first_type is the first bound type
84       typedef _T2 second_type;   ///<  @c second_type is the second bound type
85
86       _T1 first;                 ///< @c first is a copy of the first object
87       _T2 second;                ///< @c second is a copy of the second object
88
89       // _GLIBCXX_RESOLVE_LIB_DEFECTS
90       // 265.  std::pair::pair() effects overly restrictive
91       /** The default constructor creates @c first and @c second using their
92        *  respective default constructors.  */
93       pair()
94       : first(), second() { }
95
96       /** Two objects may be passed to a @c pair constructor to be copied.  */
97       pair(const _T1& __a, const _T2& __b)
98       : first(__a), second(__b) { }
99
100 #ifdef __GXX_EXPERIMENTAL_CXX0X__
101       pair(const pair&) = default;
102
103       // DR 811.
104       template<class _U1, class = typename
105                std::enable_if<std::is_convertible<_U1, _T1>::value>::type>
106         pair(_U1&& __x, const _T2& __y)
107         : first(std::forward<_U1>(__x)),
108           second(__y) { }
109
110       template<class _U2, class = typename
111                std::enable_if<std::is_convertible<_U2, _T2>::value>::type>
112         pair(const _T1& __x, _U2&& __y)
113         : first(__x),
114           second(std::forward<_U2>(__y)) { }
115
116       template<class _U1, class _U2, class = typename
117                std::enable_if<std::is_convertible<_U1, _T1>::value
118                               && std::is_convertible<_U2, _T2>::value>::type>
119         pair(_U1&& __x, _U2&& __y)
120         : first(std::forward<_U1>(__x)),
121           second(std::forward<_U2>(__y)) { }
122
123       pair(pair&& __p)
124       : first(std::move(__p.first)),
125         second(std::move(__p.second)) { }
126
127       template<class... _Args1, class... _Args2>
128         pair(piecewise_construct_t,
129              tuple<_Args1...> __first_args,
130              tuple<_Args2...> __second_args)
131         : first(__cons<first_type>(std::move(__first_args))),
132           second(__cons<second_type>(std::move(__second_args))) { }
133 #endif
134
135       /** There is also a templated copy ctor for the @c pair class itself.  */
136       template<class _U1, class _U2>
137         pair(const pair<_U1, _U2>& __p)
138         : first(__p.first),
139           second(__p.second) { }
140
141 #ifdef __GXX_EXPERIMENTAL_CXX0X__
142       template<class _U1, class _U2>
143         pair(pair<_U1, _U2>&& __p)
144         : first(std::move(__p.first)),
145           second(std::move(__p.second)) { }
146
147       pair&
148       operator=(pair&& __p)
149       { 
150         first = std::move(__p.first);
151         second = std::move(__p.second);
152         return *this;
153       }
154
155       template<class _U1, class _U2>
156         pair&
157         operator=(const pair<_U1, _U2>& __p)
158         {
159           first = __p.first;
160           second = __p.second;
161           return *this;
162         }
163
164       template<class _U1, class _U2>
165         pair&
166         operator=(pair<_U1, _U2>&& __p)
167         {
168           first = std::move(__p.first);
169           second = std::move(__p.second);
170           return *this;
171         }
172
173       void
174       swap(pair& __p)
175       {
176         using std::swap;
177         swap(first, __p.first);
178         swap(second, __p.second);       
179       }
180
181     private:
182       template<typename _Tp, typename... _Args>
183         static _Tp
184         __cons(tuple<_Args...>&&);
185
186       template<typename _Tp, typename... _Args, int... _Indexes>
187         static _Tp
188         __do_cons(tuple<_Args...>&&, const _Index_tuple<_Indexes...>&);
189 #endif
190     };
191
192   /// Two pairs of the same type are equal iff their members are equal.
193   template<class _T1, class _T2>
194     inline bool
195     operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
196     { return __x.first == __y.first && __x.second == __y.second; }
197
198   /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html>
199   template<class _T1, class _T2>
200     inline bool
201     operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
202     { return __x.first < __y.first
203              || (!(__y.first < __x.first) && __x.second < __y.second); }
204
205   /// Uses @c operator== to find the result.
206   template<class _T1, class _T2>
207     inline bool
208     operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
209     { return !(__x == __y); }
210
211   /// Uses @c operator< to find the result.
212   template<class _T1, class _T2>
213     inline bool
214     operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
215     { return __y < __x; }
216
217   /// Uses @c operator< to find the result.
218   template<class _T1, class _T2>
219     inline bool
220     operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
221     { return !(__y < __x); }
222
223   /// Uses @c operator< to find the result.
224   template<class _T1, class _T2>
225     inline bool
226     operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
227     { return !(__x < __y); }
228
229 #ifdef __GXX_EXPERIMENTAL_CXX0X__
230   /// See std::pair::swap().
231   // Note:  no std::swap overloads in C++03 mode, this has performance
232   //        implications, see, eg, libstdc++/38466.
233   template<class _T1, class _T2>
234     inline void
235     swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
236     { __x.swap(__y); }
237 #endif
238
239   /**
240    *  @brief A convenience wrapper for creating a pair from two objects.
241    *  @param  x  The first object.
242    *  @param  y  The second object.
243    *  @return   A newly-constructed pair<> object of the appropriate type.
244    *
245    *  The standard requires that the objects be passed by reference-to-const,
246    *  but LWG issue #181 says they should be passed by const value.  We follow
247    *  the LWG by default.
248    */
249   // _GLIBCXX_RESOLVE_LIB_DEFECTS
250   // 181.  make_pair() unintended behavior
251 #ifndef __GXX_EXPERIMENTAL_CXX0X__
252   template<class _T1, class _T2>
253     inline pair<_T1, _T2>
254     make_pair(_T1 __x, _T2 __y)
255     { return pair<_T1, _T2>(__x, __y); }
256 #else
257   // NB: DR 706.
258   template<class _T1, class _T2>
259     inline pair<typename __decay_and_strip<_T1>::__type,
260                 typename __decay_and_strip<_T2>::__type>
261     make_pair(_T1&& __x, _T2&& __y)
262     {
263       return pair<typename __decay_and_strip<_T1>::__type,
264                   typename __decay_and_strip<_T2>::__type>
265         (std::forward<_T1>(__x), std::forward<_T2>(__y));
266     }
267 #endif
268
269 _GLIBCXX_END_NAMESPACE
270
271 #endif /* _STL_PAIR_H */