libpniio
link.hpp
Go to the documentation of this file.
1 //
2 // (c) Copyright 2016 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: Mar 04, 2016
21 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
22 //
23 #pragma once
24 
25 #include <pni/core/types.hpp>
26 #include "nxpath/nxpath.hpp"
27 #include "nxlink_type.hpp"
28 #include "nxlink.hpp"
29 #include "nxobject_traits.hpp"
30 #include "../exceptions.hpp"
31 
32 namespace pni{
33 namespace io{
34 namespace nx{
35 
45  template<nximp_code IMPID> class nxlink
46  {
47  public:
54  private:
58  pni::core::string _name;
59  public:
61  nxlink():
62  _parent{},
63  _name{}
64  {}
65 
66  //-----------------------------------------------------------------
74  const pni::core::string &name):
75  _parent(parent),
76  _name(name)
77  {}
78 
79  //----------------------------------------------------------------
86  nxlink(const parent_type &parent,size_t index):
87  _parent(parent),
88  _name(link_name(parent,index))
89  {}
90 
91 
92  //-----------------------------------------------------------------
99  pni::core::string filename() const noexcept
100  {
101  return _parent.filename();
102  }
103 
104  //-----------------------------------------------------------------
110  pni::core::string name() const noexcept
111  {
112  return _name;
113  }
114 
115  //-----------------------------------------------------------------
124  object_type parent() const noexcept
125  {
126  return _parent;
127  }
128 
129  //-----------------------------------------------------------------
138  {
139  return link_target(_parent,_name);
140  }
141 
142  //-----------------------------------------------------------------
152  {
153  return pni::io::nx::link_status(_parent,_name);
154  }
155 
156  //-----------------------------------------------------------------
162  nxlink_type type() const
163  {
164  return pni::io::nx::link_type(_parent,_name);
165  }
166 
167  //-----------------------------------------------------------------
176  bool is_valid() const
177  {
179  return true;
180  else
181  return false;
182  }
183 
184  //-----------------------------------------------------------------
196  {
197  if(!is_valid())
198  throw pni::io::link_error(EXCEPTION_RECORD,
199  "Cannot resolve link!");
200 
201  return _parent.at(_name);
202  }
203 
204  };
205 
206 
207  //-------------------------------------------------------------------------
221  template<
222  typename CTYPE,
223  template<nximp_code> class GTYPE,
224  nximp_code IMPID
225  >
226  void get_links(const GTYPE<IMPID> &parent,CTYPE &container)
227  {
228  using value_type = typename CTYPE::value_type;
230  static_assert( std::is_same<link_type,value_type>::value,
231  "Container must store nxobject or nxlink objects!");
232 
233  for(size_t i=0;i<parent.size();++i)
234  container.push_back(nxlink<IMPID>(parent,link_name(parent,i)));
235  }
236 
237  //-------------------------------------------------------------------------
251  template<
252  typename CTYPE,
253  template<nximp_code> class GTYPE,
254  nximp_code IMPID
255  >
256  CTYPE get_links(const GTYPE<IMPID> &parent)
257  {
258  CTYPE container;
259  get_links(parent,container);
260 
261  return container;
262  }
263 
264  //-------------------------------------------------------------------------
279  template<
280  typename CTYPE,
281  template<nximp_code> class GTYPE,
282  nximp_code IMPID
283  >
284  void get_links_recursive(const GTYPE<IMPID> &parent,CTYPE &container)
285  {
286  for(size_t index=0;index<parent.size();++index)
287  {
288  nxlink<IMPID> l(parent,link_name(parent,index));
289  container.push_back(l);
290 
291  if(l.status() == nxlink_status::VALID)
292  {
293  auto object = parent[l.name()];
294  if(is_group(object))
295  get_links_recursive(as_group(object),container);
296  }
297  }
298  }
299 
300  //-------------------------------------------------------------------------
312  template<
313  typename CTYPE,
314  template<nximp_code> class GTYPE,
315  nximp_code IMPID
316  >
317  CTYPE get_links_recursive(const GTYPE<IMPID> &parent)
318  {
319  CTYPE container;
320  get_links_recursive(parent,container);
321  return container;
322  }
323 
324 
325 //end of namespace
326 }
327 }
328 }
void get_links_recursive(const GTYPE< IMPID > &parent, CTYPE &container)
get links recursively
Definition: link.hpp:284
nexus object traits
Definition: nxobject_traits.hpp:44
auto link_type(const GTYPE< IMPID > &parent, const pni::core::string &name) -> nxlink_type
return link type
Definition: nxlink.hpp:233
auto link_status(const GTYPE< IMPID > parent, const pni::core::string &lname) -> nxlink_status
Definition: nxlink.hpp:410
nxlink_type
Nexus link type.
Definition: nxlink_type.hpp:51
auto link_target(const GTYPE< IMPID > &parent, const pni::core::string &lname) -> nxpath
get link target
Definition: nxlink.hpp:293
Definition: cbf_reader.hpp:41
nxlink_status
Nexus link status.
Definition: nxlink_type.hpp:76
auto link_name(const GTYPE< IMPID > &parent, size_t index) -> pni::core::string
get link name
Definition: nxlink.hpp:264
void get_links(const GTYPE< IMPID > &parent, CTYPE &container)
get list of links
Definition: link.hpp:226
nximp_code
implementation codes
Definition: nximp_code.hpp:40
the link is resolvable
Nexus path class.
Definition: nxpath/nxpath.hpp:60
GTYPE as_group(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &o)
as group wrapper
Definition: as_group.hpp:167
bool is_group(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &o) noexcept
test for group
Definition: is_group.hpp:136