VISR  0.12.0
Versatile Interactive Scene Renderer
basic_vector.hpp
Go to the documentation of this file.
1 /* Copyright Institute of Sound and Vibration Research - All rights reserved */
2 
3 #ifndef VISR_EFL_BASIC_VECTOR_HPP_INCLUDED
4 #define VISR_EFL_BASIC_VECTOR_HPP_INCLUDED
5 
6 #include "aligned_array.hpp"
7 #include "alignment.hpp"
8 #include "error_codes.hpp"
9 #include "export_symbols.hpp"
10 #include "vector_functions.hpp" // for vectorZero
11 
12 #include <algorithm>
13 #include <ciso646> // should not be necessary in C++11, but MSVC requires it for whatever reason.
14 #include <stdexcept>
15 #include <utility> // for std::swap
16 
17 namespace visr
18 {
19 namespace efl
20 {
21 
27 template< typename ElementType >
28 class VISR_EFL_LIBRARY_SYMBOL BasicVector
29 {
30 public:
35  explicit BasicVector( std::size_t alignmentElements = 0 )
36  : mData( alignmentElements )
37  {
38  }
39 
46  explicit BasicVector( std::size_t size, std::size_t alignmentElements )
47  : mData( size, alignmentElements )
48  {
49  zeroFill();
50  }
51 
59  explicit BasicVector( std::initializer_list< ElementType > const & initialValues,
60  std::size_t alignmentElements = 0 )
61  : mData( initialValues.size(), alignmentElements )
62  {
63  std::copy( initialValues.begin(), initialValues.end(), data() );
64  }
65 
66  BasicVector( BasicVector<ElementType> && rhs ) = default;
67 
68  BasicVector<ElementType> & operator=( BasicVector<ElementType> && rhs ) = default;
69 
70 
75  {
76  }
77 
82  void resize( std::size_t newSize )
83  {
84  // Ensure strong exception safety by doing the swap() trick
85  AlignedArray<ElementType> newData( newSize, alignmentElements() );
86  ErrorCode const res = vectorZero( newData.data(), newSize );
87  if( res != noError )
88  {
89  throw std::runtime_error( "Zeroing of Vector failed" );
90  }
91  // the rest of the function is non-throwing
92  mData.swap( newData );
93  }
94 
100  void assign( const BasicVector<ElementType> & rhs)
101  {
102  if( rhs.size() != size() )
103  {
104  resize( rhs.size() );
105  }
106  copy( rhs );
107  }
108 
113  void zeroFill()
114  {
115  ErrorCode const res = vectorZero( data(), size() );
116  if( res != noError )
117  {
118  throw std::runtime_error( "Zeroing of vector failed" );
119  }
120  }
121 
127  void fillValue( ElementType val )
128  {
129  ErrorCode const res = vectorFill( val, data(), size() );
130  if( res != noError )
131  {
132  throw std::runtime_error( "Filling of vector failed" );
133  }
134  }
135 
143  {
144  if(( size() != rhs.size() ) or( alignmentElements() != rhs.alignmentElements() ) )
145  {
146  throw std::logic_error( "BasicVector::swap(): Vector layouts must be consistent for swapping" );
147  }
148  mData.swap( rhs.mData );
149  }
150 
156  void copy( BasicVector<ElementType> const & rhs )
157  {
158  const std::size_t minAlignment = std::min( alignmentElements(), rhs.alignmentElements() );
159  if( size() != rhs.size() )
160  {
161  throw std::invalid_argument( "BasicVector::copy(): Vector size inconsistent." );
162  }
163  if( vectorCopy( rhs.data( ), data( ), size( ), minAlignment ) != noError )
164  {
165  throw std::runtime_error( "BasicVector::copy() failed: " );
166  }
167  }
168 
173  std::size_t alignmentElements() const { return mData.alignmentElements(); }
174 
179  std::size_t size( ) const { return mData.size(); }
180 
189  ElementType& operator[]( std::size_t idx )
190  {
191  return mData[idx];
192  }
193 
201  ElementType const & operator[]( std::size_t idx ) const
202  {
203  return mData[idx];
204  }
205 
214  ElementType& at( std::size_t idx )
215  {
216  if( idx >= size() )
217  {
218  throw std::out_of_range( "Vector index exceeds size." );
219  }
220  return operator[]( idx );
221  }
222 
231  ElementType const & at( std::size_t idx ) const
232  {
233  if( idx >= size() )
234  {
235  throw std::out_of_range( "Vector index exceeds dimension." );
236  }
237  return operator[]( idx );
238  }
239 
244  ElementType * data( ) { return mData.data( ); }
245 
250  ElementType const * data() const { return mData.data(); }
251 
252 private:
258  BasicVector( const BasicVector< ElementType>& ) = delete;
259 
265  operator=( BasicVector<ElementType> const & ) = delete;
266 
271 };
272 
273 } // namespace efl
274 } // namespace visr
275 
276 #endif // #ifndef VISR_EFL_BASIC_VECTOR_HPP_INCLUDED
ElementType * data()
Definition: aligned_array.hpp:108
BasicVector(std::size_t alignmentElements=0)
Definition: basic_vector.hpp:35
void swap(BasicVector< ElementType > &rhs)
Definition: basic_vector.hpp:142
void fillValue(ElementType val)
Definition: basic_vector.hpp:127
ElementType const & at(std::size_t idx) const
Definition: basic_vector.hpp:231
~BasicVector()
Definition: basic_vector.hpp:74
ElementType const * data() const
Definition: basic_vector.hpp:250
ElementType * data()
Definition: basic_vector.hpp:244
Definition: error_codes.hpp:15
ErrorCode vectorZero(T *const dest, std::size_t numElements, std::size_t alignment)
Definition: vector_functions.cpp:40
void copy(BasicVector< ElementType > const &rhs)
Definition: basic_vector.hpp:156
void assign(const BasicVector< ElementType > &rhs)
Definition: basic_vector.hpp:100
ElementType const & operator[](std::size_t idx) const
Definition: basic_vector.hpp:201
Definition: options.cpp:10
std::size_t size() const
Definition: basic_vector.hpp:179
ErrorCode vectorFill(const T value, T *const dest, std::size_t numElements, std::size_t alignment)
Definition: vector_functions.cpp:51
ErrorCode vectorCopy(T const *const source, T *const dest, std::size_t numElements, std::size_t alignment)
Definition: vector_functions.cpp:73
BasicVector(std::initializer_list< ElementType > const &initialValues, std::size_t alignmentElements=0)
Definition: basic_vector.hpp:59
BasicVector(std::size_t size, std::size_t alignmentElements)
Definition: basic_vector.hpp:46
ElementType & at(std::size_t idx)
Definition: basic_vector.hpp:214
std::size_t alignmentElements() const
Definition: basic_vector.hpp:173
ElementType & operator[](std::size_t idx)
Definition: basic_vector.hpp:189
void resize(std::size_t newSize)
Definition: basic_vector.hpp:82
ErrorCode
Definition: error_codes.hpp:13
void zeroFill()
Definition: basic_vector.hpp:113
Definition: aligned_array.hpp:33
Definition: basic_vector.hpp:28