// Based on boost::any by Kevlin Henney

#ifndef ANY_POINTER_INCLUDED
#define ANY_POINTER_INCLUDED

#include <algorithm>
#include <typeinfo>

#include <iostream>

#include "boost/config.hpp"
#include <boost/throw_exception.hpp>
#include <boost/type_traits/is_pointer.hpp>
#include <boost/function.hpp>
#if defined(__APPLE_CC__)
#include "/usr/include/gcc/darwin/3.3/c++/cxxabi.h"
#endif
#if 0
#include <boost/type_traits/is_same.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/mpl/not.hpp>
#endif


namespace __g
{
#if 0
  using boost::enable_if;
  using boost::mpl::not_;
  using boost::is_same;
#endif

  // Had to pull in boost::any to allow for conversion from
  // any to any_pointer.

  class any;           // T
  class any_pointer;   // T*
  class any_reference; // T&
  class any_ptr_ref;   // T*&

  class any_const_pointer;   // T const*
  class any_const_reference; // T const&
  class any_ptr_const_ref;   // T* const&
  class any_const_ptr_const_ref;   // T const* const&
  class any_const_ptr_ref;   // T const*&
  
  template <class T>
  class to_type { };


  // ======================================================================

  class any
  {
  public: // structors

    any()
      : content(0)
    {
    }

    any(const any_pointer& ap);
    any(const any_const_pointer& ap);
    any(const any_reference& ap);
    any(const any_const_reference& ap);
    any(const any_ptr_ref& ap);
    any(const any_const_ptr_ref& ap);
    any(const any_const_ptr_const_ref& ap);
    any(const any_ptr_const_ref& ap);

    template<typename ValueType>
    any(const ValueType & value)
      : content(new holder<ValueType>(value))
    {
    }

    any(const any & other);

    ~any();

  public: // modifiers

    any & swap(any & rhs)
    {
      std::swap(content, rhs.content);
      return *this;
    }

    template<typename ValueType>
    any & operator=(const ValueType & rhs)
    {
      any(rhs).swap(*this);
      return *this;
    }

    any & operator=(const any & rhs)
    {
      any(rhs).swap(*this);
      return *this;
    }

  public: // queries

    bool empty() const
    {
      return !content;
    }

    const std::type_info & type() const;

  public: // types (public so any_cast can be non-friend)

    class placeholder;

    template<typename ValueType>
    class holder;


  public:

    placeholder * content;

  };

  inline any* new_any_array(int n, boost::function<any()> dcons) {
    any* a = new any[n];
    for (int i = 0; i != n; ++i)
      a[i] = dcons();
    return a;
  }

  // ======================================================================

  class any_reference
  {
  public: // structors

    any_reference()
      : content(0)
    {
    }

    any_reference(const any& a);
    any_reference(any& a);

    any_reference(const any_pointer& a);
    any_reference(any_pointer& a);

    any_reference(const any_ptr_ref& ap);
    any_reference(any_ptr_ref& ap);

    any_reference(const any_const_ptr_ref& ap);
    any_reference(any_const_ptr_ref& ap);

    any_reference(const any_reference & other);
    any_reference(any_reference & other);

    template<typename ValueType>
    any_reference(ValueType & value)
      : content(new holder<ValueType>(value))
    {
    }

    ~any_reference();

  public: // modifiers

    any_reference & swap(any_reference & rhs)
    {
      std::swap(content, rhs.content);
      return *this;
    }

    template<typename ValueType>
    any_reference & operator=(const ValueType & rhs);

    any_reference & operator=(const any_reference & rhs);
    any_reference & operator=(const any_const_reference & rhs);

    any_reference & operator=(const any & rhs);

  public: // queries

    bool empty() const
    {
      return !content;
    }

    const std::type_info & type() const;

  public: // types (public so any_cast can be non-friend)

    class placeholder;

    template<typename ValueType>
    class holder;


    placeholder * content;

  };

  // ======================================================================

  class any_const_reference
  {
  public: // structors

    any_const_reference()
      : content(0)
    {
    }

    any_const_reference(const any& a);
#if 0
    any_const_reference(any& a);
#endif
    any_const_reference(const any_pointer& a);
    any_const_reference(any_pointer& a);
    any_const_reference(any_ptr_ref ap);
    any_const_reference(any_ptr_const_ref ap);
    any_const_reference(any_const_ptr_const_ref ap);
    any_const_reference(any_const_ptr_ref ap);

    any_const_reference(any_const_reference const& other);
    any_const_reference(any_reference const& other);

    template<typename ValueType>
    any_const_reference(ValueType const & value
#if 0
, const typename enable_if< not_< is_same<ValueType,any_const_reference> >, int>::type* = 0
#endif
    )
      : content(new holder<ValueType>(value))
    {
    }

    ~any_const_reference();

  public: // queries

#if 0
    any_const_pointer operator&() const;
#endif

    bool empty() const
    {
      return !content;
    }

    const std::type_info & type() const;

  public: // types (public so any_cast can be non-friend)

    class placeholder;

    template<typename ValueType>
    class holder;


    placeholder * content;

  };

  // ======================================================================

  class any_pointer
  {
  public: // structors

    any_pointer()
      : content(0)
    {
    }

    template<typename ValueType>
    any_pointer(ValueType* ptr)
      : content(new holder<ValueType>(ptr))
    {
    }

    any_pointer(const any_pointer & other);
    any_pointer(const any_ptr_ref & other);
    any_pointer(const any_ptr_const_ref & other);
    ~any_pointer();

    any_pointer& operator++();
    any_pointer& operator--();
    any_reference operator*() const;
    any_reference operator[](std::ptrdiff_t n) const;
    any_pointer operator+(std::ptrdiff_t n) const;
    any_pointer operator-(std::ptrdiff_t) const;
    std::ptrdiff_t operator-(const any_pointer& other) const;
    bool operator==(const any_pointer& other) const;
    bool operator!=(const any_pointer& other) const;
    bool operator<(const any_pointer& other) const;
    bool operator<=(const any_pointer& other) const;
    bool operator>(const any_pointer& other) const;
    bool operator>=(const any_pointer& other) const;
    operator void*() const;

  public: // modifiers

    any_pointer & swap(any_pointer & rhs)
    {
      std::swap(content, rhs.content);
      return *this;
    }

    template<typename ValueType>
    any_pointer & operator=(const ValueType & rhs)
    {
      any_pointer(rhs).swap(*this);
      return *this;
    }

    any_pointer & operator=(const any_pointer & rhs)
    {
      any_pointer(rhs).swap(*this);
      return *this;
    }

  public: // queries

    bool empty() const
    {
      return !content;
    }

    const std::type_info & type() const;

  public: // types (public so any_pointer_cast can be non-friend)

    class placeholder;

    template<typename ValueType>
    class holder;


  public: // representation (public so any_cast can be non-friend)

    placeholder * content;
    friend class any;

  };

  // ======================================================================

  class any_const_pointer
  {
  public: // structors

    any_const_pointer()
      : content(0)
    {
    }

    template<typename ValueType>
    any_const_pointer(ValueType const* ptr)
      : content(new holder<ValueType>(ptr))
    {
    }

    any_const_pointer(const any_const_pointer & other);
    any_const_pointer(const any_pointer & other);
    any_const_pointer(const any_ptr_ref & other);
    any_const_pointer(const any_ptr_const_ref & other);
    any_const_pointer(const any_const_ptr_ref & other);
    any_const_pointer(const any_const_ptr_const_ref & other);
    ~any_const_pointer();

    any_const_pointer& operator++();
    any_const_pointer& operator--();
    any_const_reference operator*() const;
    any_const_reference operator[](std::ptrdiff_t n) const;
    any_const_pointer operator+(std::ptrdiff_t n) const;
    std::ptrdiff_t operator-(const any_const_pointer& other) const;
    any_const_pointer operator-(std::ptrdiff_t) const;
    bool operator==(const any_const_pointer& other) const;
    bool operator!=(const any_const_pointer& other) const;
    bool operator<(const any_const_pointer& other) const;
    bool operator<=(const any_const_pointer& other) const;
    bool operator>(const any_const_pointer& other) const;
    bool operator>=(const any_const_pointer& other) const;

    operator const void*() const;
    
  public: // modifiers

    any_const_pointer & swap(any_const_pointer & rhs)
    {
      std::swap(content, rhs.content);
      return *this;
    }

    template<typename ValueType>
    any_const_pointer & operator=(const ValueType & rhs)
    {
      any_const_pointer(rhs).swap(*this);
      return *this;
    }

    any_const_pointer & operator=(const any_const_pointer & rhs)
    {
      any_const_pointer(rhs).swap(*this);
      return *this;
    }
    any_const_pointer & operator=(const any_pointer & rhs)
    {
      any_const_pointer(rhs).swap(*this);
      return *this;
    }

  public: // queries

    bool empty() const
    {
      return !content;
    }

    const std::type_info & type() const;

  public: // types (public so any_const_pointer_cast can be non-friend)

    class placeholder;

    template<typename ValueType>
    class holder;


  public: // representation (public so any_cast can be non-friend)

    placeholder * content;
    friend class any;

  };

  // ======================================================================

  class any_ptr_ref
  {
  public: // structors

    any_ptr_ref()
      : content(0)
    {
    }

    template<typename ValueType>
    any_ptr_ref(ValueType*& ptr)
      : content(new holder<ValueType>(ptr))
    {
    }

    any_ptr_ref(const any_ptr_ref & other);
    any_ptr_ref(const any_pointer & other);
    ~any_ptr_ref();

