hops
H5Object_misc.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
3  *
4  * Distributed under the Boost Software License, Version 1.0.
5  * (See accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  */
9 #ifndef H5OBJECT_MISC_HPP
10 #define H5OBJECT_MISC_HPP
11 
12 #include <iostream>
13 
14 namespace HighFive {
15 
16 inline Object::Object() : _hid(H5I_INVALID_HID) {}
17 
18 inline Object::Object(hid_t hid) : _hid(hid) {}
19 
20 inline Object::Object(const Object& other) : _hid(other._hid) {
21  if (other.isValid() && H5Iinc_ref(_hid) < 0) {
22  throw ObjectException("Reference counter increase failure");
23  }
24 }
25 
26 inline Object::Object(Object&& other) noexcept
27  : _hid(other._hid) {
28  other._hid = H5I_INVALID_HID;
29 }
30 
31 inline Object& Object::operator=(const Object& other) {
32  if (this != &other) {
33  if (_hid != H5I_INVALID_HID)
34  H5Idec_ref(_hid);
35 
36  _hid = other._hid;
37  if (other.isValid() && H5Iinc_ref(_hid) < 0) {
38  throw ObjectException("Reference counter increase failure");
39  }
40  }
41  return *this;
42 }
43 
44 inline Object::~Object() {
45  if (isValid() && H5Idec_ref(_hid) < 0) {
46  std::cerr << "HighFive::~Object: reference counter decrease failure"
47  << std::endl;
48  }
49 }
50 
51 inline bool Object::isValid() const noexcept {
52  return (_hid != H5I_INVALID_HID) && (H5Iis_valid(_hid) != false);
53 }
54 
55 inline hid_t Object::getId() const noexcept {
56  return _hid;
57 }
58 
59 static inline ObjectType _convert_object_type(const H5I_type_t& h5type) {
60  switch (h5type) {
61  case H5I_FILE:
62  return ObjectType::File;
63  case H5I_GROUP:
64  return ObjectType::Group;
65  case H5I_DATATYPE:
67  case H5I_DATASPACE:
68  return ObjectType::DataSpace;
69  case H5I_DATASET:
70  return ObjectType::Dataset;
71  case H5I_ATTR:
72  return ObjectType::Attribute;
73  default:
74  return ObjectType::Other;
75  }
76 }
77 
78 inline ObjectType Object::getType() const {
79  // H5Iget_type is a very lightweight func which extracts the type from the id
80  H5I_type_t h5type;
81  if ((h5type = H5Iget_type(_hid)) == H5I_BADID) {
82  HDF5ErrMapper::ToException<ObjectException>("Invalid hid or object type");
83  }
84  return _convert_object_type(h5type);
85 }
86 
87 inline ObjectInfo Object::getInfo() const {
88  ObjectInfo info;
89 #if (H5Oget_info_vers < 3)
90  if (H5Oget_info(_hid, &info.raw_info) < 0) {
91 #else
92  if (H5Oget_info1(_hid, &info.raw_info) < 0) {
93 #endif
94  HDF5ErrMapper::ToException<ObjectException>("Unable to obtain info for object");
95  }
96  return info;
97 }
98 
99 inline haddr_t ObjectInfo::getAddress() const noexcept {
100  return raw_info.addr;
101 }
102 inline size_t ObjectInfo::getRefCount() const noexcept {
103  return raw_info.rc;
104 }
105 inline time_t ObjectInfo::getCreationTime() const noexcept {
106  return raw_info.btime;
107 }
108 inline time_t ObjectInfo::getModificationTime() const noexcept {
109  return raw_info.mtime;
110 }
111 
112 
113 
114 } // namespace
115 
116 #endif // H5OBJECT_MISC_HPP
HighFive::ObjectType::Dataset
@ Dataset
HighFive::Object::isValid
bool isValid() const noexcept
isValid
Definition: H5Object_misc.hpp:51
HighFive::Object::getInfo
ObjectInfo getInfo() const
Retrieve several infos about the current object (address, dates, etc)
Definition: H5Object_misc.hpp:87
HighFive::ObjectType::Other
@ Other
HighFive::ObjectType::DataSpace
@ DataSpace
HighFive::Object::getType
ObjectType getType() const
Gets the fundamental type of the object (dataset, group, etc)
Definition: H5Object_misc.hpp:78
HighFive::ObjectInfo::getCreationTime
time_t getCreationTime() const noexcept
Retrieve the object's creation time.
Definition: H5Object_misc.hpp:105
HighFive::Object
Definition: H5Object.hpp:36
HighFive::Object::getId
hid_t getId() const noexcept
getId
Definition: H5Object_misc.hpp:55
HighFive::_convert_object_type
static ObjectType _convert_object_type(const H5I_type_t &h5type)
Definition: H5Object_misc.hpp:59
HighFive::ObjectType::Attribute
@ Attribute
HighFive::ObjectType
ObjectType
Enum of the types of objects (H5O api)
Definition: H5Object.hpp:25
HighFive::ObjectException
Exception specific to HighFive Object interface.
Definition: H5Exception.hpp:75
HighFive::ObjectType::File
@ File
HighFive::ObjectInfo::raw_info
H5O_info_t raw_info
Definition: H5Object.hpp:112
HighFive::Object::_hid
hid_t _hid
Definition: H5Object.hpp:81
HighFive::Object::operator=
Object & operator=(const Object &other)
Definition: H5Object_misc.hpp:31
HighFive::Object::Object
Object()
Definition: H5Object_misc.hpp:16
HighFive::Object::~Object
~Object()
Definition: H5Object_misc.hpp:44
HighFive::ObjectType::Group
@ Group
HighFive::ObjectInfo
A class for accessing hdf5 objects info.
Definition: H5Object.hpp:94
HighFive::ObjectType::UserDataType
@ UserDataType
HighFive::ObjectInfo::getAddress
haddr_t getAddress() const noexcept
Retrieve the address of the object (within its file)
Definition: H5Object_misc.hpp:99
HighFive::ObjectInfo::getModificationTime
time_t getModificationTime() const noexcept
Retrieve the object's last modification time.
Definition: H5Object_misc.hpp:108
HighFive::ObjectInfo::getRefCount
size_t getRefCount() const noexcept
Retrieve the number of references to this object.
Definition: H5Object_misc.hpp:102
HighFive
Definition: H5_definitions.hpp:15