Files

152 lines
4.3 KiB
C++

// Copyright Daniel Wallin 2006.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// 2009.10.21 TDS remove depenency on Boost::python::detail::referent_storage
//
#ifndef ACTIV_BOOST_PARAMETER_MAYBE_091021_HPP
#define ACTIV_BOOST_PARAMETER_MAYBE_091021_HPP
namespace Boost { namespace parameter { namespace aux {
template <typename T>
struct referent_size;
}}} // namespace Boost::parameter::aux
#include <ActivBoost/parameter/config.hpp>
namespace Boost { namespace parameter { namespace aux {
template <typename T>
struct referent_size<T&>
{
ACTIV_BOOST_STATIC_CONSTANT(::std::size_t, value = sizeof(T));
};
}}} // namespace Boost::parameter::aux
#include <ActivBoost/type_traits/aligned_storage.hpp>
namespace Boost { namespace parameter { namespace aux {
// A metafunction returning a POD type which can store U, where T == U&.
// If T is not a reference type, returns a POD which can store T.
template <typename T>
struct referent_storage
: ::Boost::aligned_storage<
::Boost::parameter::aux::referent_size<T>::value
>
{
};
}}} // namespace Boost::parameter::aux
#include <ActivBoost/parameter/aux_/is_maybe.hpp>
#include <ActivBoost/optional/optional.hpp>
#if defined(ACTIV_BOOST_PARAMETER_CAN_USE_MP11)
#include <type_traits>
#else // !defined(ACTIV_BOOST_PARAMETER_CAN_USE_MP11)
#include <ActivBoost/type_traits/add_lvalue_reference.hpp>
#include <ActivBoost/type_traits/remove_cv.hpp>
#if !ACTIV_BOOST_WORKAROUND(ACTIV_BOOST_BORLANDC, ACTIV_BOOST_TESTED_AT(0x564))
#include <ActivBoost/type_traits/add_const.hpp>
#endif
#endif // ACTIV_BOOST_PARAMETER_CAN_USE_MP11
namespace Boost { namespace parameter { namespace aux {
template <typename T>
struct maybe : ::Boost::parameter::aux::maybe_base
{
#if defined(ACTIV_BOOST_PARAMETER_CAN_USE_MP11)
typedef typename ::std::add_lvalue_reference<
typename ::std::add_const<T>::type
#else // !defined(ACTIV_BOOST_PARAMETER_CAN_USE_MP11)
typedef typename ::Boost::add_lvalue_reference<
#if ACTIV_BOOST_WORKAROUND(ACTIV_BOOST_BORLANDC, ACTIV_BOOST_TESTED_AT(0x564))
T const
#else
typename ::Boost::add_const<T>::type
#endif
#endif // ACTIV_BOOST_PARAMETER_CAN_USE_MP11
>::type reference;
#if defined(ACTIV_BOOST_PARAMETER_CAN_USE_MP11)
typedef typename ::std::remove_cv<
typename ::std::remove_reference<reference>::type
#else
typedef typename ::Boost::remove_cv<
ACTIV_BOOST_DEDUCED_TYPENAME ::Boost::remove_reference<reference>::type
#endif
>::type non_cv_value;
inline explicit maybe(T value_) : value(value_), constructed(false)
{
}
inline maybe() : value(), constructed(false)
{
}
~maybe()
{
if (this->constructed)
{
this->destroy();
}
}
inline reference construct(reference value_) const
{
return value_;
}
template <typename U>
reference construct2(U const& value_) const
{
new (this->m_storage.address()) non_cv_value(value_);
this->constructed = true;
return *reinterpret_cast<non_cv_value*>(
this->m_storage.address()
);
}
template <typename U>
inline reference construct(U const& value_) const
{
return this->construct2(value_);
}
void destroy()
{
reinterpret_cast<non_cv_value*>(
this->m_storage.address()
)->~non_cv_value();
}
typedef reference(
::Boost::parameter::aux::maybe<T>::*safe_bool
)() const;
inline operator safe_bool() const
{
return this->value ? &::Boost::parameter::aux::maybe<T>::get : 0;
}
inline reference get() const
{
return this->value.get();
}
private:
::Boost::optional<T> value;
mutable bool constructed;
mutable typename ::Boost::parameter::aux
::referent_storage<reference>::type m_storage;
};
}}} // namespace Boost::parameter::aux
#endif // include guard