    any_ptr_ref& operator++();
    any_ptr_ref& operator--();
    any_reference operator*() const;
    any_reference operator[](std::ptrdiff_t n) const;
    any_pointer operator+(std::ptrdiff_t n) const;
    std::ptrdiff_t operator-(const any_ptr_ref& other) const;
    any_pointer operator-(std::ptrdiff_t) const;
    bool operator==(const any_ptr_ref& other) const;
    bool operator!=(const any_ptr_ref& other) const;
    bool operator<(const any_ptr_ref& other) const;
    bool operator<=(const any_ptr_ref& other) const;
    bool operator>(const any_ptr_ref& other) const;
    bool operator>=(const any_ptr_ref& other) const;

  public: // modifiers

    any_ptr_ref & swap(any_ptr_ref & rhs)
    {
      std::swap(content, rhs.content);
      return *this;
    }

    template<typename ValueType>
    any_ptr_ref & operator=(const ValueType & rhs);

    any_ptr_ref & operator=(const any_ptr_ref & rhs);
    any_ptr_ref & operator=(const any_ptr_const_ref & rhs);

    any_ptr_ref & operator=(const any_pointer & rhs);

  public: // queries

    bool empty() const
    {
      return !content;
    }

    const std::type_info & type() const;

  public: // types (public so any_ptr_ref_cast can be non-friend)

    class placeholder;

    template<typename ValueType>
    class holder;


  public: // representation (public so any_cast can be non-friend)

    placeholder * content;
    friend class any;

  };

  // ======================================================================

  class any_const_ptr_ref
  {
  public: // structors

    any_const_ptr_ref()
      : content(0)
    {
    }

    template<typename ValueType>
    any_const_ptr_ref(ValueType const*& ptr)
      : content(new holder<ValueType>(ptr))
    {
    }

    any_const_ptr_ref(const any_const_ptr_ref & other);
    any_const_ptr_ref(const any_ptr_ref & other);
    ~any_const_ptr_ref();

    any_const_ptr_ref& operator++();
    any_const_ptr_ref& operator--();
    any_const_reference operator*() const;
    any_const_reference operator[](std::ptrdiff_t n) const;
    any_const_pointer operator+(std::ptrdiff_t n) const;
    any_const_pointer operator-(std::ptrdiff_t) const;
    std::ptrdiff_t operator-(const any_const_ptr_ref& other) const;
    bool operator==(const any_const_ptr_ref& other) const;
    bool operator!=(const any_const_ptr_ref& other) const;
    bool operator<(const any_const_ptr_ref& other) const;
    bool operator<=(const any_const_ptr_ref& other) const;
    bool operator>(const any_const_ptr_ref& other) const;
    bool operator>=(const any_const_ptr_ref& other) const;


  public: // modifiers

    any_const_ptr_ref & swap(any_const_ptr_ref & rhs)
    {
      std::swap(content, rhs.content);
      return *this;
    }

    template<typename ValueType>
    any_const_ptr_ref & operator=(const ValueType & rhs);

    any_const_ptr_ref & operator=(const any_const_ptr_ref & rhs);
    any_const_ptr_ref & operator=(const any_ptr_ref & rhs);

    any_const_ptr_ref & operator=(const any_const_pointer & rhs);

  public: // queries

    bool empty() const
    {
      return !content;
    }

    const std::type_info & type() const;

  public: // types (public so any_const_ptr_ref_cast can be non-friend)

    class placeholder;

    template<typename ValueType>
    class holder;


  public: // representation (public so any_cast can be non-friend)

    placeholder * content;
    friend class any;

  };

  // ======================================================================

  class any_const_ptr_const_ref
  {
  public: // structors

    any_const_ptr_const_ref()
      : content(0)
    {
    }

    template<typename ValueType>
    any_const_ptr_const_ref(ValueType const*& ptr)
      : content(new holder<ValueType>(ptr))
    {
    }

    any_const_ptr_const_ref(const any_const_ptr_const_ref & other);
    any_const_ptr_const_ref(const any_const_ptr_ref & other);
    any_const_ptr_const_ref(const any_ptr_const_ref & other);
    any_const_ptr_const_ref(const any_ptr_ref & other);
    ~any_const_ptr_const_ref();

    any_const_ptr_const_ref& operator++();
    any_const_ptr_const_ref& operator--();
    any_const_reference operator*() const;
    any_const_reference operator[](std::ptrdiff_t n) const;
    any_const_pointer operator+(std::ptrdiff_t n) const;
    any_const_pointer operator-(std::ptrdiff_t) const;
    std::ptrdiff_t operator-(const any_const_ptr_const_ref& other) const;
    bool operator==(const any_const_ptr_const_ref& other) const;
    bool operator!=(const any_const_ptr_const_ref& other) const;
    bool operator<(const any_const_ptr_const_ref& other) const;
    bool operator<=(const any_const_ptr_const_ref& other) const;
    bool operator>(const any_const_ptr_const_ref& other) const;
    bool operator>=(const any_const_ptr_const_ref& other) const;

  public: // queries

    bool empty() const
    {
      return !content;
    }

    const std::type_info & type() const;

  public:

    class placeholder;

    template<typename ValueType>
    class holder;


  public: // representation (public so any_cast can be non-friend)

    placeholder * content;
    friend class any;

  };

  // ======================================================================

  class any_ptr_const_ref
  {
  public: // structors

    any_ptr_const_ref()
      : content(0)
    {
    }

    template<typename ValueType>
    any_ptr_const_ref(ValueType* const& ptr)
      : content(new holder<ValueType>(ptr))
    {
    }

    any_ptr_const_ref(const any_ptr_const_ref & other);
    any_ptr_const_ref(const any_ptr_ref & other);
    any_ptr_const_ref(const any_pointer& other);
    ~any_ptr_const_ref();

    any_ptr_const_ref& operator++();
    any_ptr_const_ref& operator--();
    any_reference operator*() const;
    any_reference operator[](std::ptrdiff_t n) const;
    any_pointer operator+(std::ptrdiff_t n) const;
    any_pointer operator-(std::ptrdiff_t) const;
    std::ptrdiff_t operator-(const any_ptr_const_ref& other) const;
    bool operator==(const any_ptr_const_ref& other) const;
    bool operator!=(const any_ptr_const_ref& other) const;
    bool operator<(const any_ptr_const_ref& other) const;
    bool operator<=(const any_ptr_const_ref& other) const;
    bool operator>(const any_ptr_const_ref& other) const;
    bool operator>=(const any_ptr_const_ref& other) const;
    operator void*() const;

  public: // queries

    bool empty() const
    {
      return !content;
    }

    const std::type_info & type() const;

  public:

    class placeholder;

    template<typename ValueType>
    class holder;


  public: // representation (public so any_cast can be non-friend)

    placeholder * content;
    friend class any;

  };

  // ======================================================================


  class bad_any_cast : public std::bad_cast
  {
  public:
    virtual const char * what() const throw()
    {
      return "boost::bad_any_cast: "
	"failed conversion using boost::any_cast";
    }
  };

  // ======================================================================

  class any::placeholder
  {
  public: // structors
    
    virtual ~placeholder()
    {
    }

  public: // queries

    virtual const std::type_info & type() const = 0;

    virtual placeholder * clone() const = 0;
    virtual any_pointer ptr() const = 0;
    virtual any_const_pointer const_ptr() const = 0;
    virtual any_reference::placeholder* to_any_ref_holder() const = 0;
    virtual any_const_reference::placeholder* to_any_const_ref_holder() const = 0;
  };

  template <typename ValueType>
  struct make_ptr;

  template <typename ValueType>
  struct make_const_ptr;


  template<typename ValueType>
  class any::holder : public any::placeholder
  {
  public: // structors

    holder(const ValueType & value)
      : held(value)
    {
    }

  public: // queries

    virtual const std::type_info & type() const
    {
      return typeid(ValueType);
    }

    virtual placeholder * clone() const
    {
      return new holder(held);
    }

    virtual any_pointer ptr() const {
      return make_ptr<ValueType>::go(held);
    }
    virtual any_const_pointer const_ptr() const {
      return make_const_ptr<ValueType>::go(held);
    }

    virtual any_reference::placeholder* 
    to_any_ref_holder() const {
      return new any_reference::holder<ValueType>(const_cast<ValueType&>(held));
    }
    virtual any_const_reference::placeholder* 
    to_any_const_ref_holder() const {
      return new any_const_reference::holder<ValueType>(held);
    }
  public: // representation

    ValueType held;

  };

  template <class T>
  struct make_ptr {
    static any_pointer go(const T& held) { boost::throw_exception(bad_any_cast()); }
  };
  template <class T>
  struct make_ptr<T*> {
    static any_pointer go(T* const& held) { return any_pointer(held); }
  };
  template <class T>
  struct make_ptr<T const*> {
    static any_pointer go(T const* const& held) { boost::throw_exception(bad_any_cast()); }
  };

  template <class T>
  struct make_const_ptr {
    static any_const_pointer go(const T& held) { boost::throw_exception(bad_any_cast()); }
  };
  template <class T>
  struct make_const_ptr<T const*> {
    static any_const_pointer go(T const* const& held) { return any_const_pointer(held); }
  };


  /* Illegal */
  template<> class any::holder<any>;
  template<> class any::holder<any_reference>;
  template<> class any::holder<void>;

  // ======================================================================

  class any_reference::placeholder
  {
  public: // structors
    
    virtual ~placeholder()
    {
    }

  public: // queries

    virtual const std::type_info & type() const = 0;

    virtual placeholder * clone() const = 0;
    virtual any_ptr_ref ptr() const = 0;
    virtual any_const_ptr_ref const_ptr() const = 0;
    virtual any::placeholder * to_any_holder() const = 0;
    virtual any_const_reference::placeholder * to_any_const_ref_holder() const = 0;
    virtual void assign(const any&) const = 0;
    virtual void assign(const any_reference&) const = 0;
    virtual void assign(const any_const_reference&) const = 0;
  };

  template <typename ValueType>
  struct make_ptr_ref;

  template <typename ValueType>
  struct make_const_ptr_ref;

