Header <boost/any.hpp>int and string, and freely convert between them,
for instance interpreting 5 as "5" or vice-versa.
Such types are common in scripting and other interpreted languages.
boost::lexical_cast supports such conversion functionality.
5 is held strictly as an int and is not implicitly
convertible either to "5" or to 5.0. Their indifference to
interpretation but awareness of type effectively makes them safe, generic containers of single
values, with no scope for surprises from ambiguous conversions.
void *, which offers plenty of scope for surprising, undefined behavior.
any class (based on the class of the same name described in
"Valued Conversions"
by Kevlin Henney, C++ Report 12(7), July/August 2000) is a variant value type based on the second
category. It supports copying of any value type and safe checked extraction of that value strictly
against its type. A similar design, offering more appropriate operators, can be used for a generalized
function adaptor, any_function, a generalized iterator adaptor, any_iterator,
and other object types that need uniform runtime treatment but support only compile-time template
parameter conformance.
any objects:
#include <list>
#include <boost/any.hpp>
typedef std::list<boost::any> many;
void append_int(many & values, int value)
{
boost::any to_append = value;
values.push_back(to_append);
}
void append_string(many & values, const std::string & value)
{
values.push_back(value);
}
void append_char_ptr(many & values, const char * value)
{
values.push_back(value);
}
void append_any(many & values, const boost::any & value)
{
values.push_back(value);
}
void append_nothing(many & values)
{
values.push_back(boost::any());
}
The following predicates follow on from the previous definitions and demonstrate
the use of queries on any objects:
bool is_empty(const boost::any & operand)
{
return operand.empty();
}
bool is_int(const boost::any & operand)
{
return operand.type() == typeid(int);
}
bool is_char_ptr(const boost::any & operand)
{
try
{
any_cast<const char *>(operand);
return true;
}
catch(const boost::bad_any_cast &)
{
return false;
}
}
bool is_string(const boost::any & operand)
{
return any_cast<std::string>(&operand);
}
void count_all(many & values, std::ostream & out)
{
out << "#empty == "
<< std::count_if(values.begin(), values.end(), is_empty) << std::endl;
out << "#int == "
<< std::count_if(values.begin(), values.end(), is_int) << std::endl;
out << "#const char * == "
<< std::count_if(values.begin(), values.end(), is_char_ptr) << std::endl;
out << "#string == "
<< std::count_if(values.begin(), values.end(), is_string) << std::endl;
}
The following type, patterned after the OMG's Property Service, defines namevalue
pairs for arbitrary value types:
struct property
{
property();
property(const std::string &, const boost::any &);
std::string name;
boost::any value;
};
typedef std::list<property> properties;
The following base class demonstrates one approach to runtime polymorphism based callbacks that also
require arbitrary argument types. The absence of virtual member templates requires that
different solutions have different trade-offs in terms of efficiency, safety, and generality. Using
a checked variant type offers one approach:
class consumer
{
public:
virtual void notify(const any &) = 0;
...
};
"boost/any.hpp":
#include <typeinfo>
namespace boost
{
class any;
class bad_any_cast;
template<typename ValueType>
ValueType any_cast(const any &);
}
Test harness defined in "any_test.cpp".
As the emphasis of a value lies in its state not its identity, values can be copied and typically assigned one to another, requiring the explicit or implicit definition of a public copy constructor and public assignment operator. Values typically live within other scopes, i.e. within objects or blocks, rather than on the heap. Values are therefore normally passed around and manipulated directly as variables or through references, but not as pointers that emphasize identity and indirection.
The specific requirements on value types to be used in an any are:
any
class any
{
public: // structors
any();
any(const any &);
template<typename ValueType>
any(const ValueType &);
~any();
public: // modifiers
any & swap(any &);
any & operator=(const any &);
template<typename ValueType>
any & operator=(const ValueType &);
public: // queries
bool empty() const;
const std::type_info & type() const;
private: // representation
...
};
A class whose instances can hold instances of any type that satisfies
ValueType requirements: effectively an unbounded union type.
Note that any itself satisfies ValueType requirements with
assignment.
Default constructor that sets new instance to empty.any::any();
Copy constructor that copies content ofany::any(const any & other);
other into new instance, so that
any content is equivalent in both type and value to the content of other,
or empty if other is empty. May fail with a std::bad_alloc exception
or any exceptions arising from the copy constructor of the contained type.
Templated converting constructor makes a copy oftemplate<typename ValueType> any::any(const ValueType & value);
value, so that the initial
content of the new instance is equivalent in both type and value to value.
May fail with a std::bad_alloc exception or any exceptions arising from the
copy constructor of
the contained type.
Non-throwing destructor that releases any and all resources used in management of instance.any::~any();
Non-throwing exchange of the contents ofany & swap(any & rhs);
*this and rhs.
Copy assignment operator that copies content ofany & operator=(const any & rhs);
rhs into current instance, discarding
previous content, so that the new content is equivalent in both type and value to the content of
rhs, or empty if rhs.empty(). May fail with a std::bad_alloc
exception or any exceptions arising from the copy constructor of the contained type.
Assignment satisfies the strong guarantee of exception safety.
Templated assignment operator makes a copy oftemplate<typename ValueType> any & operator=(const ValueType & rhs);
rhs, discarding previous content,
so that the new content of is equivalent in both type and value to rhs. May fail with a
std::bad_alloc exception or any exceptions arising from the copy constructor of
the contained type. Assignment satisfies the strong guarantee of exception safety.
Test for emptiness, returningbool empty() const;
true if instance is empty, otherwise false.
Returns theconst std::type_info & type() const;
typeid of the contained value if instance is non-empty, otherwise
typeid(void) returned. Useful for querying against types known either at
compile time or only at runtime.
bad_any_cast
class bad_any_cast : public std::bad_cast
{
public:
virtual const char * what() const;
};
The exception thrown in the event of a failed any_cast of
an any value.
any_castCustom keyword cast for extracting a value of a given type from antemplate<typename ValueType> ValueType any_cast(const any & operand); template<typename ValueType> const ValueType * any_cast(const any * operand); template<typename ValueType> ValueType * any_cast(any * operand);
any. If passed a pointer, it returns a similarly qualified
pointer to the value content if successful, otherwise null is returned. If passed a value or
reference, it returns a copy of the value content if successful, otherwise a
bad_any_cast exception is thrown.