libpniio
xml_to_nexus.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 8, 2013
20 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
21 //
22 #pragma once
23 
24 #include <sstream>
25 #include <pni/core/types.hpp>
26 #include <pni/core/error.hpp>
27 #include "../algorithms/write.hpp"
28 #include "../algorithms/get_type.hpp"
29 #include "../nxobject.hpp"
30 #include "../nxobject_traits.hpp"
31 #include "../../exceptions.hpp"
32 #include "../../parsers.hpp"
33 
34 #include "node.hpp"
35 #include "data_node.hpp"
36 #include "group.hpp"
37 #include "attribute.hpp"
38 #include "field.hpp"
39 #include "default.hpp"
40 #include "link.hpp"
41 
42 
43 namespace pni{
44 namespace io{
45 namespace nx{
46 namespace xml{
47 
48  //-------------------------------------------------------------------------
73  template<
74  typename T,
75  typename OTYPE
76  >
77  void write_node_data(const OTYPE &object,const node &n)
78  {
79  typedef std::vector<T> buffer_type;
80 
81  auto buffer = io_node::data_from_xml<buffer_type>(n);
82 
83  write(object,buffer);
84 
85  }
86 
87  //-------------------------------------------------------------------------
111  template<typename OTYPE>
112  void write_node_data(const OTYPE &object,const node &n)
113  {
114  using namespace pni::core;
115 
116  switch(get_type(object))
117  {
118  case type_id_t::UINT8:
119  write_node_data<uint8>(object,n); break;
120  case type_id_t::INT8:
121  write_node_data<int8>(object,n); break;
122  case type_id_t::UINT16:
123  write_node_data<uint16>(object,n); break;
124  case type_id_t::INT16:
125  write_node_data<int16>(object,n); break;
126  case type_id_t::UINT32:
127  write_node_data<uint32>(object,n); break;
128  case type_id_t::INT32:
129  write_node_data<int32>(object,n); break;
130  case type_id_t::UINT64:
131  write_node_data<uint64>(object,n); break;
132  case type_id_t::INT64:
133  write_node_data<int64>(object,n); break;
134  case type_id_t::FLOAT32:
135  write_node_data<float32>(object,n); break;
136  case type_id_t::FLOAT64:
137  write_node_data<float64>(object,n); break;
138  case type_id_t::FLOAT128:
139  write_node_data<float128>(object,n); break;
140  case type_id_t::COMPLEX32:
141  write_node_data<complex32>(object,n); break;
142  case type_id_t::COMPLEX64:
143  write_node_data<complex64>(object,n); break;
144  case type_id_t::COMPLEX128:
145  write_node_data<complex128>(object,n); break;
146  case type_id_t::BOOL:
147  write_node_data<bool_t>(object,n); break;
148  case type_id_t::STRING:
149  //need some special handling for string data - only support
150  //scalar strings
151  write(object,io_node::data_from_xml<string>(n));
152  //write_node_data<string>(object,n);
153  break;
154  default:
155  throw type_error(EXCEPTION_RECORD,
156  "Unrecognized data type!");
157  }
158  }
159 
160 
161  //--------------------------------------------------------------------------
185  template<
186  typename GTYPE,
187  typename FTYPE,
188  typename ATYPE,
189  typename LTYPE,
190  typename PTYPE
191  >
193  PTYPE write_data)
194  {
195  //typedef nxobject<GTYPE,FTYPE,ATYPE> object_type;
196  for(auto child: t)
197  {
198  if(child.first == "group")
199  {
200  auto g = group::object_from_xml(parent,child.second);
201  //recursive call of create_objects
202  xml_to_nexus(child.second,g,write_data);
203 
204  }
205  else if(child.first == "field")
206  {
207  auto f = field::object_from_xml(parent,child.second);
208 
209  if(write_data(f) && !data_node::read(child.second).empty())
210  write_node_data(f,child.second);
211 
212  //need to call here xml_to_nexus recursively to add
213  //custom attributes
214  xml_to_nexus(child.second,f,write_data);
215  }
216  else if(child.first == "link")
217  {
218  link::object_from_xml(parent,child.second);
219  }
220  else if(child.first == "attribute")
221  {
222  //attach attributes to the parent object
223  auto a = attribute::object_from_xml(parent,child.second);
224 
225  if(write_data(a) && !data_node::read(child.second).empty())
226  write_node_data(a,child.second);
227  }
228  }
229  }
230 
231  //-------------------------------------------------------------------------
253  template<
254  typename GTYPE,
255  typename FTYPE,
256  typename ATYPE,
257  typename LTYPE
258  >
260  {
261  xml_to_nexus(t,parent,write_no_data());
262  }
263 
264  //-------------------------------------------------------------------------
288  template<
289  template<pni::io::nx::nximp_code> class OTYPE,
291  typename PTYPE
292  >
293  void xml_to_nexus(const node &t,const OTYPE<IMPID> &group,PTYPE write_data)
294  {
295  typedef typename nxobject_trait<IMPID>::object_type object_type;
296  object_type o = group;
297  xml_to_nexus(t,o,write_data);
298  }
299 
300  //-------------------------------------------------------------------------
324  template<
325  template<pni::io::nx::nximp_code> class OTYPE,
327  >
328  void xml_to_nexus(const node &t,const OTYPE<IMPID> &group)
329  {
330  typedef typename nxobject_trait<IMPID>::object_type object_type;
331  object_type o=group;
333  }
334 
335 //end of namespace
336 }
337 }
338 }
339 }
nexus object traits
Definition: nxobject_traits.hpp:44
read and write groups
Definition: group.hpp:45
void xml_to_nexus(const node &t, const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &parent, PTYPE write_data)
create objects from XML
Definition: xml_to_nexus.hpp:192
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
boost::property_tree::ptree node
alias for ptree
Definition: node.hpp:39
static nxobject< GTYPE, FTYPE, ATYPE, LTYPE > object_from_xml(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &parent, const node &attr_node)
create attribute from XML description
Definition: attribute.hpp:75
default data predicate
Definition: default.hpp:39
nximp_code
implementation codes
Definition: nximp_code.hpp:40
static pni::core::string read(const node &n)
read data from node
Definition: data_node.cpp:34
static nxobject< GTYPE, FTYPE, ATYPE, LTYPE > object_from_xml(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &parent, const node &field_node)
create field from XML
Definition: field.hpp:121
void write_node_data(const OTYPE &object, const node &n)
write data from buffer
Definition: xml_to_nexus.hpp:77
pni::core::type_id_t get_type(const OTYPE< IMPID > &o)
get type
Definition: get_type.hpp:55
boost::variant< GTYPE, FTYPE, ATYPE, LTYPE > nxobject
Definition: nxobject.hpp:44
void write(const OTYPE< IMPID > &o, const ATYPE &a)
write data to an attribute or field
Definition: write.hpp:61