C++ Boost

Boost.dynamic_any

Header <boost/dynamic_any/any.hpp >

Contents

Introduction
Classes
Template class any
Template class any synopsis
Template class any constructors and destructor
Template class any operators
Class bad_extract
Class bad_extract synopsis
Functions
extract
static_extract
Examples
Hello, World!

Introduction

Template class any, operators and related functions defined in this header file.

Classes

Template class any

Template class any synopsis

namespace boost { namespace dynamic_any {

  template<class OperationList = empty_list>
  class any
  {
    public:

      typedef OperationList operation_list;

      // Template class any constructors and destructor
      any(); // throw()
      any(const any& other);
      template<class T> any(const T& value);
      ~any(); // throw()

      bool empty() const; // throw()
      const std::type_info& type() const; // throw()

      void swap(any& other); // throw()

      // Template class any operators
      any& operator=(const any& other);
      template<class T> any& operator=(const T& value);

      bool operator!() const; // logical_not
      any operator-() const;  // negate
      any& operator++();      // increment
      any operator++(int);
      any& operator--();      // decrement
      any operator--(int);

      Result operator()(Arg1, ..., ArgN) const; // function_call<Result (Arg1, ..., ArgN)>

  }; // class any

  // Extract a copy or a reference to some subobject of a value held by ref
  template<class T, class OperationList> T extract(any<OperationList>& ref);
  template<class T, class OperationList> T extract(const any<OperationList>& ref);

  // Extract a pointer to some subobject of a value held by *ptr
  template<class T, class OperationList> T* extract(any<OperationList>* ptr);
  template<class T, class OperationList> T* extract(const any<OperationList>* ptr);

  // Extract a copy or a reference to a value held by ref
  template<class T, class OperationList> T static_extract(any<OperationList>& ref);
  template<class T, class OperationList> T static_extract(const any<OperationList>& ref);

} } // namespace boost { namespace dynamic_any {

Template class any constructors and destructor

any();
Effects: Construct an object with empty content.
Postconditions: this->empty() && this->type() == typeid(void).
Throws: doesn't throw.
any(const any& other);
Effects: Construct an object x of class any<OperationList> which holds a copy of a value held by other. If other is empty then x is also empty; otherwise, if other holds a value of type T then the construction of x is equivalent to the following construction of y:
const T& content = extract<const T&>(other);
any<OperationList> y(content);
Postconditions: x.type() == other.type().
Throws: doesn't throw unless copying of other content throws or if there is no memory to make a copy.
template<class T> any(const T& value);
Requires: T is ValueType and T is valid type for every operation from OperationList. { TODO: ConceptName }
Effects: Construct an object which holds a copy of value.
Postconditions: !this->empty() && this->type() == typeid(T).
Throws: doesn't throw unless copying of value throws or if there is no memory to make a copy.
~any();
Effects: destroy an object with its content.
Throws: doesn't throw.

Template class any operators

