source: soft/giet_vm/memo/include/libelfpp/dpp/vlarray @ 827

Last change on this file since 827 was 163, checked in by karaoui, 12 years ago

changing mover to memo
changing soft.bin to soft.elf
...

File size: 6.7 KB
Line 
1/* -*- c++ -*-
2
3   Simple C++ variable array of non-POD objects
4
5   This file is part of the dpp library of C++ template classes
6
7   doc: http://diaxen.ssji.net/dpp/index.html
8   repo: https://www.ssji.net/svn/projets/trunk/libdpp
9
10   This program is free software: you can redistribute it and/or
11   modify it under the terms of the GNU Lesser General Public License
12   as published by the Free Software Foundation, either version 3 of
13   the License, or (at your option) any later version.
14
15   This program is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public
21   License along with this program.  If not, see
22   <http://www.gnu.org/licenses/>.
23
24   (c) 2011 Alexandre Becoulet <alexandre.becoulet@free.fr>
25
26*/
27
28#ifndef DPP_VLARRAY_HH_
29#define DPP_VLARRAY_HH_
30
31#include <memory>
32#include <stdexcept>
33
34#include <stddef.h>
35#include <stdint.h>
36
37/** @file @module{Variable length array} */
38
39namespace dpp {
40
41  /** @short Variable length array iterator class
42      @module {Variable length array}
43      @header dpp/vlarray
44      @internal
45  */
46  template <typename X, int direction>
47  class vlarray_iterator
48  {
49    template <class, int> friend class vlarray_iterator;
50    template <class> friend class vlarray;
51
52    vlarray_iterator(X *vlarray, unsigned int i)
53      : _array(vlarray),
54        _i(i)
55    {
56    }
57
58  public:
59
60    typedef X value_type;
61    typedef X & reference;
62    typedef const X & const_reference;
63    typedef X * pointer;
64    typedef const X * const_pointer;
65    typedef unsigned int size_type;
66    typedef int difference_type;
67
68    typedef std::bidirectional_iterator_tag iterator_category;
69
70    vlarray_iterator()
71    {
72    }
73
74    template <class T>
75    vlarray_iterator(const vlarray_iterator<T, direction> &i)
76      : _array(i._array),
77        _i(i._i)
78    {
79    }
80
81    X & operator*()
82    {
83      return _array[_i];
84    }
85
86    X * operator->()
87    {
88      return &_array[_i];
89    }
90
91    vlarray_iterator & operator++()
92    {
93      _i += direction;
94      return *this;
95    }
96
97    vlarray_iterator operator++(int)
98    {
99      vlarray_iterator tmp(*this);
100      _i += direction;
101      return tmp;
102    }
103
104    vlarray_iterator & operator--()
105    {
106      _i -= direction;
107      return *this;
108    }
109
110    vlarray_iterator operator--(int)
111    {
112      vlarray_iterator tmp(*this);
113      _i -= direction;
114      return tmp;
115    }
116
117    vlarray_iterator operator-(int x) const
118    {
119      return vlarray_iterator(_array, _i - x * direction);
120    }
121
122    vlarray_iterator operator+(int x) const
123    {
124      return vlarray_iterator(_array, _i + x * direction);
125    }
126
127    difference_type operator-(const vlarray_iterator & i) const
128    {
129      return (_i - i._i) * direction;
130    }
131
132    bool operator<(const vlarray_iterator & i) const
133    {
134      return _i < i._i;
135    }
136
137    bool operator==(const vlarray_iterator &i) const
138    {
139      return _i == i._i;
140    }
141
142    bool operator!=(const vlarray_iterator &i) const
143    {
144      return _i != i._i;
145    }
146
147  private:
148    X *_array;
149    unsigned int _i;
150  };
151
152  /** @short Variable length array class
153      @module {Variable length array}
154      @header dpp/vlarray
155
156      Variable lenght arrays of non POD types are not supported in
157      C++. This class provides a container array which uses storage
158      space passed to constructor. The @ref #DPP_VLARRAY macro can be
159      used to declare storage space buffer along with array container,
160      providing variable lenght array feature:
161
162      @example test/test_vlarray.cc:example
163  */
164  template <typename X>
165  class vlarray
166  {
167  public:
168
169    /** Variable array declaration macro. @This macro declares a
170        buffer array of integers along with a @ref vlarray object and
171        pass the buffer array as storage space to the @ref vlarray
172        constructor. @hidecontent */
173#define DPP_VLARRAY(type, size, name)           \
174  uint64_t _##name[sizeof(type) * size / 8 + 1];        \
175  ::dpp::vlarray<type> name(_##name, size); /* FIXME size alignment */
176
177    typedef X value_type;
178    typedef X & reference;
179    typedef const X & const_reference;
180    typedef X * pointer;
181    typedef const X * const_pointer;
182    typedef unsigned int size_type;
183    typedef int difference_type;
184
185    typedef vlarray_iterator<X, 1> iterator;
186    typedef vlarray_iterator<const X, 1> const_iterator;
187    typedef vlarray_iterator<X, -1> reverse_iterator;
188    typedef vlarray_iterator<const X, -1> const_reverse_iterator;
189
190    /* @This declare a variable lenght array with given storage space
191       and elements count. The default constructor is invoked for all
192       new array items. */
193    vlarray(void *array, size_t size)
194      : _array((X*)array),
195        _size(size)
196    {
197      for (size_t i = 0; i < _size; i++)
198        new ((X*)(_array + i)) X();
199    }
200
201    /* @This invokes destructor for all array items */
202    ~vlarray()
203    {
204      for (size_t i = 0; i < _size; i++)
205        (_array + i)->~X();
206    }
207
208    size_t size() const
209    {
210      return _size;
211    }
212
213    size_t capacity() const
214    {
215      return _size;
216    }
217
218    X & operator[](int i)
219    {
220      return _array[i];
221    }
222
223    const X & operator[](int i) const
224    {
225      return _array[i];
226    }
227
228    X & at(int i)
229    {
230      if (i >= _size)
231        throw std::out_of_range("");
232      return _array[i];
233    }
234
235    const X & at(int i) const
236    {
237      if (i >= _size)
238        throw std::out_of_range("");
239      return _array[i];
240    }
241
242    /** @This returns @ref iterator to first object in pool */
243    iterator begin()
244    {
245      return iterator(_array, 0);
246    }
247
248    /** @This returns end @ref iterator */
249    iterator end()
250    {
251      return iterator(_array, size());
252    }
253
254    /** @This returns @ref const_iterator to first object in pool */
255    const_iterator begin() const
256    {
257      return const_iterator(_array, 0); 
258    }
259
260    /** @This returns end @ref const_iterator */
261    const_iterator end() const
262    {
263      return const_iterator(_array, size());
264    }
265
266    /** @This returns @ref reverse_iterator to last object in pool */
267    reverse_iterator rbegin()
268    {
269      return reverse_iterator(_array, size() - 1);
270    }
271
272    /** @This returns end @ref reverse_iterator */
273    reverse_iterator rend()
274    {
275      return reverse_iterator(_array, -1);
276    }
277
278    /** @This returns @ref const_reverse_iterator to first object in pool */
279    const_reverse_iterator rbegin() const
280    {
281      return const_reverse_iterator(_array, size() - 1);
282    }
283
284    /** @This returns end @ref const_reverse_iterator */
285    const_reverse_iterator rend() const
286    {
287      return const_reverse_iterator(_array, -1);
288    }
289
290  private:
291    X *_array;
292    size_t _size;
293  };
294
295}
296
297#endif
298
Note: See TracBrowser for help on using the repository browser.