libpniio
get_attributes.hpp
Go to the documentation of this file.
1 //
2 // (c) Copyright 2015 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 31, 2015
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 
30 namespace pni{
31 namespace io{
32 namespace nx{
33 
55  template<
56  typename CTYPE,
57  template<nximp_code> class PTYPE,
58  nximp_code IMPID
59  >
60  CTYPE get_attributes(const PTYPE<IMPID> &parent)
61  {
62  typedef typename nxobject_trait<IMPID>::object_type object_type;
63  typedef typename nxobject_trait<IMPID>::attribute_type attribute_type;
64  typedef typename CTYPE::value_type value_type;
65 
66  static_assert(std::is_same<value_type,object_type>::value ||
67  std::is_same<value_type,attribute_type>::value,
68  "The containers value_type must be either an attribute"
69  " or an object type!");
70 
71  CTYPE attributes;
72 
73  std::copy(parent.attributes.begin(),parent.attributes.end(),
74  std::back_inserter(attributes));
75 
76  return attributes;
77  }
78 
79 
80  //------------------------------------------------------------------------
93  template<
94  typename CTYPE,
95  typename GTYPE,
96  typename FTYPE,
97  typename ATYPE,
98  typename LTYPE
99  >
100  class get_attributes_visitor : public boost::static_visitor<CTYPE>
101  {
102  public:
104  using result_type = CTYPE;
106  using group_type = GTYPE;
108  using field_type = FTYPE;
110  using attribute_type = ATYPE;
112  using link_type = LTYPE;
113 
114  //---------------------------------------------------------------------
126  {
127  return get_attributes<result_type>(g);
128  }
129 
130  //---------------------------------------------------------------------
142  {
143  return get_attributes<result_type>(f);
144  }
145 
146  //---------------------------------------------------------------------
159  {
160  using namespace pni::core;
161  throw type_error(EXCEPTION_RECORD,
162  "Attributes cannot have attributes by themself!");
163  return result_type();
164  }
165 
166  //----------------------------------------------------------------
178  {
179  using namespace pni::core;
180  throw type_error(EXCEPTION_RECORD,
181  "Instances of link do not have attributes!");
182  return result_type();
183  }
184  };
185 
205  template<
206  typename CTYPE,
207  typename GTYPE,
208  typename FTYPE,
209  typename ATYPE,
210  typename LTYPE
211  >
213  {
215  return boost::apply_visitor(visitor_type(),parent);
216  }
217 
218 //end of namespace
219 }
220 }
221 }
result_type operator()(const group_type &g) const
process groups
Definition: get_attributes.hpp:125
GTYPE group_type
Nexus group type.
Definition: get_attributes.hpp:106
nexus object traits
Definition: nxobject_traits.hpp:44
CTYPE get_attributes(const PTYPE< IMPID > &parent)
get attributes from an object
Definition: get_attributes.hpp:60
result_type operator()(const attribute_type &) const
process attributes
Definition: get_attributes.hpp:158
result_type operator()(const field_type &f) const
process fields
Definition: get_attributes.hpp:141
result_type operator()(const link_type &) const
process link
Definition: get_attributes.hpp:177
Definition: cbf_reader.hpp:41
ATYPE attribute_type
Nexus attribute type.
Definition: get_attributes.hpp:110
LTYPE link_type
NeXus link type.
Definition: get_attributes.hpp:112
CTYPE result_type
result type
Definition: get_attributes.hpp:104
nximp_code
implementation codes
Definition: nximp_code.hpp:40
get attribute visitor
Definition: get_attributes.hpp:100
FTYPE field_type
Nexus field type.
Definition: get_attributes.hpp:108
boost::variant< GTYPE, FTYPE, ATYPE, LTYPE > nxobject
Definition: nxobject.hpp:44