libpniio
write.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 "../nxobject.hpp"
27 #include "../nxobject_traits.hpp"
28 
29 namespace pni{
30 namespace io{
31 namespace nx{
32 
33  //------------------------------------------------------------------------
56  template<
57  template<nximp_code> class OTYPE,
58  nximp_code IMPID,
59  typename ATYPE
60  >
61  void write(const OTYPE<IMPID> &o,const ATYPE &a)
62  {
63  typedef nxobject_trait<IMPID> trait_type;
64  typedef typename trait_type::field_type field_type;
65  typedef typename trait_type::attribute_type attribute_type;
66 
67  static_assert(std::is_same<OTYPE<IMPID>,field_type>::value ||
68  std::is_same<OTYPE<IMPID>,attribute_type>::value,
69  "WRITE CAN ONLY BE USED WITH FIELDS AND "
70  "ATTRIBUTES!");
71  o.write(a);
72  }
73 
74  //------------------------------------------------------------------------
100  template<
101  template<nximp_code> class OTYPE,
102  nximp_code IMPID,
103  typename ATYPE,
104  typename ...ITYPES
105  >
106  void write(const OTYPE<IMPID> &o,const ATYPE &a,ITYPES ...indices)
107  {
108  typedef nxobject_trait<IMPID> trait_type;
109  typedef typename trait_type::field_type field_type;
110  typedef typename trait_type::attribute_type attribute_type;
111 
112  static_assert(std::is_same<OTYPE<IMPID>,field_type>::value ||
113  std::is_same<OTYPE<IMPID>,attribute_type>::value,
114  "WRITE CAN ONLY BE USED WITH FIELDS AND "
115  "ATTRIBUTES!");
116 
117  o(indices...).write(a);
118  }
119 
120  //-------------------------------------------------------------------------
133  template<
134  typename ATYPE,
135  typename GTYPE,
136  typename FTYPE,
137  typename AATYPE,
138  typename LTYPE
139  >
140  class write_visitor : public boost::static_visitor<void>
141  {
142  public:
144  typedef std::vector<pni::core::slice> selection_t;
145  private:
147  const ATYPE &_data;
149  selection_t _selection;
150  public:
152  using result_type = void;
154  using group_type = GTYPE;
156  using field_type = FTYPE;
158  using attribute_type = AATYPE;
160  using link_type = LTYPE;
161 
162  //-----------------------------------------------------------------
169  write_visitor(const ATYPE &data,
170  const selection_t &s=selection_t()):
171  _data(data),
172  _selection(s)
173  {}
174 
175  //-----------------------------------------------------------------
185  {
186  using namespace pni::core;
187  throw type_error(EXCEPTION_RECORD,
188  "One cannot write data to a group object!");
189  }
190 
191  //-----------------------------------------------------------------
212  {
213  if(_selection.size())
214  f(_selection).write(_data);
215  else
216  f.write(_data);
217  }
218 
219  //-----------------------------------------------------------------
241  {
242  if(_selection.size())
243  a(_selection).write(_data);
244  else
245  a.write(_data);
246  }
247 
248  //-----------------------------------------------------------------
256  {
257  using namespace pni::core;
258  throw type_error(EXCEPTION_RECORD,
259  "Cannot write data to a link object!");
260  }
261  };
262 
263  //------------------------------------------------------------------------
291  template<
292  typename ATYPE,
293  typename GTYPE,
294  typename FTYPE,
295  typename AATYPE,
296  typename LTYPE,
297  typename ...ITYPES
298  >
299  void write(nxobject<GTYPE,FTYPE,AATYPE,LTYPE> &&o,const ATYPE &a,
300  ITYPES ...indices)
301  {
303  std::vector<pni::core::slice> sel{pni::core::slice(indices)...};
304  return boost::apply_visitor(visitor_type(a,sel),o);
305  }
306 
307  //------------------------------------------------------------------------
308  template<
309  typename ATYPE,
310  typename GTYPE,
311  typename FTYPE,
312  typename AATYPE,
313  typename LTYPE,
314  typename ...ITYPES
315  >
316  void write(const nxobject<GTYPE,FTYPE,AATYPE,LTYPE> &o,const ATYPE &a,
317  ITYPES ...indices)
318  {
320  std::vector<pni::core::slice> sel{pni::core::slice(indices)...};
321 
322  return boost::apply_visitor(visitor_type(a,sel),o);
323  }
324 
325  //------------------------------------------------------------------------
326  template<
327  typename ATYPE,
328  typename GTYPE,
329  typename FTYPE,
330  typename AATYPE,
331  typename LTYPE
332  >
333  void write(const nxobject<GTYPE,FTYPE,AATYPE,LTYPE> &o,const ATYPE &a,
334  const std::vector<pni::core::slice> &sel)
335  {
337 
338  return boost::apply_visitor(visitor_type(a,sel),o);
339  }
340 
341 
342 
343 //end of namespace
344 }
345 }
346 }
result_type operator()(const group_type &) const
process group instances
Definition: write.hpp:184
FTYPE field_type
Nexus field type.
Definition: write.hpp:156
LTYPE link_type
Nexus link type.
Definition: write.hpp:160
result_type operator()(const field_type &f) const
process field instances
Definition: write.hpp:211
GTYPE group_type
Nexus group type.
Definition: write.hpp:154
AATYPE attribute_type
Nexus attribute type.
Definition: write.hpp:158
write_visitor(const ATYPE &data, const selection_t &s=selection_t())
constructor
Definition: write.hpp:169
Definition: cbf_reader.hpp:41
selection_t _selection
selection vector
Definition: write.hpp:149
void result_type
result type
Definition: write.hpp:152
result_type operator()(const attribute_type &a) const
process attribute instances
Definition: write.hpp:240
nximp_code
implementation codes
Definition: nximp_code.hpp:40
std::vector< pni::core::slice > selection_t
selection type for partial IO
Definition: write.hpp:144
write visitor
Definition: write.hpp:140
result_type operator()(const link_type &) const
process link instances
Definition: write.hpp:255
boost::variant< GTYPE, FTYPE, ATYPE, LTYPE > nxobject
Definition: nxobject.hpp:44
const ATYPE & _data
reference to the data holding object
Definition: write.hpp:147
void write(const OTYPE< IMPID > &o, const ATYPE &a)
write data to an attribute or field
Definition: write.hpp:61