libpniio
get_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> pni::core::string get_unit(const FTYPE &field)
51  {
52  typedef nximp_code_map<FTYPE> imp_map;
53  typedef typename nxobject_trait<imp_map::icode>::field_type field_type;
54 
55  static_assert(std::is_same<FTYPE,field_type>::value,
56  "UNIT CAN ONLY BE RETRIEFVED FROM A FIELD TYPE!");
57 
58  pni::core::string buffer;
59 
60  if(field.attributes.exists("units"))
61  field.attributes["units"].read(buffer);
62 
63  return buffer;
64  }
65 
66  //------------------------------------------------------------------------
78  template<
79  typename GTYPE,
80  typename FTYPE,
81  typename ATYPE,
82  typename LTYPE
83  >
84  class get_unit_visitor : public boost::static_visitor<pni::core::string>
85  {
86  public:
88  using result_type = pni::core::string;
90  using group_type = GTYPE;
92  using field_type = FTYPE;
94  using attribute_type = ATYPE;
96  using link_type = LTYPE;
97 
98  //-----------------------------------------------------------------
110  {
111  using namespace pni::core;
112  throw type_error(EXCEPTION_RECORD,
113  "Cannot retreive a unit from a group!");
114 
115  return result_type();
116  }
117 
118  //-----------------------------------------------------------------
136  {
137  return get_unit(f);
138  }
139 
140  //-----------------------------------------------------------------
152  {
153  using namespace pni::core;
154  throw type_error(EXCEPTION_RECORD,
155  "Cannot retrieve a unit from an attribute type!");
156  return result_type();
157  }
158 
159  //-----------------------------------------------------------------
169  {
170  using namespace pni::core;
171  throw type_error(EXCEPTION_RECORD,
172  "Cannot retrieve a unit from a link type!");
173  return result_type();
174  }
175  };
176 
177  //------------------------------------------------------------------------
202  template<
203  typename GTYPE,
204  typename FTYPE,
205  typename ATYPE,
206  typename LTYPE
207  >
208  pni::core::string get_unit(const nxobject<GTYPE,FTYPE,ATYPE,LTYPE> &o)
209  {
210  using visitor_type = get_unit_visitor<GTYPE,FTYPE,ATYPE,LTYPE>;
211  return boost::apply_visitor(visitor_type(),o);
212  }
213 
214 //end of namespace
215 }
216 }
217 }
pni::core::string result_type
result type
Definition: get_unit.hpp:88
ATYPE attribute_type
Nexus attribute type.
Definition: get_unit.hpp:94
LTYPE link_type
Nexus link type.
Definition: get_unit.hpp:96
pni::core::string get_unit(const FTYPE &field)
get the unit of a field
Definition: get_unit.hpp:50
GTYPE group_type
Nexus group type.
Definition: get_unit.hpp:90
nexus object traits
Definition: nxobject_traits.hpp:44
Definition: cbf_reader.hpp:41
FTYPE field_type
Nexus field type.
Definition: get_unit.hpp:92
result_type operator()(const link_type &) const
process links
Definition: get_unit.hpp:168
result_type operator()(const group_type &) const
process groups
Definition: get_unit.hpp:109
result_type operator()(const attribute_type &) const
process attributes
Definition: get_unit.hpp:151
implementation code type map
Definition: nximp_code_map.hpp:37
get unit visitor
Definition: get_unit.hpp:84
boost::variant< GTYPE, FTYPE, ATYPE, LTYPE > nxobject
Definition: nxobject.hpp:44
result_type operator()(const field_type &f) const
process fields
Definition: get_unit.hpp:135