libpniio
get_size.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 5, 2013
20 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
21 //
22 #pragma once
23 
24 #include <pni/core/types.hpp>
25 #include "../nximp_code.hpp"
26 #include "../nxobject.hpp"
27 #include "../nxobject_traits.hpp"
28 
29 namespace pni{
30 namespace io{
31 namespace nx{
32 
57  template<
58  template<nximp_code> class OTYPE,
59  nximp_code IMPID
60  >
61  size_t get_size(const OTYPE<IMPID> &o)
62  {
63  using field_type = typename nxobject_trait<IMPID>::field_type;
64  using attr_type = typename nxobject_trait<IMPID>::attribute_type;
65  using group_type = typename nxobject_trait<IMPID>::group_type;
66 
67  static_assert(std::is_same<field_type,OTYPE<IMPID>>::value ||
68  std::is_same<attr_type,OTYPE<IMPID>>::value ||
69  std::is_same<group_type,OTYPE<IMPID>>::value,
70  "Argument must be a group, field, or "
71  "attribute instance!");
72 
73  return o.size();
74  }
75 
76  //-------------------------------------------------------------------------
91  template<
92  typename GTYPE,
93  typename FTYPE,
94  typename ATYPE,
95  typename LTYPE
96  >
97  class get_size_visitor : public boost::static_visitor<size_t>
98  {
99  public:
101  using result_type = size_t;
103  using group_type = GTYPE;
105  using field_type = FTYPE;
107  using attribute_type = ATYPE;
109  using link_type = LTYPE;
110 
111  //-----------------------------------------------------------------
124  {
125  return g.size();
126  }
127 
128  //-----------------------------------------------------------------
141  {
142  return f.size();
143  }
144 
145  //-----------------------------------------------------------------
158  {
159  return a.size();
160  }
161 
162  //-----------------------------------------------------------------
171  {
172  using namespace pni::core;
173  throw type_error(EXCEPTION_RECORD,
174  "Links do not have a size!");
175  return 0;
176  }
177 
178  };
179 
189 
195  template<
207  typename GTYPE,
208  typename FTYPE,
209  typename ATYPE,
210  typename LTYPE
211  >
213  {
214  using visitor_type = get_size_visitor<GTYPE,FTYPE,ATYPE,LTYPE>;
215 
216  return boost::apply_visitor(visitor_type(),o);
217  }
218 
219 //end of namespace
220 }
221 }
222 }
get size visitor
Definition: get_size.hpp:97
nexus object traits
Definition: nxobject_traits.hpp:44
result_type operator()(const field_type &f) const
process field instances
Definition: get_size.hpp:140
FTYPE field_type
Nexus field type.
Definition: get_size.hpp:105
size_t get_size(const OTYPE< IMPID > &o)
get number of elements
Definition: get_size.hpp:61
result_type operator()(const group_type &g) const
process group instances
Definition: get_size.hpp:123
Definition: cbf_reader.hpp:41
result_type operator()(const link_type &) const
process nxlink instances
Definition: get_size.hpp:170
size_t result_type
result type
Definition: get_size.hpp:101
nximp_code
implementation codes
Definition: nximp_code.hpp:40
GTYPE group_type
Nexus group type.
Definition: get_size.hpp:103
ATYPE attribute_type
Nexus attribute type.
Definition: get_size.hpp:107
LTYPE link_type
NeXus link type.
Definition: get_size.hpp:109
boost::variant< GTYPE, FTYPE, ATYPE, LTYPE > nxobject
Definition: nxobject.hpp:44
result_type operator()(const attribute_type &a) const
process attribute instances
Definition: get_size.hpp:157