  template<typename ValueType>
  class any_reference::holder : public any_reference::placeholder
  {
  public: // structors

    holder(ValueType & value)
      : held(value)
    {
    }

  public: // queries

    virtual const std::type_info & type() const
    {
      return typeid(ValueType);
    }

    virtual placeholder * clone() const
    {
      return new holder(held);
    }
    virtual any_ptr_ref ptr() const {
      return make_ptr_ref<ValueType>::go(const_cast<ValueType&>(held));
    }
    virtual any_const_ptr_ref const_ptr() const {
      return make_const_ptr_ref<ValueType>::go(const_cast<ValueType&>(held));
    }

    virtual any::placeholder * to_any_holder() const
    {
      return new any::holder<ValueType>(held);
    }

    virtual any_const_reference::placeholder*
    to_any_const_ref_holder() const
    {
      return new any_const_reference::holder<ValueType>(held);
    }

    virtual void assign(const any& other) const {
      if (other.type() == typeid(ValueType))
	held = static_cast<any::holder<ValueType> *>(other.content)->held;
      else
	boost::throw_exception(bad_any_cast());
    }

    virtual void assign(const any_reference& other) const {
      if (other.type() == typeid(ValueType))
	held = static_cast<any_reference::holder<ValueType> *>(other.content)->held;
      else
	boost::throw_exception(bad_any_cast());
    }
    virtual void assign(const any_const_reference& other) const {
      if (other.type() == typeid(ValueType))
	held = static_cast<any_const_reference::holder<ValueType> *>(other.content)->held;
      else
	boost::throw_exception(bad_any_cast());
    }

  public: // representation

    ValueType& held;

  };

  /* these are illegal */
  template<class T>
  class any_reference::holder<T const>;
  template<>
  class any_reference::holder<any_reference>;
  template<>
  class any_reference::holder<any_const_reference>;
  template<>
  class any_reference::holder<any>;
  template<>
  class any_reference::holder<void>;


  template <class T>
  struct make_ptr_ref {
    static any_ptr_ref go(T& held) { boost::throw_exception(bad_any_cast()); }
  };
  template <class T>
  struct make_const_ptr_ref {
    static any_ptr_ref go(T& held) { boost::throw_exception(bad_any_cast()); }
  };
  template <class T>
  struct make_const_ptr_ref<T const*> {
    static any_const_ptr_ref go(T const*& held) { return any_const_ptr_ref(held); }
  };

  template <class T>
  struct make_ptr_ref<T const*> {
    static any_ptr_ref go(T const*& held) { boost::throw_exception(bad_any_cast()); }
  };
  template <class T>
  struct make_ptr_ref<T*> {
    static any_ptr_ref go(T*& held) { return any_ptr_ref(held); }
  };

  // ======================================================================

  class any_const_reference::placeholder
  {
  public: // structors
    
    virtual ~placeholder()
    {
    }

  public: // queries

    virtual const std::type_info & type() const = 0;

    virtual placeholder * clone() const = 0;
    virtual any_ptr_const_ref ptr() const = 0;
#if 0
    virtual any_const_pointer to_ptr() const = 0;
#endif
    virtual any::placeholder * to_any_holder() const = 0;
  };

  template <typename ValueType>
  struct make_ptr_ref2;

  template <typename ValueType>
  struct make_const_ptr_ref2;

  template<typename ValueType>
  class any_const_reference::holder : public any_const_reference::placeholder
  {
  public: // structors

    holder(ValueType const & value)
      : held(value)
    {
    }

  public: // queries

    virtual const std::type_info & type() const
    {
      return typeid(ValueType);
    }

    virtual placeholder * clone() const
    {
      return new holder(held);
    }
    virtual any_ptr_const_ref ptr() const {
      return make_ptr_ref2<ValueType>::go(const_cast<ValueType&>(held));
    }

    virtual any_const_ptr_const_ref const_ptr() const {
      return make_const_ptr_ref2<ValueType>::go(const_cast<ValueType&>(held));
    }

#if 0
    virtual any_const_pointer to_ptr() const {
      return &held;
    }
#endif

    virtual any::placeholder * to_any_holder() const
    {
      return new any::holder<ValueType>(held);
    }

  public: // representation

    ValueType const& held;

  };

  /* these are illegal */
  template<>
  class any_const_reference::holder<any_reference>;
  template<>
  class any_const_reference::holder<any_const_reference>;
  template<>
  class any_const_reference::holder<any>;
  template<>
  class any_const_reference::holder<any_pointer>;
  template<>
  class any_const_reference::holder<any_ptr_ref>;
  template<>
  class any_const_reference::holder<void>;


  template <class T>
  struct make_ptr_ref2 {
    static any_ptr_const_ref go(T& held) { boost::throw_exception(bad_any_cast()); }
  };
  template <class T>
  struct make_ptr_ref2<T const*> {
    static any_ptr_const_ref go(T const*& held) { boost::throw_exception(bad_any_cast()); }
  };
  template <class T>
  struct make_ptr_ref2<T*> {
    static any_ptr_const_ref go(T*& held) { return any_ptr_const_ref(held); }
  };


  template <class T>
  struct make_const_ptr_ref2 {
    static any_const_ptr_const_ref go(T& held) { boost::throw_exception(bad_any_cast()); }
  };
  template <class T>
  struct make_const_ptr_ref2<T const*> {
    static any_const_ptr_const_ref go(T const*& held) { boost::throw_exception(bad_any_cast()); }
  };

  // ======================================================================

  class any_pointer::placeholder
  {
  public: // structors
    
    virtual ~placeholder()
    {
    }

  public: // queries

    virtual const std::type_info & type() const = 0;

    virtual placeholder * clone() const = 0;
    virtual any::placeholder * to_any_holder() const = 0;
    virtual any_reference::placeholder * to_any_ref_holder() const = 0;
    virtual any_const_reference::placeholder * to_any_const_ref_holder() const = 0;
    virtual any_const_pointer::placeholder * to_const_ptr_holder() const = 0;
    virtual any_ptr_const_ref::placeholder * to_any_ptr_const_ref_holder() const = 0;
    virtual any_ptr_ref::placeholder * to_any_ptr_ref_holder() const = 0;

    virtual void increment() = 0;
    virtual void decrement() = 0;
    virtual any_reference dereference() const = 0;
    virtual any_reference arrayelt(std::ptrdiff_t) const = 0;
    virtual any_pointer advance(std::ptrdiff_t) const = 0;
    virtual bool equal(const placeholder*) const = 0;
    virtual bool less_than(const placeholder*) const = 0;
    virtual std::ptrdiff_t diff(const placeholder*) const = 0;
    virtual void* to_void_ptr() const = 0;
  };

  // ======================================================================

  class any_const_pointer::placeholder
  {
  public: // structors
    
    virtual ~placeholder()
    {
    }

  public: // queries

    virtual const std::type_info & type() const = 0;

    virtual placeholder * clone() const = 0;
    virtual any::placeholder * to_any_holder() const = 0;
    virtual const void * void_ptr() const = 0;

    virtual void increment() = 0;
    virtual void decrement() = 0;
    virtual any_const_reference dereference() const = 0;
    virtual any_const_reference arrayelt(std::ptrdiff_t) const = 0;
    virtual any_const_pointer advance(std::ptrdiff_t) const = 0;
    virtual bool equal(const placeholder*) const = 0;
    virtual bool less_than(const placeholder*) const = 0;
    virtual std::ptrdiff_t diff(const placeholder*) const = 0;
  };

  // ======================================================================

  class any_ptr_ref::placeholder
  {
  public: // structors
    
    virtual ~placeholder()
    {
    }

  public: // queries

    virtual const std::type_info & type() const = 0;

    virtual placeholder * clone() const = 0;
    virtual any::placeholder * to_any_holder() const = 0;
    virtual any_reference::placeholder * to_any_ref_holder() const = 0;
    virtual any_const_reference::placeholder * to_any_const_ref_holder() const = 0;
    virtual any_pointer::placeholder * to_ptr_holder() const = 0;
    virtual any_const_pointer::placeholder * to_const_ptr_holder() const = 0;
    virtual any_ptr_const_ref::placeholder * to_any_ptr_const_ref_holder() const = 0;
    virtual any_const_ptr_ref::placeholder * to_any_const_ptr_ref_holder() const = 0;
    virtual any_const_ptr_const_ref::placeholder * to_any_const_ptr_const_ref_holder() const = 0;

    virtual void assign(const any_pointer&) const = 0;
    virtual void assign(const any_ptr_ref&) const = 0;
    virtual void assign(const any_ptr_const_ref&) const = 0;

    virtual void increment() = 0;
    virtual void decrement() = 0;
    virtual any_reference dereference() const = 0;
    virtual any_reference arrayelt(std::ptrdiff_t) const = 0;
    virtual any_pointer advance(std::ptrdiff_t) const = 0;
    virtual bool equal(const placeholder*) const = 0;
    virtual bool less_than(const placeholder*) const = 0;
    virtual std::ptrdiff_t diff(const placeholder*) const = 0;
  };

  // ======================================================================

  class any_const_ptr_ref::placeholder
  {
  public: // structors
    
    virtual ~placeholder()
    {
    }

  public: // queries

    virtual const std::type_info & type() const = 0;

    virtual placeholder * clone() const = 0;
    virtual any::placeholder * to_any_holder() const = 0;
    virtual any_reference::placeholder * to_any_ref_holder() const = 0;
    virtual any_const_reference::placeholder * to_any_const_ref_holder() const = 0;
    virtual any_const_pointer::placeholder * to_const_ptr_holder() const = 0;
    virtual any_const_ptr_const_ref::placeholder * to_any_const_ptr_const_ref_holder() const = 0;

    virtual void assign(const any_const_pointer&) const = 0;
    virtual void assign(const any_const_ptr_ref&) const = 0;

