Data Access Library (DAL)
/home/amesfoort/DAL/dal/hdf5/types/h5tuple.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_H5TUPLE_H
00018 #define DAL_H5TUPLE_H
00019 
00020 #include <hdf5.h>
00021 #include <algorithm>
00022 #include "hid_gc.h"
00023 #include "../exceptions/exceptions.h"
00024 
00025 namespace dal {
00026 
00037 // A non-templated common root, required for IsDerivedFrom checking
00038 class TupleUntemplated {
00039 protected:
00040   TupleUntemplated() {} // prevent direct instantiation
00041 public:
00042   ~TupleUntemplated() {} // prevent generating a vtable by forbidding virtual functions
00043 }; 
00044 
00060 template<typename T, size_t N> class TupleBase: public TupleUntemplated {
00061 protected:
00062   TupleBase() {} // prevent direct instantiation
00063 public:
00067   typedef T type;
00068 
00072   static size_t size() { return N; }
00073 
00074   /*
00075    * Provide some array-like and vector-like functionality.
00076    */
00077 
00078   typedef T *iterator;
00079   typedef const T *const_iterator;
00080 
00081   const T& operator[](size_t index) const { return *(begin() + index); }
00082         T& operator[](size_t index)       { return *(begin() + index); }
00083 
00084   const_iterator begin() const { return reinterpret_cast<const T*>(this); }
00085         iterator begin()       { return reinterpret_cast<T*>(this); }
00086 
00087   const_iterator end() const { return begin() + size(); }
00088         iterator end()       { return begin() + size(); }
00089 
00093   std::vector<T> get() const {
00094     std::vector<T> result(size());
00095 
00096     std::copy(begin(), end(), result.begin());
00097 
00098     return result;
00099   }
00100 
00104   void set( const std::vector<T> &other ) {
00105     if (other.size() != size())
00106       throw DALValueError("Can not set a tuple using a vector of a different size");
00107 
00108     std::copy(other.begin(), other.end(), begin());
00109   }
00110 };
00111 
00136 template<typename T, size_t N> class Tuple: public TupleBase<T,N> {
00137 private:
00138   // prevent construction of generic form, as it provides no storage
00139   Tuple();
00140   ~Tuple();
00141 };
00142 
00143 template<typename T> class Tuple<T,1>: public TupleBase<T,1> {
00144 public:
00145   T first;  
00146 };
00147 
00148 template<typename T> class Tuple<T,2>: public TupleBase<T,2> {
00149 public:
00150   T first;  
00151   T second; 
00152 };
00153 
00154 template<typename T> class Tuple<T,3>: public TupleBase<T,3> {
00155 public:
00156   T first;  
00157   T second; 
00158   T third;  
00159 };
00160 
00161 template<typename T> class Tuple<T,4>: public TupleBase<T,4> {
00162 public:
00163   T first;  
00164   T second; 
00165   T third;  
00166   T fourth; 
00167 };
00168 
00172 template<typename T, size_t N> class Array: public TupleBase<T,N> {
00173 public:
00174   T elems[N];
00175 };
00176 
00177 inline hid_gc h5tupleType( hid_t element, size_t num )
00178 {
00179   hsize_t dims[1];
00180 
00181   dims[0] = num;
00182 
00183   hid_gc tuple_id(H5Tarray_create2(element, 1, dims), H5Tclose, "Could not create tuple datatype");
00184 
00185   return tuple_id;
00186 }
00187 
00188 }
00189 
00190 #endif
00191 
 All Classes Functions Variables Typedefs Friends