libpniio
get_child.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 1, 2013
20 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
21 //
22 #pragma once
23 
24 #include "../nximp_code.hpp"
25 #include "../nxobject.hpp"
26 #include "object_predicates.hpp"
27 #include "iterators.hpp"
28 #include "is_group.hpp"
29 #include "get_parent.hpp"
30 #include "is_valid.hpp"
31 
32 namespace pni{
33 namespace io{
34 namespace nx{
35 
58  template<typename OTYPE>
59  auto get_child(const OTYPE &o,const pni::core::string &n,
60  const pni::core::string &c="")
61  ->decltype(get_parent(o))
62  {
63  using namespace pni::core;
64  typedef decltype(get_parent(o)) object_type;
65 
66  object_type object(o); //convert the input always to nxobject
67  if(!is_group(object))
68  throw type_error(EXCEPTION_RECORD,"Function expects a group!");
69 
70  if(!n.empty()) //if object name is given
71  {
72  if(!c.empty()) //if object class is given
73  return get_object_by_name_and_class(object,n,c);
74  else //if no object class is given
75  return get_object_by_name(object,n);
76  }
77  else if(!c.empty()) //if only the class is provided by the user
78  return get_object_by_class(object,c);
79 
80  //if the user has neither provided a name or a class there is
81  //something wrong
82  throw value_error(EXCEPTION_RECORD,
83  "get_child() requires a name or a class for the search!");
84  }
85 
86  //------------------------------------------------------------------------
107  template<
108  template<nximp_code> class OTYPE,
109  nximp_code IMPID
110  >
111  auto get_child(const OTYPE<IMPID> &parent,size_t index)
112  ->decltype(get_parent(parent))
113  {
114  using group_type = typename nxobject_trait<IMPID>::group_type;
115 
116  static_assert(std::is_same<group_type,OTYPE<IMPID>>::value,
117  "Parent type must be an instance of nxgroup!");
118 
119  return parent.at(index);
120  }
121 
122  //------------------------------------------------------------------------
147  template<
148  typename GTYPE,
149  typename FTYPE,
150  typename ATYPE,
151  typename LTYPE
152  >
153  auto get_child(const nxobject<GTYPE,FTYPE,ATYPE,LTYPE> &parent,size_t index)
154  ->decltype(get_parent(parent))
155  {
156  using namespace pni::core;
157  if(!is_group(parent))
158  throw type_error(EXCEPTION_RECORD,"Parent must be a group");
159 
160  return get_child(as_group(parent),index);
161  }
162 
163 //end of namespace
164 }
165 }
166 }
OTYPE get_object_by_name(const OTYPE &parent, const pni::core::string &name)
search for object by name
Definition: object_predicates.hpp:54
nexus object traits
Definition: nxobject_traits.hpp:44
Definition: cbf_reader.hpp:41
nximp_code
implementation codes
Definition: nximp_code.hpp:40
auto get_child(const OTYPE &o, const pni::core::string &n, const pni::core::string &c="") -> decltype(get_parent(o))
get child by name and/or class
Definition: get_child.hpp:59
OTYPE get_object_by_class(const OTYPE &parent, pni::core::string c)
find object by class
Definition: object_predicates.hpp:121
nxobject_trait< IMPID >::object_type get_parent(const OTYPE< IMPID > &o)
return parent
Definition: get_parent.hpp:53
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
OTYPE get_object_by_name_and_class(const OTYPE &parent, const pni::core::string &name, const pni::core::string &c)
search for object by name and class
Definition: object_predicates.hpp:96
bool is_group(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &o) noexcept
test for group
Definition: is_group.hpp:136