libpniio
nxattribute.hpp
Go to the documentation of this file.
1 //
2 // Declaration of Nexus object template.
3 //
4 // (c) Copyright 2011 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
5 //
6 // This file is part of libpniio.
7 //
8 // libpniio is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // libpniio is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with libpniio. If not, see <http://www.gnu.org/licenses/>.
20 // ===========================================================================
21 //
22 // Created on: Feb 11, 2012
23 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
24 //
25 
26 #pragma once
27 
28 #include <pni/core/types.hpp>
29 #include <pni/core/error.hpp>
30 #include <pni/core/arrays/mdarray.hpp>
31 #include <pni/core/arrays/array_view.hpp>
32 #include <pni/core/arrays/slice.hpp>
33 #include <pni/core/type_erasures/array.hpp>
34 
35 #include "nximp_map.hpp"
36 #include "nxobject_traits.hpp"
37 #include "nxobject_type.hpp"
39 #include "../exceptions.hpp"
40 
41 
42 namespace pni{
43 namespace io{
44 namespace nx{
45 
54  template<nximp_code IMPID>
56  {
57  public:
64  private:
66  implementation_type _imp;
67 
68  //-----------------------------------------------------------------
86  template<typename ATYPE>
87  void _write_array(const ATYPE &a) const
88  {
89  using namespace pni::core;
90  check_allocation_state(a,EXCEPTION_RECORD);
91  check_equal_size(a,*this,EXCEPTION_RECORD);
92 
93  _imp.write(pni::core::type_id(a),a.data());
94  }
95 
96  //-----------------------------------------------------------------
114  template<typename ATYPE>
115  void _read_array(ATYPE &a) const
116  {
117  using namespace pni::core;
118  check_allocation_state(a,EXCEPTION_RECORD);
119  check_equal_size(a,*this,EXCEPTION_RECORD);
120 
121 
122  _imp.read(pni::core::type_id(a),a.data());
123  }
124 
125  public:
126  //==========constructors and destructors============================
130  explicit nxattribute() noexcept:_imp(){}
131 
132  //------------------------------------------------------------------
143  explicit nxattribute(implementation_type &&i):_imp(std::move(i))
144  { }
145 
146  //----------------------------------------------------------------
159  _imp()
160  {
161  *this = o;
162  }
163 
164  //===================assignment operators===========================
177  attribute_type &operator=(const typename
179  {
180  *this = as_attribute(o);
181  return *this;
182  }
183 
184  //====================IO methods====================================
205  template<
206  typename STORAGE,
207  typename IMAP,
208  typename IPA
209  >
210  void write(const pni::core::mdarray<STORAGE,IMAP,IPA> &o) const
211  {
212  _write_array(o);
213  }
214 
215  //----------------------------------------------------------------
234  template<typename ATYPE>
235  void write(const pni::core::array_view<ATYPE> &v) const
236  {
237  if(v.is_contiguous())
238  _write_array(v);
239  else
240  {
241  typedef typename ATYPE::value_type value_type;
242  typedef pni::core::dynamic_array<value_type> array_type;
243  auto buffer = array_type::create(v.template shape<pni::core::shape_t>());
244  std::copy(v.begin(),v.end(),buffer.begin());
245  write(buffer);
246  }
247  }
248 
249  //----------------------------------------------------------------
268  template<typename T>
269  void write(size_t n,const T *data) const
270  {
271  using namespace pni::core;
272  static_assert(!std::is_pointer<T>::value,"no const pointer");
273 
274  if(n!=size())
275  throw size_mismatch_error(EXCEPTION_RECORD,
276  "Memory and attribute size do not match!");
277 
278  _imp.write(type_id_map<T>::type_id,data);
279  }
280 
281  //----------------------------------------------------------------
297  template<typename T>
298  void write(const std::vector<T> &data) const
299  {
300  write(data.size(),data.data());
301  }
302 
303  //-----------------------------------------------------------------
319  template<typename T >
320  void write(const T &value) const
321  {
322  using namespace pni::core;
323  static_assert(!std::is_pointer<T>::value,"no const pointer");
324 
325  if(size()!=1)
326  throw size_mismatch_error(EXCEPTION_RECORD,
327  "Field is not scalar!");
328 
329  _imp.write(type_id_map<T>::type_id,&value);
330  }
331 
332 
333  //------------------------------------------------------------------
348  void write(const char *value) const
349  {
350  pni::core::string s(value);
351  write(s);
352  }
353 
354  //-----------------------------------------------------------------
369  void write(const pni::core::array &a) const
370  {
371  _write_array(a);
372  }
373 
374  //-----------------------------------------------------------------
395  template<
396  typename STORAGE,
397  typename IMAP,
398  typename IPA
399  >
400  void read(pni::core::mdarray<STORAGE,IMAP,IPA> &o) const
401  {
402  _read_array(o);
403  }
404 
405  //-----------------------------------------------------------------
406  template<typename ATYPE>
407  void read(pni::core::array_view<ATYPE> &v) const
408  {
409  if(v.is_contiguous())
410  _read_array(v);
411  else
412  {
413  typedef typename ATYPE::value_type value_type;
414  typedef pni::core::dynamic_array<value_type> buffer_type;
415  auto buffer = buffer_type::create(v.template shape<pni::core::shape_t>());
416  _read_array(buffer);
417  std::copy(buffer.begin(),buffer.end(),v.begin());
418  }
419  }
420 
421  //-----------------------------------------------------------------
422  template<typename ATYPE>
423  void read(pni::core::array_view<ATYPE> &&v) const
424  {
425  read(v);
426  }
427 
428  //-----------------------------------------------------------------
441  void read(pni::core::array &a) const
442  {
443  _read_array(a);
444  }
445 
446 
447  //-----------------------------------------------------------------
462  template<typename T>
463  void read(T &value) const
464  {
465  using namespace pni::core;
466  if(size()!=1)
467  throw size_mismatch_error(EXCEPTION_RECORD,
468  "Try to read a scalar from an array field!");
469 
470  _imp.read(type_id_map<T>::type_id,&value);
471  }
472 
473  //---------------------------------------------------------------
491  template<typename T>
492  void read(size_t n,T *value) const
493  {
494  using namespace pni::core;
495  if(n!=size())
496  throw size_mismatch_error(EXCEPTION_RECORD,
497  "Memory and attribute size do not match!");
498 
499  _imp.read(type_id_map<T>::type_id,value);
500  }
501 
502  //---------------------------------------------------------------
517  template<typename T>
518  void read(std::vector<T> &data) const
519  {
520  read(data.size(),data.data());
521  }
522 
523  //============simple maintenance methods========================
533  template<typename CTYPE>
534  CTYPE shape() const
535  {
536  using namespace pni::core;
537  if(!is_valid())
538  throw invalid_object_error(EXCEPTION_RECORD,
539  "Cannot get shape from invalid attribute!");
540  return type_type::template from_index_vector<CTYPE>(_imp.shape());
541  }
542 
543  //--------------------------------------------------------------
555  size_t rank() const { return this->_imp.rank(); }
556 
557  //--------------------------------------------------------------
568  size_t size() const { return _imp.size(); }
569 
570  //--------------------------------------------------------------
582  pni::core::type_id_t type_id() const { return _imp.type_id(); }
583 
584  //--------------------------------------------------------------
591  void close() { _imp.close(); }
592 
593  //---------------------------------------------------------------
601  bool is_valid() const { return _imp.is_valid(); }
602 
603  //---------------------------------------------------------------
617  pni::core::string name() const { return _imp.name(); }
618 
619  //---------------------------------------------------------------
633  pni::core::string filename() const
634  {
635  return _imp.filename();
636  }
637 
638  //-----------------------------------------------------------------
643  template<typename ...ITYPES>
644  attribute_type operator()(ITYPES ...indices) const
645  {
646  //generate a copy of the attribute
647  attribute_type a(*this);
648 
649  //generate a selection vector
650  typename type_type::selection_vector_type selection{pni::core::slice(indices)...};
651 
652  //apply the selection
653  a._imp.apply_selection(selection);
654 
655  return a;
656 
657  }
658 
659  //-----------------------------------------------------------------
660  attribute_type operator()(const std::vector<pni::core::slice> &indices) const
661  {
662  //generate a copy of the attribute
663  attribute_type a(*this);
664 
665  //generate a selection vector
666  typename type_type::selection_vector_type selection(indices.size());
667  std::copy(indices.begin(),indices.end(),selection.begin());
668 
669  //apply the selection
670  a._imp.apply_selection(selection);
671 
672  return a;
673  }
674 
675  //-----------------------------------------------------------------
688  {
689  using namespace pni::core;
690  typedef typename nxobject_trait<IMPID>::group_type group_type;
691  typedef typename nxobject_trait<IMPID>::field_type field_type;
692  typedef typename nximp_map<IMPID>::group_imp group_imp_type;
693  typedef typename nximp_map<IMPID>::field_imp field_imp_type;
694 
695  typename nximp_map<IMPID>::object_imp p = _imp.parent();
696 
697  if(p.nxobject_type() == nxobject_type::NXFIELD)
698  return field_type(field_imp_type(std::move(p)));
699  else if(p.nxobject_type() == nxobject_type::NXGROUP)
700  return group_type(group_imp_type(std::move(p)));
701  else
702  throw type_error(EXCEPTION_RECORD,
703  "Cannot convert parent type");
704 
705  }
706 
707  };
708 
709 //end of namespace
710 }
711 }
712 }
void read(pni::core::array &a) const
read data to array
Definition: nxattribute.hpp:441
void read(pni::core::array_view< ATYPE > &&v) const
Definition: nxattribute.hpp:423
size_t size() const
obtain attribute size
Definition: nxattribute.hpp:568
pni::core::type_id_t type_id() const
obtain type id
Definition: nxattribute.hpp:582
implementation map
Definition: nximp_map.hpp:50
nexus object traits
Definition: nxobject_traits.hpp:44
bool is_valid() const
check validity of the attribute
Definition: nxattribute.hpp:601
unexpected invalid object
Definition: exceptions.hpp:130
void read(std::vector< T > &data) const
read data to an STL vector
Definition: nxattribute.hpp:518
STL namespace.
void write(const pni::core::array_view< ATYPE > &v) const
write view data to an attribute
Definition: nxattribute.hpp:235
void _read_array(ATYPE &a) const
read data to array
Definition: nxattribute.hpp:115
attribute object
Definition: nxattribute.hpp:55
void write(size_t n, const T *data) const
write data from a plain C pointer
Definition: nxattribute.hpp:269
pni::core::type_id_t type_id(const h5datatype &o)
get type code
Definition: h5datatype.cpp:240
attribute_type & operator=(const typename nxobject_trait< IMPID >::object_type &o)
conversion assignment
Definition: nxattribute.hpp:177
void write(const T &value) const
write a single scalar value
Definition: nxattribute.hpp:320
Definition: cbf_reader.hpp:41
void close()
close attribute
Definition: nxattribute.hpp:591
void read(size_t n, T *value) const
read data to pointer
Definition: nxattribute.hpp:492
nximp_map< IMPID >::type_imp type_type
implementation of the type trait
Definition: nxattribute.hpp:61
void write(const pni::core::mdarray< STORAGE, IMAP, IPA > &o) const
write data from an mdarray instance
Definition: nxattribute.hpp:210
size_t rank() const
return attribute rank
Definition: nxattribute.hpp:555
attribute_type operator()(const std::vector< pni::core::slice > &indices) const
Definition: nxattribute.hpp:660
void write(const std::vector< T > &data) const
write data from a vector
Definition: nxattribute.hpp:298
void read(T &value) const
read a single scalar value
Definition: nxattribute.hpp:463
ATYPE as_attribute(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &o)
as attribute wrapper
Definition: as_attribute.hpp:170
CTYPE shape() const
obtain attribute shape
Definition: nxattribute.hpp:534
pni::core::string filename() const
return filename
Definition: nxattribute.hpp:633
nximp_map< IMPID >::attribute_imp implementation_type
implementation type of the attribute
Definition: nxattribute.hpp:59
void read(pni::core::mdarray< STORAGE, IMAP, IPA > &o) const
read data to an array
Definition: nxattribute.hpp:400
attribute_type operator()(ITYPES...indices) const
apply selection to an attribute
Definition: nxattribute.hpp:644
void write(const pni::core::array &a) const
write array type erasure data
Definition: nxattribute.hpp:369
void _write_array(const ATYPE &a) const
write data from array
Definition: nxattribute.hpp:87
pni::core::string name() const
get attribute name
Definition: nxattribute.hpp:617
nxobject_trait< IMPID >::object_type parent() const
return parent object
Definition: nxattribute.hpp:687
nxattribute(implementation_type &&i)
move constructor from implementation
Definition: nxattribute.hpp:143
implementation_type _imp
implementation of the attribute object
Definition: nxattribute.hpp:66
nxattribute() noexcept
default constructor
Definition: nxattribute.hpp:130
void write(const char *value) const
write a C-string
Definition: nxattribute.hpp:348
void read(pni::core::array_view< ATYPE > &v) const
Definition: nxattribute.hpp:407
nxattribute(const typename nxobject_trait< IMPID >::object_type &o)
conversion constructor
Definition: nxattribute.hpp:158
nxattribute< IMPID > attribute_type
define the actual type of this object
Definition: nxattribute.hpp:63