libpniio
nxgroup.hpp
Go to the documentation of this file.
1 //
2 // (c) Copyright 2011 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: Jul 1, 2011
21 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
22 //
23 #pragma once
24 
25 #include <sstream>
26 #include <pni/core/types.hpp>
27 #include <pni/core/utilities.hpp>
28 
29 #include "nxattribute_manager.hpp"
30 #include "nximp_map.hpp"
31 #include "nxobject_traits.hpp"
32 #include "nxfilter.hpp"
33 #include "../exceptions.hpp"
34 #include "nxlink.hpp"
35 #include "algorithms/as_group.hpp"
36 
37 
38 namespace pni{
39 namespace io{
40 namespace nx{
41 
56 
71 
83  template<nximp_code IMPID> class nxgroup
90  {
91 
92  public:
93  //===================public types==================================
100 
104  using iterator = pni::core::container_iterator<const group_type>;
111 
112  private:
121 
122  //===================private functions============================
123 
136  template<
137  typename CTYPE,
138  typename STYPE
139  >
140  CTYPE _create_auto_chunk(const STYPE &shape) const
141  {
142  auto chunk = pni::core::container_utils<CTYPE>::create(shape.size());
143 
144  std::copy(shape.begin(),shape.end(),chunk.begin());
145  chunk.front()=1;
146 
147  return chunk;
148  }
149 
150  //----------------------------------------------------------------
173  template<
174  typename T,
175  typename CTYPE
176  >
177  field_type _create_field(const pni::core::string &n,
178  const CTYPE &shape,
179  const CTYPE &chunk) const
180  {
181  return field_type(field_imp_type(_imp,n,
183  type_type::to_index_vector(shape),
184  type_type::to_index_vector(chunk)));
185 
186  }
187 
188  //----------------------------------------------------------------
214  template<
215  typename T,
216  typename CTYPE,
217  typename FIMP
218  >
219  field_type
220  _create_field(const pni::core::string &n,const CTYPE &shape,
221  const CTYPE &chunk,
222  const nxfilter<FIMP> &filter) const
223  {
224  return field_type(field_imp_type(_imp,n,
226  type_type::to_index_vector(shape),
227  type_type::to_index_vector(chunk),
228  filter.imp()));
229  }
230  public:
231  //==================public attributes==============================
240  //==============constructors and destructor========================
242  explicit nxgroup():_imp(),attributes(_imp) { }
243 
244  //-----------------------------------------------------------------
254  nxgroup(const group_type &o):
255  _imp(o._imp),
256  attributes(_imp)
257  { }
258 
259  //-----------------------------------------------------------------
264  _imp(std::move(o._imp)),
265  attributes(_imp)
266  { }
267 
268  //-----------------------------------------------------------------
279  explicit nxgroup(imp_type &&imp):
280  _imp(std::move(imp)),
281  attributes(_imp)
282  { }
283 
284  //-----------------------------------------------------------------
289  _imp(),
290  attributes(_imp)
291  {
292  *this = o;
293  }
294 
295  //========================assignment operators=====================
300  {
301  if(this == &g) return *this;
302 
303  _imp = g._imp;
304  return *this;
305  }
306 
307  //-----------------------------------------------------------------
312  {
313  if(this == &g) return *this;
314 
315  _imp = std::move(g._imp);
316 
317  return *this;
318  }
319 
320  //-----------------------------------------------------------------
324  group_type &operator=(const typename
326  {
327  *this = as_group(o);
328  return *this;
329  }
330 
331  //====================group creation methods=======================
347  group_type create_group(const pni::core::string &n,
348  const pni::core::string &type=pni::core::string()) const
349  {
350  //we need to do here two things
351  //we have to check if the particular group type
352  //exists and add a check object to the class
353 
354  group_type g = group_type(imp_type(_imp,n));
355 
356  //if the type string is not empty we write the
357  //appropriate attribute.
358  if(!type.empty())
359  g.attributes.template create<pni::core::string>("NX_class").write(type);
360 
361  return g;
362  }
363 
364  //----------------------------------------------------------------
392  template<
393  typename T,
394  typename CTYPE = pni::core::shape_t
395  >
396  field_type create_field(const pni::core::string &n,
397  const CTYPE &shape={1}) const
398  {
399  using namespace pni::core;
400 
401  if(!shape.size())
402  throw size_mismatch_error(EXCEPTION_RECORD,
403  "Shape container must not be empty!");
404 
405  auto chunk = _create_auto_chunk<shape_t>(shape);
406 
407  return this->_create_field<T>(n,shape,chunk);
408 
409  }
410 
411  //-----------------------------------------------------------------
416 
426  template<
444  typename T,
445  typename CTYPE=pni::core::shape_t
446  >
447  field_type create_field(const pni::core::string &n,
448  const CTYPE &shape,
449  const CTYPE &chunk) const
450  {
451  return _create_field<T>(n,shape,chunk);
452  }
453 
454  //------------------------------------------------------------------
461 
471  template<
487  typename T,
488  typename CTYPES,
489  typename FIMP
490  >
491  field_type create_field(const pni::core::string &n,
492  const CTYPES &shape,
493  const nxfilter<FIMP> &filter) const
494  {
495  using namespace pni::core;
496 
497  if(!shape.size())
498  throw size_mismatch_error(EXCEPTION_RECORD,
499  "Shape must not be empty!");
500 
501  auto chunk = _create_auto_chunk<shape_t>(shape);
502 
503  return _create_field<T>(n,shape,chunk,filter);
504 
505  }
506 
507 
508  //-----------------------------------------------------------------
513 
525  template<typename T,
542  typename CTYPES,
543  typename CTYPEC,
544  typename FIMP
545  >
546  field_type create_field(const pni::core::string &n,
547  const CTYPES &s,
548  const CTYPEC &cs,
549  const nxfilter<FIMP> &filter) const
550  {
551  return _create_field<T>(n,s,cs,filter);
552  }
553 
554 
555  //===============methods to open objects===========================
572  at(const pni::core::string &n) const
573  {
574  using namespace pni::core;
575 
576  if(find(n.begin(),n.end(),'/')!=n.end())
577  throw value_error(EXCEPTION_RECORD,
578  "Invalid character in object name!");
579 
580  //need to check the status of the link for the requested
581  //object - if the link is invalid we return a link
582  //object.
583  if(link_status(*this,n)==nxlink_status::INVALID)
584  return link_type(*this,n);
585 
586 
587  object_imp_type object = _imp.at(n);
588 
589  if(object.nxobject_type() == nxobject_type::NXFIELD)
590  return field_type(field_imp_type(std::move(object)));
591  else if(object.nxobject_type() == nxobject_type::NXGROUP)
592  return group_type(imp_type(std::move(object)));
593  else
594  throw type_error(EXCEPTION_RECORD,
595  "Unknown Nexus object type!");
596 
597  }
598 
599  //-----------------------------------------------------------------
615  operator[](const pni::core::string &n) const
616  {
617  return at(n);
618  }
619 
620  //----------------------------------------------------------------
631  size_t size() const { return _imp.size(); }
632 
633  //-----------------------------------------------------------------
646  {
647  group_type g(imp_type(_imp.parent()));
648  return g;
649  }
650 
651  //-----------------------------------------------------------------
668  at(size_t i) const
669  {
670  return this->at(link_name(*this,i));
671  }
672 
673  //-----------------------------------------------------------------
688  operator[](size_t i) const
689  {
690  return this->at(i);
691  }
692 
693  //-----------------------------------------------------------------
706  bool has_child(const pni::core::string &n) const
707  {
708  if(n.find('/')==pni::core::string::npos)
709  return _imp.has_child(n);
710  else
711  return false;
712  }
713 
714  //-----------------------------------------------------------------
731  void remove(const pni::core::string &n) const{ _imp.remove(n); }
732 
733  //-----------------------------------------------------------------
742  iterator begin() const noexcept { return iterator(this); }
743 
744  //-----------------------------------------------------------------
755  iterator end() const
756  {
757  return iterator(this, this->size());
758  }
759 
760  //---------------------------------------------------------------
774  pni::core::string name() const { return _imp.name(); }
775 
776  //---------------------------------------------------------------
790  pni::core::string filename() const
791  {
792  return _imp.filename();
793  }
794 
795  //---------------------------------------------------------------
803  void close() { _imp.close(); }
804 
805  //---------------------------------------------------------------
815  bool is_valid() const { return _imp.is_valid(); }
816 
817  //---------------------------------------------------------------
821  const imp_type &imp() const noexcept{ return _imp; }
822 
823 
824  };
825 
826  //=============comparison operators====================================
840  template<nximp_code IMPID>
841  bool operator==(const nxgroup<IMPID> &a,const nxgroup<IMPID> &b)
842  {
843  if(a.imp().object() == b.imp().object()) return true;
844  return false;
845  }
846 
847  //-----------------------------------------------------------------------
861  template<nximp_code IMPID>
862  bool operator!=(const nxgroup<IMPID> &a,const nxgroup<IMPID> &b)
863  {
864  if(a == b) return false;
865  return true;
866  }
867 
868 
869 //end of namespace
870 }
871 }
872 }
attribute manager template
Definition: nxattribute_manager.hpp:42
Filter object.
Definition: nxfilter.hpp:42
nxobject_trait< IMPID >::object_type parent() const
return parent object
Definition: nxgroup.hpp:645
typename nxobject_trait< IMPID >::link_type link_type
link type
Definition: nxgroup.hpp:110
pni::core::string filename() const
return filename
Definition: nxgroup.hpp:790
bool operator!=(const nxgroup< IMPID > &a, const nxgroup< IMPID > &b)
group inequality check
Definition: nxgroup.hpp:862
nxgroup(group_type &&o)
move constructor
Definition: nxgroup.hpp:263
typename nximp_map< IMPID >::group_imp imp_type
group implementation type
Definition: nxgroup.hpp:95
implementation map
Definition: nximp_map.hpp:50
nexus object traits
Definition: nxobject_traits.hpp:44
pni::io::nx::nxgroup< nximp_code::HDF5 > nxgroup
Definition: nx_hdf5_implementation.hpp:49
group_type & operator=(const typename nxobject_trait< IMPID >::object_type &o)
conversion assignment
Definition: nxgroup.hpp:324
const imp_type & imp() const noexcept
get constant reference to the implementation
Definition: nxgroup.hpp:821
STL namespace.
nxobject_type
Nexus typd ids.
Definition: nxobject_type.hpp:39
nxobject_trait< IMPID >::object_type operator[](const pni::core::string &n) const
open an object
Definition: nxgroup.hpp:615
auto link_status(const GTYPE< IMPID > parent, const pni::core::string &lname) -> nxlink_status
Definition: nxlink.hpp:410
nxgroup< IMPID > group_type
the group type
Definition: nxgroup.hpp:99
typename nximp_map< IMPID >::type_imp type_type
type implementation
Definition: nxgroup.hpp:97
NXgroup object.
Definition: nxgroup.hpp:89
group_type & operator=(group_type &&g)
move assignment
Definition: nxgroup.hpp:311
nxobject_trait< IMPID >::object_type operator[](size_t i) const
open object by index
Definition: nxgroup.hpp:688
pni::core::type_id_t type_id(const h5datatype &o)
get type code
Definition: h5datatype.cpp:240
field_type _create_field(const pni::core::string &n, const CTYPE &shape, const CTYPE &chunk, const nxfilter< FIMP > &filter) const
field creation function
Definition: nxgroup.hpp:220
void close()
close the group
Definition: nxgroup.hpp:803
field_type create_field(const pni::core::string &n, const CTYPE &shape={1}) const
create a field
Definition: nxgroup.hpp:396
Definition: cbf_reader.hpp:41
field_type _create_field(const pni::core::string &n, const CTYPE &shape, const CTYPE &chunk) const
field creation function
Definition: nxgroup.hpp:177
iterator end() const
iterator on last child
Definition: nxgroup.hpp:755
bool has_child(const pni::core::string &n) const
check if a particular object exists
Definition: nxgroup.hpp:706
auto link_name(const GTYPE< IMPID > &parent, size_t index) -> pni::core::string
get link name
Definition: nxlink.hpp:264
nxobject_trait< IMPID >::object_type at(size_t i) const
open object by index
Definition: nxgroup.hpp:668
typename nxobject_trait< IMPID >::attribute_type attribute_type
attribute type
Definition: nxgroup.hpp:108
group_type & operator=(const group_type &g)
copy assignment
Definition: nxgroup.hpp:299
nxgroup(imp_type &&imp)
move construct from implementation object
Definition: nxgroup.hpp:279
CTYPE _create_auto_chunk(const STYPE &shape) const
automatic chunk creation
Definition: nxgroup.hpp:140
imp_type & imp()
get non-const ref
Definition: nxfilter.hpp:64
typename nximp_map< IMPID >::object_imp object_imp_type
object implementation type
Definition: nxgroup.hpp:116
nxgroup()
default constructor
Definition: nxgroup.hpp:242
bool operator==(const nxgroup< IMPID > &a, const nxgroup< IMPID > &b)
group equality check
Definition: nxgroup.hpp:841
imp_type _imp
the implementation instance of nxgroup
Definition: nxgroup.hpp:120
typename nximp_map< IMPID >::field_imp field_imp_type
field implementation type
Definition: nxgroup.hpp:114
bool is_valid() const
check group validity
Definition: nxgroup.hpp:815
field_type create_field(const pni::core::string &n, const CTYPES &s, const CTYPEC &cs, const nxfilter< FIMP > &filter) const
create a multidimensional field (explicit chunk) with filter
Definition: nxgroup.hpp:546
nxobject_trait< IMPID >::object_type at(const pni::core::string &n) const
open an arbitrary object
Definition: nxgroup.hpp:572
field_type create_field(const pni::core::string &n, const CTYPES &shape, const nxfilter< FIMP > &filter) const
Creates a multidimensional field with a filter.
Definition: nxgroup.hpp:491
GTYPE as_group(const nxobject< GTYPE, FTYPE, ATYPE, LTYPE > &o)
as group wrapper
Definition: as_group.hpp:167
pni::core::string name() const
name of the group
Definition: nxgroup.hpp:774
pni::core::container_iterator< const group_type > iterator
iterator type
Definition: nxgroup.hpp:104
nxgroup(const typename nxobject_trait< IMPID >::object_type &o)
conversion constructor
Definition: nxgroup.hpp:288
group_type create_group(const pni::core::string &n, const pni::core::string &type=pni::core::string()) const
create a group as nexus class
Definition: nxgroup.hpp:347
typename nxobject_trait< IMPID >::field_type field_type
field type
Definition: nxgroup.hpp:106
iterator begin() const noexcept
iterator on first child
Definition: nxgroup.hpp:742
field_type create_field(const pni::core::string &n, const CTYPE &shape, const CTYPE &chunk) const
create a field
Definition: nxgroup.hpp:447
size_t size() const
return number of children
Definition: nxgroup.hpp:631
nxgroup(const group_type &o)
copy constructor
Definition: nxgroup.hpp:254
nxattribute_manager< group_type > attributes
attribute manager
Definition: nxgroup.hpp:239
typename nxobject_trait< IMPID >::object_type value_type
nxobject as container value_type
Definition: nxgroup.hpp:102