    virtual void increment() = 0;
    virtual void decrement() = 0;
    virtual any_const_reference dereference() const = 0;
    virtual any_const_reference arrayelt(std::ptrdiff_t) const = 0;
    virtual any_const_pointer advance(std::ptrdiff_t) const = 0;
    virtual bool equal(const placeholder*) const = 0;
    virtual bool less_than(const placeholder*) const = 0;
    virtual std::ptrdiff_t diff(const placeholder*) const = 0;
  };

  // ======================================================================

  class any_const_ptr_const_ref::placeholder
  {
  public: // structors
    
    virtual ~placeholder()
    {
    }

  public: // queries

    virtual const std::type_info & type() const = 0;

    virtual placeholder * clone() const = 0;
    virtual any::placeholder * to_any_holder() const = 0;
    virtual any_const_reference::placeholder * to_any_const_ref_holder() const = 0;
    virtual any_const_pointer::placeholder * to_const_ptr_holder() const = 0;

    virtual any_const_reference dereference() const = 0;
    virtual any_const_reference arrayelt(std::ptrdiff_t) const = 0;
    virtual any_const_pointer advance(std::ptrdiff_t) const = 0;
    virtual bool equal(const placeholder*) const = 0;
    virtual bool less_than(const placeholder*) const = 0;
    virtual std::ptrdiff_t diff(const placeholder*) const = 0;
  };

  // ======================================================================

  class any_ptr_const_ref::placeholder
  {
  public: // structors
    
    virtual ~placeholder()
    {
    }

  public: // queries

    virtual const std::type_info & type() const = 0;

    virtual placeholder * clone() const = 0;
    virtual any::placeholder * to_any_holder() const = 0;
    virtual any_const_reference::placeholder * to_any_const_ref_holder() const = 0;
    virtual any_pointer::placeholder * to_ptr_holder() const = 0;
    virtual any_const_pointer::placeholder * to_const_ptr_holder() const = 0;
    virtual any_const_ptr_const_ref::placeholder * to_any_const_ptr_const_ref_holder() const = 0;

    virtual any_reference dereference() const = 0;
    virtual any_reference arrayelt(std::ptrdiff_t) const = 0;
    virtual any_pointer advance(std::ptrdiff_t) const = 0;
    virtual bool equal(const placeholder*) const = 0;
    virtual bool less_than(const placeholder*) const = 0;
    virtual std::ptrdiff_t diff(const placeholder*) const = 0;
    virtual void* to_void_ptr() const = 0;
  };

  // ======================================================================


  inline any::any(const any_pointer& other)
    : content(other.content ? other.content->to_any_holder() : 0)
  {
  }

  inline any::any(const any_const_pointer& other)
    : content(other.content ? other.content->to_any_holder() : 0)
  {
  }

  inline any::any(const any_reference& other)
    : content(other.content ? other.content->to_any_holder() : 0)
  {
  }

  inline any::any(const any_const_reference& other)
    : content(other.content ? other.content->to_any_holder() : 0)
  {
  }

  inline any::any(const any_ptr_ref& other)
    : content(other.content ? other.content->to_any_holder() : 0)
  {
  }

  inline any::any(const any_const_ptr_ref& other)
    : content(other.content ? other.content->to_any_holder() : 0)
  {
  }
  inline any::any(const any_const_ptr_const_ref& other)
    : content(other.content ? other.content->to_any_holder() : 0)
  {
  }
  inline any::any(const any_ptr_const_ref& other)
    : content(other.content ? other.content->to_any_holder() : 0)
  {
  }

  inline any::any(const any & other)
    : content(other.content ? other.content->clone() : 0)
  {
  }
  inline any::~any()
  {
    delete content;
  }

  inline const std::type_info & any::type() const
  {
    return content ? content->type() : typeid(void);
  }

  // ======================================================================

  inline any_reference::any_reference(const any& other)
    : content(other.content ? other.content->to_any_ref_holder() : 0)
  {
  }
  inline any_reference::any_reference(any& other)
    : content(other.content ? other.content->to_any_ref_holder() : 0)
  {
  }

  inline any_reference::any_reference(const any_pointer& other)
    : content(other.content ? other.content->to_any_ref_holder() : 0)
  {
  }
  inline any_reference::any_reference(any_pointer& other)
    : content(other.content ? other.content->to_any_ref_holder() : 0)
  {
  }

  inline any_reference::any_reference(const any_ptr_ref& other)
    : content(other.content ? other.content->to_any_ref_holder() : 0)
  {
  }
  inline any_reference::any_reference(any_ptr_ref& other)
    : content(other.content ? other.content->to_any_ref_holder() : 0)
  {
  }

  inline any_reference::any_reference(const any_const_ptr_ref& other)
    : content(other.content ? other.content->to_any_ref_holder() : 0)
  {
  }
  inline any_reference::any_reference(any_const_ptr_ref& other)
    : content(other.content ? other.content->to_any_ref_holder() : 0)
  {
  }

  inline any_reference::any_reference(const any_reference & other)
    : content(other.content ? other.content->clone() : 0)
  {
  }

  inline any_reference::any_reference(any_reference & other)
    : content(other.content ? other.content->clone() : 0)
  {
  }

  template<typename ValueType>
  any_reference & any_reference::operator=(const ValueType & rhs)
  {
    content->assign(any_reference(const_cast<ValueType&>(rhs)));
    return *this;
  }

  inline any_reference & any_reference::operator=(const any_reference & rhs)
  {
    content->assign(rhs);
    return *this;
  }

  inline any_reference & any_reference::operator=(const any_const_reference & rhs)
  {
    content->assign(rhs);
    return *this;
  }

  inline any_reference & any_reference::operator=(const any & rhs)
  {
    content->assign(rhs);
    return *this;
  }

  inline any_reference::~any_reference()
  {
    delete content;
  }

  inline const std::type_info & any_reference::type() const
  {
    return content ? content->type() : typeid(void);
  }

  // ======================================================================

  inline any_const_reference::any_const_reference(const any& other)
    : content(other.content ? other.content->to_any_const_ref_holder() : 0)
  {
  }
#if 0
  inline any_const_reference::any_const_reference(any& other)
    : content(other.content ? other.content->to_any_const_ref_holder() : 0)
  {
  }
#endif

  inline any_const_reference::any_const_reference(const any_pointer& other)
    : content(other.content ? other.content->to_any_const_ref_holder() : 0)
  {
  }
  inline any_const_reference::any_const_reference(any_pointer& other)
    : content(other.content ? other.content->to_any_const_ref_holder() : 0)
  {
  }

  inline any_const_reference::any_const_reference(any_ptr_ref other)
    : content(other.content ? other.content->to_any_const_ref_holder() : 0)
  {
  }

  inline any_const_reference::any_const_reference(any_ptr_const_ref other)
    : content(other.content ? other.content->to_any_const_ref_holder() : 0)
  {
  }

  inline any_const_reference::any_const_reference(any_const_ptr_const_ref other)
    : content(other.content ? other.content->to_any_const_ref_holder() : 0)
  {
  }

  inline any_const_reference::any_const_reference(any_const_ptr_ref other)
    : content(other.content ? other.content->to_any_const_ref_holder() : 0)
  {
  }

  inline any_const_reference::any_const_reference(any_reference const& other)
    : content(other.content ? other.content->to_any_const_ref_holder() : 0)
  {
  }

  inline any_const_reference::any_const_reference(any_const_reference const& other)
    : content(other.content ? other.content->clone() : 0)
  {
  }

  inline any_const_reference::~any_const_reference()
  {
    delete content;
  }

#if 0
  inline any_const_pointer any_const_reference::operator&() const 
  {
    return any_const_pointer(content->to_ptr());
  }
#endif

  inline const std::type_info & any_const_reference::type() const
  {
    return content ? content->type() : typeid(void);
  }


  // ======================================================================

  template<typename ValueType>
  any_ptr_ref & any_ptr_ref::operator=(const ValueType & rhs)
  {
    content->assign(any_ptr_ref(const_cast<ValueType&>(rhs)));
    return *this;
  }

  inline any_ptr_ref & any_ptr_ref::operator=(const any_ptr_ref & rhs)
  {
    content->assign(rhs);
    return *this;
  }
  inline any_ptr_ref & any_ptr_ref::operator=(const any_ptr_const_ref & rhs)
  {
    content->assign(rhs);
    return *this;
  }

  inline any_ptr_ref & any_ptr_ref::operator=(const any_pointer & rhs)
  {
    content->assign(rhs);
    return *this;
  }


  // ======================================================================

  template<typename ValueType>
  any_const_ptr_ref & any_const_ptr_ref::operator=(const ValueType & rhs)
  {
    content->assign(any_const_ptr_ref(const_cast<ValueType&>(rhs)));
    return *this;
  }

  inline any_const_ptr_ref & any_const_ptr_ref::operator=(const any_const_ptr_ref & rhs)
  {
    content->assign(rhs);
    return *this;
  }

  inline any_const_ptr_ref & any_const_ptr_ref::operator=(const any_const_pointer & rhs)
  {
    content->assign(rhs);
    return *this;
  }


  // ======================================================================

  template<typename ValueType>
  ValueType * any_cast(to_type<ValueType>, any * operand)
  {
    return operand && operand->type() == typeid(ValueType)
      ? &static_cast<any::holder<ValueType> *>(operand->content)->held
      : 0;
  }

