libpniio
create_group.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 3, 2013
20 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
21 //
22 #pragma once
23 
24 #include <pni/core/types.hpp>
25 #include "../nxobject.hpp"
26 #include "get_object.hpp"
27 #include "utils.hpp"
28 
29 namespace pni{
30 namespace io{
31 namespace nx{
32 
47  template<
48  typename GTYPE,
49  typename FTYPE,
50  typename ATYPE,
51  typename LTYPE
52  >
53  class create_group_visitor : public boost::static_visitor<
54  nxobject<GTYPE,FTYPE,ATYPE,LTYPE>
55  >
56  {
57  private:
59  pni::core::string _name;
61  pni::core::string _class;
62  public:
66  using group_type = GTYPE;
68  using field_type = FTYPE;
70  using attribute_type = ATYPE;
72  using link_type = LTYPE;
73 
82  create_group_visitor(const pni::core::string &n,
83  const pni::core::string &c):
84  _name(n),
85  _class(c)
86  {}
87 
88  //-----------------------------------------------------------------
104  {
105  using namespace pni::core;
106 
107  group_type group;
108  if(!_name.empty())
109  {
110  if(!_class.empty())
111  group = g.create_group(_name,_class);
112  else
113  group = g.create_group(_name);
114  }
115  else
116  //you have to at least provide a name
117  throw value_error(EXCEPTION_RECORD,
118  "No name provided for the new group!");
119 
120  return result_type(group);
121 
122  }
123 
124  //-----------------------------------------------------------------
137  {
138  using namespace pni::core;
139 
140  throw type_error(EXCEPTION_RECORD,
141  "Cannot create a group below a field!");
142  return result_type();
143  }
144 
145  //-----------------------------------------------------------------
158  {
159  using namespace pni::core;
160  throw type_error(EXCEPTION_RECORD,
161  "Cannot create a group below an attribute!");
162  return result_type();
163  }
164 
165  //-----------------------------------------------------------------
177  {
178  using namespace pni::core;
179  throw type_error(EXCEPTION_RECORD,
180  "Cannot create a group below a link!");
181  return result_type();
182  }
183 
184  };
185 
186  //-------------------------------------------------------------------------
218  template<
219  typename GTYPE,
220  typename FTYPE,
221  typename ATYPE,
222  typename LTYPE,
223  typename PATHT
224  >
225  nxobject<GTYPE,FTYPE,ATYPE,LTYPE>
227  {
229  using object_type = nxobject<GTYPE,FTYPE,ATYPE,LTYPE>;
230 
231  nxpath parent_path(get_path(path));
232  nxpath::element_type e = parent_path.back();
233  parent_path.pop_back();
234 
235  object_type parent;
236 
237  if(parent_path.size())
238  parent = get_object(o,parent_path);
239  else
240  parent = o;
241 
242  return boost::apply_visitor(visitor_type(e.first,e.second),parent);
243  }
244 
245 //end of namespace
246 }
247 }
248 }
ATYPE attribute_type
Nexus attribute type.
Definition: create_group.hpp:70
create group visitor
Definition: create_group.hpp:53
nxobject< GTYPE, FTYPE, ATYPE, LTYPE > create_group(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &o, const PATHT &path)
create_group wrapper
Definition: create_group.hpp:226
size_t size() const
return number of group entries
Definition: nxpath.cpp:138
result_type operator()(const group_type &g) const
process group instances
Definition: create_group.hpp:103
void pop_back()
remove first element from path
Definition: nxpath.cpp:126
LTYPE link_type
link type
Definition: create_group.hpp:72
result_type operator()(const field_type &) const
process field instances
Definition: create_group.hpp:136
result_type operator()(const attribute_type &) const
process attribute instances
Definition: create_group.hpp:157
Definition: cbf_reader.hpp:41
create_group_visitor(const pni::core::string &n, const pni::core::string &c)
constructor
Definition: create_group.hpp:82
result_type operator()(const link_type &) const
process link instances
Definition: create_group.hpp:176
nxobject< GTYPE, FTYPE, ATYPE, LTYPE > result_type
result type
Definition: create_group.hpp:64
std::pair< pni::core::string, pni::core::string > element_type
object element (groupname:class)
Definition: nxpath/nxpath.hpp:64
GTYPE group_type
Nexus group type.
Definition: create_group.hpp:66
pni::core::string _name
the name of the group
Definition: create_group.hpp:59
auto get_object(const OTYPE &o, const PATHT &path) -> decltype(get_parent(o))
retrieve an object from path
Definition: get_object.hpp:62
FTYPE field_type
Nexus field type.
Definition: create_group.hpp:68
pni::core::string _class
the Nexus class of the group
Definition: create_group.hpp:61
Nexus path class.
Definition: nxpath/nxpath.hpp:60
element_type back() const
get first element
Definition: nxpath.cpp:132
pni::core::string get_path(const OTYPE< IMPID > &o)
get object path
Definition: get_path.hpp:59
boost::variant< GTYPE, FTYPE, ATYPE, LTYPE > nxobject
Definition: nxobject.hpp:44