1 // Boost.TypeErasure library
3 // Copyright 2011 Steven Watanabe
5 // Distributed under the Boost Software License Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_TYPE_ERASURE_DETAIL_STORAGE_HPP_INCLUDED
12 #define BOOST_TYPE_ERASURE_DETAIL_STORAGE_HPP_INCLUDED
14 #include <boost/config.hpp>
15 #include <boost/type_traits/remove_reference.hpp>
16 #include <boost/type_traits/remove_cv.hpp>
18 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
19 # include <utility> // for std::forward, std::move
24 #pragma warning(disable:4521)
28 namespace type_erasure {
34 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
35 storage(storage& other) : data(other.data) {}
36 storage(const storage& other) : data(other.data) {}
37 storage(storage&& other) : data(other.data) {}
38 storage& operator=(const storage& other) { data = other.data; return *this; }
40 explicit storage(T&& arg) : data(new typename remove_cv<
41 typename remove_reference<T>::type>::type(std::forward<T>(arg))) {}
44 explicit storage(const T& arg) : data(new T(arg)) {}
50 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
53 T extract(T arg) { return std::forward<T>(arg); }
58 T extract(T arg) { return arg; }
63 T extract(storage& arg)
65 return *static_cast<typename ::boost::remove_reference<T>::type*>(arg.data);
69 T extract(const storage& arg)
71 return *static_cast<const typename ::boost::remove_reference<T>::type*>(arg.data);
74 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
77 T extract(storage&& arg)
79 return std::move(*static_cast<typename ::boost::remove_reference<T>::type*>(arg.data));