libpniio
read.hpp
Go to the documentation of this file.
1 //
2 // (c) Copyright 2013 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
3 //
4 // This file is part of libpniio.
5 //
6 // libpniio 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 // libpniio 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 libpniio. If not, see <http://www.gnu.org/licenses/>.
18 // ===========================================================================
19 // Created on: Jul 5, 2013
20 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
21 //
22 #pragma once
23 
24 #include <pni/core/types.hpp>
25 #include <pni/core/arrays/slice.hpp>
26 #include <vector>
27 #include "../nxobject.hpp"
28 #include "../nxobject_traits.hpp"
29 
30 namespace pni{
31 namespace io{
32 namespace nx{
33 
58  template<
59  template<nximp_code> class OTYPE,
60  nximp_code IMPID,
61  typename ATYPE
62  >
63  void read(const OTYPE<IMPID> &o,ATYPE &a)
64  {
65  typedef nxobject_trait<IMPID> trait_type;
66  typedef typename trait_type::field_type field_type;
67  typedef typename trait_type::attribute_type attribute_type;
68 
69  static_assert(std::is_same<OTYPE<IMPID>,field_type>::value ||
70  std::is_same<OTYPE<IMPID>,attribute_type>::value,
71  "WRITE CAN ONLY BE USED WITH FIELDS AND "
72  "ATTRIBUTES!");
73  o.read(a);
74  }
75 
76  //------------------------------------------------------------------------
105  template<
106  template<nximp_code> class OTYPE,
107  nximp_code IMPID,
108  typename ATYPE,
109  typename ...ITYPES
110  >
111  void read(const OTYPE<IMPID> &o,ATYPE &a,ITYPES ...indices)
112  {
113  typedef nxobject_trait<IMPID> trait_type;
114  typedef typename trait_type::field_type field_type;
115  typedef typename trait_type::attribute_type attribute_type;
116 
117  static_assert(std::is_same<OTYPE<IMPID>,field_type>::value ||
118  std::is_same<OTYPE<IMPID>,attribute_type>::value,
119  "WRITE CAN ONLY BE USED WITH FIELDS AND "
120  "ATTRIBUTES!");
121 
122  o(indices...).read(a);
123  }
124 
125  //------------------------------------------------------------------------
135  template<
136  typename ATYPE,
137  typename GTYPE,
138  typename FTYPE,
139  typename ATTYPE,
140  typename LTYPE
141  >
142  class read_visitor : public boost::static_visitor<void>
143  {
144  public:
146  typedef std::vector<pni::core::slice> selection_t;
147  private:
149  ATYPE &_data;
151  selection_t _selection;
152  public:
154  using result_type = void;
156  using group_type = GTYPE;
158  using field_type = FTYPE;
160  using attribute_type = ATTYPE;
162  using link_type = LTYPE;
163 
164  //-----------------------------------------------------------------
171  read_visitor(ATYPE &data,const selection_t &s=selection_t()):
172  _data(data),
173  _selection(s)
174  {}
175 
176  //-----------------------------------------------------------------
186  {
187  using namespace pni::core;
188  throw type_error(EXCEPTION_RECORD,
189  "One cannot read data to a group object!");
190  }
191 
192  //-----------------------------------------------------------------
213  {
214  if(_selection.size())
215  f(_selection).read(_data);
216  else
217  f.read(_data);
218  }
219 
220  //-----------------------------------------------------------------
241  {
242  if(_selection.size())
243  a(_selection).read(_data);
244  else
245  a.read(_data);
246  }
247 
248  //-----------------------------------------------------------------
258  {
259  using namespace pni::core;
260  throw type_error(EXCEPTION_RECORD,
261  "Cannot read data from a link!");
262  }
263  };
264 
265  //------------------------------------------------------------------------
298  template<typename ATYPE,
299  typename GTYPE,
300  typename FTYPE,
301  typename ATTYPE,
302  typename LTYPE,
303  typename ...ITYPES
304  >
305  void read(const nxobject<GTYPE,FTYPE,ATTYPE,LTYPE> &o,ATYPE &a,ITYPES ...indices)
306  {
308  std::vector<pni::core::slice> sel{pni::core::slice(indices)...};
309  visitor_t visitor(a,sel);
310  return boost::apply_visitor(visitor,o);
311  }
312 
313  //------------------------------------------------------------------------
314  template<typename ATYPE,
315  typename GTYPE,
316  typename FTYPE,
317  typename ATTYPE,
318  typename LTYPE,
319  typename ...ITYPES
320  >
321  void read(nxobject<GTYPE,FTYPE,ATTYPE,LTYPE> &&o,ATYPE &a,ITYPES ...indices)
322  {
324  std::vector<pni::core::slice> sel{pni::core::slice(indices)...};
325  visitor_t visitor(a,sel);
326  return boost::apply_visitor(visitor,o);
327  }
328 
329  //------------------------------------------------------------------------
362  template<
363  typename ATYPE,
364  typename GTYPE,
365  typename FTYPE,
366  typename ATTYPE,
367  typename LTYPE
368  >
369  void read(const nxobject<GTYPE,FTYPE,ATTYPE,LTYPE> &o,ATYPE &a,
370  const std::vector<pni::core::slice> &sel)
371  {
373  visitor_t visitor(a,sel);
374  return boost::apply_visitor(visitor,o);
375  }
376 
377  //------------------------------------------------------------------------
378  template<
379  typename ATYPE,
380  typename GTYPE,
381  typename FTYPE,
382  typename ATTYPE,
383  typename LTYPE
384  >
386  const std::vector<pni::core::slice> &sel)
387  {
389  visitor_t visitor(a,sel);
390  return boost::apply_visitor(visitor,o);
391  }
392 //end of namespace
393 }
394 }
395 }
selection_t _selection
selection vector
Definition: read.hpp:151
FTYPE field_type
Nexus field type.
Definition: read.hpp:158
read visitor
Definition: read.hpp:142
GTYPE group_type
Nexus group type.
Definition: read.hpp:156
std::vector< pni::core::slice > selection_t
selection type for partial IO
Definition: read.hpp:146
result_type operator()(const attribute_type &a) const
process attribute instances
Definition: read.hpp:240
Definition: cbf_reader.hpp:41
result_type operator()(const link_type &) const
process link instances
Definition: read.hpp:257
ATTYPE attribute_type
Nexus attribute type.
Definition: read.hpp:160
result_type operator()(const field_type &f) const
process field instances
Definition: read.hpp:212
ATYPE & _data
reference to the data holding object
Definition: read.hpp:149
nximp_code
implementation codes
Definition: nximp_code.hpp:40
result_type operator()(const group_type &) const
process group instances
Definition: read.hpp:185
void result_type
result type
Definition: read.hpp:154
LTYPE link_type
NeXus link type.
Definition: read.hpp:162
boost::variant< GTYPE, FTYPE, ATYPE, LTYPE > nxobject
Definition: nxobject.hpp:44
read_visitor(ATYPE &data, const selection_t &s=selection_t())
constructor
Definition: read.hpp:171
void read(const OTYPE< IMPID > &o, ATYPE &a)
read data
Definition: read.hpp:63