  // Return by value, or else risk a dangling reference to
  // a no longer existent temporary!
  template<typename ValueType>
  ValueType any_cast(to_type<ValueType>, any& operand)
  {
    ValueType* result = any_cast(to_type<ValueType>(), &operand);
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
#endif
      boost::throw_exception(bad_any_cast());
    }
    return *result;
  }

  template<typename ValueType>
  ValueType any_cast(to_type<ValueType>, const any& operand)
  {
    ValueType* result = any_cast(to_type<ValueType>(), (any*)&operand);
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
#endif
      boost::throw_exception(bad_any_cast());
    }
    return *result;
  }


  // Casting to reference when the any is not a temporary.
  template<typename ValueType>
  ValueType& any_cast(to_type<ValueType&>, any& operand)
  {
    ValueType* result = any_cast(to_type<ValueType>(), &operand);
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
#endif
      boost::throw_exception(bad_any_cast());
    }
    return *result;
  }

  template<typename ValueType>
  ValueType& any_cast(to_type<ValueType&>, const any& operand)
  {
    ValueType* result = any_cast(to_type<ValueType>(), (any*)&operand);
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
#endif
      boost::throw_exception(bad_any_cast());
    }
    return *result;
  }


  // ======================================================================

  template<typename ValueType>
  ValueType * any_cast(to_type<ValueType>, any_reference * operand)
  {
    return operand && operand->type() == typeid(ValueType)
      ? &static_cast<any_reference::holder<ValueType> *>(operand->content)->held
      : 0;
  }

  template<typename ValueType>
  ValueType& any_cast(to_type<ValueType&>, const any_reference& operand)
  {
    ValueType* result = any_cast(to_type<ValueType>(), (any_reference*)&operand);
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
#endif
      boost::throw_exception(bad_any_cast());
    }
    return *result;
  }

  template<typename ValueType>
  ValueType& any_cast(to_type<ValueType>, const any_reference& operand)
  {
    ValueType* result = any_cast(to_type<ValueType>(), (any_reference*)&operand);
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
#endif
      boost::throw_exception(bad_any_cast());
    }
    return *result;
  }

  // ======================================================================

  template<typename ValueType>
  ValueType const* any_cast(to_type<ValueType>, any_const_reference * operand)
  {
    return operand && operand->type() == typeid(ValueType)
      ? &static_cast<any_const_reference::holder<ValueType> *>(operand->content)->held
      : 0;
  }

  template<typename ValueType>
  ValueType const& any_cast(to_type<ValueType&>, const any_const_reference& operand)
  {
    ValueType const* result = any_cast(to_type<ValueType>(), (any_const_reference*)&operand);
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
#endif
      boost::throw_exception(bad_any_cast());
    }
    return *result;
  }

  template<typename ValueType>
  ValueType const& any_cast(to_type<ValueType>, const any_const_reference& operand)
  {
    ValueType const* result = any_cast(to_type<ValueType>(), (any_const_reference*)&operand);
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
      boost::throw_exception(bad_any_cast());
#endif
    }
    return *result;
  }


  // ======================================================================

  template<typename ValueType>
  class any_pointer::holder : public any_pointer::placeholder
  {
  public: // structors

    holder(ValueType* ptr)
      : held(ptr)
    {
    }

  public: // queries

    virtual const std::type_info & type() const
    {
      return typeid(ValueType);
    }

    virtual placeholder * clone() const
    {
      return new holder(held);
    }

    virtual any::placeholder * to_any_holder() const
    {
      return new any::holder<ValueType*>(held);
    }
    virtual any_reference::placeholder * to_any_ref_holder() const
    {
      return new any_reference::holder<ValueType*>(const_cast<ValueType*&>(held));
    }
    virtual any_const_reference::placeholder * to_any_const_ref_holder() const
    {
      return new any_const_reference::holder<ValueType*>(held);
    }

    virtual any_const_pointer::placeholder * to_const_ptr_holder() const
    {
      return new any_const_pointer::holder<ValueType>(held);
    }

    virtual any_ptr_const_ref::placeholder * to_any_ptr_const_ref_holder() const
    {
      return new any_ptr_const_ref::holder<ValueType>(held);
    }
    virtual any_ptr_ref::placeholder * to_any_ptr_ref_holder() const
    {
      return new any_ptr_ref::holder<ValueType>(const_cast<ValueType*&>(held));
    }
    virtual void* to_void_ptr() const
    {
      return held;
    }

    virtual void increment()
    {
      ++held;
    }

    virtual void decrement()
    {
      --held;
    }

    virtual any_reference dereference() const
    {
      return *held;
    }

    virtual any_reference arrayelt(std::ptrdiff_t n) const
    {
      return held[n];
    }
    virtual any_pointer advance(std::ptrdiff_t n) const
    {
      return any_pointer(held + n);
    }
    virtual bool equal(const placeholder* p) const
    {
      return held == dynamic_cast<const holder<ValueType>*>(p)->held;
    }
    virtual bool less_than(const placeholder* p) const
    {
      return held < dynamic_cast<const holder<ValueType>*>(p)->held;
    }
    virtual std::ptrdiff_t diff(const placeholder* p) const
    {
      return held - dynamic_cast<const holder<ValueType>*>(p)->held;
    }

  public: // representation

    ValueType* held;

  };

  // Illegal, use any_const_pointer
  template <typename T>
  class any_pointer::holder<T const>;

  template <>
  class any_pointer::holder<void>;

  inline any_pointer::any_pointer(const any_pointer & other)
    : content(other.content ? other.content->clone() : 0)
  {
  }

  inline any_pointer::any_pointer(const any_ptr_ref & other)
    : content(other.content ? other.content->to_ptr_holder() : 0)
  {
  }

  inline any_pointer::any_pointer(const any_ptr_const_ref & other)
    : content(other.content ? other.content->to_ptr_holder() : 0)
  {
  }


  inline any_pointer& any_pointer::operator++()
  {
    content->increment();
    return *this;
  }
  inline any_pointer& any_pointer::operator--()
  {
    content->decrement();
    return *this;
  }
  inline any_reference any_pointer::operator*() const
  {
    return content->dereference();
  }
  inline any_pointer::operator void*() const
  {
    return content->to_void_ptr();
  }
  inline any_reference any_pointer::operator[](std::ptrdiff_t n) const
  {
    return content->arrayelt(n);
  }
  inline any_pointer any_pointer::operator+(std::ptrdiff_t n) const
  {
    return content->advance(n);
  }
  inline any_pointer any_pointer::operator-(std::ptrdiff_t n) const
  {
    return content->advance(-n);
  }
  inline std::ptrdiff_t any_pointer::operator-(const any_pointer& other) const
  {
    return content->diff(other.content);
  }

  inline bool any_pointer::operator==(const any_pointer& other) const
  {
    return content->equal(other.content);
  }
  inline bool any_pointer::operator!=(const any_pointer& other) const
  {
    return ! content->equal(other.content);
  }
  inline bool any_pointer::operator<(const any_pointer& other) const
  {
    return content->less_than(other.content);
  }
  inline bool any_pointer::operator<=(const any_pointer& other) const
  {
    return ! other.content->less_than(content);
  }
  inline bool any_pointer::operator>(const any_pointer& other) const
  {
    return other.content->less_than(content);
  }
  inline bool any_pointer::operator>=(const any_pointer& other) const
  {
    return !content->less_than(other.content);
  }

  inline any_pointer::~any_pointer()
  {
    delete content;
  }

  inline const std::type_info & any_pointer::type() const
  {
    return content ? content->type() : typeid(void);
  }


  // ======================================================================

  template<typename ValueType>
  class any_const_pointer::holder : public any_const_pointer::placeholder
  {
  public: // structors

    holder(ValueType const* ptr)
      : held(ptr)
    {
    }

  public: // queries

    virtual const std::type_info & type() const
    {
      return typeid(ValueType);
    }

    virtual placeholder * clone() const
    {
      return new holder(held);
    }

    virtual any::placeholder * to_any_holder() const
    {
      return new any::holder<ValueType const*>(held);
    }

    virtual const void* void_ptr() const {
      return held;
    }

    virtual void increment()
    {
      ++held;
    }

    virtual void decrement()
    {
      --held;
    }

    virtual any_const_reference dereference() const
    {
      return *held;
    }

    virtual any_const_reference arrayelt(std::ptrdiff_t n) const
    {
      return held[n];
    }
    virtual any_const_pointer advance(std::ptrdiff_t n) const
    {
      return any_const_pointer(held + n);
    }
    virtual bool equal(const placeholder* p) const
    {
      return held == dynamic_cast<const holder<ValueType>*>(p)->held;
    }
    virtual bool less_than(const placeholder* p) const
    {
      return held < dynamic_cast<const holder<ValueType>*>(p)->held;
    }
    virtual std::ptrdiff_t diff(const placeholder* p) const
    {
      return held - dynamic_cast<const holder<ValueType>*>(p)->held;
    }

  public: // representation

    ValueType const* held;

  };

  // Illegal
  template<>
  class any_const_pointer::holder<void>;


  inline any_const_pointer::any_const_pointer(const any_const_pointer & other)
    : content(other.content ? other.content->clone() : 0)
  {
  }

  inline any_const_pointer::any_const_pointer(const any_pointer & other)
    : content(other.content ? other.content->to_const_ptr_holder() : 0)
  {
  }

  inline any_const_pointer::any_const_pointer(const any_ptr_ref & other)
    : content(other.content ? other.content->to_const_ptr_holder() : 0)
  {
  }

  inline any_const_pointer::any_const_pointer(const any_ptr_const_ref & other)
    : content(other.content ? other.content->to_const_ptr_holder() : 0)
  {
  }

  inline any_const_pointer::any_const_pointer(const any_const_ptr_ref & other)
    : content(other.content ? other.content->to_const_ptr_holder() : 0)
  {
  }

  inline any_const_pointer::any_const_pointer(const any_const_ptr_const_ref & other)
    : content(other.content ? other.content->to_const_ptr_holder() : 0)
  {
  }


  inline any_const_pointer::operator const void*() const
  {
    return content->void_ptr();
  }

  inline any_const_pointer& any_const_pointer::operator++()
  {
    content->increment();
    return *this;
  }
  inline any_const_pointer& any_const_pointer::operator--()
  {
    content->decrement();
    return *this;
  }
  inline any_const_reference any_const_pointer::operator*() const
  {
    return content->dereference();
  }
  inline any_const_reference any_const_pointer::operator[](std::ptrdiff_t n) const
  {
    return content->arrayelt(n);
  }
  inline any_const_pointer any_const_pointer::operator+(std::ptrdiff_t n) const
  {
    return content->advance(n);
  }
  inline any_const_pointer any_const_pointer::operator-(std::ptrdiff_t n) const
  {
    return content->advance(-n);
  }
  inline std::ptrdiff_t any_const_pointer::operator-(const any_const_pointer& other) const
  {
    return content->diff(other.content);
  }

  inline bool any_const_pointer::operator==(const any_const_pointer& other) const
  {
    return content->equal(other.content);
  }
  inline bool any_const_pointer::operator!=(const any_const_pointer& other) const
  {
    return ! content->equal(other.content);
  }
  inline bool any_const_pointer::operator<(const any_const_pointer& other) const
  {
    return content->less_than(other.content);
  }
  inline bool any_const_pointer::operator<=(const any_const_pointer& other) const
  {
    return ! other.content->less_than(content);
  }
  inline bool any_const_pointer::operator>(const any_const_pointer& other) const
  {
    return other.content->less_than(content);
  }
  inline bool any_const_pointer::operator>=(const any_const_pointer& other) const
  {
    return !content->less_than(other.content);
  }

  inline any_const_pointer::~any_const_pointer()
  {
    delete content;
  }

  inline const std::type_info & any_const_pointer::type() const
  {
    return content ? content->type() : typeid(void);
  }


  // ======================================================================


  template<typename ValueType>
  class any_ptr_ref::holder : public any_ptr_ref::placeholder
  {
  public: // structors

    holder(ValueType*& ptr)
      : held(ptr)
    {
    }

  public: // queries

    virtual const std::type_info & type() const
    {
      return typeid(ValueType);
    }

    virtual placeholder * clone() const
    {
      return new holder(held);
    }

    virtual any::placeholder * to_any_holder() const
    {
      return new any::holder<ValueType*>(held);
    }

    virtual any_reference::placeholder* to_any_ref_holder() const
    {
      return new any_reference::holder<ValueType*>(held);
    }

    virtual any_const_reference::placeholder* to_any_const_ref_holder() const
    {
      return new any_const_reference::holder<ValueType*>(held);
    }

    virtual any_pointer::placeholder* to_ptr_holder() const
    {
      return new any_pointer::holder<ValueType>(held);
    }

    virtual any_const_pointer::placeholder* to_const_ptr_holder() const
    {
      return new any_const_pointer::holder<ValueType>(held);
    }

    virtual any_ptr_const_ref::placeholder* to_any_ptr_const_ref_holder() const
    {
      return new any_ptr_const_ref::holder<ValueType>(held);
    }

    virtual any_const_ptr_ref::placeholder* to_any_const_ptr_ref_holder() const
    {
      ValueType const* x = held;
      return new any_const_ptr_ref::holder<ValueType>(x);
    }

    virtual any_const_ptr_const_ref::placeholder* to_any_const_ptr_const_ref_holder() const
    {
      return new any_const_ptr_const_ref::holder<ValueType>(held);
    }

    virtual void increment()
    {
      ++held;
    }

    virtual void decrement()
    {
      --held;
    }

    virtual any_reference dereference() const
    {
      return *held;
    }

    virtual any_reference arrayelt(std::ptrdiff_t n) const
    {
      return held[n];
    }
    virtual any_pointer advance(std::ptrdiff_t n) const
    {
      return any_pointer(held + n);
    }
    virtual bool equal(const placeholder* p) const
    {
      return held == dynamic_cast<const holder<ValueType>*>(p)->held;
    }
    virtual bool less_than(const placeholder* p) const
    {
      return held < dynamic_cast<const holder<ValueType>*>(p)->held;
    }
    virtual std::ptrdiff_t diff(const placeholder* p) const
    {
      return held - dynamic_cast<const holder<ValueType>*>(p)->held;
    }

    virtual void assign(const any_pointer& other) const {
      if (other.type() == typeid(ValueType))
	held = static_cast<any_pointer::holder<ValueType> *>(other.content)->held;
      else
	boost::throw_exception(bad_any_cast());
    }

    virtual void assign(const any_ptr_ref& other) const {
      if (other.type() == typeid(ValueType))
	held = static_cast<any_ptr_ref::holder<ValueType> *>(other.content)->held;
      else
	boost::throw_exception(bad_any_cast());
    }

    virtual void assign(const any_ptr_const_ref& other) const {
      if (other.type() == typeid(ValueType))
	held = static_cast<any_ptr_const_ref::holder<ValueType> *>(other.content)->held;
      else
	boost::throw_exception(bad_any_cast());
    }

  public: // representation

    ValueType*& held;

  };

  // Illegal
  template<typename T>
  class any_ptr_ref::holder<T const>;

  template<>
  class any_ptr_ref::holder<void>;

  inline any_ptr_ref::any_ptr_ref(const any_ptr_ref & other)
    : content(other.content ? other.content->clone() : 0)
  {
  }

  inline any_ptr_ref::any_ptr_ref(const any_pointer& other)
    : content(other.content ? other.content->to_any_ptr_ref_holder() : 0)
  {
  }


  inline any_ptr_ref& any_ptr_ref::operator++()
  {
    content->increment();
    return *this;
  }
  inline any_ptr_ref& any_ptr_ref::operator--()
  {
    content->decrement();
    return *this;
  }
  inline any_reference any_ptr_ref::operator*() const
  {
    return content->dereference();
  }
  inline any_reference any_ptr_ref::operator[](std::ptrdiff_t n) const
  {
    return content->arrayelt(n);
  }
  inline any_pointer any_ptr_ref::operator+(std::ptrdiff_t n) const
  {
    return content->advance(n);
  }
  inline any_pointer any_ptr_ref::operator-(std::ptrdiff_t n) const
  {
    return content->advance(-n);
  }
  inline std::ptrdiff_t any_ptr_ref::operator-(const any_ptr_ref& other) const
  {
    return content->diff(other.content);
  }

  inline bool any_ptr_ref::operator==(const any_ptr_ref& other) const
  {
    return content->equal(other.content);
  }
  inline bool any_ptr_ref::operator!=(const any_ptr_ref& other) const
  {
    return ! content->equal(other.content);
  }
  inline bool any_ptr_ref::operator<(const any_ptr_ref& other) const
  {
    return content->less_than(other.content);
  }
  inline bool any_ptr_ref::operator<=(const any_ptr_ref& other) const
  {
    return ! other.content->less_than(content);
  }
  inline bool any_ptr_ref::operator>(const any_ptr_ref& other) const
  {
    return other.content->less_than(content);
  }
  inline bool any_ptr_ref::operator>=(const any_ptr_ref& other) const
  {
    return !content->less_than(other.content);
  }

  inline any_ptr_ref::~any_ptr_ref()
  {
    delete content;
  }

  inline const std::type_info & any_ptr_ref::type() const
  {
    return content ? content->type() : typeid(void);
  }

  // ======================================================================


  template<typename ValueType>
  class any_const_ptr_ref::holder : public any_const_ptr_ref::placeholder
  {
  public: // structors

    holder(ValueType const*& ptr)
      : held(ptr)
    {
    }

  public: // queries

    virtual const std::type_info & type() const
    {
      return typeid(ValueType);
    }

    virtual placeholder * clone() const
    {
      return new holder(held);
    }

    virtual any::placeholder * to_any_holder() const
    {
      return new any::holder<ValueType const*>(held);
    }

    virtual any_reference::placeholder* to_any_ref_holder() const
    {
      return new any_reference::holder<ValueType const*>(held);
    }

    virtual any_const_reference::placeholder* to_any_const_ref_holder() const
    {
      return new any_const_reference::holder<ValueType const*>(held);
    }

    virtual any_const_pointer::placeholder* to_const_ptr_holder() const
    {
      return new any_const_pointer::holder<ValueType>(held);
    }

    virtual any_const_ptr_const_ref::placeholder* to_any_const_ptr_const_ref_holder() const
    {
      return new any_const_ptr_const_ref::holder<ValueType>(held);
    }

    virtual void increment()
    {
      ++held;
    }

    virtual void decrement()
    {
      --held;
    }

    virtual any_const_reference dereference() const
    {
      return *held;
    }

    virtual any_const_reference arrayelt(std::ptrdiff_t n) const
    {
      return held[n];
    }
    virtual any_const_pointer advance(std::ptrdiff_t n) const
    {
      return any_const_pointer(held + n);
    }
    virtual bool equal(const placeholder* p) const
    {
      return held == dynamic_cast<const holder<ValueType>*>(p)->held;
    }
    virtual bool less_than(const placeholder* p) const
    {
      return held < dynamic_cast<const holder<ValueType>*>(p)->held;
    }
    virtual std::ptrdiff_t diff(const placeholder* p) const
    {
      return held - dynamic_cast<const holder<ValueType>*>(p)->held;
    }

    virtual void assign(const any_const_pointer& other) const {
      if (other.type() == typeid(ValueType))
	held = static_cast<any_const_pointer::holder<ValueType> *>(other.content)->held;
      else
	boost::throw_exception(bad_any_cast());
    }

    virtual void assign(const any_const_ptr_ref& other) const {
      if (other.type() == typeid(ValueType))
	held = static_cast<any_const_ptr_ref::holder<ValueType> *>(other.content)->held;
      else
	boost::throw_exception(bad_any_cast());
    }

  public: // representation

    ValueType const*& held;

  };
  // Illegal
  template<>
  class any_const_ptr_ref::holder<void>;


  inline any_const_ptr_ref::any_const_ptr_ref(const any_const_ptr_ref & other)
    : content(other.content ? other.content->clone() : 0)
  {
  }
  inline any_const_ptr_ref::any_const_ptr_ref(const any_ptr_ref & other)
    : content(other.content ? other.content->to_any_const_ptr_ref_holder() : 0)
  {
  }

  inline any_const_ptr_ref& any_const_ptr_ref::operator++()
  {
    content->increment();
    return *this;
  }
  inline any_const_ptr_ref& any_const_ptr_ref::operator--()
  {
    content->decrement();
    return *this;
  }
  inline any_const_reference any_const_ptr_ref::operator*() const
  {
    return content->dereference();
  }
  inline any_const_reference any_const_ptr_ref::operator[](std::ptrdiff_t n) const
  {
    return content->arrayelt(n);
  }
  inline any_const_pointer any_const_ptr_ref::operator+(std::ptrdiff_t n) const
  {
    return content->advance(n);
  }
  inline any_const_pointer any_const_ptr_ref::operator-(std::ptrdiff_t n) const
  {
    return content->advance(-n);
  }
  inline std::ptrdiff_t any_const_ptr_ref::operator-(const any_const_ptr_ref& other) const
  {
    return content->diff(other.content);
  }

  inline bool any_const_ptr_ref::operator==(const any_const_ptr_ref& other) const
  {
    return content->equal(other.content);
  }
  inline bool any_const_ptr_ref::operator!=(const any_const_ptr_ref& other) const
  {
    return ! content->equal(other.content);
  }
  inline bool any_const_ptr_ref::operator<(const any_const_ptr_ref& other) const
  {
    return content->less_than(other.content);
  }
  inline bool any_const_ptr_ref::operator<=(const any_const_ptr_ref& other) const
  {
    return ! other.content->less_than(content);
  }
  inline bool any_const_ptr_ref::operator>(const any_const_ptr_ref& other) const
  {
    return other.content->less_than(content);
  }
  inline bool any_const_ptr_ref::operator>=(const any_const_ptr_ref& other) const
  {
    return !content->less_than(other.content);
  }



  inline any_const_ptr_ref::~any_const_ptr_ref()
  {
    delete content;
  }

  inline const std::type_info & any_const_ptr_ref::type() const
  {
    return content ? content->type() : typeid(void);
  }

  // ======================================================================


  template<typename ValueType>
  class any_const_ptr_const_ref::holder : public any_const_ptr_const_ref::placeholder
  {
  public: // structors

    holder(ValueType const* const& ptr)
      : held(ptr)
    {
    }

  public: // queries

    virtual const std::type_info & type() const
    {
      return typeid(ValueType);
    }

    virtual placeholder * clone() const
    {
      return new holder(held);
    }

    virtual any::placeholder * to_any_holder() const
    {
      return new any::holder<ValueType const*>(held);
    }

    virtual any_const_reference::placeholder* to_any_const_ref_holder() const
    {
      return new any_const_reference::holder<ValueType const*>(held);
    }

    virtual any_const_pointer::placeholder* to_const_ptr_holder() const
    {
      return new any_const_pointer::holder<ValueType>(held);
    }

    virtual any_const_reference dereference() const
    {
      return *held;
    }

    virtual any_const_reference arrayelt(std::ptrdiff_t n) const
    {
      return held[n];
    }
    virtual any_const_pointer advance(std::ptrdiff_t n) const
    {
      return any_const_pointer(held + n);
    }
    virtual bool equal(const placeholder* p) const
    {
      return held == dynamic_cast<const holder<ValueType>*>(p)->held;
    }
    virtual bool less_than(const placeholder* p) const
    {
      return held < dynamic_cast<const holder<ValueType>*>(p)->held;
    }
    virtual std::ptrdiff_t diff(const placeholder* p) const
    {
      return held - dynamic_cast<const holder<ValueType>*>(p)->held;
    }

  public: // representation

    ValueType const* const& held;

  };

  template<>
  class any_const_ptr_const_ref::holder<void>;

  inline any_const_ptr_const_ref::any_const_ptr_const_ref(const any_const_ptr_const_ref & other)
    : content(other.content ? other.content->clone() : 0)
  {
  }

  inline any_const_ptr_const_ref::any_const_ptr_const_ref(const any_const_ptr_ref & other)
    : content(other.content ? other.content->to_any_const_ptr_const_ref_holder() : 0)
  {
  }
  inline any_const_ptr_const_ref::any_const_ptr_const_ref(const any_ptr_const_ref & other)
    : content(other.content ? other.content->to_any_const_ptr_const_ref_holder() : 0)
  {
  }
  inline any_const_ptr_const_ref::any_const_ptr_const_ref(const any_ptr_ref & other)
    : content(other.content ? other.content->to_any_const_ptr_const_ref_holder() : 0)
  {
  }

  inline any_const_reference any_const_ptr_const_ref::operator*() const
  {
    return content->dereference();
  }
  inline any_const_reference any_const_ptr_const_ref::operator[](std::ptrdiff_t n) const
  {
    return content->arrayelt(n);
  }
  inline any_const_pointer any_const_ptr_const_ref::operator+(std::ptrdiff_t n) const
  {
    return content->advance(n);
  }
  inline any_const_pointer any_const_ptr_const_ref::operator-(std::ptrdiff_t n) const
  {
    return content->advance(-n);
  }
  inline std::ptrdiff_t any_const_ptr_const_ref::operator-(const any_const_ptr_const_ref& other) const
  {
    return content->diff(other.content);
  }

  inline bool any_const_ptr_const_ref::operator==(const any_const_ptr_const_ref& other) const
  {
    return content->equal(other.content);
  }
  inline bool any_const_ptr_const_ref::operator!=(const any_const_ptr_const_ref& other) const
  {
    return ! content->equal(other.content);
  }
  inline bool any_const_ptr_const_ref::operator<(const any_const_ptr_const_ref& other) const
  {
    return content->less_than(other.content);
  }
  inline bool any_const_ptr_const_ref::operator<=(const any_const_ptr_const_ref& other) const
  {
    return ! other.content->less_than(content);
  }
  inline bool any_const_ptr_const_ref::operator>(const any_const_ptr_const_ref& other) const
  {
    return other.content->less_than(content);
  }
  inline bool any_const_ptr_const_ref::operator>=(const any_const_ptr_const_ref& other) const
  {
    return !content->less_than(other.content);
  }

  inline any_const_ptr_const_ref::~any_const_ptr_const_ref()
  {
    delete content;
  }

  inline const std::type_info & any_const_ptr_const_ref::type() const
  {
    return content ? content->type() : typeid(void);
  }

  // ======================================================================


  template<typename ValueType>
  class any_ptr_const_ref::holder : public any_ptr_const_ref::placeholder
  {
  public: // structors

    holder(ValueType* const& ptr)
      : held(ptr)
    {
    }

  public: // queries

    virtual const std::type_info & type() const
    {
      return typeid(ValueType);
    }

    virtual placeholder * clone() const
    {
      return new holder(held);
    }

    virtual any::placeholder * to_any_holder() const
    {
      return new any::holder<ValueType*>(held);
    }

    virtual any_const_reference::placeholder * to_any_const_ref_holder() const 
    {
      return new any_const_reference::holder<ValueType*>(held);
    }

    virtual any_pointer::placeholder* to_ptr_holder() const
    {
      return new any_pointer::holder<ValueType>(held);
    }

    virtual any_const_pointer::placeholder* to_const_ptr_holder() const
    {
      return new any_const_pointer::holder<ValueType>(held);
    }

    virtual any_const_ptr_const_ref::placeholder * to_any_const_ptr_const_ref_holder() const 
    {
      return new any_const_ptr_const_ref::holder<ValueType>(held);
    }


    virtual void* to_void_ptr() const
    {
      return held;
    }

    virtual any_reference dereference() const
    {
      return *held;
    }

    virtual any_reference arrayelt(std::ptrdiff_t n) const
    {
      return held[n];
    }
    virtual any_pointer advance(std::ptrdiff_t n) const
    {
      return any_pointer(held + n);
    }
    virtual bool equal(const placeholder* p) const
    {
      return held == dynamic_cast<const holder<ValueType>*>(p)->held;
    }
    virtual bool less_than(const placeholder* p) const
    {
      return held < dynamic_cast<const holder<ValueType>*>(p)->held;
    }
    virtual std::ptrdiff_t diff(const placeholder* p) const
    {
      return held - dynamic_cast<const holder<ValueType>*>(p)->held;
    }

  public: // representation

    ValueType* const& held;

  };

  // Illegal
  template<typename T>
  class any_ptr_const_ref::holder<T const>;
  template<>
  class any_ptr_const_ref::holder<void>;


  inline any_ptr_const_ref::any_ptr_const_ref(const any_ptr_const_ref & other)
    : content(other.content ? other.content->clone() : 0)
  {
  }

  inline any_ptr_const_ref::any_ptr_const_ref(const any_ptr_ref & other)
    : content(other.content ? other.content->to_any_ptr_const_ref_holder() : 0)    
  {
  }

