libpniio
set_unit.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 1, 2013
20 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
21 //
22 #pragma once
23 
24 #include <pni/core/types.hpp>
25 #include <pni/core/error.hpp>
26 #include "../nxobject.hpp"
27 #include "../nxobject_traits.hpp"
28 
29 namespace pni{
30 namespace io{
31 namespace nx{
32 
50  template<typename FTYPE>
51  void set_unit(const FTYPE &field,const pni::core::string &value)
52  {
53  using namespace pni::core;
54  typedef nximp_code_map<FTYPE> map_type;
55  typedef typename nxobject_trait<map_type::icode>::field_type field_type;
56 
57  static_assert(std::is_same<field_type,FTYPE>::value,
58  "A UNIT CAN ONLY SET ON A FIELD TYPE!");
59 
60  auto attribute = field.attributes.template create<string>("units",true);
61  attribute.write(value);
62  }
63 
64 
65  //-------------------------------------------------------------------------
75  template<
76  typename GTYPE,
77  typename FTYPE,
78  typename ATYPE,
79  typename LTYPE
80  >
81  class set_unit_visitor : public boost::static_visitor<void>
82  {
83  private:
84  pni::core::string _unit;
85  public:
87  using result_type = void;
89  using group_type = GTYPE;
91  using field_type = FTYPE;
93  using attribute_type = ATYPE;
95  using link_type = LTYPE;
96 
97  //-----------------------------------------------------------------
103  set_unit_visitor(const pni::core::string &s):_unit(s) {}
104 
105  //-----------------------------------------------------------------
115  {
116  using namespace pni::core;
117  throw type_error(EXCEPTION_RECORD,
118  "Groups do not have units!");
119  }
120 
121  //-----------------------------------------------------------------
136  {
137  set_unit(f,_unit);
138  }
139 
140  //-----------------------------------------------------------------
150  {
151  using namespace pni::core;
152  throw type_error(EXCEPTION_RECORD,
153  "Attributes do not have units!");
154  }
155 
156  //-----------------------------------------------------------------
163  {
164  using namespace pni::core;
165  throw type_error(EXCEPTION_RECORD,
166  "Links do not have units!");
167  }
168  };
169 
170  //------------------------------------------------------------------------
191  template<
192  typename GTYPE,
193  typename FTYPE,
194  typename ATYPE,
195  typename LTYPE
196  >
198  const pni::core::string &s)
199  {
200  using visitor_type = set_unit_visitor<GTYPE,FTYPE,ATYPE,LTYPE>;
201 
202  return boost::apply_visitor(visitor_type(s),o);
203  }
204 
205 //end of namespace
206 }
207 }
208 }
pni::core::string _unit
unit string
Definition: set_unit.hpp:84
FTYPE field_type
Nexus field type.
Definition: set_unit.hpp:91
GTYPE group_type
Nexus group type.
Definition: set_unit.hpp:89
ATYPE attribute_type
Nexus attribute type.
Definition: set_unit.hpp:93
result_type operator()(const group_type &) const
process groups
Definition: set_unit.hpp:114
nexus object traits
Definition: nxobject_traits.hpp:44
void result_type
result type
Definition: set_unit.hpp:87
set_unit_visitor(const pni::core::string &s)
constructor
Definition: set_unit.hpp:103
result_type operator()(const link_type &) const
process links
Definition: set_unit.hpp:162
Definition: cbf_reader.hpp:41
LTYPE link_type
NeXus link type.
Definition: set_unit.hpp:95
implementation code type map
Definition: nximp_code_map.hpp:37
void set_unit(const FTYPE &field, const pni::core::string &value)
set the unit
Definition: set_unit.hpp:51
result_type operator()(const attribute_type &) const
process attributes
Definition: set_unit.hpp:149
boost::variant< GTYPE, FTYPE, ATYPE, LTYPE > nxobject
Definition: nxobject.hpp:44
result_type operator()(const field_type &f) const
process fields
Definition: set_unit.hpp:135
set unit visitor
Definition: set_unit.hpp:81