libpniio
nxfield.hpp
Go to the documentation of this file.
1 //
2 // Declaration of the NXfield 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 // Declaration of the NXfield template
23 //
24 // Created on: Jul 3, 2011
25 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
26 //
27 #pragma once
28 
29 #include <sstream>
30 
31 #include <pni/core/types.hpp>
32 #include <pni/core/arrays/mdarray.hpp>
33 #include <pni/core/arrays/array_view.hpp>
34 #include <pni/core/arrays/slice.hpp>
35 #include <pni/core/type_erasures/array.hpp>
36 #include <pni/core/error.hpp>
37 
38 #include "nxattribute_manager.hpp"
39 #include "nxobject_traits.hpp"
40 #include "algorithms/as_field.hpp"
41 
42 
43 namespace pni{
44 namespace io{
45 namespace nx{
46 
79 
92 
117  template<nximp_code IMPID> class nxfield
125  {
126  public:
136  private:
138  imp_type _imp;
139 
150  static pni::core::string
151  _shape_mismatch_error_message(const pni::core::shape_t &ashape,
152  const pni::core::shape_t &fshape)
153  {
154  std::stringstream ss;
155  ss<<"Array shape ( ";
156  for(auto v: ashape) ss<<v<<" ";
157  ss<<") and field shape ( ";
158  for(auto v: fshape) ss<<v<<" ";
159  ss<<") do not match!";
160 
161  return ss.str();
162  }
163 
164 
165  //-----------------------------------------------------------------
183  template<typename ATYPE>
184  void _read_array(ATYPE &a) const
185  {
186  using namespace pni::core;
187 
188  typedef typename type_type::index_vector_type index_vector_type;
189  check_allocation_state(a,EXCEPTION_RECORD);
190  check_equal_size(a,*this,EXCEPTION_RECORD);
191 
192  _imp.read(pni::core::type_id(a),
193  a.template shape<index_vector_type>(),a.data());
194 
195  }
196 
197  //-----------------------------------------------------------------
215  template<typename ATYPE>
216  void _write_array(const ATYPE &a) const
217  {
218  using namespace pni::core;
219 
220  typedef typename type_type::index_vector_type index_vector_type;
221  check_allocation_state(a,EXCEPTION_RECORD);
222  check_equal_size(a,*this,EXCEPTION_RECORD);
223 
224  _imp.write(pni::core::type_id(a),
225  a.template shape<index_vector_type>(),a.data());
226  }
227 
228 
229  public:
230  //===================public attributes=============================
239  //============constructors and destructors=========================
243  explicit nxfield():
244  _imp(),
245  attributes(_imp)
246  { }
247 
248  //-----------------------------------------------------------------
252  nxfield(const field_type &o):
253  _imp(o._imp),
254  attributes(_imp)
255  { }
256 
257  //-----------------------------------------------------------------
261  nxfield(field_type &&o):
262  _imp(std::move(o._imp)),
263  attributes(_imp)
264  { }
265 
266  //-----------------------------------------------------------------
270  explicit nxfield(imp_type &&o):
271  _imp(std::move(o)),
272  attributes(_imp)
273  {}
274 
275  //-----------------------------------------------------------------
285  _imp(),
286  attributes(_imp)
287  {
288  *this = o;
289  }
290 
291 
292  //-----------------------------------------------------------------
297  {
298  _imp.clear_selections();
299  }
300 
301  //===============assignment operator===============================
308  field_type &operator=(const field_type &f)
309  {
310  if(this == &f) return *this;
311 
312  _imp = f._imp;
313  return *this;
314  }
315 
316  //----------------------------------------------------------------
323  field_type &operator=(field_type &&f)
324  {
325  if(this == &f) return *this;
326 
327  _imp = std::move(f._imp);
328  return *this;
329  }
330 
331  //-----------------------------------------------------------------
340  field_type &operator=(const typename nxobject_trait<IMPID>::object_type &o)
341  {
342  *this = as_field(o);
343  return *this;
344  }
345 
346 
347  //=================dataset inquiry methods=========================
358  template<typename CTYPE> CTYPE shape() const
359  {
360  return type_type::template from_index_vector<CTYPE>(_imp.shape());
361  }
362 
363  //-----------------------------------------------------------------
374  size_t size() const
375  {
376  return _imp.size();
377  }
378 
379  //-----------------------------------------------------------------
391  pni::core::type_id_t type_id() const
392  {
393  return _imp.type_id();
394  }
395 
396  //-----------------------------------------------------------------
410  {
411  typedef typename nxobject_trait<IMPID>::group_type group_type;
412  typedef typename nximp_map<IMPID>::group_imp group_imp_type;
413 
414  return group_type(group_imp_type(_imp.parent()));
415  }
416 
417  //-----------------------------------------------------------------
433  void grow(const size_t &e,const size_t &n=1)
434  {
435  _imp.grow(e,n);
436  }
437 
438  //-----------------------------------------------------------------
449  size_t rank() const
450  {
451  return _imp.rank();
452  }
453 
454  //=============methods for reading data============================
461 
467  template<typename T> void read(T &value) const
478  {
479  using namespace pni::core;
480 
481  typedef typename type_type::index_vector_type index_vector_type;
482  if(size() != 1)
483  throw size_mismatch_error(EXCEPTION_RECORD,
484  "Field is not scalar!");
485 
486  _imp.read(type_id_map<T>::type_id,index_vector_type{1},&value);
487  }
488 
489  //-----------------------------------------------------------------
508  template<typename T>
509  void read(size_t n,T *values) const
510  {
511  using namespace pni::core;
512  typedef typename type_type::index_vector_type index_vector_type;
513  if(n!=size())
514  throw size_mismatch_error(EXCEPTION_RECORD,
515  "Field size does not match memory size!");
516 
517  _imp.read(type_id_map<T>::type_id,index_vector_type{n},values);
518  }
519 
520  //-----------------------------------------------------------------
537  template<typename T>
538  void read(std::vector<T> &data) const
539  {
540  read(data.size(),data.data());
541  }
542 
543 
544  //-----------------------------------------------------------------
552 
558  template<
573  typename STORAGE,
574  typename IMAP,
575  typename IPA
576  >
577  void read(pni::core::mdarray<STORAGE,IMAP,IPA> &array) const
578  {
579  _read_array(array);
580  }
581 
582  //-----------------------------------------------------------------
583  template<typename ATYPE>
584  void read(pni::core::array_view<ATYPE> &v) const
585  {
586  if(v.is_contiguous())
587  _read_array(v);
588  else
589  {
590  typedef typename ATYPE::value_type value_type;
591  typedef dynamic_array<value_type> buffer_type;
592  auto buffer = buffer_type::create(v.template shape<pni::core::shape_t>());
593  _read_array(buffer);
594  std::copy(buffer.begin(),buffer.end(),v.begin());
595  }
596  }
597 
598  //-----------------------------------------------------------------
599  template<typename ATYPE>
600  void read(pni::core::array_view<ATYPE> &&v) const
601  {
602  read(v);
603  }
604 
605  //-----------------------------------------------------------------
622  void read(array &a) const
623  {
624  _read_array(a);
625  }
626 
627 
628  //=================methods for writing data========================
634 
640  template<typename T> void write(const T &value) const
651  {
652  using namespace pni::core;
653  typedef typename type_type::index_vector_type index_vector_type;
654  static_assert(!std::is_pointer<T>::value,"no pointer");
655  if(_imp.size()!=1)
656  throw size_mismatch_error(EXCEPTION_RECORD,
657  "Field is not scalar!");
658 
659  _imp.write(type_id_map<T>::type_id,index_vector_type({1}),&value);
660  }
661 
662  //-----------------------------------------------------------------
683  template<typename T> void write(size_t n,const T *value) const
684  {
685  using namespace pni::core;
686  typedef typename type_type::index_vector_type index_vector_type;
687 
688  if(n!=size())
689  throw size_mismatch_error(EXCEPTION_RECORD,
690  "Field size does not match memory size!");
691 
692 #ifdef __clang__
693  _imp.write(type_id_map<T>::type_id,index_vector_type{n},
694  static_cast<const void*>(value));
695 #else
696  _imp.write(type_id_map<T>::type_id,index_vector_type{{n}},
697  static_cast<const void*>(value));
698 #endif
699  }
700 
701  //-----------------------------------------------------------------
719  template<typename T>
720  void write(const std::vector<T> &data) const
721  {
722  write(data.size(),data.data());
723  }
724 
725  //----------------------------------------------------------------
746  template<
747  typename STORAGE,
748  typename IMAP,
749  typename IPA
750  >
751  void write(const pni::core::mdarray<STORAGE,IMAP,IPA> &a) const
752  {
753  _write_array(a);
754  }
755 
756  //-----------------------------------------------------------------
779  template<typename ATYPE>
780  void write(const pni::core::array_view<ATYPE> &v) const
781  {
782  if(v.is_contiguous())
783  _write_array(v);
784  else
785  {
786  typedef typename ATYPE::value_type value_type;
787  typedef dynamic_array<value_type> array_type;
788  auto buffer = array_type::create(v.template shape<pni::core::shape_t>());
789  std::copy(v.begin(),v.end(),buffer.begin());
790  write(buffer);
791  }
792 
793  }
794 
795 
796  //-----------------------------------------------------------------
812  void write(const char *value) const
813  {
814  string s(value);
815  write(s);
816 
817  }
818 
819  //-----------------------------------------------------------------
836  void write(const pni::core::array &a) const
837  {
838  _write_array(a);
839  }
840 
841  //---------------------------------------------------------------
848 
857  template<typename ...ITYPES>
870  field_type operator()(ITYPES ...indices) const
871  {
872  typedef std::vector<pni::core::slice> container_type;
873  field_type new_field(*this);
874 
875  new_field._imp.apply_selection(container_type({pni::core::slice(indices)...}));
876 
877  return new_field;
878  }
879 
880  //---------------------------------------------------------------
890  field_type operator()()
891  {
892  _imp.clear_selections();
893  return *this;
894  }
895 
896 
897  //---------------------------------------------------------------
914  field_type operator()(const std::vector<pni::core::slice> &selection) const
915  {
916  field_type new_field(*this);
917  new_field._imp.apply_selection(selection);
918  return new_field;
919  }
920 
921  //---------------------------------------------------------------
932  pni::core::string name() const
933  {
934  return _imp.name();
935  }
936 
937  //---------------------------------------------------------------
951  pni::core::string filename() const
952  {
953  return _imp.filename();
954  }
955 
956  //---------------------------------------------------------------
964  void close()
965  {
966  _imp.close();
967  }
968 
969  //---------------------------------------------------------------
976  bool is_valid() const noexcept
977  {
978  return _imp.is_valid();
979  }
980 
981  };
982 
983 
984 //end of namespace
985 }
986 }
987 }
attribute manager template
Definition: nxattribute_manager.hpp:42
void read(size_t n, T *values) const
reading data to memory
Definition: nxfield.hpp:509
size_t size() const
return size
Definition: nxfield.hpp:374
pni::io::nx::nxfield< nximp_code::HDF5 > nxfield
Definition: nx_hdf5_implementation.hpp:50
nxobject_trait< IMPID >::attribute_type attribute_type
attribute type
Definition: nxfield.hpp:135
void write(const T &value) const
write a single value
Definition: nxfield.hpp:650
nxfield(field_type &&o)
move constructor
Definition: nxfield.hpp:261
void grow(const size_t &e, const size_t &n=1)
grow field along a particular dimension
Definition: nxfield.hpp:433
imp_type _imp
field implementation
Definition: nxfield.hpp:138
implementation map
Definition: nximp_map.hpp:50
void read(pni::core::array_view< ATYPE > &&v) const
Definition: nxfield.hpp:600
nexus object traits
Definition: nxobject_traits.hpp:44
STL namespace.
bool is_valid() const noexcept
check validity
Definition: nxfield.hpp:976
size_t rank() const
number of dimensions
Definition: nxfield.hpp:449
field_type & operator=(const field_type &f)
copy assignment operator
Definition: nxfield.hpp:308
void read(array &a) const
read data to a array instance
Definition: nxfield.hpp:622
nxfield base class
Definition: nxfield.hpp:124
void read(pni::core::mdarray< STORAGE, IMAP, IPA > &array) const
read data to array
Definition: nxfield.hpp:577
nxfield(imp_type &&o)
move constructor from implementation object
Definition: nxfield.hpp:270
pni::core::string name() const
return field name
Definition: nxfield.hpp:932
pni::core::type_id_t type_id(const h5datatype &o)
get type code
Definition: h5datatype.cpp:240
Definition: cbf_reader.hpp:41
field_type operator()(ITYPES...indices) const
set a selection on the field
Definition: nxfield.hpp:870
void write(const pni::core::array &a) const
write array erasure
Definition: nxfield.hpp:836
field_type & operator=(const typename nxobject_trait< IMPID >::object_type &o)
conversion assignment
Definition: nxfield.hpp:340
~nxfield()
destructor
Definition: nxfield.hpp:296
field_type & operator=(field_type &&f)
move assignment operator
Definition: nxfield.hpp:323
nxattribute_manager< field_type > attributes
attribute manager
Definition: nxfield.hpp:238
nxobject_trait< IMPID >::object_type parent() const
get parent object
Definition: nxfield.hpp:409
void _write_array(const ATYPE &a) const
write data from array
Definition: nxfield.hpp:216
static pni::core::string _shape_mismatch_error_message(const pni::core::shape_t &ashape, const pni::core::shape_t &fshape)
gernerate error message
Definition: nxfield.hpp:151
FTYPE as_field(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &o)
as field wrapper
Definition: as_field.hpp:166
void write(const pni::core::array_view< ATYPE > &v) const
write data from array view
Definition: nxfield.hpp:780
void close()
close field
Definition: nxfield.hpp:964
nximp_map< IMPID >::type_imp type_type
type implementation type
Definition: nxfield.hpp:130
nxfield< IMPID > field_type
typedef for this type
Definition: nxfield.hpp:132
CTYPE shape() const
field shape
Definition: nxfield.hpp:358
void read(std::vector< T > &data) const
reading data vector
Definition: nxfield.hpp:538
pni::core::string filename() const
return filename
Definition: nxfield.hpp:951
field_type operator()(const std::vector< pni::core::slice > &selection) const
set a selection on the field
Definition: nxfield.hpp:914
nxfield(const field_type &o)
copy constructor
Definition: nxfield.hpp:252
void write(size_t n, const T *value) const
write data from memory
Definition: nxfield.hpp:683
void write(const char *value) const
write old style string
Definition: nxfield.hpp:812
void read(pni::core::array_view< ATYPE > &v) const
Definition: nxfield.hpp:584
field_type operator()()
clear a selection
Definition: nxfield.hpp:890
nxfield(const typename nxobject_trait< IMPID >::object_type &o)
constructor
Definition: nxfield.hpp:284
void write(const std::vector< T > &data) const
write data from vector
Definition: nxfield.hpp:720
void write(const pni::core::mdarray< STORAGE, IMAP, IPA > &a) const
write data from mdarray
Definition: nxfield.hpp:751
nxfield()
default constructor
Definition: nxfield.hpp:243
nximp_map< IMPID >::field_imp imp_type
implementation type
Definition: nxfield.hpp:128
void read(T &value) const
reading simple data from the dataset
Definition: nxfield.hpp:477
pni::core::type_id_t type_id() const
get the type ID
Definition: nxfield.hpp:391
void _read_array(ATYPE &a) const
read data to array
Definition: nxfield.hpp:184