#if 0
  inline any_ptr_const_ref::any_ptr_const_ref(const any_pointer & other)
    : content(other.content ? other.content->to_any_ptr_const_ref_holder() : 0)    
  {
  }
#else //debugging, getting a bus error from the above code

  inline any_ptr_const_ref::any_ptr_const_ref(const any_pointer & other)
    : content(other.content ? other.content->to_any_ptr_const_ref_holder() : 0)    
  {
    if (other.content)
      content = other.content->to_any_ptr_const_ref_holder();
    else
      content = 0;
  }
#endif

  inline any_reference any_ptr_const_ref::operator*() const
  {
    return content->dereference();
  }
  inline any_ptr_const_ref::operator void*() const
  {
    return content->to_void_ptr();
  }
  inline any_reference any_ptr_const_ref::operator[](std::ptrdiff_t n) const
  {
    return content->arrayelt(n);
  }
  inline any_pointer any_ptr_const_ref::operator+(std::ptrdiff_t n) const
  {
    return content->advance(n);
  }
  inline any_pointer any_ptr_const_ref::operator-(std::ptrdiff_t n) const
  {
    return content->advance(-n);
  }
  inline std::ptrdiff_t any_ptr_const_ref::operator-(const any_ptr_const_ref& other) const
  {
    return content->diff(other.content);
  }

  inline bool any_ptr_const_ref::operator==(const any_ptr_const_ref& other) const
  {
    return content->equal(other.content);
  }
  inline bool any_ptr_const_ref::operator!=(const any_ptr_const_ref& other) const
  {
    return ! content->equal(other.content);
  }
  inline bool any_ptr_const_ref::operator<(const any_ptr_const_ref& other) const
  {
    return content->less_than(other.content);
  }
  inline bool any_ptr_const_ref::operator<=(const any_ptr_const_ref& other) const
  {
    return ! other.content->less_than(content);
  }
  inline bool any_ptr_const_ref::operator>(const any_ptr_const_ref& other) const
  {
    return other.content->less_than(content);
  }
  inline bool any_ptr_const_ref::operator>=(const any_ptr_const_ref& other) const
  {
    return !content->less_than(other.content);
  }

  inline any_ptr_const_ref::~any_ptr_const_ref()
  {
    delete content;
  }

  inline const std::type_info & any_ptr_const_ref::type() const
  {
    return content ? content->type() : typeid(void);
  }


  // ======================================================================

  template<typename ValueType>
  ValueType** any_cast(to_type<ValueType>, any_pointer * operand)
  {
    return operand && operand->type() == typeid(ValueType)
      ? &static_cast<any_pointer::holder<ValueType> *>(operand->content)->held
      : 0;
  }

  template<typename ValueType>
  ValueType* any_cast(to_type<ValueType*>, const any_pointer& operand)
  {
    ValueType** result = any_cast(to_type<ValueType>(), (any_pointer*)&operand);
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
#endif
      boost::throw_exception(bad_any_cast());
    }
    return *result;
  }

  // Casting to a reference when the any_pointer is not a temporary
  template<typename ValueType>
  ValueType*& any_cast(to_type<ValueType*&>, const any_pointer& operand)
  {
    ValueType** result = any_cast(to_type<ValueType>(), (any_pointer*)&operand);
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
#endif
      boost::throw_exception(bad_any_cast());
    }
    return *result;
  }


  // ======================================================================

  template<typename ValueType>
  ValueType const* * any_cast(to_type<ValueType>, any_const_pointer * operand)
  {
    return operand && operand->type() == typeid(ValueType)
      ? &static_cast<any_const_pointer::holder<ValueType> *>(operand->content)->held
      : 0;
  }

  template<typename ValueType>
  ValueType const*& any_cast(to_type<ValueType const*>, const any_const_pointer& operand)
  {
    ValueType const ** result = any_cast(to_type<ValueType>(), (any_const_pointer*)&operand);
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
      boost::throw_exception(bad_any_cast());
#endif
    }
    return *result;
  }

  // ======================================================================

  template<typename ValueType>
  ValueType** any_cast(to_type<ValueType>, any_ptr_ref * operand)
  {
    return operand && operand->type() == typeid(ValueType)
      ? &static_cast<any_ptr_ref::holder<ValueType> *>(operand->content)->held
      : 0;
  }

  template<typename ValueType>
  ValueType*& any_cast(to_type<ValueType*&>, const any_ptr_ref& operand)
  {
    ValueType ** result = any_cast(to_type<ValueType>(), (any_ptr_ref*)&operand);
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
#endif
      boost::throw_exception(bad_any_cast());
    }
    return *result;
  }

  template<typename ValueType>
  ValueType*& any_cast(to_type<ValueType*>, const any_ptr_ref& operand)
  {
    ValueType ** result = const_cast<ValueType**>(any_cast(to_type<ValueType>(), (any_ptr_ref*)&operand));
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
#endif
      boost::throw_exception(bad_any_cast());
    }
    return *result;
  }

  // ======================================================================

  template<typename ValueType>
  ValueType const ** any_cast(to_type<ValueType>, any_const_ptr_ref * operand)
  {
    return operand && operand->type() == typeid(ValueType)
      ? &static_cast<any_const_ptr_ref::holder<ValueType> *>(operand->content)->held
      : 0;
  }

  template<typename ValueType>
  ValueType const*& any_cast(to_type<ValueType const*&>, const any_const_ptr_ref& operand)
  {
    ValueType const** result = any_cast(to_type<ValueType>(), (any_const_ptr_ref*)&operand);
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
#endif
      boost::throw_exception(bad_any_cast());
    }
    return *result;
  }

  template<typename ValueType>
  ValueType const*& any_cast(to_type<ValueType const*>, const any_const_ptr_ref& operand)
  {
    ValueType const** result = any_cast(to_type<ValueType>(), (any_const_ptr_ref*)&operand);
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
      boost::throw_exception(bad_any_cast());
#endif
    }
    return *result;
  }

  // ======================================================================

  template<typename ValueType>
  ValueType const ** any_cast(to_type<ValueType>, any_const_ptr_const_ref * operand)
  {
    return operand && operand->type() == typeid(ValueType)
      ? &static_cast<any_const_ptr_ref::holder<ValueType> *>(operand->content)->held
      : 0;
  }

  template<typename ValueType>
  ValueType const* const& any_cast(to_type<ValueType const* const&>, 
				   const any_const_ptr_const_ref& operand)
  {
    ValueType const** result = any_cast(to_type<ValueType>(), (any_const_ptr_const_ref*)&operand);
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
#endif
      boost::throw_exception(bad_any_cast());
    }
    return *result;
  }

  template<typename ValueType>
  ValueType const* const& any_cast(to_type<ValueType const* const>, 
				   const any_const_ptr_const_ref& operand)
  {
    ValueType const** result = any_cast(to_type<ValueType>(), (any_const_ptr_const_ref*)&operand);
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
#endif
      boost::throw_exception(bad_any_cast());
    }
    return *result;
  }

  // ======================================================================

  template<typename ValueType>
  ValueType*const* any_cast(to_type<ValueType>, any_ptr_const_ref * operand)
  {
    return operand && operand->type() == typeid(ValueType)
      ? &static_cast<any_ptr_const_ref::holder<ValueType> *>(operand->content)->held
      : 0;
  }

  template<typename ValueType>
  ValueType* const& any_cast(to_type<ValueType* const&>, 
			     const any_ptr_const_ref& operand)
  {
    ValueType*const* result = any_cast(to_type<ValueType>(), (any_ptr_const_ref*)&operand);
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
#endif
      boost::throw_exception(bad_any_cast());
    }
    return *result;
  }

  template<typename ValueType>
  ValueType* const& any_cast(to_type<ValueType* const>, 
			     const any_ptr_const_ref& operand)
  {
    ValueType*const* result = any_cast(to_type<ValueType>(), (any_ptr_const_ref*)&operand);
    if(!result) {
#if defined(__APPLE_CC__)
      char* operand_name = (char*)malloc(sizeof(char) * 1000);
      char* this_name = (char*)malloc(sizeof(char) * 1000);
      int status;
      size_t size = 1000;
      abi::__cxa_demangle(operand.type().name(), operand_name, &size, &status);
      abi::__cxa_demangle(typeid(ValueType).name(), this_name, &size, &status);
      std::cout << "could not cast from " << operand_name
		<< " to type " << this_name << std::endl;
#endif
      boost::throw_exception(bad_any_cast());
    }
    return *result;
  }

  // ======================================================================

  // U = T*
  inline any_pointer any_cast(to_type<any_pointer>, const any & operand)
  {
    return operand.content->ptr();
  }
  inline any_pointer any_cast(to_type<any_pointer>, any & operand)
  {
    return operand.content->ptr();
  }

  inline any_ptr_ref any_cast(to_type<any_ptr_ref>, const any_reference & operand)
  {
    return operand.content->ptr();
  }
  inline any_ptr_ref any_cast(to_type<any_ptr_ref>, any_reference & operand)
  {
    return operand.content->ptr();
  }

  inline any_ptr_const_ref any_cast(to_type<any_ptr_const_ref>, const any_const_reference & operand)
  {
    return operand.content->ptr();
  }
  inline any_ptr_const_ref any_cast(to_type<any_ptr_const_ref>, any_const_reference & operand)
  {
    return operand.content->ptr();
  }

  // U = T const*
  inline any_const_pointer any_cast(to_type<any_const_pointer>, const any & operand)
  {
    return operand.content->const_ptr();
  }
  inline any_const_pointer any_cast(to_type<any_const_pointer>, any & operand)
  {
    return operand.content->const_ptr();
  }

  inline any_const_ptr_ref any_cast(to_type<any_const_ptr_ref>, const any_reference & operand)
  {
    return operand.content->ptr();
  }
  inline any_const_ptr_ref any_cast(to_type<any_const_ptr_ref>, any_reference & operand)
  {
    return operand.content->ptr();
  }

  inline any_const_ptr_const_ref any_cast(to_type<any_const_ptr_const_ref>, const any_const_reference & operand)
  {
    return operand.content->ptr();
  }
  inline any_const_ptr_const_ref any_cast(to_type<any_const_ptr_const_ref>, any_const_reference & operand)
  {
    return operand.content->ptr();
  }

  // Need this for builtin's, like assignment
  template <class T>
  T& any_cast(to_type<T>, T& x) { return x; }
  template <class T>
  T& any_cast(to_type<T&>, T& x) { return x; }

  template <class T>
  const T& any_cast(to_type<T>, const T& x) { return x; }
  template <class T>
  const T& any_cast(to_type<T&>, const T& x) { return x; }


} // namespace __g


#endif

