OSDN Git Service

2004-10-16 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / tr1 / array
1 // class template array -*- C++ -*-
2
3 // Copyright (C) 2004 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 #ifndef _ARRAY
31 #define _ARRAY 1
32
33 #include <new>
34 #include <iterator>
35
36 //namespace std::tr1
37 namespace std
38 {
39 namespace tr1
40 {
41   // [6.2.2] Class template array template
42   // Requires complete type _Tp.
43   // Use of char array allows _Tp to skirt default constructable requirement.
44   template<typename _Tp, size_t _Nm = 1>
45     struct array
46     {
47       enum { _S_index = _Nm };
48
49       typedef _Tp                               value_type;
50       typedef value_type&                       reference;
51       typedef const value_type&                 const_reference;
52       typedef value_type*                       iterator;
53       typedef const value_type*                 const_iterator;
54       typedef size_t                            size_type;
55       typedef ptrdiff_t                         difference_type;
56       typedef std::reverse_iterator<iterator>   reverse_iterator;
57       typedef std::reverse_iterator<const_iterator>     const_reverse_iterator;
58
59       value_type _M_instance[_Nm];
60
61       // No explicit construct/copy/destroy for aggregate type.
62
63       void 
64       assign(const value_type& u); 
65
66       void 
67       swap(array&);
68
69       // Iterators.
70       iterator 
71       begin()
72       { return reinterpret_cast<iterator>(&_M_instance[0]); }
73
74       const_iterator 
75       begin() const 
76       { return reinterpret_cast<const_iterator>(&_M_instance[0]); }
77
78       iterator 
79       end() 
80       { return reinterpret_cast<iterator>(&_M_instance[_S_index - 1]); }
81
82       const_iterator 
83       end() const
84       { return reinterpret_cast<const_iterator>(&_M_instance[_S_index - 1]); }
85
86       reverse_iterator 
87       rbegin()
88       { return reverse_iterator(this->end()); }
89
90       const_reverse_iterator 
91       rbegin() const
92       { return const_reverse_iterator(this->end()); }
93
94       reverse_iterator 
95       rend()
96       { return reverse_iterator(this->begin()); }
97
98       const_reverse_iterator 
99       rend() const
100       { return const_reverse_iterator(this->begin()); }
101
102       // Capacity.
103       size_type 
104       size() const { return _S_index; }
105
106       size_type 
107       max_size() const
108       { 
109         // XXX Not specified. Unnecessary, this is fixed-size.
110         return _S_index; 
111       }
112
113       bool 
114       empty() const;
115
116       // Element access.
117       reference 
118       operator[](size_type __n)
119       { return reinterpret_cast<reference>(_M_instance[__n]); }
120
121       const_reference 
122       operator[](size_type __n) const
123       { return reinterpret_cast<const_reference>(_M_instance[__n]); }
124
125       const_reference 
126       at(size_type __n) const
127       { 
128         if (__builtin_expect(__n > _S_index, false))
129           throw std::bad_alloc();
130         return reinterpret_cast<const_reference>(_M_instance[__n]); 
131       }
132
133       reference 
134       at(size_type __n)
135       { 
136         if (__builtin_expect(__n > _S_index, false))
137           throw std::bad_alloc();
138         return reinterpret_cast<reference>(_M_instance[__n]); 
139       }
140
141       reference 
142       front(); 
143
144       const_reference 
145       front() const; 
146
147       reference 
148       back(); 
149
150       const_reference 
151       back() const; 
152
153       _Tp* 
154       data(); 
155
156       const _Tp* 
157       data() const;
158     };
159
160   // Array comparisons.
161  template<typename _Tp, size_t _Nm>
162    bool 
163    operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
164    { return false; }
165
166  template<typename _Tp, size_t _Nm>
167    bool 
168    operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
169    { return !(__one == __two); }
170
171  template<typename _Tp, size_t _Nm>
172    bool 
173    operator<(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
174    { return false; }
175
176  template<typename _Tp, size_t _Nm>
177    bool 
178    operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
179    { return false; }
180
181  template<typename _Tp, size_t _Nm>
182    bool 
183    operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
184    { return false; }
185
186  template<typename _Tp, size_t _Nm>
187    bool 
188    operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
189    { return false; }
190
191   // [6.2.2.2] Specialized algorithms.
192  template<typename _Tp, size_t _Nm>
193    void
194    swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
195    { swap_ranges(__one.begin(), __one.end(), __two.begin()); }
196 } // namespace std::tr1
197 }
198
199 #endif