os/ossrv/ossrv_pub/boost_apis/boost/variant/get.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
//-----------------------------------------------------------------------------
sl@0
     2
// boost variant/get.hpp header file
sl@0
     3
// See http://www.boost.org for updates, documentation, and revision history.
sl@0
     4
//-----------------------------------------------------------------------------
sl@0
     5
//
sl@0
     6
// Copyright (c) 2003
sl@0
     7
// Eric Friedman, Itay Maman
sl@0
     8
//
sl@0
     9
// Distributed under the Boost Software License, Version 1.0. (See
sl@0
    10
// accompanying file LICENSE_1_0.txt or copy at
sl@0
    11
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
    12
sl@0
    13
#ifndef BOOST_VARIANT_GET_HPP
sl@0
    14
#define BOOST_VARIANT_GET_HPP
sl@0
    15
sl@0
    16
#include <exception>
sl@0
    17
sl@0
    18
#include "boost/config.hpp"
sl@0
    19
#include "boost/detail/workaround.hpp"
sl@0
    20
#include "boost/utility/addressof.hpp"
sl@0
    21
#include "boost/variant/variant_fwd.hpp"
sl@0
    22
sl@0
    23
#include "boost/type_traits/add_reference.hpp"
sl@0
    24
#include "boost/type_traits/add_pointer.hpp"
sl@0
    25
sl@0
    26
#if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
sl@0
    27
#   include "boost/mpl/bool.hpp"
sl@0
    28
#   include "boost/mpl/or.hpp"
sl@0
    29
#   include "boost/type_traits/is_same.hpp"
sl@0
    30
#endif
sl@0
    31
sl@0
    32
