summaryrefslogtreecommitdiffstats
path: root/c++/src/H5Object.cpp
blob: a692d0df916f31d56fd700bf17a5dcd6a60ec642 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <string>

#include "H5Include.h"
#include "H5RefCounter.h"
#include "H5Exception.h"
#include "H5IdComponent.h"
#include "H5Idtemplates.h"
#include "H5PropList.h"
#include "H5Object.h"
#include "H5DataType.h"
#include "H5DataSpace.h"
#include "H5AbstractDs.h"
#include "H5Attribute.h"

#ifndef H5_NO_NAMESPACE
namespace H5 {
#endif

// userAttrOpWrpr simply interfaces between the user's function and the
// C library function H5Aiterate; used to resolve the different prototype 
// problem.  May be moved to Iterator later.
extern "C" herr_t userAttrOpWrpr( hid_t loc_id, const char* attr_name, void* op_data )
{
   string s_attr_name = string( attr_name );
#ifdef NO_STATIC_CAST
   UserData4Aiterate* myData = (UserData4Aiterate *) op_data;
#else
   UserData4Aiterate* myData = static_cast <UserData4Aiterate *> (op_data);
#endif
   myData->op( *myData->object, s_attr_name, myData->opData );
   return 0;
}

// Default constructor - set id to 0 by default here but may be set
// to a valid HDF5 id, if any, by a subclass constructor.
H5Object::H5Object() : IdComponent() {}

// Constructs an object from an existing HDF5 id
H5Object::H5Object( const hid_t object_id ) : IdComponent( object_id ) {}

// Copy constructor: makes a copy of the original H5Object instance
H5Object::H5Object( const H5Object& original ) : IdComponent( original ) {}

// Creates an attribute for a group, dataset, or named datatype.
Attribute H5Object::createAttribute( const char* name, const DataType& data_type, const DataSpace& data_space, const PropList& create_plist ) const
{
   hid_t type_id = data_type.getId();
   hid_t space_id = data_space.getId();
   hid_t plist_id = create_plist.getId();
   hid_t attr_id = H5Acreate( id, name, type_id, space_id, plist_id );

   // If the attribute id is valid, create and return the Attribute object
   if( attr_id > 0 )
   {
      Attribute attr( attr_id );
      return( attr );
   }
   else
   {
      throw AttributeIException();
   }
}

// Creates an attribute for a group, dataset, or named datatype.
Attribute H5Object::createAttribute( const string& name, const DataType& data_type, const DataSpace& data_space, const PropList& create_plist ) const
{
   return( createAttribute( name.c_str(), data_type, data_space, create_plist ));
}

// Opens an attribute given its name; name is given as char*
Attribute H5Object::openAttribute( const char* name ) const
{
   hid_t attr_id = H5Aopen_name( id, name );
   if( attr_id > 0 )
   {
      Attribute attr( attr_id );
      return( attr );
   }
   else
   {
      throw AttributeIException();
   }
}

// Opens an attribute given its name; name is given as string
Attribute H5Object::openAttribute( const string& name ) const
{
   return( openAttribute( name.c_str()) );
}

// Opens an attribute given its index.
Attribute H5Object::openAttribute( const unsigned int idx ) const
{
   hid_t attr_id = H5Aopen_idx( id, idx );
   if( attr_id > 0 )
   {
      Attribute attr( attr_id );
      return( attr );
   }
   else
   {
      throw AttributeIException();
   }
}

// Iterates a user's function over all the attributes of the dataset
int H5Object::iterateAttrs( attr_operator_t user_op, unsigned * idx, void *op_data )
{
   // store the user's function and data
   UserData4Aiterate* userData = new UserData4Aiterate;
   userData->opData = op_data;
   userData->idx = idx;
   userData->op = user_op;
   userData->object = this;

   // call the C library routine H5Aiterate to iterate the attributes
   int ret_value = H5Aiterate( id, idx, userAttrOpWrpr, (void *) userData );
   // release memory
   delete userData;

   if( ret_value >= 0 )
      return( ret_value );
   else  // raise exception when H5Aiterate returns a negative value
   {
      throw AttributeIException();
   }
}

// Determines the number of attributes attached to 
int H5Object::getNumAttrs() const
{
   int num_attrs = H5Aget_num_attrs( id );
   if( num_attrs < 0 )
   {
      throw AttributeIException();
   }
   else
      return( num_attrs );
}

// Removes the named attribute from this object.
void H5Object::removeAttr( const char* name ) const
{
   herr_t ret_value = H5Adelete( id, name );
   if( ret_value < 0 )
   {
      throw AttributeIException();
   }
}
void H5Object::removeAttr( const string& name ) const
{
   removeAttr( name.c_str() );
}

// Flushes all buffers associated with a file to disk.
void H5Object::flush(H5F_scope_t scope ) const
{
   herr_t ret_value = H5Fflush( id, scope );
   if( ret_value < 0 )
   {
      throw FileIException();
   }
}

// each subclass' destructor calls the template function resetIdComponent()
// to reset the corresponding IdComponent object and close the HDF5 object
// where appropriate.
H5Object::~H5Object() {}

#ifndef H5_NO_NAMESPACE
} // end namespace
#endif