Data Access Library (DAL)
/home/amesfoort/DAL/dal/hdf5/Attribute.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_ATTRIBUTE_H
00018 #define DAL_ATTRIBUTE_H
00019 
00020 #include <cstdlib>
00021 #include <string>
00022 #include <vector>
00023 #include <ostream>
00024 #include <hdf5.h>
00025 #include "types/h5typemap.h"
00026 #include "types/versiontype.h"
00027 #include "Node.h"
00028 
00029 namespace dal {
00030 
00031 class Group;
00032 
00036 class AttributeBase: public Node {
00037 public:
00041   AttributeBase( Group &parent, const std::string &name ): Node(parent, name) {}
00042 
00046   virtual ~AttributeBase() {}
00047 
00051   virtual bool exists() const;
00052 
00057   void remove() const;
00058 
00062   size_t size() const;
00063 
00068   virtual bool valid() const;
00069 };
00070 
00071 #ifndef SWIG
00072 
00073 template<typename T> class Attribute;
00074 
00090 template<typename T> class AttributeValue {
00091 public:
00095   AttributeValue<T>& operator=( const T& value );
00096 
00100   AttributeValue<T>& operator=( const AttributeValue<T>& value );
00101 
00105   operator T() const;
00106 
00110   void del() const;
00111 
00112 private:
00113   // Do not allow copying, as attr of the copy might get out of scope
00114   AttributeValue( const AttributeValue& );
00115 
00116   AttributeValue( Attribute<T> &attr ): attr(attr) {}
00117 
00118   Attribute<T> &attr;
00119 
00120   // Only Attribute<T> can create instances
00121   friend class Attribute<T>;
00122 };
00123 
00124 template<typename T> std::ostream& operator<<(std::ostream &out, const AttributeValue<T> &val);
00125 
00126 #endif /* !SWIG */
00127 
00181 template<typename T> class Attribute: public AttributeBase {
00182 public:
00183   typedef T value_type;
00184 
00188   Attribute( Group &parent, const std::string &name ): AttributeBase(parent, name), value(*this) {}
00189 
00190   Attribute( const Attribute &other ): AttributeBase(other), value(*this) {}
00191 
00195   virtual ~Attribute() {}
00196 
00201   Attribute<T>& create();
00202 
00207   T get() const;
00208 
00213   void set( const T &value );
00214 
00251   virtual bool valid() const;
00252 
00253   AttributeValue<T> value;
00254 };
00255 
00259 template<typename T> class Attribute< std::vector<T> >: public AttributeBase {
00260 public:
00261   typedef std::vector<T> value_type;
00262 
00266   Attribute( Group &parent, const std::string &name ): AttributeBase(parent, name), value(*this) {}
00267 
00268   Attribute( const Attribute &other ): AttributeBase(other), value(*this) {}
00269 
00273   virtual ~Attribute() {}
00274 
00280   Attribute< std::vector<T> > &create( size_t length = 0 );
00281 
00286   std::vector<T> get() const;
00287 
00292   void set( const std::vector<T> &value );
00293 
00298   virtual bool valid() const;
00299 
00300   AttributeValue< std::vector<T> > value;
00301 };
00302 
00303 }
00304 
00305 #include "Attribute.tcc"
00306 
00307 #endif
00308 
 All Classes Functions Variables Typedefs Friends