Data Access Library (DAL)
/home/amesfoort/DAL/dal/hdf5/types/hid_gc.h
00001 /* Copyright 2011-2012  ASTRON, Netherlands Institute for Radio Astronomy
00002  * This file is part of the Data Access Library (DAL).
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either 
00007  * version 3 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public 
00015  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #ifndef DAL_HID_GC_H
00018 #define DAL_HID_GC_H
00019 
00020 #include <hdf5.h>
00021 #include <algorithm>
00022 #include "../exceptions/exceptions.h"
00023 
00024 namespace dal {
00025 
00027 class hid_gc
00028 {
00029 public:
00030   // allow deference of actual construction to operator=
00031   hid_gc(): hid(0), closefunc(0) {}
00032 
00033   hid_gc(hid_t hid, hid_t (*closefunc)(hid_t) = 0, const std::string &errordesc = ""): hid(hid), closefunc(closefunc) {
00034     // checking for success here greatly reduces the code base
00035     if (hid <= 0)
00036       throw HDF5Exception(errordesc);
00037 
00038     H5Iinc_ref(hid);
00039   }
00040 
00041   hid_gc( const hid_gc &other ): hid(other.hid), closefunc(other.closefunc) {
00042     if (isset()) {
00043       H5Iinc_ref(hid);
00044     }  
00045   }
00046 
00047   ~hid_gc() {
00048     if (isset() && H5Idec_ref(hid) == 1 && closefunc) {
00049       closefunc(hid);
00050     }
00051   }
00052 
00053   hid_gc &operator=( hid_gc other ) {
00054     swap(*this, other);
00055     return *this;
00056   }
00057 
00058   friend void swap( hid_gc& first, hid_gc& second ) {
00059     std::swap(first.hid,       second.hid);
00060     std::swap(first.closefunc, second.closefunc);
00061   }
00062 
00064   operator hid_t() const { return hid; }
00065 
00069   bool isset() const { return hid > 0; }
00070 
00071 private:
00072   hid_t hid;
00073   hid_t (*closefunc)(hid_t);
00074 };
00075 
00080 class hid_gc_noref
00081 {
00082 public:
00083   hid_gc_noref(hid_t hid, hid_t (*closefunc)(hid_t) = 0, const std::string &errordesc = ""): hid(hid), closefunc(closefunc) {
00084     // checking for success here greatly reduces the code base
00085     if (hid <= 0)
00086       throw HDF5Exception(errordesc);
00087   }
00088 
00089   ~hid_gc_noref() {
00090     if (isset() && closefunc) {
00091       closefunc(hid);
00092     }  
00093   }
00094 
00096   operator hid_t() const { return hid; }
00097 
00101   bool isset() const { return hid > 0; }
00102 
00103 private:
00104   // do not use
00105   hid_gc_noref( const hid_gc &other );
00106   hid_gc_noref &operator=( const hid_gc & );
00107   void swap( hid_gc& first, hid_gc& second );
00108 
00109   const hid_t hid;
00110   hid_t (*const closefunc)(hid_t);
00111 };
00112 
00113 }
00114 
00115 #endif
00116 
 All Classes Functions Variables Typedefs Friends