|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r80967 - in sandbox/type_erasure: boost/type_erasure libs/type_erasure/test
From: steven_at_[hidden]
Date: 2012-10-12 14:55:43
Author: steven_watanabe
Date: 2012-10-12 14:55:42 EDT (Fri, 12 Oct 2012)
New Revision: 80967
URL: http://svn.boost.org/trac/boost/changeset/80967
Log:
Handle array-to-pointer decay.
Text files modified:
sandbox/type_erasure/boost/type_erasure/any.hpp | 32 ++++++++++++++++++++++++++++++++
sandbox/type_erasure/libs/type_erasure/test/test_construct.cpp | 31 +++++++++++++++++++++++++++++++
2 files changed, 63 insertions(+), 0 deletions(-)
Modified: sandbox/type_erasure/boost/type_erasure/any.hpp
==============================================================================
--- sandbox/type_erasure/boost/type_erasure/any.hpp (original)
+++ sandbox/type_erasure/boost/type_erasure/any.hpp 2012-10-12 14:55:42 EDT (Fri, 12 Oct 2012)
@@ -207,6 +207,30 @@
BOOST_MPL_ASSERT((::boost::is_same<
typename ::boost::mpl::at<Map, T>::type, U>));
}
+ // Handle array/function-to-pointer decay
+ /** INTERNAL ONLY */
+ template<class U>
+ any(U* data_arg)
+ : table((
+ BOOST_TYPE_ERASURE_INSTANTIATE1(Concept, T, U*),
+ ::boost::type_erasure::make_binding<
+ ::boost::mpl::map< ::boost::mpl::pair<T, U*> >
+ >()
+ )),
+ data(data_arg)
+ {}
+ /** INTERNAL ONLY */
+ template<class U, class Map>
+ any(U* data_arg, const static_binding<Map>& binding_arg)
+ : table((
+ BOOST_TYPE_ERASURE_INSTANTIATE(Concept, Map),
+ binding_arg
+ )),
+ data(data_arg)
+ {
+ BOOST_MPL_ASSERT((::boost::is_same<
+ typename ::boost::mpl::at<Map, T>::type, U*>));
+ }
/**
* Copies an @ref any.
*
@@ -402,6 +426,14 @@
data(data_arg)
{}
template<class U, class Map>
+ any(U* data_arg, static_binding<Map>& binding_arg)
+ : table((
+ BOOST_TYPE_ERASURE_INSTANTIATE(Concept, Map),
+ binding_arg
+ )),
+ data(data_arg)
+ {}
+ template<class U, class Map>
any(const U& data_arg, static_binding<Map>& binding_arg)
: table((
BOOST_TYPE_ERASURE_INSTANTIATE(Concept, Map),
Modified: sandbox/type_erasure/libs/type_erasure/test/test_construct.cpp
==============================================================================
--- sandbox/type_erasure/libs/type_erasure/test/test_construct.cpp (original)
+++ sandbox/type_erasure/libs/type_erasure/test/test_construct.cpp 2012-10-12 14:55:42 EDT (Fri, 12 Oct 2012)
@@ -34,6 +34,37 @@
BOOST_CHECK_EQUAL(any_cast<int>(x), 1);
}
+void func() {}
+
+BOOST_AUTO_TEST_CASE(test_decay) {
+ char array[] = "Hello World!";
+ const char carray[] = "Hello World!";
+
+ any<common<> > x1(array);
+ any<common<> > y1(func);
+ any<common<> > z1(carray);
+ BOOST_CHECK_EQUAL(any_cast<char *>(x1), &array[0]);
+ BOOST_CHECK(any_cast<void(*)()>(y1) == &func);
+ BOOST_CHECK_EQUAL(any_cast<const char *>(z1), &carray[0]);
+
+ any<common<> > x2(array, make_binding<boost::mpl::map<boost::mpl::pair<_self, char *> > >());
+ any<common<> > y2(func, make_binding<boost::mpl::map<boost::mpl::pair<_self, void(*)()> > >());
+ any<common<> > z2(carray, make_binding<boost::mpl::map<boost::mpl::pair<_self, const char *> > >());
+ BOOST_CHECK_EQUAL(any_cast<char *>(x2), &array[0]);
+ BOOST_CHECK(any_cast<void(*)()>(y2) == &func);
+ BOOST_CHECK_EQUAL(any_cast<const char *>(z2), &carray[0]);
+
+ static_binding<boost::mpl::map<boost::mpl::pair<_self, char *> > > bx3;
+ static_binding<boost::mpl::map<boost::mpl::pair<_self, void (*)()> > > by3;
+ static_binding<boost::mpl::map<boost::mpl::pair<_self, const char *> > > bz3;
+ any<common<> > x3(array, bx3);
+ any<common<> > y3(func, by3);
+ any<common<> > z3(carray, bz3);
+ BOOST_CHECK_EQUAL(any_cast<char *>(x3), &array[0]);
+ BOOST_CHECK(any_cast<void(*)()>(y3) == &func);
+ BOOST_CHECK_EQUAL(any_cast<const char *>(z3), &carray[0]);
+}
+
BOOST_AUTO_TEST_CASE(test_unary)
{
typedef ::boost::mpl::vector<
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk