libpniio
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: Nov 26, 2014
21 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
22 //
23 #pragma once
24 
25 #include <pni/core/types.hpp>
26 
27 #include "../nxobject.hpp"
28 #include "../algorithms/create_field.hpp"
29 #include "../algorithms/create_group.hpp"
30 #include "../algorithms/get_name.hpp"
31 #include "../algorithms/get_class.hpp"
32 #include "../algorithms/set_class.hpp"
33 #include "node.hpp"
34 #include "data_node.hpp"
35 
36 
37 namespace pni{
38 namespace io{
39 namespace nx{
40 namespace xml{
41 
45  struct group
46  {
47 
69  template<
70  typename GTYPE,
71  typename FTYPE,
72  typename ATYPE,
73  typename LTYPE
74  >
77  const node &group_node)
78  {
79  using namespace pni::core;
80  //typedef GTYPE group_type;
81  //typedef typename GTYPE::value_type object_type;
82 
83  //check if the group tag provides a name attribute
84  if(!has_attribute(group_node,"name"))
85  throw value_error(EXCEPTION_RECORD,
86  "XML group does not provide a name!");
87 
88  //fetch the name for the group
89  node name_attr = get_attribute(group_node,"name");
90  string name = data_node::read(name_attr);
91 
92  if(name == "/" && get_name(parent) == "/")
93  return parent;
94 
95  //create the group
96  auto group = create_group(parent,name);
97 
98  //if the tag has a type attribute add its value as an NX_class
99  //attribute
100  if(has_attribute(group_node,"type"))
101  {
102  node class_attr = get_attribute(group_node,"type");
103  string gclass = data_node::read(class_attr);
104 
105  set_class(group,gclass);
106  }
107 
108  return group;
109  }
110 
111  //-----------------------------------------------------------------
128  template<
129  typename GTYPE,
130  typename FTYPE,
131  typename ATYPE,
132  typename LTYPE
133  >
135  {
136  node group_node;
137 
138  //write name and type attributes
139  group_node.put("<xmlattr>.name",get_name(group));
140 
141  pni::core::string gclass = get_class(group);
142  if(!gclass.empty())
143  group_node.put("<xmlattr>.type",gclass);
144 
145  return group_node;
146  }
147  };
148 
149 //end of namespace
150 }
151 }
152 }
153 }
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
bool has_attribute(const node &parent, const pni::core::string &name)
check for attribute existence
read and write groups
Definition: group.hpp:45
pni::core::string get_name(const node &n)
get name of a node
Definition: node.cpp:138
static nxobject< GTYPE, FTYPE, ATYPE, LTYPE > object_from_xml(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &parent, const node &group_node)
create group from XML
Definition: group.hpp:76
Definition: cbf_reader.hpp:41
void set_class(const OTYPE< IMPID > &o, const pni::core::string &nxclass)
set class for a group
Definition: set_class.hpp:54
boost::property_tree::ptree node
alias for ptree
Definition: node.hpp:39
static pni::core::string read(const node &n)
read data from node
Definition: data_node.cpp:34
pni::core::string get_class(const OTYPE< IMPID > &group)
get the class of a group
Definition: get_class.hpp:56
node get_attribute(const node &parent, const pni::core::string &name)
get attribute node
boost::variant< GTYPE, FTYPE, ATYPE, LTYPE > nxobject
Definition: nxobject.hpp:44
static node object_to_xml(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &group)
write group to XML
Definition: group.hpp:134