libpniio
flat_group.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 //
20 // Created on: Sep 10, 2014
21 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
22 //
23 #pragma once
24 
25 #include <vector>
26 #include "algorithms/is_valid.hpp"
27 #include "algorithms/is_group.hpp"
28 #include "algorithms/as_group.hpp"
29 #include "nxobject_traits.hpp"
30 
31 namespace pni{
32 namespace io{
33 namespace nx{
34 
46 
55 
69  template<typename GTYPE> class flat_group
72  {
73  public:
75  typedef GTYPE group_type;
79  typedef std::vector<value_type> container_type;
81  typedef typename container_type::iterator iterator;
83  typedef typename container_type::const_iterator const_iterator;
84  private:
86  container_type _container;
87 
101  void append_children(const group_type &parent)
102  {
103  for(auto c: parent) //will throw invalid_object_error or
104  //object_error
105  {
106  //add the child to the container
107  _container.push_back(c);
108 
109  //if the child is a group recusively call append_children
110  if(is_group(c)) append_children(as_group(c));
111  }
112  }
113  public:
114  //====================public types==================================
125  explicit flat_group(const group_type &parent):
126  _container()
127  {
128  if(!is_valid(parent))
129  throw invalid_object_error(EXCEPTION_RECORD,
130  "Cannot create an flat group from an invalid object!");
131  append_children(parent);
132  }
133 
134  //------------------------------------------------------------------
143  value_type operator[](size_t index) const noexcept
144  {
145  return _container[index];
146  }
147 
148  //------------------------------------------------------------------
158  value_type &operator[](size_t index) noexcept
159  {
160  return _container[index];
161  }
162 
163  //------------------------------------------------------------------
172  size_t size() const noexcept
173  {
174  return _container.size();
175  }
176 
177  //------------------------------------------------------------------
185  iterator begin() noexcept
186  {
187  return _container.begin();
188  }
189 
190  //------------------------------------------------------------------
198  iterator end() noexcept
199  {
200  return _container.end();
201  }
202 
203  //------------------------------------------------------------------
211  const_iterator begin() const noexcept
212  {
213  return _container.begin();
214  }
215 
216  //------------------------------------------------------------------
224  const_iterator end() const noexcept
225  {
226  return _container.end();
227  }
228  };
229 
230  //-------------------------------------------------------------------------
247  template<
248  template<nximp_code> class GTYPE,
249  nximp_code IMPID
250  >
251  flat_group<GTYPE<IMPID>> make_flat(const GTYPE<IMPID> &group)
252  {
253  return flat_group<GTYPE<IMPID>>(group);
254  }
255 
256  //-------------------------------------------------------------------------
275  template<
276  typename GTYPE,
277  typename FTYPE,
278  typename ATYPE,
279  typename LTYPE
280  >
281 
283  {
284  return flat_group<GTYPE>(as_group(object));
285  }
286 
287 
288 //end of namespace
289 }
290 }
291 }
container_type::const_iterator const_iterator
the constant interator type
Definition: flat_group.hpp:83
container_type::iterator iterator
the iterator type
Definition: flat_group.hpp:81
flat_group(const group_type &parent)
constructor
Definition: flat_group.hpp:125
value_type & operator[](size_t index) noexcept
get element reference
Definition: flat_group.hpp:158
nexus object traits
Definition: nxobject_traits.hpp:44
unexpected invalid object
Definition: exceptions.hpp:130
const_iterator begin() const noexcept
get iterator to first
Definition: flat_group.hpp:211
nxobject_trait< nximp_code_map< group_type >::icode >::object_type value_type
nxobject is the value type of the flat_group container
Definition: flat_group.hpp:77
iterator end() noexcept
get iterator to last+1
Definition: flat_group.hpp:198
Definition: cbf_reader.hpp:41
void append_children(const group_type &parent)
append children to container
Definition: flat_group.hpp:101
value_type operator[](size_t index) const noexcept
get element
Definition: flat_group.hpp:143
bool is_valid(const OTYPE &o) noexcept
visitor checking object validity
Definition: is_valid.hpp:53
const_iterator end() const noexcept
get iterator to last+1
Definition: flat_group.hpp:224
iterator begin() noexcept
get iterator to first
Definition: flat_group.hpp:185
flat group adapter
Definition: flat_group.hpp:71
nximp_code
implementation codes
Definition: nximp_code.hpp:40
std::vector< value_type > container_type
the internal container type
Definition: flat_group.hpp:79
GTYPE group_type
the group type
Definition: flat_group.hpp:75
size_t size() const noexcept
get size
Definition: flat_group.hpp:172
GTYPE as_group(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &o)
as group wrapper
Definition: as_group.hpp:167
boost::variant< GTYPE, FTYPE, ATYPE, LTYPE > nxobject
Definition: nxobject.hpp:44
container_type _container
internal container holding the objects
Definition: flat_group.hpp:86
flat_group< GTYPE< IMPID > > make_flat(const GTYPE< IMPID > &group)
create a flat group
Definition: flat_group.hpp:251
bool is_group(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &o) noexcept
test for group
Definition: is_group.hpp:136