1 // Copyright Daniel Wallin 2006.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
7 // 2009.10.21 TDS remove depenency on boost::python::detail::referent_storage
9 #ifndef BOOST_PARAMETER_MAYBE_091021_HPP
10 #define BOOST_PARAMETER_MAYBE_091021_HPP
12 namespace boost { namespace parameter { namespace aux {
16 }}} // namespace boost::parameter::aux
18 #include <boost/parameter/config.hpp>
20 namespace boost { namespace parameter { namespace aux {
23 struct referent_size<T&>
25 BOOST_STATIC_CONSTANT(::std::size_t, value = sizeof(T));
27 }}} // namespace boost::parameter::aux
29 #include <boost/type_traits/aligned_storage.hpp>
31 namespace boost { namespace parameter { namespace aux {
33 // A metafunction returning a POD type which can store U, where T == U&.
34 // If T is not a reference type, returns a POD which can store T.
36 struct referent_storage
37 : ::boost::aligned_storage<
38 ::boost::parameter::aux::referent_size<T>::value
42 }}} // namespace boost::parameter::aux
44 #include <boost/parameter/aux_/is_maybe.hpp>
45 #include <boost/optional/optional.hpp>
47 #if defined(BOOST_PARAMETER_CAN_USE_MP11)
48 #include <type_traits>
49 #else // !defined(BOOST_PARAMETER_CAN_USE_MP11)
50 #include <boost/type_traits/add_lvalue_reference.hpp>
51 #include <boost/type_traits/remove_cv.hpp>
52 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
53 #include <boost/type_traits/add_const.hpp>
55 #endif // BOOST_PARAMETER_CAN_USE_MP11
57 namespace boost { namespace parameter { namespace aux {
60 struct maybe : ::boost::parameter::aux::maybe_base
62 #if defined(BOOST_PARAMETER_CAN_USE_MP11)
63 typedef typename ::std::add_lvalue_reference<
64 typename ::std::add_const<T>::type
65 #else // !defined(BOOST_PARAMETER_CAN_USE_MP11)
66 typedef typename ::boost::add_lvalue_reference<
67 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
70 typename ::boost::add_const<T>::type
72 #endif // BOOST_PARAMETER_CAN_USE_MP11
75 #if defined(BOOST_PARAMETER_CAN_USE_MP11)
76 typedef typename ::std::remove_cv<
77 typename ::std::remove_reference<reference>::type
79 typedef typename ::boost::remove_cv<
80 BOOST_DEDUCED_TYPENAME ::boost::remove_reference<reference>::type
84 inline explicit maybe(T value_) : value(value_), constructed(false)
88 inline maybe() : value(), constructed(false)
94 if (this->constructed)
100 inline reference construct(reference value_) const
105 template <typename U>
106 reference construct2(U const& value_) const
108 new (this->m_storage.address()) non_cv_value(value_);
109 this->constructed = true;
110 return *reinterpret_cast<non_cv_value*>(
111 this->m_storage.address()
115 template <typename U>
116 inline reference construct(U const& value_) const
118 return this->construct2(value_);
123 reinterpret_cast<non_cv_value*>(
124 this->m_storage.address()
129 ::boost::parameter::aux::maybe<T>::*safe_bool
132 inline operator safe_bool() const
134 return this->value ? &::boost::parameter::aux::maybe<T>::get : 0;
137 inline reference get() const
139 return this->value.get();
143 ::boost::optional<T> value;
144 mutable bool constructed;
145 mutable typename ::boost::parameter::aux
146 ::referent_storage<reference>::type m_storage;
148 }}} // namespace boost::parameter::aux
150 #endif // include guard