[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

| Any Class Reference |

Typesafe storage of arbitrary values. More...

`#include <vigra/any.hxx>`

## Public Member Functions
| Any ()
template<class T >
| Any (T const &t)
| Any (Any const &other)
void | destroy ()
bool | empty () const
template<class T >
T & | get ()
template<class T >
T const & | get () const
template<class T >
bool | is_readable () const
template<class T >
bool | is_type () const
| operator bool () const
bool | operator!= (Any const &other) const
template<class T >
Any & | operator= (T const &t)
Any & | operator= (Any const &other)
bool | operator== (Any const &other) const
template<class T >
T | read () const
void | swap (Any &other)

## Friends
void | swap (Any &l, Any &r)

## Detailed Description

Typesafe storage of arbitrary values.

Items are always stored by value, but it is of course possible to store pointers and smart pointers.

Usage:

Any a(10); // store integer '10'
assert(a.is_type<int>());
assert(a.is_convertible<int>());
// retrieve the stored value (throws when types don't match)
assert(a.get<int>() == 10);
// change the value
a = 20;
assert(a.get<int>() == 20);
// values of arithmetic types can be converted into each other
// (this is currently not implemented for other types)
assert(a.is_convewrtible<double>());
assert(a.cast<double>() == 20.0);
// delete the stored value
a.destroy();
assert(a.empty());
assert(a == false);
// store a shared_ptr
typedef std::shared_ptr<int> Ptr;
Any p(Ptr(new int(5))), q = p;
assert(*(p.get<Ptr>()) == 5);
// the addresses of the elements in p and q are the same
assert(p.get<Ptr>().get() == p.get<Ptr>().get());

## Constructor & Destructor Documentation

Any | ( |  | ) |

Construct empty 'Any' object.

Any | ( | T const & | t | ) |

Construct 'Any' object holding the given value.

Any | ( | Any const & | other | ) |

Construct 'Any' object holding a copy of other's value.

## Member Function Documentation

Any& operator= | ( | T const & | t | ) |

Assign the given value to this 'Any' object (overwrites the old value, regardless of types).

Any& operator= | ( | Any const & | other | ) |

Assign a copy of other's value to this 'Any' object (overwrites the old value, regardless of types).

void destroy | ( |  | ) |

Delete the contained object (make this 'Any' object empty).

void swap | ( | Any & | other | ) |

Exchange the value of this object with other's.

bool operator== | ( | Any const & | other | ) | const

Check if this object contains the same type and value as other. Also true if both 'Any' objects are empty.

bool operator!= | ( | Any const & | other | ) | const

Check if this object differs from other by type or value.

operator bool | ( |  | ) | const

Convert 'Any' to `false` if this object is empty, `true` otherwise.

bool empty | ( |  | ) | const

Check if this object is empty (holds no value).

bool is_type | ( |  | ) | const

Check if this object holds a value of the given type.

bool is_readable | ( |  | ) | const

Check if this object's value is convertible to the given type. At present, this only succeeds if `T` matches the stored type exactly or is an arithmetic type convertible from the stored type.

T& get | ( |  | ) |

Read-write access to the contained value. This throws an exception if the types don't match.

T const& get | ( |  | ) | const

Read-only access to the contained value. This throws an exception if the types don't match.

T read | ( |  | ) | const

By-value access to the stored value. This throws an exception if the stored type doesn't match `T` and `T` is not an arithmetic type.

## Friends And Related Function Documentation

void swap | ( | Any & | l,
|  | Any & | r
| ) |  |
| friend

Exchange the value of objects l and r.

The documentation for this class was generated from the following file:

- vigra/any.hxx

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany | html generated using doxygen and Python vigra 1.11.0 (Fri May 19 2017)