~ubuntu-branches/ubuntu/saucy/merkaartor/saucy

« back to all changes in this revision

Viewing changes to include/builtin-boost/boost/parameter/aux_/maybe.hpp

Tags: upstream-0.15.3+svn20934
ImportĀ upstreamĀ versionĀ 0.15.3+svn20934

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright Daniel Wallin 2006. Use, modification and distribution is
 
2
// subject to the Boost Software License, Version 1.0. (See accompanying
 
3
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 
4
 
 
5
#ifndef BOOST_PARAMETER_MAYBE_060211_HPP
 
6
# define BOOST_PARAMETER_MAYBE_060211_HPP
 
7
 
 
8
# include <boost/mpl/if.hpp>
 
9
# include <boost/mpl/identity.hpp>
 
10
# include <boost/type_traits/is_reference.hpp>
 
11
# include <boost/type_traits/add_reference.hpp>
 
12
# include <boost/optional.hpp>
 
13
# include <boost/python/detail/referent_storage.hpp>
 
14
# include <boost/type_traits/remove_cv.hpp>
 
15
# include <boost/type_traits/add_const.hpp>
 
16
 
 
17
namespace boost { namespace parameter { namespace aux {
 
18
 
 
19
struct maybe_base {};
 
20
 
 
21
template <class T>
 
22
struct maybe : maybe_base
 
23
{
 
24
    typedef typename add_reference<
 
25
# if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
 
26
        T const
 
27
# else 
 
28
        typename add_const<T>::type
 
29
# endif 
 
30
    >::type reference;
 
31
    
 
32
    typedef typename remove_cv<
 
33
        BOOST_DEDUCED_TYPENAME remove_reference<reference>::type
 
34
    >::type non_cv_value;
 
35
        
 
36
    explicit maybe(T value)
 
37
      : value(value)
 
38
      , constructed(false)
 
39
    {}
 
40
 
 
41
    maybe()
 
42
      : constructed(false)
 
43
    {}
 
44
 
 
45
    ~maybe()
 
46
    {
 
47
        if (constructed)
 
48
            this->destroy();
 
49
    }
 
50
 
 
51
    reference construct(reference value) const
 
52
    {
 
53
        return value;
 
54
    }
 
55
 
 
56
    template <class U>
 
57
    reference construct2(U const& value) const
 
58
    {
 
59
        new (m_storage.bytes) non_cv_value(value);
 
60
        constructed = true;
 
61
        return *(non_cv_value*)m_storage.bytes;
 
62
    }
 
63
 
 
64
    template <class U>
 
65
    reference construct(U const& value) const
 
66
    {
 
67
        return this->construct2(value);
 
68
    }
 
69
 
 
70
    void destroy()
 
71
    {
 
72
        ((non_cv_value*)m_storage.bytes)->~non_cv_value();
 
73
    }
 
74
 
 
75
    typedef reference(maybe<T>::*safe_bool)() const;
 
76
 
 
77
    operator safe_bool() const
 
78
    {
 
79
        return value ? &maybe<T>::get : 0 ;
 
80
    }
 
81
 
 
82
    reference get() const
 
83
    {
 
84
        return value.get();
 
85
    }
 
86
 
 
87
private:
 
88
    boost::optional<T> value;
 
89
    mutable bool constructed;
 
90
    mutable typename boost::python::detail::referent_storage<
 
91
        reference
 
92
    >::type m_storage;
 
93
};
 
94
 
 
95
}}} // namespace boost::parameter::aux
 
96
 
 
97
#endif // BOOST_PARAMETER_MAYBE_060211_HPP
 
98