casacore
HDF5Object.h
Go to the documentation of this file.
1 //# HDF5Object.h: An abstract base class representing an HDF5 object
2 //# Copyright (C) 2008
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef CASA_HDF5OBJECT_H
29 #define CASA_HDF5OBJECT_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
33 #include <casacore/casa/BasicSL/String.h>
34 
35 //# Define hid_t and hsize_t if not defined (thus if HDF5 disabled).
36 //# They should be the same as used by HDF5.
37 //# This is checked by functions check_hid_t and check_hsize_t.
38 #ifdef HAVE_HDF5
39 # include <hdf5.h>
40 #else
41  typedef int hid_t;
42  typedef unsigned long long hsize_t;
43 #endif
44 
45 
46 namespace casacore { //# NAMESPACE CASACORE - BEGIN
47 
48  // Define 2 functions to check that hid_t and hsize_t are mapped correctly.
49  // They are called by the constructor, so the compiler will scream if
50  // incorrect.
51  // <group>
52  void throwInvHDF5();
53  inline void check_hid_t (int) {}
54  template<typename T> inline void check_hid_t (T) {throwInvHDF5();}
55  inline void check_hsize_t (unsigned long long) {}
56  template<typename T> inline void check_hsize_t (T) {throwInvHDF5();}
57  // </group>
58 
59 
60  // <summary>
61  // An abstract base class representing an HDF5 object
62  // </summary>
63 
64  // <use visibility=export>
65 
66  // <reviewed reviewer="" date="" tests="tHDF5Dataset.cc">
67  // </reviewed>
68 
69  // <synopsis>
70  // This class wraps a basic HDF5 object. It offers several benefits:
71  // <ul>
72  // <li> The most important is resource management. In case of an exception,
73  // the object's hid will automatically be closed by the destructor.
74  // <li> It acts as the base class for the basic objects file, group,
75  // and dataset.
76  // <li> An HDF5 hid is a kind of pointer and should not be copied.
77  // These classes forbid making a copy, but make it possible to use
78  // them in a shared pointer context.
79  // </ul>
80  // </synopsis>
81 
82  class HDF5Object
83  {
84  public:
85  // Default constructor sets to invalid hid.
87  : itsHid(-1)
88  {
89  check_hid_t (hid_t(0));
90  check_hsize_t (hsize_t(0));
91  }
92 
93  // The destructor in a derived class should close the hid appropriately.
94  virtual ~HDF5Object();
95 
96  // Check if there is HDF5 support compiled in.
97  static Bool hasHDF5Support();
98 
99  // Close the hid if valid.
100  virtual void close() = 0;
101 
102  // Is it a valid hid?
103  bool isValid() const
104  { return itsHid >= 0; }
105 
106  // Get the hid.
107  hid_t getHid() const
108  { return itsHid; }
109 
110  // Convert automatically to hid_t.
111  operator hid_t() const
112  { return itsHid; }
113 
114  // Get or set the name.
115  // <group>
116  void setName (const String& name)
117  { itsName = name; }
118  const String& getName() const
119  { return itsName; }
120  // </group>
121 
122  // If no HDF5, throw an exception that HDF5 is not supported.
123  static void throwNoHDF5();
124 
125  protected:
126  // Set the hid.
127  void setHid (hid_t hid)
128  { itsHid = hid; }
129 
130  // Clear the hid (set to invalid).
131  void clearHid()
132  { itsHid = -1; }
133 
134  private:
135  //# Data members
136  hid_t itsHid;
138 
139  private:
140  // Copy constructor cannot be used.
141  HDF5Object (const HDF5Object& that);
142  // Assignment cannot be used.
143  HDF5Object& operator= (const HDF5Object& that);
144  };
145 
146 
147 }
148 
149 #endif
virtual void close()=0
Close the hid if valid.
static Bool hasHDF5Support()
Check if there is HDF5 support compiled in.
static void throwNoHDF5()
If no HDF5, throw an exception that HDF5 is not supported.
void setHid(hid_t hid)
Set the hid.
Definition: HDF5Object.h:127
void check_hsize_t(unsigned long long)
Definition: HDF5Object.h:55
void setName(const String &name)
Get or set the name.
Definition: HDF5Object.h:116
virtual ~HDF5Object()
The destructor in a derived class should close the hid appropriately.
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
void clearHid()
Clear the hid (set to invalid).
Definition: HDF5Object.h:131
HDF5Object()
Default constructor sets to invalid hid.
Definition: HDF5Object.h:86
const String & getName() const
Definition: HDF5Object.h:118
void check_hid_t(int)
Definition: HDF5Object.h:53
An abstract base class representing an HDF5 object.
Definition: HDF5Object.h:82
bool isValid() const
Is it a valid hid?
Definition: HDF5Object.h:103
String: the storage and methods of handling collections of characters.
Definition: String.h:223
void throwInvHDF5()
Define 2 functions to check that hid_t and hsize_t are mapped correctly.
HDF5Object & operator=(const HDF5Object &that)
Assignment cannot be used.
hid_t getHid() const
Get the hid.
Definition: HDF5Object.h:107
this file contains all the compiler specific defines
Definition: mainpage.dox:28