namespace boost {
sl@0
    33
sl@0
    34
//////////////////////////////////////////////////////////////////////////
sl@0
    35
// class bad_get
sl@0
    36
//
sl@0
    37
// The exception thrown in the event of a failed get of a value.
sl@0
    38
//
sl@0
    39
class bad_get
sl@0
    40
    : public std::exception
sl@0
    41
{
sl@0
    42
public: // std::exception implementation
sl@0
    43
sl@0
    44
    virtual const char * what() const throw()
sl@0
    45
    {
sl@0
    46
        return "boost::bad_get: "
sl@0
    47
               "failed value get using boost::get";
sl@0
    48
    }
sl@0
    49
sl@0
    50
};
sl@0
    51
sl@0
    52
//////////////////////////////////////////////////////////////////////////
sl@0
    53
// function template get<T>
sl@0
    54
//
sl@0
    55
// Retrieves content of given variant object if content is of type T.
sl@0
    56
// Otherwise: pointer ver. returns 0; reference ver. throws bad_get.
sl@0
    57
//
sl@0
    58
sl@0
    59
namespace detail { namespace variant {
sl@0
    60
sl@0
    61
// (detail) class template get_visitor
sl@0
    62
//
sl@0
    63
// Generic static visitor that: if the value is of the specified type,
sl@0
    64
// returns a pointer to the value it visits; else a null pointer.
sl@0
    65
//
sl@0
    66
template <typename T>
sl@0
    67
struct get_visitor
sl@0
    68
{
sl@0
    69
private: // private typedefs
sl@0
    70
sl@0
    71
    typedef typename add_pointer<T>::type pointer;
sl@0
    72
    typedef typename add_reference<T>::type reference;
sl@0
    73
sl@0
    74
public: // visitor typedefs
sl@0
    75
sl@0
    76
    typedef pointer result_type;
sl@0
    77
sl@0
    78
public: // visitor interfaces
sl@0
    79
sl@0
    80
#if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
sl@0
    81
sl@0
    82
    pointer operator()(reference operand) const
sl@0
    83
    {
sl@0
    84
        return boost::addressof(operand);
sl@0
    85
    }
sl@0
    86
sl@0
    87
    template <typename U>
sl@0
    88
    pointer operator()(const U&) const
sl@0
    89
    {
sl@0
    90
        return static_cast<pointer>(0);
sl@0
    91
    }
sl@0
    92
sl@0
    93
#else // MSVC6
sl@0
    94
sl@0
    95
private: // helpers, for visitor interfaces (below)
sl@0
    96
sl@0
    97
    pointer execute_impl(reference operand, mpl::true_) const
sl@0
    98
    {
sl@0
    99
        return boost::addressof(operand);
sl@0
   100
    }
sl@0
   101
sl@0
   102
    template <typename U>
sl@0
   103
    pointer execute_impl(const U& operand, mpl::false_) const
sl@0
   104
    {
sl@0
   105
        return static_cast<pointer>(0);
sl@0
   106
    }
sl@0
   107
sl@0
   108
public: // visitor interfaces
sl@0
   109
sl@0
   110
    template <typename U>
sl@0
   111
    pointer operator()(U& operand) const
sl@0
   112
    {
sl@0
   113
        // MSVC6 finds normal implementation (above) ambiguous,
sl@0
   114
        // so we must explicitly disambiguate
sl@0
   115
sl@0
   116
        typedef typename mpl::or_<
sl@0
   117
              is_same<U, T>
sl@0
   118
            , is_same<const U, T>
sl@0
   119
            >::type U_is_T;
sl@0
   120
sl@0
   121
        return execute_impl(operand, U_is_T());
sl@0
   122
    }
sl@0
   123
sl@0
   124
#endif // MSVC6 workaround
sl@0
   125
sl@0
   126
};
sl@0
   127
sl@0
   128
}} // namespace detail::variant
sl@0
   129
sl@0
   130
#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551))
sl@0
   131
#   define BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t)  \
sl@0
   132
    BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(t)
sl@0
   133
#else
sl@0
   134
#   define BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t)  \
sl@0
   135
    , t* = 0
sl@0
   136
#endif
sl@0
   137
sl@0
   138
template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
sl@0
   139
inline
sl@0
   140
    typename add_pointer<U>::type
sl@0
   141
get(
sl@0
   142
      boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
sl@0
   143
      BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
sl@0
   144
    )
sl@0
   145
{
sl@0
   146
    typedef typename add_pointer<U>::type U_ptr;
sl@0
   147
    if (!operand) return static_cast<U_ptr>(0);
sl@0
   148
sl@0
   149
    detail::variant::get_visitor<U> v;
sl@0
   150
    return operand->apply_visitor(v);
sl@0
   151
}
sl@0
   152
sl@0
   153
template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
sl@0
   154
inline
sl@0
   155
    typename add_pointer<const U>::type
sl@0
   156
get(
sl@0
   157
      const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
sl@0
   158
      BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
sl@0
   159
    )
sl@0
   160
{
sl@0
   161
    typedef typename add_pointer<const U>::type U_ptr;
sl@0
   162
    if (!operand) return static_cast<U_ptr>(0);
sl@0
   163
sl@0
   164
    detail::variant::get_visitor<const U> v;
sl@0
   165
    return operand->apply_visitor(v);
sl@0
   166
}
sl@0
   167
sl@0
   168
template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
sl@0
   169
inline
sl@0
   170
    typename add_reference<U>::type
sl@0
   171
get(
sl@0
   172
      boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
sl@0
   173
      BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
sl@0
   174
    )
sl@0
   175
{
sl@0
   176
    typedef typename add_pointer<U>::type U_ptr;
sl@0
   177
    U_ptr result = get<U>(&operand);
sl@0
   178
sl@0
   179
    if (!result)
sl@0
   180
        throw bad_get();
sl@0
   181
    return *result;
sl@0
   182
}
sl@0
   183
sl@0
   184
template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
sl@0
   185
inline
sl@0
   186
    typename add_reference<const U>::type
sl@0
   187
get(
sl@0
   188
      const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
sl@0
   189
      BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
sl@0
   190
    )
sl@0
   191
{
sl@0
   192
    typedef typename add_pointer<const U>::type U_ptr;
sl@0
   193
    U_ptr result = get<const U>(&operand);
sl@0
   194
sl@0
   195
    if (!result)
sl@0
   196
        throw bad_get();
sl@0
   197
    return *result;
sl@0
   198
}
sl@0
   199
sl@0
   200
} // namespace boost
sl@0
   201
sl@0
   202
#endif // BOOST_VARIANT_GET_HPP