libpniio
object_predicates.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 // Created on: Sep 12, 2014
20 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
21 //
22 #pragma once
23 
24 #include "../nxobject.hpp"
25 #include "iterators.hpp"
26 #include "is_field.hpp"
27 #include "get_name.hpp"
28 #include "is_class.hpp"
29 #include "as_group.hpp"
30 
31 namespace pni{
32 namespace io{
33 namespace nx{
34 
53  template<typename OTYPE>
54  OTYPE get_object_by_name(const OTYPE &parent,const pni::core::string &name)
55  {
56  using namespace pni::core;
57 
58  auto group = as_group(parent);
59 
60  if(!group.has_child(name))
61  throw key_error(EXCEPTION_RECORD,
62  "Group has no child ["+name+"]!");
63 
64  return group[name];
65  }
66 
67  //-------------------------------------------------------------------------
95  template<typename OTYPE>
96  OTYPE get_object_by_name_and_class(const OTYPE &parent,
97  const pni::core::string &name,
98  const pni::core::string &c)
99  {
100  using namespace pni::core;
101  auto result = get_object_by_name(parent,name);
102 
103  if(is_field(result))
104  throw type_error(EXCEPTION_RECORD,
105  "Object is a field and thus does not have a class!");
106 
107  if(!is_class(result,c))
108  throw key_error(EXCEPTION_RECORD,
109  "No object ["+name+":"+c+"] found!");
110 
111  return result;
112  }
113 
114  //-------------------------------------------------------------------------
120  template<typename OTYPE>
121  OTYPE get_object_by_class(const OTYPE &parent,pni::core::string c)
122  {
123  using namespace pni::core;
124 
125  auto iter = std::find_if(begin(parent),end(parent),
126  [&c](const OTYPE &o) {
127  if(is_field(o)) return false;
128 
129  return is_class(o,c);
130  });
131 
132  if(iter == end(parent))
133  throw key_error(EXCEPTION_RECORD,
134  "No group of class ["+c+"] found!");
135 
136  return *iter;
137  }
138 
139 
140 //end of namespace
141 }
142 }
143 }
bool is_field(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &o) noexcept
check if field
Definition: is_field.hpp:135
OTYPE get_object_by_name(const OTYPE &parent, const pni::core::string &name)
search for object by name
Definition: object_predicates.hpp:54
Definition: cbf_reader.hpp:41
auto begin(const OTYPE< IMPID > &group) -> decltype(group.begin())
get iterator to first element
Definition: iterators.hpp:46
auto end(const OTYPE< IMPID > &group) -> decltype(group.end())
get iterator to last element
Definition: iterators.hpp:69
OTYPE get_object_by_class(const OTYPE &parent, pni::core::string c)
find object by class
Definition: object_predicates.hpp:121
GTYPE as_group(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &o)
as group wrapper
Definition: as_group.hpp:167
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_class(const OTYPE< IMPID > &object, const pni::core::string &type)
checks group type
Definition: is_class.hpp:58