libpniio
create_attribute.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 4, 2013
20 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
21 //
22 #pragma once
23 
24 #include <pni/core/types.hpp>
25 #include "../nxobject.hpp"
26 
27 namespace pni{
28 namespace io{
29 namespace nx{
30 
31  //-------------------------------------------------------------------------
49  template<
50  typename GTYPE,
51  typename FTYPE,
52  typename ATYPE,
53  typename LTYPE,
54  typename T,
55  typename STYPE
56  >
57  class create_attribute_visitor : public boost::static_visitor<
58  nxobject<GTYPE,FTYPE,ATYPE,LTYPE>
59  >
60  {
61  private:
62  pni::core::string _name;
63  STYPE _shape;
64  bool _overwrite;
65  public:
69  using group_type = GTYPE;
71  using field_type = FTYPE;
73  using attribute_type = ATYPE;
75  using link_type = LTYPE;
76 
77  //-----------------------------------------------------------------
85  create_attribute_visitor(const pni::core::string &n,
86  const STYPE &s,bool o):
87  _name(n),
88  _shape(s),
89  _overwrite(o)
90  {}
91 
92  //-----------------------------------------------------------------
106  {
107  auto attr = g.attributes.template create<T>(_name,_shape,_overwrite);
108  return result_type(attr);
109  }
110 
111  //-----------------------------------------------------------------
125  {
126  auto attr = f.attributes.template create<T>(_name,_shape,_overwrite);
127  return result_type(attr);
128  }
129 
130  //-----------------------------------------------------------------
144  {
145  using namespace pni::core;
146  throw type_error(EXCEPTION_RECORD,
147  "Cannot create an attribute at an attribute!");
148  return result_type();
149  }
150 
151  //-----------------------------------------------------------------
163  {
164  using namespace pni::core;
165  throw type_error(EXCEPTION_RECORD,
166  "Cannot create an attribute on a link!");
167  return result_type();
168  }
169  };
170 
171  //-------------------------------------------------------------------------
178 
184  template<
207  typename T,
208  typename GTYPE,
209  typename FTYPE,
210  typename ATYPE,
211  typename LTYPE,
212  typename STYPE
213  >
214  nxobject<GTYPE,FTYPE,ATYPE,LTYPE>
216  const pni::core::string &name,
217  const STYPE &shape,
218  bool overwrite=false)
219  {
221  return boost::apply_visitor(visitor_t(name,shape,overwrite),parent);
222  }
223 
224  //-------------------------------------------------------------------------
231 
237  template<
258  typename T,
259  typename GTYPE,
260  typename FTYPE,
261  typename ATYPE,
262  typename LTYPE
263  >
264  nxobject<GTYPE,FTYPE,ATYPE,LTYPE>
266  const pni::core::string &name,
267  bool overwrite=false)
268  {
269  using namespace pni::core;
270  return create_attribute<T>(parent,name,shape_t{},overwrite);
271  }
272 
273  //------------------------------------------------------------------------
281 
290  template<
312  typename GTYPE,
313  typename FTYPE,
314  typename ATYPE,
315  typename LTYPE,
316  typename ...ARGTS
317  >
318  nxobject<GTYPE,FTYPE,ATYPE,LTYPE>
320  pni::core::type_id_t tid,
321  ARGTS ...args)
322  {
323  using namespace pni::core;
324 
325  if(tid == type_id_t::UINT8)
326  return create_attribute<uint8>(o,args...);
327  else if(tid == type_id_t::INT8)
328  return create_attribute<int8>(o,args...);
329  else if(tid == type_id_t::UINT16)
330  return create_attribute<uint16>(o,args...);
331  else if(tid == type_id_t::INT16)
332  return create_attribute<int16>(o,args...);
333  else if(tid == type_id_t::UINT32)
334  return create_attribute<uint32>(o,args...);
335  else if(tid == type_id_t::INT32)
336  return create_attribute<int32>(o,args...);
337  else if(tid == type_id_t::UINT64)
338  return create_attribute<uint64>(o,args...);
339  else if(tid == type_id_t::INT64)
340  return create_attribute<int64>(o,args...);
341  else if(tid == type_id_t::FLOAT32)
342  return create_attribute<float32>(o,args...);
343  else if(tid == type_id_t::FLOAT64)
344  return create_attribute<float64>(o,args...);
345  else if(tid == type_id_t::FLOAT128)
346  return create_attribute<float128>(o,args...);
347  else if(tid == type_id_t::COMPLEX32)
348  return create_attribute<complex32>(o,args...);
349  else if(tid == type_id_t::COMPLEX64)
350  return create_attribute<complex64>(o,args...);
351  else if(tid == type_id_t::COMPLEX128)
352  return create_attribute<complex128>(o,args...);
353  else if(tid == type_id_t::BOOL)
354  return create_attribute<bool_t>(o,args...);
355  else if(tid == type_id_t::STRING)
356  return create_attribute<string>(o,args...);
357  else
358  throw type_error(EXCEPTION_RECORD,
359  "Unknown type code!");
360 
361  }
362 
363 //end of namespace
364 }
365 }
366 }
ATYPE attribute_type
Nexus attribute type.
Definition: create_attribute.hpp:73
result_type operator()(const attribute_type &) const
process attribute instances
Definition: create_attribute.hpp:143
GTYPE group_type
Nexus group type.
Definition: create_attribute.hpp:69
nxobject< GTYPE, FTYPE, ATYPE, LTYPE > result_type
result type
Definition: create_attribute.hpp:67
FTYPE field_type
Nexus field type.
Definition: create_attribute.hpp:71
Definition: cbf_reader.hpp:41
pni::core::string _name
the name of the field
Definition: create_attribute.hpp:62
LTYPE link_type
link type
Definition: create_attribute.hpp:75
result_type operator()(const field_type &f) const
process field instances
Definition: create_attribute.hpp:124
nxobject< GTYPE, FTYPE, ATYPE, LTYPE > create_attribute(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &parent, const pni::core::string &name, const STYPE &shape, bool overwrite=false)
create multidimensional attribute
Definition: create_attribute.hpp:215
create_attribute_visitor(const pni::core::string &n, const STYPE &s, bool o)
constructor
Definition: create_attribute.hpp:85
STYPE _shape
shape of field
Definition: create_attribute.hpp:63
create attribute visitor
Definition: create_attribute.hpp:57
result_type operator()(const link_type &) const
process link instances
Definition: create_attribute.hpp:162
result_type operator()(const group_type &g) const
process group instances
Definition: create_attribute.hpp:105
boost::variant< GTYPE, FTYPE, ATYPE, LTYPE > nxobject
Definition: nxobject.hpp:44
bool _overwrite
overwrite flag for the attribute
Definition: create_attribute.hpp:64