Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

Any Class Reference

Contains an arbitrary type. More...

#include <cxxtools/any.h>

List of all members.

Public Member Functions

template<typename T>
 Any (const T &type)
 Construct with value.
 Any ()
 Default constructor.
Anyassign (Value *value)
 Assigns an abstract value.
 Any (const Any &val)
 Copy constructor.
 ~Any ()
 Destructor.
void clear ()
 Clear content.
bool empty () const
 Check if empty.
Anyswap (Any &other)
 Swap values.
const char * typeName () const
 Check typename of assigned type.
const std::type_info & type () const
 Returns type info of assigned type.
template<typename T>
Anyoperator= (const T &rhs)
 Assign value.
Anyoperator= (const Any &rhs)
 Assign value of other Any.
template<typename T>
bool operator== (const T &value) const
 Check if equal.
bool operator== (const Any &a) const
 Check if equal.
bool operator!= (const Any &a) const
 Check if inequal.
bool operator< (const Any &a) const
 Check if less.
const Any::Value * value () const
Any::Value * value ()

Friends

template<typename T>
any_cast (const Any &)
 Get contained value.

Classes

class  BasicValue
class  Value


Detailed Description

Any can contain any other type that is default- and copy constructible and less-than and equality comparable. When a value is assigned to an Any a copy is made, just like when a type is inserted in a standard C++ container. The contained type can be accessed via cxxtools::any_cast<>. It is only possible to get the contained value if the type matches

            Any a = 5;
            int i = any_cast<int>( a );    // i is 5 now
            float f = any_cast<float>( a ) // throws std::bad_cast

Anys can be compared by the contained types and values. Two Anys are considered equal when the contained values are equal and of the same type. A special case is less-than comparison, when the contained types are different. std::type_info::before will be used to decide which Any is less.

            Any a = 6;
            Any b = 6;
            Any c = '6';
            Any d = 1;

            // true, same type, same value
            a == b;

            // false, different types
            b == c;

            // true, same type and less
            d \< a;

            // implementation dependent
            d \< c;


Constructor & Destructor Documentation

Any ( const T &  type  ) 

Constructs the Any from an value of arbitrary type. The type to be assigned must be copy-constructible. Memory is allocated to store the value. If an exception is thrown during construction, the Any will be empty and the exception is porpagated.

Parameters:
type Value to assign

Any (  ) 

Constructs an empty any. No memory needs to be allocated for empty Anys.

Any ( const Any val  ) 

Constructs the Any by copying the value of the other Any. It is legal to assign an empty Any. If an exception is thrown during construction, the Any will be empty and the exception is porpagated.

Parameters:
val Any to assign

~Any (  ) 

Deallocates the memory needed to hold the value. This will also destruct the contained type.


Member Function Documentation

void clear (  ) 

Removes the stored type resulting in a destructor call for the stored type. All memory required to hold the value is deallocated.

bool empty (  )  const

Returns true if no value has been assigned, false otherwise.

Returns:
True if empty

Any& swap ( Any other  ) 

The member function swaps the assigned values between *this and right. No exceptions are thrown, and no memory needs to be allocated.

Parameters:
other Other any to swap value
Returns:
self reference

const char* typeName (  )  const

Returns the typename of the currently assigned type. If the Any is empty "void" is returned.

Returns:
Typename

const std::type_info& type (  )  const

Returns the std::type_info of the currently assigned type. If the Any is empty the type_info of void is returned.

Returns:
Type info

Any& operator= ( const T &  rhs  ) 

Assigns a value of an arbitrary type. The type to be assigned must be copy-constructible. Memory is allocated to store the value. If an exception is thrown during construction, the Any will remain unaltered and the exception is porpagated.

Parameters:
rhs Value to assign

Any& operator= ( const Any rhs  ) 

Assignes the value of another Any by copying the value of the other Any. It is legal to assign an empty Any. If an exception is thrown during assignment, the Any will remain unchanged and the exception is porpagated.

Parameters:
rhs Any to assign

bool operator== ( const T &  value  )  const

Returns true if the contained type and the passed type are equal and have equal values.

Returns:
True if equal

bool operator== ( const Any a  )  const

Returns true if the contained types are equal and have equal values.

Returns:
True if equal

bool operator!= ( const Any a  )  const

Returns true if the contained types have different values or if the conatained types are different.

Returns:
True if different

bool operator< ( const Any a  )  const

Returns true if the value of the contained type is less than the contained value of the other Any. If the contained types are different type_info::before decides which Any is less.

Returns:
True if less


Friends And Related Function Documentation

T any_cast ( const Any any  )  [friend]

This function is used to get the contained value from an Any. It is not possible to get a float out of an Any if the contained value is an int, but the typeid's must match. It is, however, possible to get a const reference to the contained type.

Parameters:
any Any to read to
Returns:
contained value
Exceptions:
std::bad_cast on type mismatch

Copyright © 2008 The Tntnet Development Team
Tntnet 1.6