42#if !defined(BOOST_SPIRIT_HOLD_ANY_MAY_02_2007_0857AM)
43#define BOOST_SPIRIT_HOLD_ANY_MAY_02_2007_0857AM
49#include <boost/config.hpp>
50#include <boost/type_traits/remove_reference.hpp>
51#include <boost/type_traits/is_reference.hpp>
52#include <boost/throw_exception.hpp>
53#include <boost/static_assert.hpp>
54#include <boost/mpl/bool.hpp>
55#include <boost/assert.hpp>
56#include <boost/core/typeinfo.hpp>
65#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
67#pragma warning(disable : 4100)
68#pragma warning(disable : 4127)
76 boost::core::typeinfo
const& dest)
77 :
from(src.name()),
to(dest.name()) {}
79 virtual const char*
what()
const throw() {
return "bad any cast"; }
88 boost::core::typeinfo
const& (*get_type)();
91 void (*
clone)(
void*
const*,
void**);
92 void (*
move)(
void*
const*,
void**);
96template <
typename Small>
struct fxns;
98template <>
struct fxns<mpl::true_> {
99 template <
typename T,
typename Char>
struct type {
101 return BOOST_CORE_TYPEID(T);
104 static void destruct(
void** x) {
reinterpret_cast<T*
>(x)->~T(); }
105 static void clone(
void*
const* src,
void** dest) {
106 new (dest) T(*
reinterpret_cast<T const*
>(src));
108 static void move(
void*
const* src,
void** dest) {
109 *
reinterpret_cast<T*
>(dest) = *
reinterpret_cast<T const*
>(src);
115template <>
struct fxns<mpl::false_> {
116 template <
typename T,
typename Char>
struct type {
118 return BOOST_CORE_TYPEID(T);
122 delete (*
reinterpret_cast<T**
>(x));
126 (*
reinterpret_cast<T**
>(x))->~T();
128 static void clone(
void*
const* src,
void** dest) {
129 *dest =
new T(**
reinterpret_cast<T* const*
>(src));
131 static void move(
void*
const* src,
void** dest) {
132 **
reinterpret_cast<T**
>(dest) = **
reinterpret_cast<T* const*
>(src);
138 typedef mpl::bool_<(
sizeof(T) <=
sizeof(
void*))>
is_small;
148 return &static_table;
160 template <
typename T>
162 : table(
spirit::
detail::get_table<T>::template get<Char>()), object(0) {
167 : table(
spirit::
detail::get_table<::
std::string>::template get<Char>()),
169 new (&object)::std::string(x);
186 template <
typename T>
187 static void new_object(
void*&
object, T
const& x, mpl::true_) {
191 template <
typename T>
192 static void new_object(
void*&
object, T
const& x, mpl::false_) {
199 if (table == x.table) {
201 table->move(&x.object, &
object);
204 x.table->clone(&x.object, &
object);
215 if (table == x_table) {
217 table->destruct(&
object);
222 table->destruct(&
object);
245 std::swap(table, x.table);
246 std::swap(
object, x.object);
250 boost::core::typeinfo
const&
type()
const {
return table->get_type(); }
252 template <
typename T> T
const&
cast()
const {
253 if (
type() != BOOST_CORE_TYPEID(T))
257 ? *
reinterpret_cast<T const*
>(&object)
258 : *
reinterpret_cast<T const*
>(object);
264#ifdef BOOST_SPIRIT_ANY_IMPLICIT_CASTING
266 template <
typename T>
operator T
const&()
const {
return cast<T>(); }
276 table->static_delete(&
object);
283#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
285 template <
typename T,
typename Char_>
296template <
typename T,
typename Char>
298 if (operand && operand->
type() == BOOST_CORE_TYPEID(T)) {
300 ?
reinterpret_cast<T*
>(&operand->object)
301 :
reinterpret_cast<T*
>(operand->object);
306template <
typename T,
typename Char>
312 typedef BOOST_DEDUCED_TYPENAME remove_reference<T>::type nonref;
314#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
321 BOOST_STATIC_ASSERT(!is_reference<nonref>::value);
330template <
typename T,
typename Char>
332 typedef BOOST_DEDUCED_TYPENAME remove_reference<T>::type nonref;
334#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
337 BOOST_STATIC_ASSERT(!is_reference<nonref>::value);
351template <
typename Char>
359#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
basic_hold_any & operator=(basic_hold_any const &x)
basic_hold_any & operator=(T const &x)
const void * castsmallvoid() const
basic_hold_any(basic_hold_any const &x)
basic_hold_any & swap(basic_hold_any &x)
basic_hold_any & assign(T const &x)
static void new_object(void *&object, T const &x, mpl::true_)
basic_hold_any(T const &x)
basic_hold_any & assign(basic_hold_any const &x)
static void new_object(void *&object, T const &x, mpl::false_)
boost::core::typeinfo const & type() const
friend T * any_cast(basic_hold_any< Char_ > *)
basic_hold_any(const char *x)
basic_hold_any< wchar_t > whold_any
basic_hold_any< char > hold_any
T * any_cast(basic_hold_any< Char > *operand)
bad_any_cast(boost::core::typeinfo const &src, boost::core::typeinfo const &dest)
virtual const char * what() const
void(* static_delete)(void **)
void(* move)(void *const *, void **)
void(* destruct)(void **)
void(* clone)(void *const *, void **)
static boost::core::typeinfo const & get_type()
static void destruct(void **x)
static void move(void *const *src, void **dest)
static void static_delete(void **x)
static void clone(void *const *src, void **dest)
static void move(void *const *src, void **dest)
static void clone(void *const *src, void **dest)
static boost::core::typeinfo const & get_type()
static void static_delete(void **x)
static void destruct(void **x)
static fxn_ptr_table< Char > * get()
mpl::bool_<(sizeof(T)<=sizeof(void *))> is_small