casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
HDF5Object.h
Go to the documentation of this file.
00001 //# HDF5Object.h: An abstract base class representing an HDF5 object
00002 //# Copyright (C) 2008
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: HDF5Object.h 20739 2009-09-29 01:15:15Z Malte.Marquarding $
00027 
00028 #ifndef CASA_HDF5OBJECT_H
00029 #define CASA_HDF5OBJECT_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 #include <casa/BasicSL/String.h>
00034 
00035 //# Define hid_t and hsize_t if not defined (thus if HDF5 disabled).
00036 //# They should be the same as used by HDF5.
00037 //# This is checked by functions check_hid_t and check_hsize_t.
00038 #ifdef HAVE_HDF5
00039 # include <hdf5.h>
00040 #else 
00041   typedef int                hid_t;
00042   typedef unsigned long long hsize_t;
00043 #endif
00044 
00045 
00046 namespace casa { //# NAMESPACE CASA - BEGIN
00047 
00048   // Define 2 functions to check that hid_t and hsize_t are mapped correctly.
00049   // They are called by the constructor, so the compiler will scream if
00050   // incorrect.
00051   // <group>
00052   void throwInvHDF5();
00053   inline void check_hid_t (int) {}
00054   template<typename T> inline void check_hid_t (T) {throwInvHDF5();}
00055   inline void check_hsize_t (unsigned long long) {}
00056   template<typename T> inline void check_hsize_t (T) {throwInvHDF5();}
00057   // </group>
00058 
00059 
00060   // <summary>
00061   // An abstract base class representing an HDF5 object
00062   // </summary>
00063 
00064   // <use visibility=export>
00065 
00066   // <reviewed reviewer="" date="" tests="tHDF5Dataset.cc">
00067   // </reviewed>
00068 
00069   // <synopsis>
00070   // This class wraps a basic HDF5 object. It offers several benefits:
00071   // <ul>
00072   //  <li> The most important is resource management. In case of an exception,
00073   //       the object's hid will automatically be closed by the destructor.
00074   //  <li> It acts as the base class for the basic objects file, group,
00075   //       and dataset.
00076   //  <li> An HDF5 hid is a kind of pointer and should not be copied.
00077   //       These classes forbid making a copy, but make it possible to use
00078   //       them in a shared pointer context.
00079   // </ul>
00080   // </synopsis>
00081 
00082   class HDF5Object
00083   {
00084   public: 
00085     // Default constructor sets to invalid hid.
00086     HDF5Object()
00087     : itsHid(-1)
00088     {
00089       check_hid_t   (hid_t(0));
00090       check_hsize_t (hsize_t(0));
00091     }
00092 
00093     // The destructor in a derived class should close the hid appropriately.
00094     virtual ~HDF5Object();
00095 
00096     // Check if there is HDF5 support compiled in.
00097     static Bool hasHDF5Support();
00098 
00099     // Close the hid if valid.
00100     virtual void close() = 0;
00101 
00102     // Is it a valid hid?
00103     bool isValid() const
00104       { return itsHid >= 0; }
00105 
00106     // Get the hid.
00107     hid_t getHid() const
00108       { return itsHid; }
00109 
00110     // Convert automatically to hid_t.
00111     operator hid_t() const
00112       { return itsHid; }
00113 
00114     // Get or set the name.
00115     // <group>
00116     void setName (const String& name)
00117       { itsName = name; }
00118     const String& getName() const
00119       { return itsName; }
00120     // </group>
00121 
00122     // If no HDF5, throw an exception that HDF5 is not supported.
00123     static void throwNoHDF5();
00124 
00125   protected:
00126     // Set the hid.
00127     void setHid (hid_t hid)
00128       { itsHid = hid; }
00129 
00130     // Clear the hid (set to invalid).
00131     void clearHid()
00132       { itsHid = -1; }
00133 
00134   private:
00135     //# Data members
00136     hid_t  itsHid;
00137     String itsName;
00138 
00139   private:
00140     // Copy constructor cannot be used.
00141     HDF5Object (const HDF5Object& that);
00142     // Assignment cannot be used.
00143     HDF5Object& operator= (const HDF5Object& that);
00144   };
00145 
00146 
00147 }
00148 
00149 #endif