fix doc example typo
[boost.git] / boost / variant / get.hpp
blob99a65f6b6df7869aa7dc279acbcccbc4e3057d93
1 //-----------------------------------------------------------------------------
2 // boost variant/get.hpp header file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
5 //
6 // Copyright (c) 2003
7 // Eric Friedman, Itay Maman
8 //
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 #ifndef BOOST_VARIANT_GET_HPP
14 #define BOOST_VARIANT_GET_HPP
16 #include <exception>
18 #include "boost/config.hpp"
19 #include "boost/detail/workaround.hpp"
20 #include "boost/utility/addressof.hpp"
21 #include "boost/variant/variant_fwd.hpp"
23 #include "boost/type_traits/add_reference.hpp"
24 #include "boost/type_traits/add_pointer.hpp"
26 #if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
27 # include "boost/mpl/bool.hpp"
28 # include "boost/mpl/or.hpp"
29 # include "boost/type_traits/is_same.hpp"
30 #endif
32 namespace boost {
34 //////////////////////////////////////////////////////////////////////////
35 // class bad_get
37 // The exception thrown in the event of a failed get of a value.
39 class bad_get
40 : public std::exception
42 public: // std::exception implementation
44 virtual const char * what() const throw()
46 return "boost::bad_get: "
47 "failed value get using boost::get";
52 //////////////////////////////////////////////////////////////////////////
53 // function template get<T>
55 // Retrieves content of given variant object if content is of type T.
56 // Otherwise: pointer ver. returns 0; reference ver. throws bad_get.
59 namespace detail { namespace variant {
61 // (detail) class template get_visitor
63 // Generic static visitor that: if the value is of the specified type,
64 // returns a pointer to the value it visits; else a null pointer.
66 template <typename T>
67 struct get_visitor
69 private: // private typedefs
71 typedef typename add_pointer<T>::type pointer;
72 typedef typename add_reference<T>::type reference;
74 public: // visitor typedefs
76 typedef pointer result_type;
78 public: // visitor interfaces
80 #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
82 pointer operator()(reference operand) const
84 return boost::addressof(operand);
87 template <typename U>
88 pointer operator()(const U&) const
90 return static_cast<pointer>(0);
93 #else // MSVC6
95 private: // helpers, for visitor interfaces (below)
97 pointer execute_impl(reference operand, mpl::true_) const
99 return boost::addressof(operand);
102 template <typename U>
103 pointer execute_impl(const U& operand, mpl::false_) const
105 return static_cast<pointer>(0);
108 public: // visitor interfaces
110 template <typename U>
111 pointer operator()(U& operand) const
113 // MSVC6 finds normal implementation (above) ambiguous,
114 // so we must explicitly disambiguate
116 typedef typename mpl::or_<
117 is_same<U, T>
118 , is_same<const U, T>
119 >::type U_is_T;
121 return execute_impl(operand, U_is_T());
124 #endif // MSVC6 workaround
128 }} // namespace detail::variant
130 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551))
131 # define BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t) \
132 BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(t)
133 #else
134 # define BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t) \
135 , t* = 0
136 #endif
138 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
139 inline
140 typename add_pointer<U>::type
141 get(
142 boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
143 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
146 typedef typename add_pointer<U>::type U_ptr;
147 if (!operand) return static_cast<U_ptr>(0);
149 detail::variant::get_visitor<U> v;
150 return operand->apply_visitor(v);
153 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
154 inline
155 typename add_pointer<const U>::type
156 get(
157 const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
158 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
161 typedef typename add_pointer<const U>::type U_ptr;
162 if (!operand) return static_cast<U_ptr>(0);
164 detail::variant::get_visitor<const U> v;
165 return operand->apply_visitor(v);
168 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
169 inline
170 typename add_reference<U>::type
171 get(
172 boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
173 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
176 typedef typename add_pointer<U>::type U_ptr;
177 U_ptr result = get<U>(&operand);
179 if (!result)
180 throw bad_get();
181 return *result;
184 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
185 inline
186 typename add_reference<const U>::type
187 get(
188 const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
189 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
192 typedef typename add_pointer<const U>::type U_ptr;
193 U_ptr result = get<const U>(&operand);
195 if (!result)
196 throw bad_get();
197 return *result;
200 } // namespace boost
202 #endif // BOOST_VARIANT_GET_HPP