libpniio
get_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 2, 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 
30 namespace pni{
31 namespace io{
32 namespace nx{
33 
54  template<
55  template<nximp_code> class PTYPE,
56  nximp_code IMPID
57  >
58  typename nxobject_trait<IMPID>::object_type
59  get_attribute(const PTYPE<IMPID> &parent,const pni::core::string &name)
60  {
61  using field_type = typename nxobject_trait<IMPID>::field_type;
62  using group_type = typename nxobject_trait<IMPID>::group_type;
63 
64  static_assert(std::is_same<field_type,PTYPE<IMPID>>::value ||
65  std::is_same<group_type,PTYPE<IMPID>>::value,
66  "Parent must be a field or a group!");
67 
68  return parent.attributes[name];
69  }
70 
71 
72  //------------------------------------------------------------------------
85  template<
86  typename GTYPE,
87  typename FTYPE,
88  typename ATYPE,
89  typename LTYPE
90  >
91  class get_attribute_visitor : public boost::static_visitor<
92  nxobject<GTYPE,FTYPE,ATYPE,LTYPE>
93  >
94  {
95  private:
96  pni::core::string _attribute;
97  public:
99  using result_type = ATYPE;
101  using group_type = GTYPE;
103  using field_type = FTYPE;
105  using attribute_type = ATYPE;
107  using link_type = LTYPE;
108 
109  //-----------------------------------------------------------------
116  get_attribute_visitor(const pni::core::string &attr):
117  _attribute(attr)
118  {}
119 
120  //---------------------------------------------------------------------
134  {
135  return get_attribute(g,_attribute);
136  }
137 
138  //---------------------------------------------------------------------
152  {
153  return get_attribute(f,_attribute);
154  }
155 
156  //---------------------------------------------------------------------
171  {
172  using namespace pni::core;
173  throw type_error(EXCEPTION_RECORD,
174  "Attributes cannot have attributes by themself!");
175  return result_type();
176  }
177 
178  //-----------------------------------------------------------------
190  {
191  using namespace pni::core;
192  throw type_error(EXCEPTION_RECORD,
193  "Instances of link do not have attribtues!");
194  return result_type();
195  }
196  };
197 
219  template<
220  typename GTYPE,
221  typename FTYPE,
222  typename ATYPE,
223  typename LTYPE
224  >
225  nxobject<GTYPE,FTYPE,ATYPE,LTYPE>
227  const pni::core::string &name)
228  {
230  return boost::apply_visitor(visitor_type(name),parent);
231  }
232 
233 //end of namespace
234 }
235 }
236 }
result_type operator()(const attribute_type &) const
process attributes
Definition: get_attribute.hpp:170
GTYPE group_type
Nexus group type.
Definition: get_attribute.hpp:101
nexus object traits
Definition: nxobject_traits.hpp:44
get_attribute_visitor(const pni::core::string &attr)
constructor
Definition: get_attribute.hpp:116
result_type operator()(const group_type &g) const
process groups
Definition: get_attribute.hpp:133
Definition: cbf_reader.hpp:41
result_type operator()(const field_type &f) const
process fields
Definition: get_attribute.hpp:151
get attribute visitor
Definition: get_attribute.hpp:91
nxobject_trait< IMPID >::object_type get_attribute(const PTYPE< IMPID > &parent, const pni::core::string &name)
get attribute by name
Definition: get_attribute.hpp:59
ATYPE result_type
result type
Definition: get_attribute.hpp:99
ATYPE attribute_type
Nexus attribute type.
Definition: get_attribute.hpp:105
nximp_code
implementation codes
Definition: nximp_code.hpp:40
LTYPE link_type
Nexus link type.
Definition: get_attribute.hpp:107
FTYPE field_type
Nexus field type.
Definition: get_attribute.hpp:103
result_type operator()(const link_type &) const
process link instances
Definition: get_attribute.hpp:189
pni::core::string _attribute
name of the attribute to retrieve
Definition: get_attribute.hpp:96
boost::variant< GTYPE, FTYPE, ATYPE, LTYPE > nxobject
Definition: nxobject.hpp:44