[ 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)
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 | release ()
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.release();
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() [1/3]

Any | ( |  | ) |

Construct empty 'Any' object.

## ◆ Any() [2/3]

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

Construct 'Any' object holding the given value.

## ◆ Any() [3/3]

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

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

## Member Function Documentation

## ◆ operator=() [1/2]

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

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

## ◆ operator=() [2/2]

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

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

## ◆ release()

void release | ( |  | ) |

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

## ◆ swap()

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

Exchange the value of this object with other's.

## ◆ operator==()

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.

## ◆ operator!=()

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

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

## ◆ operator bool()

operator bool | ( |  | ) | const

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

## ◆ empty()

bool empty | ( |  | ) | const

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

## ◆ is_type()

bool is_type | ( |  | ) | const

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

## ◆ is_readable()

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.

## ◆ get() [1/2]

T& get | ( |  | ) |

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

## ◆ get() [2/2]

T const& get | ( |  | ) | const

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

## ◆ read()

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

## ◆ swap

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