any


Motivation

There are times when a generic (in the sense of general as opposed to template-based programming) type is needed: variables that are truly variable, accommodating values of many other more specific types rather than C++'s normal strict and static types. We can distinguish three basic kinds of generic type:
  1. Converting types that can hold one of a number of possible value types, e.g. 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.
  2. Discriminated types that contain values of different types but do not attempt conversion between them, i.e. 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.
  3. Indiscriminate types that can refer to anything but are oblivious to the actual underlying type, entrusting all forms of access and interpretation to the programmer. This niche is dominated by void *, which offers plenty of scope for surprising, undefined behavior.
The any class ("Valued Conversions", 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.


Examples

The following code demonstrates the syntax for using implicit conversions and copying to write to any objects:
using namespace std;
using namespace boost;

list<any> values;

void append_int(int value)
{
    any to_append = value;
    values.push_back(to_append);
}

void append_string(const string & value)
{
    values.push_back(value);
}

void append_char_ptr(const char * value)
{
    values.push_back(value);
}

void append_any(const any & value)
{
    values.push_back(value);
}

void append_nothing()
{
    values.push_back(any());
}
The following predicates follow on from the previous definitions and demonstrate the use of queries on any objects:
bool is_empty(const any & operand)
{
    return operand;
}

bool is_int(const any & operand)
{
    return operand.type() == typeid(int);
}

bool is_char_ptr(const any & operand)
{
    const char * unused;
    return operand.copy_to(unused);
}

bool is_string(const any & operand)
{
    return operand.to_ptr<string>();
}

void count_all()
{
    cout << "#empty == "
         << count_if(values.begin(), values.end(), is_empty) << endl;
    cout << "#int == "
         << count_if(values.begin(), values.end(), is_int) << endl;
    cout << "#const char * == "
         << count_if(values.begin(), values.end(), is_char_ptr) << endl;
    cout << "#string == "
         << count_if(values.begin(), values.end(), is_string) << endl;
}
The following type, patterned after the OMG's Property Service, defines name–value pairs for arbitrary value types:
struct property
{
    property();
    property(const std::string &, const boost::any &);

    std::string name;
    boost::any value;
};

typedef 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;
    ...
};

Synopsis

Dependencies and library features defined in "any.hpp":
#include <typeinfo>

namespace boost
{
    class any;
    class bad_any_cast;
    template<typename ValueType>
      ValueType any_cast(const any &);
}
Test harness defined in "test-any.cpp".


ValueType requirements

Values are strongly informational objects for which identity is not significant, i.e. the focus is principally on their state content and any behavior organized around that. Another distinguishing feature of values is their granularity: normally fine-grained objects representing simple concepts in the system such as quantities.

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

    operator const void *() const;
    const std::type_info & type() const;
    template<typename ValueType>
      bool copy_to(ValueType &) const;
    template<typename ValueType>
      ValueType * to_ptr();
    template<typename ValueType>
      const ValueType * to_ptr() const;

private: // representation
    ...
};
An exception-safe 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.


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.


any_cast

template<typename ValueType>
  ValueType any_cast(const any & operand);
Custom keyword cast for extracting a value of a given type from an any. Returns a copy of the extracted result if successful, otherwise a bad_any_cast exception is thrown. any_cast is used normally for conversions that are expected to succeed.


Portability

To date the code and test harness have been compiled and tested successfully in its intended form using Borland C++ 5.5. Member template problems have meant some workarounds and a slightly restricted interface for compilation under Microsoft Visual C++ 6.0, so that any::to_ptr is not supported.


Future directions

Proper integration with boost directory structure and use of <boost/config.hpp>.


© Copyright Kevlin Henney, 2000