any& operator=(const any& other);
Effects: destroy the previous content and set a copy of a value held by other as a new content.
Postconditions: this->type() == other.type().
Returns: *this.
Throws: doesn't throw unless copying of other content throws or if there is no memory to make a copy.
template<class T> any& operator=(const T& value);
Requires: T is ValueType and T is valid type for every operation from OperationList. { TODO: ConceptName }
Effects: destroy the previous content and set a copy of value as a new content.
Postconditions: !this->empty() && this->type() == typeid(T).
Returns: *this.
Throws: doesn't throw unless copying of value throws or if there is no memory to make a copy.
bool operator!() const;
Requires: OperationList contains logical_not operation; otherwise, the compilation error is issued.
Effects: for any object x of class any<OperationList> expression !x is equivalent to logical_not()(x).
Postconditions: !x == !extract<const T&>(x) if x holds a value of type T.
Returns: a result of operator! applied to x content.
Throws: bad_function_call if x has an empty content. Otherwise, it doesn't throw unless operator! applied to x content throws.
any operator-() const;
Requires: OperationList contains negate operation; otherwise, the compilation error is issued.
Effects: for any object x of class any<OperationList> expression -x is equivalent to negate()(x). If content of x has known type T this operator do the same as do_negate function:
any<OperationList> do_negate(const any<OperationList>& x)
{
    const T& content = extract<const T&>(x);
    // -content and any<OperationList> constructor may throw
    return any<OperationList>(-content);
}
Postconditions: expressions extract<R>(-x) and -extract<T>(x) should return identical objects if x holds a value of type T and R is a type of -extract<T>(x) expression.
Returns: if x holds a value of type T the result is the same as for do_negate(x) call.
Throws: bad_function_call if x has an empty content. Otherwise, it throws when do_negate(x) call throws.
any& operator++();
any operator++(int);
Requires: OperationList contains increment operation; otherwise, the compilation error is issued.
Effects: for any object x of class any<OperationList> expression ++x is equivalent to increment()(x). If content of x has known type T the effect is increment of the content: ++extract<T&>(x).
Returns: expression ++x returns a reference to x, expression x++ returns a copy of x taken before increment.
Throws: bad_function_call if x has an empty content. Otherwise, it doesn't throw unless pre-increment operator applied to x content throws.
any& operator--();
any operator--(int);
Requires: OperationList contains decrement operation; otherwise, the compilation error is issued.
Effects: for any object x of class any<OperationList> expression --x is equivalent to decrement()(x). If content of x has known type T the effect is decrement of the content: --extract<T&>(x).
Returns: expression --x returns a reference to x, expression x-- returns a copy of x taken before decrement.
Throws: bad_function_call if x has an empty content. Otherwise, it doesn't throw unless pre-decrement operator applied to x content throws.

Class bad_extract

Exception class used to report that extract function call failed.

Class bad_extract synopsis

namespace boost { namespace dynamic_any {

  class bad_extract : public std::exception
  {
    public:
      virtual const char * what() const throw()
      {
          return "boost.dynamic_any: failed extract";
      }
  };

} } // namespace boost { namespace dynamic_any {

Functions

extract

template<class T, class OperationList> T extract(any<OperationList>& ref);
template<class T, class OperationList> T extract(const any<OperationList>& ref);
Requires: T is a complete type or a reference to complete type. To prevent casting away constness, second function also requires that T is not a reference to non-const type.
Returns:
if T has type cv U& and ref holds a value of type V, which, let assume can be accessed through
if T has type cv U& and ref holds a value of type V and U is a public unambiguous base of V, the return is a reference to subobject of type U of a value held by ref; otherwise,
if T is not a reference, the return is T(extract<const T&>(ref));
Throws: bad_extract if T(extract<const T&>(ref)); otherwise, if T is cv V& and ref holds a value of type Y, reference to that value if Y and V are same types, or, if V is a public unambiguous base of Y, a reference to subobject of type V of a value held by ref.

extract

template<class T, class OperationList> T* extract(any<OperationList>* ptr);
template<class T, class OperationList> T* extract(const any<OperationList>* ptr);

static_extract

template<class T, class OperationList> T static_extract(any<OperationList>& ref);
template<class T, class OperationList> T static_extract(const any<OperationList>& ref);

Examples

Hello, World!

Demonstrates how to print

#include <string>
#include <iostream>
#include <boost/mpl/list.hpp>
#include <boost/dynamic_any/any.hpp>
#include <boost/dynamic_any/operations.hpp>

typedef boost::mpl::list<boost::dynamic_any::to_ostream> print_op;
typedef boost::dynamic_any::any<print_op> printable_any;

int main()
{
    using boost::dynamic_any::extract; // for broken compilers

    printable_any a(std::string("Hello"));
    std::cout << a;

    a = ',';
    std::cout << a;

    char& c = extract<char&>(a);
    c = ' ';
    std::cout << a;

    a = (const char *)"World!";
    
    std::cout << a << '\n';
}


Revised 23 March, 2003

© Copyright Alexander Nasonov 2002-2003. All Rights Reserved.