soma-io  4.7.0
allocatedvector.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 #ifndef SOMAIO_UTILITIES_ALLOCATEDVECTOR_H
35 #define SOMAIO_UTILITIES_ALLOCATEDVECTOR_H
36 //--- soma-io ----------------------------------------------------------------
40 //----------------------------------------------------------------------------
41 
42 namespace soma
43 {
44 
61  template <typename T>
63  {
64  public:
65  typedef size_t size_type;
66  typedef T* iterator;
67  typedef const T* const_iterator;
68 
69  AllocatedVector( size_type n, const AllocatorContext & ac );
72  AllocatedVector( const AllocatedVector & other );
75  AllocatedVector( const AllocatedVector & other,
76  const AllocatorContext & ac );
79  AllocatedVector( size_type n, T* buffer );
81 
82  size_type size() const;
83  iterator begin();
84  const_iterator begin() const;
85  iterator end();
86  const_iterator end() const;
87  T & operator [] ( size_type i );
88  const T & operator [] ( size_type i ) const;
95  void copy( const AllocatedVector &, const AllocatorContext & );
96 
97  const AllocatorContext & allocatorContext() const;
98 
99  void free();
100  void allocate( size_type n, const AllocatorContext & ac );
101 
102  private:
103  AllocatorContext _allocatorContext;
104  size_type _size;
105  T *_items;
106  };
107 
108  //==========================================================================
109  // I N L I N E F U N C T I O N S
110  //==========================================================================
111 
112  template<typename T> inline
114  const AllocatorContext &ac )
115  : _allocatorContext( ac ), _size( n ),
116  _items( _allocatorContext.allocate( _items, n ) )
117  {
118  }
119 
120  template<typename T> inline
122  : _allocatorContext(), _size( other.size() ),
123  _items( _allocatorContext.allocate( _items, _size ) )
124  {
125  if( _allocatorContext.allocatorType() != AllocatorStrategy::ReadOnlyMap
126  && _allocatorContext.isAllocated()
127  && other._allocatorContext.isAllocated() )
128  {
129  iterator i, e = end();
130  const_iterator j = other.begin();
131  for( i = begin(); i != e; ++i, ++j )
132  *i = *j;
133  }
134  }
135 
136  template<typename T> inline
138  const AllocatorContext &ac )
139  : _allocatorContext( ac ), _size( other.size() ),
140  _items( _allocatorContext.allocate( _items, _size ) )
141  {
142  if( _allocatorContext.allocatorType() != AllocatorStrategy::ReadOnlyMap
143  && _allocatorContext.isAllocated()
144  && other._allocatorContext.isAllocated() )
145  {
146  iterator i, e = end();
147  const_iterator j = other.begin();
148  for( i = begin(); i != e; ++i, ++j )
149  *i = *j;
150  }
151  }
152 
153  template<typename T> inline
155  : _allocatorContext( AllocatorStrategy::NotOwner,
156  carto::rc_ptr<DataSource>
157  ( new BufferDataSource( reinterpret_cast<char *>
158  ( buffer ), n * sizeof( T ),
159  DataSource::ReadWrite ) ) ),
160  _size( n ),
161  _items( _allocatorContext.allocate( _items, _size ) )
162  {
163  }
164 
165  template<typename T> inline
167  {
168  free();
169  }
170 
171  template<typename T> inline
173  {
174  return _allocatorContext;
175  }
176 
177  template<typename T> inline
179  const AllocatorContext & ac )
180  {
181  free();
182  _size = n;
183  _allocatorContext = ac;
184  _items = _allocatorContext.allocate( _items, n );
185  }
186 
187  template<typename T> inline
189  {
190  _allocatorContext.deallocate( _items, _size );
191  _size = 0;
192  _items = 0;
193  }
194 
195  template<typename T> inline
197  {
198  return _size;
199  }
200 
201  template<typename T> inline
204  {
205  copy( other, AllocatorContext() );
206  return *this;
207  }
208 
209  template<typename T> inline
211  const AllocatorContext & ac )
212  {
213  if( &other == this )
214  return;
215  allocate( other.size(), ac );
216  if( _allocatorContext.allocatorType() != AllocatorStrategy::ReadOnlyMap
217  && _allocatorContext.isAllocated()
218  && other._allocatorContext.isAllocated() )
219  {
220  iterator i, e = end();
221  const_iterator j = other.begin();
222  for( i = begin(); i != e; ++i, ++j )
223  *i = *j;
224  }
225  }
226 
227 
228  template<typename T> inline
230  {
231  return _items;
232  }
233 
234  template<typename T> inline
237  {
238  return _items;
239  }
240 
241  template<typename T> inline
243  {
244  return _items + _size;
245  }
246 
247  template<typename T> inline
249  {
250  return _items + _size;
251  }
252 
253  template<typename T> inline
255  {
256  return _items[i];
257  }
258 
259  template<typename T> inline
261  {
262  return _items[i];
263  }
264 
265 }
266 
267 #endif
void allocate(size_type n, const AllocatorContext &ac)
void copy(const AllocatedVector &, const AllocatorContext &)
contrarily to operator = (), this copy function may use a different allocator, which may be the copie...
Allocation context.
Definition: allocator.h:260
bool isAllocated() const
Definition: allocator.h:308
MappingMode allocatorType() const
Abstraction layer for various data sources (file, buffer, socket...).
Definition: datasource.h:64
Definition: allocator.h:48
A STL-like vector with a smart and slow allocation system for large data buffers. ...
AllocatedVector(size_type n, const AllocatorContext &ac)
const AllocatorContext & allocatorContext() const
size_type size() const
T * allocate(T *&ptr, size_t n) const
return value is the same as modifiable input param ptr
Definition: allocator.h:330
void deallocate(T *ptr, size_t n) const
Definition: allocator.h:360
T & operator[](size_type i)
Determination of the allocation type depending of the buffer size to allocate and the disk format of ...
Definition: allocator.h:150
AllocatedVector & operator=(const AllocatedVector &)
The copy operator uses a "standard" AllocatorContext using memory allocation.