libpnicore
array_holder.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: 11 08, 2013
22 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
23 //
24 #pragma once
25 
26 #include "../algorithms.hpp"
27 #include "array_holder_interface.hpp"
28 
29 namespace pni{
30 namespace core{
31 
41  template<typename ATYPE>
42  const void *get_pointer(const ATYPE &a)
43  {
44  return (void *)(a.data());
45  }
46 
47 
48  //-------------------------------------------------------------------------
56  template<typename OT>
58  {
59  private:
60  OT _object;
61  public:
62  //==================constructors and destructor====================
64  array_holder(const OT &o):_object(o) {}
65 
66  //-----------------------------------------------------------------
68  array_holder(OT &&o):_object(std::move(o)) {}
69 
70  //-----------------------------------------------------------------
73  _object(o._object)
74  {}
75 
76  //-----------------------------------------------------------------
79  _object(std::move(o._object))
80  {}
81 
82  //-----------------------------------------------------------------
89  virtual array_holder_interface *clone() const
90  {
91  return new array_holder<OT>(_object);
92  }
93 
94  //====================public member functions======================
96  virtual type_id_t type_id() const{ return OT::type_id; }
97 
98  //-----------------------------------------------------------------
100  virtual size_t rank() const { return _object.rank(); }
101 
102  //-----------------------------------------------------------------
104  virtual shape_t shape() const
105  {
106  return _object.template shape<shape_t>();
107  }
108 
109  //-----------------------------------------------------------------
111  virtual size_t size() const { return _object.size(); }
112 
113  //-----------------------------------------------------------------
126  virtual value operator[](size_t i) const
127  {
128  return value(_object[i]);
129  }
130 
131  //-----------------------------------------------------------------
143  virtual value_ref operator[](size_t i)
144  {
145  return value_ref(std::ref(_object[i]));
146  }
147 
148  //-----------------------------------------------------------------
159  virtual value at(size_t i) const
160  {
161  return value(_object.at(i));
162  }
163 
164  //-----------------------------------------------------------------
175  virtual value_ref at(size_t i)
176  {
177  return value_ref(std::ref(_object.at(i)));
178  }
179 
180  //-----------------------------------------------------------------
182  virtual value operator()(const element_index &index) const
183  {
184  return value(_object(index));
185  }
186 
187  //-----------------------------------------------------------------
189  virtual value_ref operator()(const element_index &index)
190  {
191  return value_ref(std::ref(_object(index)));
192  }
193 
194  //-----------------------------------------------------------------
196  virtual std::ostream &write(std::ostream &os) const
197  {
198  os<<_object;
199  return os;
200  }
201 
202  //-----------------------------------------------------------------
204  virtual std::istream &read(std::istream &is)
205  {
206  is>>_object;
207  return is;
208  }
209 
210  //-----------------------------------------------------------------
212  virtual string type_name() const
213  {
214  return typeid(OT).name();
215  }
216 
217  //-----------------------------------------------------------------
219  virtual const void *ptr() const
220  {
221  return get_pointer(_object);
222  }
223 
224  };
225 
226 //end of namespace
227 }
228 }
OT _object
the original object
Definition: array_holder.hpp:60
array_holder(array_holder< OT > &&o)
move constructor
Definition: array_holder.hpp:78
virtual std::ostream & write(std::ostream &os) const
write data to output stream
Definition: array_holder.hpp:196
virtual value_ref operator()(const element_index &index)
return element reference
Definition: array_holder.hpp:189
virtual std::istream & read(std::istream &is)
read data from input stream
Definition: array_holder.hpp:204
const void * get_pointer(const ATYPE &a)
get array pointer
Definition: array_holder.hpp:42
array holder template
Definition: array_holder.hpp:57
STL namespace.
array_holder(const OT &o)
construct by copying o
Definition: array_holder.hpp:64
virtual value_ref operator[](size_t i)
get value reference
Definition: array_holder.hpp:143
array_holder(OT &&o)
construct by moving o
Definition: array_holder.hpp:68
Definition: add_op.hpp:29
virtual type_id_t type_id() const
return type ID of the held array
Definition: array_holder.hpp:96
virtual value_ref at(size_t i)
get reference
Definition: array_holder.hpp:175
virtual string type_name() const
return the name of the type
Definition: array_holder.hpp:212
type_id_t type_id(const array_view< ATYPE > &)
specialization for type_id
Definition: array_view.hpp:821
virtual const void * ptr() const
return pointer to data
Definition: array_holder.hpp:219
virtual value operator()(const element_index &index) const
return element value
Definition: array_holder.hpp:182
std::vector< size_t > element_index
single element index type
Definition: array_holder_interface.hpp:43
array_holder(const array_holder< OT > &o)
copy constructor
Definition: array_holder.hpp:72
array holder interface
Definition: array_holder_interface.hpp:39
type_id_t
type codes for PNI data types
Definition: types/types.hpp:148
type erasure for references to POD data
Definition: value_ref.hpp:43
virtual array_holder_interface * clone() const
clone the holder
Definition: array_holder.hpp:89
type erasure for POD data
Definition: value.hpp:46
virtual value operator[](size_t i) const
get value
Definition: array_holder.hpp:126
virtual size_t rank() const
return the rank of the held array
Definition: array_holder.hpp:100
virtual value at(size_t i) const
get value
Definition: array_holder.hpp:159
virtual size_t size() const
return the total number of elements in the array
Definition: array_holder.hpp:111
virtual shape_t shape() const
return the shape of the held array
Definition: array_holder.hpp:104