libpniio
create_field.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 //
20 // Created on: Jul 4, 2013
21 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
22 //
23 #pragma once
24 
25 #include <pni/core/types.hpp>
26 #include "../nxobject.hpp"
27 #include "../nxobject_traits.hpp"
28 #include "../nximp_code_map.hpp"
29 #include "../nxfilter.hpp"
30 #include "get_object.hpp"
31 #include "utils.hpp"
32 
33 namespace pni{
34 namespace io{
35 namespace nx{
36 
37  //-------------------------------------------------------------------------
56  template<
57  typename GTYPE,
58  typename FTYPE,
59  typename ATYPE,
60  typename LTYPE,
61  typename T,
62  typename STYPE,
63  typename FILTERT
64  >
65  class create_field_visitor : public boost::static_visitor<
66  nxobject<GTYPE,FTYPE,ATYPE,LTYPE>
67  >
68  {
69  public:
73  using group_type = GTYPE;
75  using field_type = FTYPE;
77  using attribute_type = ATYPE;
79  using link_type = LTYPE;
81  using filter_type = FILTERT;
82  private:
83  pni::core::string _name;
84  STYPE _shape;
85  STYPE _cshape;
87  public:
88 
89  //----------------------------------------------------------------
99  create_field_visitor(const pni::core::string &n,const STYPE &s,
100  const STYPE &cs,const filter_type &filter):
101  _name(n),
102  _shape(s),
103  _cshape(cs),
104  _filter(filter)
105  {}
106 
107  //-----------------------------------------------------------------
125  {
126  auto f = g.template create_field<T>(_name,_shape,_cshape,_filter);
127 
128  return result_type(f);
129  }
130 
131  //-----------------------------------------------------------------
143  {
144  using namespace pni::core;
145  throw type_error(EXCEPTION_RECORD,
146  "Cannot create a field below a field!");
147  return result_type();
148  }
149 
150  //-----------------------------------------------------------------
164  {
165  using namespace pni::core;
166  throw type_error(EXCEPTION_RECORD,
167  "Cannot create a field below an attribute!");
168  return result_type();
169  }
170 
171  //----------------------------------------------------------------
183  {
184  using namespace pni::core;
185  throw type_error(EXCEPTION_RECORD,
186  "Cannot create a field below a link instance!");
187  return result_type();
188  }
189  };
190 
191  //------------------------------------------------------------------------
221  template<
222  typename T,
223  typename GTYPE,
224  typename FTYPE,
225  typename ATYPE,
226  typename LTYPE,
227  typename STYPE,
228  typename FILTERT,
229  typename PATHT
230  >
231  nxobject<GTYPE,FTYPE,ATYPE,LTYPE>
233  const PATHT &path,
234  const STYPE &shape,const STYPE &chunk,
235  const FILTERT &filter)
236  {
238 
239  nxpath fpath = get_path(path);
240 
241  pni::core::string field_name = fpath.back().first;
242  nxpath parent_path(fpath);
243  parent_path.pop_back();
244 
245  nxobject<GTYPE,FTYPE,ATYPE,LTYPE> parent = get_object(location,parent_path);
246  return boost::apply_visitor(visitor_type(field_name,shape,chunk,filter),parent);
247 
248  }
249 
250  //------------------------------------------------------------------------
277  template<
278  typename T,
279  typename GTYPE,
280  typename FTYPE,
281  typename ATYPE,
282  typename LTYPE,
283  typename STYPE,
284  typename PATHT
285  >
286  nxobject<GTYPE,FTYPE,ATYPE,LTYPE>
287  create_field(const nxobject<GTYPE,FTYPE,ATYPE,LTYPE> &parent,const PATHT &path,
288  const STYPE &shape,const STYPE &chunk)
289  {
290  //create the default filter
292 
293  return create_field<T>(parent,path,shape,chunk,filter_type());
294  }
295 
296  //------------------------------------------------------------------------
326  template<
327  typename T,
328  typename GTYPE,
329  typename FTYPE,
330  typename ATYPE,
331  typename LTYPE,
332  typename STYPE,
333  typename PATHT,
334  typename FILTERIMP
335  >
336  nxobject<GTYPE,FTYPE,ATYPE,LTYPE>
337  create_field(const nxobject<GTYPE,FTYPE,ATYPE,LTYPE> &parent,const PATHT &path,
338  const STYPE &shape,const nxfilter<FILTERIMP> &filter)
339  {
340  //create the default filter
341  STYPE chunk(shape);
342  chunk.front() = 1;
343 
344  return create_field<T>(parent,path,shape,chunk,filter);
345  }
346 
347  //------------------------------------------------------------------------
371  template<
372  typename T,
373  typename GTYPE,
374  typename FTYPE,
375  typename ATYPE,
376  typename LTYPE,
377  typename PATHT,
378  typename STYPE = pni::core::shape_t
379  >
380  nxobject<GTYPE,FTYPE,ATYPE,LTYPE>
381  create_field(const nxobject<GTYPE,FTYPE,ATYPE,LTYPE> &parent,const PATHT &path,
382  const STYPE &shape = {1})
383  {
384  STYPE chunk(shape);
385  chunk.front() = 1;
386 
387  return create_field<T>(parent,path,shape,chunk);
388  }
389 
390  //--------------------------------------------------------------------------
415  template<
416  typename GTYPE,
417  typename FTYPE,
418  typename ATYPE,
419  typename LTYPE,
420  typename ...ARGTS
421  >
422  nxobject<GTYPE,FTYPE,ATYPE,LTYPE>
424  pni::core::type_id_t tid,ARGTS...args)
425  {
426  using namespace pni::core;
427  switch(tid)
428  {
429  case type_id_t::UINT8: return create_field<uint8>(o,args...);
430  case type_id_t::INT8: return create_field<int8>(o,args...);
431  case type_id_t::UINT16: return create_field<uint16>(o,args...);
432  case type_id_t::INT16: return create_field<int16>(o,args...);
433  case type_id_t::UINT32: return create_field<uint32>(o,args...);
434  case type_id_t::INT32: return create_field<int32>(o,args...);
435  case type_id_t::UINT64: return create_field<uint64>(o,args...);
436  case type_id_t::INT64: return create_field<int64>(o,args...);
437  case type_id_t::FLOAT32: return create_field<float32>(o,args...);
438  case type_id_t::FLOAT64: return create_field<float64>(o,args...);
439  case type_id_t::FLOAT128: return create_field<float128>(o,args...);
440  case type_id_t::COMPLEX32: return create_field<complex32>(o,args...);
441  case type_id_t::COMPLEX64: return create_field<complex64>(o,args...);
442  case type_id_t::COMPLEX128: return create_field<complex128>(o,args...);
443  case type_id_t::BINARY: return create_field<binary>(o,args...);
444  case type_id_t::STRING: return create_field<string>(o,args...);
445  case type_id_t::BOOL: return create_field<bool_t>(o,args...);
446  default:
447  throw type_error(EXCEPTION_RECORD,
448  "Unknown type id!");
449  };
450  }
451 
452 //end of namespace
453 }
454 }
455 }
Filter object.
Definition: nxfilter.hpp:42
ATYPE attribute_type
Nexus attribute type.
Definition: create_field.hpp:77
nexus object traits
Definition: nxobject_traits.hpp:44
void pop_back()
remove first element from path
Definition: nxpath.cpp:126
result_type operator()(const group_type &g) const
process group instances
Definition: create_field.hpp:124
FTYPE field_type
Nexus field type.
Definition: create_field.hpp:75
nxobject< GTYPE, FTYPE, ATYPE, LTYPE > create_field(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &location, const PATHT &path, const STYPE &shape, const STYPE &chunk, const FILTERT &filter)
field construction
Definition: create_field.hpp:232
Definition: cbf_reader.hpp:41
create_field_visitor(const pni::core::string &n, const STYPE &s, const STYPE &cs, const filter_type &filter)
constructor
Definition: create_field.hpp:99
STYPE _cshape
chunk shape of the field
Definition: create_field.hpp:85
GTYPE group_type
Nexus group type.
Definition: create_field.hpp:73
FILTERT filter_type
define the filter type
Definition: create_field.hpp:81
auto get_object(const OTYPE &o, const PATHT &path) -> decltype(get_parent(o))
retrieve an object from path
Definition: get_object.hpp:62
STYPE _shape
shape of field
Definition: create_field.hpp:84
result_type operator()(const attribute_type &) const
process attribute instances
Definition: create_field.hpp:163
result_type operator()(const field_type &) const
process field instances
Definition: create_field.hpp:142
pni::core::string _name
the name of the field
Definition: create_field.hpp:83
Nexus path class.
Definition: nxpath/nxpath.hpp:60
filter_type _filter
reference to the filter
Definition: create_field.hpp:86
create field visitor
Definition: create_field.hpp:65
element_type back() const
get first element
Definition: nxpath.cpp:132
nxobject< GTYPE, FTYPE, ATYPE, LTYPE > result_type
result type
Definition: create_field.hpp:71
pni::core::string get_path(const OTYPE< IMPID > &o)
get object path
Definition: get_path.hpp:59
result_type operator()(const link_type &) const
process link instances
Definition: create_field.hpp:182
boost::variant< GTYPE, FTYPE, ATYPE, LTYPE > nxobject
Definition: nxobject.hpp:44
LTYPE link_type
link type
Definition: create_field.hpp:79