libpnicore
array.hpp
1 //
2 // (c) Copyright 2013 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
3 //
4 // This file is part of libpnicore.
5 //
6 // libpnicore is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // libpnicore 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
17 // along with libpnicore. If not, see <http://www.gnu.org/licenses/>.
18 //
19 // ============================================================================
20 //
21 // Created on: Jan 11, 2013
22 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
23 //
24 #pragma once
25 #include<iostream>
26 #include<memory>
27 
28 #include "../error/exceptions.hpp"
29 #include "../error/exception_utils.hpp"
30 #include "../types/types.hpp"
31 #include "../arrays.hpp"
32 #include "array_holder.hpp"
33 #include "array_iterator.hpp"
34 #include "../algorithms.hpp"
35 
36 
37 namespace pni{
38 namespace core{
39 
47 
52 
62  class array //the type erasure
75  {
76  private:
78  typedef std::unique_ptr<array_holder_interface> pointer_type;
79 
80  //----------------------------------------------------------------
92  {
94  "Instance of data_object holds no data!");
95  }
96 
97  //----------------------------------------------------------------
108  static void _check_pointer(const pointer_type &ptr,
109  const exception_record &r)
110  {
111  if(!ptr)
113  }
114 
116  pointer_type _ptr;
117  public:
118  //====================public types=================================
119  typedef value value_type;
126  typedef size_t size_type;
127  //===================constructors and destructor===================
129  array():_ptr(nullptr) {}
130 
131  //-----------------------------------------------------------------
138 
145  template<typename T,
153  //this template expression is necessary to avoid that this
154  //constructor is callend when data_object itself is to be
155  //copied (see below for the correct copy constructor)
156  typename = typename std::enable_if<
157  !std::is_same<array,typename
158  std::remove_reference<T>::type >::value
159  >::type
160  >
161  explicit array(const T &o):_ptr(new array_holder<T>(o))
162  { }
163 
164  //------------------------------------------------------------------
169 
174  template<typename T,
178  //this template expression is necessary to avoid that this
179  //constructor is used when a data_object by itself is to be
180  //moved (seel below for the correct move constructor)
181  typename = typename std::enable_if<
182  !std::is_same<array,typename
183  std::remove_reference<T>::type >::value
184  >::type
185  >
186  explicit array(T &&o):
187  _ptr(new array_holder<typename std::remove_cv<
188  typename std::remove_reference<T>::type>::type>
189  (std::forward<T>(o)))
190  { }
191 
192 
193  //------------------------------------------------------------------
195  array(const array &e);
196  //------------------------------------------------------------------
198  array(array &&e);
199 
200  //==============assignment operators===============================
202  array &operator=(const array &a);
203 
204  //-----------------------------------------------------------------
206  array &operator=(array &&a);
207 
208  //=====================public member functions=====================
215  type_id_t type_id() const;
216 
217  //-----------------------------------------------------------------
227  size_t rank() const;
228 
229  //-----------------------------------------------------------------
239  shape_t shape() const;
240 
241  //-----------------------------------------------------------------
247 
252  template<typename CTYPE> CTYPE shape() const;
259 
260  //-----------------------------------------------------------------
269  size_t size() const;
270 
271 
272  //-----------------------------------------------------------------
284  value operator[](size_t i) const;
285 
286  //-----------------------------------------------------------------
298  value at(size_t i) const;
299 
300  //-----------------------------------------------------------------
312  value_ref operator[](size_t i);
313 
314  //-----------------------------------------------------------------
319  value_ref at(size_t i);
320 
321  //-----------------------------------------------------------------
331  value_ref operator()(const element_index &index);
332 
333  //-----------------------------------------------------------------
342  value operator()(const element_index &index) const;
343 
344  //-----------------------------------------------------------------
346  string type_name() const;
347 
348  //-----------------------------------------------------------------
350  iterator begin() { return iterator(this,0); }
351 
352  //-----------------------------------------------------------------
354  iterator end() { return iterator(this,size()); }
355 
356  //-----------------------------------------------------------------
358  const_iterator begin() const { return const_iterator(this,0); }
359 
360  //-----------------------------------------------------------------
362  const_iterator end() const { return const_iterator(this,size()); }
363 
364  //-----------------------------------------------------------------
366  const void *data() const;
367 
368  //-----------------------------------------------------------------
370  void *data() ;
371 
372  };
373 
374  //======================implementation of template members=================
375  template<typename CTYPE> CTYPE array::shape() const
376  {
377  shape_t s = this->shape();
378  CTYPE c(s.size());
379  std::copy(s.begin(),s.end(),c.begin());
380  return c;
381  }
382 
383  //-------------------------------------------------------------------------
393  type_id_t type_id(const array &a);
394 
395 //end of namespace
396 }
397 }
398 
array(const T &o)
copy original object
Definition: array.hpp:161
string type_name() const
return the type name
exception record
Definition: exceptions.hpp:90
size_t size_type
type used for array size
Definition: array.hpp:126
array_holder_interface::element_index element_index
element index type
Definition: array.hpp:123
type erasure array types
Definition: array.hpp:74
const_iterator begin() const
get const iterator to first element
Definition: array.hpp:358
value value_type
value type of the array
Definition: array.hpp:119
value_ref operator()(const element_index &index)
get reference to element
array holder template
Definition: array_holder.hpp:57
STL namespace.
std::vector< slice > view_index
view index type
Definition: array_holder_interface.hpp:45
type_id_t type_id() const
get type id
array_iterator< 1 > const_iterator
Definition: array.hpp:121
pointer_type _ptr
pointer to an instance of array_holder
Definition: array.hpp:116
shape_t shape() const
return shape
array()
default constructor
Definition: array.hpp:129
Definition: add_op.hpp:29
array_holder_interface::view_index view_index
view index type
Definition: array.hpp:125
array iterator
Definition: array_iterator.hpp:121
invoke< std::enable_if< C::value >> enable_if
shortcut for std::enable_if
Definition: sfinae_macros.hpp:108
array(T &&o)
move original object
Definition: array.hpp:186
array_iterator< 0 > iterator
read/write iterator
Definition: array.hpp:120
std::vector< size_t > element_index
single element index type
Definition: array_holder_interface.hpp:43
memory not allocated error
Definition: exceptions.hpp:321
std::unique_ptr< array_holder_interface > pointer_type
local pointer type used to hold the array
Definition: array.hpp:78
iterator end()
get iterator to last+1 element
Definition: array.hpp:354
size_t rank() const
return rank of array
const_iterator end() const
get const iterator to last+1 element
Definition: array.hpp:362
array & operator=(const array &a)
copy assignment operator
type_id_t type_id(const array &a)
get type id
const void * data() const
get const pointer to data
value operator[](size_t i) const
get value at index i
type_id_t
type codes for PNI data types
Definition: types/types.hpp:148
value at(size_t i) const
get value at index i
type erasure for references to POD data
Definition: value_ref.hpp:43
type erasure for POD data
Definition: value.hpp:46
static void _check_pointer(const pointer_type &ptr, const exception_record &r)
check pointer
Definition: array.hpp:108
static void _throw_not_allocated_error(const exception_record &r)
throw exception
Definition: array.hpp:91
iterator begin()
get iterator to first element
Definition: array.hpp:350
size_t size() const
return number of elements