Data Access Library (DAL)
 All Classes Functions Variables Typedefs Friends
Public Types | Public Member Functions | Public Attributes
dal::Attribute< T > Class Template Reference

#include <Attribute.h>

Inheritance diagram for dal::Attribute< T >:
dal::AttributeBase dal::Node

List of all members.

Public Types

typedef T value_type

Public Member Functions

 Attribute (Group &parent, const std::string &name)
 Attribute (const Attribute &other)
virtual ~Attribute ()
Attribute< T > & create ()
get () const
void set (const T &value)
virtual bool valid () const

Public Attributes

AttributeValue< T > value

Detailed Description

template<typename T>
class dal::Attribute< T >

Represents an attribute containing a scalar or a string.

Python example:

     # Create a new HDF5 file with some string attribute
     >>> f = File("example.h5", File.CREATE)
     >>> a = AttributeString(f, "EXAMPLE_STRING")

     # Because we are creating the file, the attribute does initially not exist
     >>> a.value is None
     True

     # Once we set the value, the attribute exists and can be read
     >>> a.value = "hello world!"
     >>> a.value
     'hello world!'

     # The attribute can also be removed
     >>> del a.value
     >>> a.value is None
     True

     # Low-level functions allow finer control ...
     >>> a.exists()
     False
     >>> a.create() # returns a
     <...>
     >>> a.exists()
     True
     >>> a.set("hello world!")
     >>> a.get()
     'hello world!'
     >>> a.remove()

     # ... but raise errors if used incorrectly
     >>> a.exists()
     False
     >>> a.set("hello world!")
     Traceback (most recent call last):
     HDF5Exception: Could not open attribute
     >>> a.get()
     Traceback (most recent call last):
     HDF5Exception: Could not open attribute
     >>> a.remove()
     Traceback (most recent call last):
     HDF5Exception: Could not delete element

     # Clean up:
     >>> import os
     >>> os.remove("example.h5")

Constructor & Destructor Documentation

template<typename T>
dal::Attribute< T >::Attribute ( Group parent,
const std::string &  name 
) [inline]

Represent an attribute called `name' within group `parent'.

template<typename T>
virtual dal::Attribute< T >::~Attribute ( ) [inline, virtual]

Destruct an Attribute object.


Member Function Documentation

template<typename T>
Attribute<T>& dal::Attribute< T >::create ( )

Creates this attribute. Returns a reference to the attribute, so you can create and set in one expression: attr.create().set(val).

template<typename T>
T dal::Attribute< T >::get ( ) const

Returns the value of this attribute, retrieved from the HDF5 file. An exception is thrown if the attribute does not exist.

template<typename T>
void dal::Attribute< T >::set ( const T &  value)

Stores the value of this attribute in the HDF5 file. An exception is thrown if the attribute does not exist.

template<typename T>
virtual bool dal::Attribute< T >::valid ( ) const [virtual]

Validates the attribute by checking whether it exists, and whether it can be read using the type defined by this object.

Python example:

     # Create a new HDF5 file with some string attribute
     >>> f = File("example.h5", File.CREATE)

     # Refer to a string attribute
     >>> a = AttributeString(f, "EXAMPLE_STRING")

     # The attribute has not been written yet, so it's not valid
     >>> a.exists()
     False
     >>> a.valid()
     False

     # Create and set the attribute
     >>> a.value = "hello world"

     # This attribute is valid because it exists and the type we specified
     # matches the type stored on disk.
     >>> a.valid()
     True

     # Read the same value as an integer
     >>> b = AttributeUInt(f, "EXAMPLE_STRING")
     >>> b.valid()
     False

     # Clean up:
     >>> import os
     >>> os.remove("example.h5")

Reimplemented from dal::AttributeBase.


The documentation for this class was generated from the following file:
 All Classes Functions Variables Typedefs Friends