libpniio
set_class.hpp
Go to the documentation of this file.
1 //
2 // (c) Copyright 2014 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: May 29, 2014
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_traits.hpp"
27 #include "is_group.hpp"
28 
29 namespace pni{
30 namespace io{
31 namespace nx{
32 
50  template<
51  template<nximp_code> class OTYPE,
52  nximp_code IMPID
53  >
54  void set_class(const OTYPE<IMPID> &o,const pni::core::string &nxclass)
55  {
56  using namespace pni::core;
57  typedef typename nxobject_trait<IMPID>::object_type object_type;
58  typedef typename nxobject_trait<IMPID>::field_type field_type;
59  typedef typename nxobject_trait<IMPID>::attribute_type attribute_type;
60 
61  static_assert(!std::is_same<OTYPE<IMPID>,field_type>::value,
62  "EXEPCTED A GROUP TYPE - GOT A FIELD!");
63  static_assert(!std::is_same<OTYPE<IMPID>,attribute_type>::value,
64  "EXPECTED A GROUP TYPE - GOT AN ATTRIBUTE!");
65 
66  if(!is_group(object_type(o)))
67  throw type_error(EXCEPTION_RECORD,
68  "Cannot set the nexus class for a non-group "
69  "object!");
70 
71  auto attr = o.attributes.template create<string>("NX_class",true);
72  attr.write(nxclass);
73  }
74 
75 
88  template<
89  typename GTYPE,
90  typename FTYPE,
91  typename ATYPE,
92  typename LTYPE
93  >
94  class set_class_visitor : public boost::static_visitor<void>
95  {
96  private:
97  pni::core::string _class;
98  public:
100  using result_type = void;
102  using group_type = GTYPE;
104  using field_type = FTYPE;
106  using attribute_type = ATYPE;
108  using link_type = LTYPE;
109 
110  //-----------------------------------------------------------------
116  set_class_visitor(const pni::core::string &s):_class(s) {}
117 
118  //-----------------------------------------------------------------
131  {
132  set_class(g,_class);
133  }
134 
135  //-----------------------------------------------------------------
145  {
146  using namespace pni::core;
147  throw type_error(EXCEPTION_RECORD,"Fields do not have a class!");
148  }
149 
150  //-----------------------------------------------------------------
160  {
161  using namespace pni::core;
162  throw type_error(EXCEPTION_RECORD,
163  "Attributes do not have a class!");
164  }
165 
166  //-----------------------------------------------------------------
175  {
176  using namespace pni::core;
177  throw type_error(EXCEPTION_RECORD,
178  "Links do not have a class!");
179  }
180  };
181 
202  template<
203  typename GTYPE,
204  typename FTYPE,
205  typename ATYPE,
206  typename LTYPE
207  >
209  const pni::core::string &c)
210  {
211  using visitor_type = set_class_visitor<GTYPE,FTYPE,ATYPE,LTYPE>;
212  boost::apply_visitor(visitor_type(c),o);
213  }
214 
215 //end of namespace
216 }
217 }
218 }
set class visitor
Definition: set_class.hpp:94
LTYPE link_type
NeXus link type.
Definition: set_class.hpp:108
result_type operator()(const attribute_type &) const
process attributes
Definition: set_class.hpp:159
nexus object traits
Definition: nxobject_traits.hpp:44
result_type operator()(const link_type &) const
process links
Definition: set_class.hpp:174
Definition: cbf_reader.hpp:41
void set_class(const OTYPE< IMPID > &o, const pni::core::string &nxclass)
set class for a group
Definition: set_class.hpp:54
pni::core::string _class
Nexus class.
Definition: set_class.hpp:97
result_type operator()(const group_type &g) const
process groups
Definition: set_class.hpp:130
ATYPE attribute_type
Nexus attribute type.
Definition: set_class.hpp:106
void result_type
result type
Definition: set_class.hpp:100
nximp_code
implementation codes
Definition: nximp_code.hpp:40
result_type operator()(const field_type &) const
process fields
Definition: set_class.hpp:144
set_class_visitor(const pni::core::string &s)
constructor
Definition: set_class.hpp:116
GTYPE group_type
Nexus group type.
Definition: set_class.hpp:102
boost::variant< GTYPE, FTYPE, ATYPE, LTYPE > nxobject
Definition: nxobject.hpp:44
FTYPE field_type
Nexus field type.
Definition: set_class.hpp:104
bool is_group(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &o) noexcept
test for group
Definition: is_group.hpp:136