libpniio
nexus_to_xml.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 19, 2013
20 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
21 //
22 #pragma once
23 
24 #include "node.hpp"
25 #include "../algorithms.hpp"
26 #include "../nxobject.hpp"
27 #include "field.hpp"
28 #include "group.hpp"
29 #include "attribute.hpp"
30 #include "dimensions.hpp"
31 #include "default.hpp"
32 
33 namespace pni{
34 namespace io{
35 namespace nx{
36 namespace xml{
37 
38 
39  using namespace pni::core;
40  using namespace pni::io::nx;
41 
55  template<
56  typename T,
57  typename OTYPE
58  >
59  void write_object_data(OTYPE &o,node &n)
60  {
61  std::vector<T> buffer(get_size(o));
62 
63  read(o,buffer);
64  io_node::data_to_xml(n,buffer);
65  }
66 
67  //-------------------------------------------------------------------------
79  template<typename OTYPE> void write_object_data(OTYPE &p,node &n)
80  {
81  switch(get_type(p))
82  {
83  case type_id_t::UINT8:
84  write_object_data<uint8>(p,n); break;
85  case type_id_t::INT8:
86  write_object_data<int8>(p,n); break;
87  case type_id_t::UINT16:
88  write_object_data<uint16>(p,n); break;
89  case type_id_t::INT16:
90  write_object_data<int16>(p,n); break;
91  case type_id_t::UINT32:
92  write_object_data<uint32>(p,n); break;
93  case type_id_t::INT32:
94  write_object_data<int32>(p,n); break;
95  case type_id_t::UINT64:
96  write_object_data<uint64>(p,n); break;
97  case type_id_t::INT64:
98  write_object_data<int64>(p,n); break;
99  case type_id_t::FLOAT32:
100  write_object_data<float32>(p,n); break;
101  case type_id_t::FLOAT64:
102  write_object_data<float64>(p,n); break;
103  case type_id_t::FLOAT128:
104  write_object_data<float128>(p,n); break;
105  case type_id_t::COMPLEX32:
106  write_object_data<complex32>(p,n); break;
107  case type_id_t::COMPLEX64:
108  write_object_data<complex64>(p,n); break;
109  case type_id_t::COMPLEX128:
110  write_object_data<complex128>(p,n); break;
111  case type_id_t::STRING:
112  write_object_data<string>(p,n); break;
113  default:
114  throw type_error(EXCEPTION_RECORD,
115  "Cannot handle Nexus data type!");
116  }
117  }
118 
119  //-------------------------------------------------------------------------
143  template<
144  typename OTYPE,
145  typename PTYPE
146  >
147  void append_attributes(const OTYPE &parent,node &p,PTYPE &write_predicate)
148  {
149  typedef decltype(get_parent(parent)) object_type;
150 
151  for(object_type a: parent.attributes)
152  {
153  string name = get_name(a);
154  if(name == "NX_class") continue;
155  if(name == "units") continue;
156  if(name == "long_name") continue;
157 
158  node attribute_node = attribute::object_to_xml(a);
159 
160  if(write_predicate(a))
161  write_object_data(a,attribute_node);
162 
163  p.add_child("attribute",attribute_node);
164  }
165  }
166 
167  //-------------------------------------------------------------------------
189  template<
190  typename GTYPE,
191  typename FTYPE,
192  typename ATYPE,
193  typename LTYPE,
194  typename PTYPE
195  >
197  PTYPE write_predicate)
198  {
199  node child;
200  string key;
201  if(is_field(p))
202  {
203  key = "field";
204  child = field::object_to_xml(p);
205  append_attributes(as_field(p),child,write_predicate);
206 
207  if(write_predicate(p))
208  write_object_data(p,child);
209  }
210  else if(is_group(p))
211  {
212  key = "group";
213 
214  //add the actual group to the parent node
215  child = group::object_to_xml(p);
216  append_attributes(as_group(p),child,write_predicate);
217 
218  //iterate over the children
219  for(auto o: as_group(p)) nexus_to_xml(o,child,write_predicate);
220  }
221 
222  //now everything is done and we have to
223  n.add_child(key,child);
224  }
225 
226  //-------------------------------------------------------------------------
243  template<
244  template<nximp_code> class OTYPE,
245  nximp_code IMPID,
246  typename PTYPE
247  >
248  void nexus_to_xml(OTYPE<IMPID> &o,node &n,PTYPE write_predicate)
249  {
250  typedef decltype(get_parent(o)) object_type;
251  object_type obj = o;
252  nexus_to_xml(obj,n,write_predicate);
253  }
254 
255  //-------------------------------------------------------------------------
273  template<
274  typename GTYPE,
275  typename FTYPE,
276  typename ATYPE,
277  typename LTYPE
278  >
280  {
282  }
283 
284  //-------------------------------------------------------------------------
285  template<
286  template<nximp_code> class OTYPE,
287  nximp_code IMPID
288  >
289  void nexus_to_xml(OTYPE<IMPID> &o,node &n)
290  {
291  typedef decltype(get_parent(o)) object_type;
292  object_type obj = o;
293 
294  nexus_to_xml(o,n);
295  }
296 
297 //end of namespace
298 }
299 }
300 }
301 }
bool is_field(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &o) noexcept
check if field
Definition: is_field.hpp:135
void write_object_data(OTYPE &o, node &n)
write data from Nexus to XML
Definition: nexus_to_xml.hpp:59
void append_attributes(const OTYPE &parent, node &p, PTYPE &write_predicate)
append attributes from a nexus type to XML
Definition: nexus_to_xml.hpp:147
static node object_to_xml(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &f)
create XML object from field
Definition: field.hpp:222
size_t get_size(const OTYPE< IMPID > &o)
get number of elements
Definition: get_size.hpp:61
pni::core::string get_name(const node &n)
get name of a node
Definition: node.cpp:138
Definition: cbf_reader.hpp:41
boost::property_tree::ptree node
alias for ptree
Definition: node.hpp:39
static void data_to_xml(node &io_node, const T &value)
write data to XML
Definition: io_node.hpp:147
static node object_to_xml(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &attr)
write attribute to XML
Definition: attribute.hpp:101
default data predicate
Definition: default.hpp:39
FTYPE as_field(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &o)
as field wrapper
Definition: as_field.hpp:166
nximp_code
implementation codes
Definition: nximp_code.hpp:40
void nexus_to_xml(nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &p, node &n, PTYPE write_predicate)
nexus to XML conversion
Definition: nexus_to_xml.hpp:196
Definition: as_attribute.hpp:29
nxobject_trait< IMPID >::object_type get_parent(const OTYPE< IMPID > &o)
return parent
Definition: get_parent.hpp:53
pni::core::type_id_t get_type(const OTYPE< IMPID > &o)
get type
Definition: get_type.hpp:55
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
static node object_to_xml(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &group)
write group to XML
Definition: group.hpp:134
void read(const OTYPE< IMPID > &o, ATYPE &a)
read data
Definition: read.hpp:63
bool is_group(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &o) noexcept
test for group
Definition: is_group.hpp:136