any.hpp
Go to the documentation of this file.
1 // See http://www.boost.org/libs/any for Documentation.
2 
3 #ifndef BOOST_ANY_INCLUDED
4 #define BOOST_ANY_INCLUDED
5 
6 #if defined(_MSC_VER)
7 # pragma once
8 #endif
9 
10 // what: variant type boost::any
11 // who: contributed by Kevlin Henney,
12 // with features contributed and bugs found by
13 // Antony Polukhin, Ed Brey, Mark Rodgers,
14 // Peter Dimov, and James Curran
15 // when: July 2001, April 2013 - May 2013
16 
17 #include <algorithm>
18 
19 #include "boost/config.hpp"
20 #include <boost/type_index.hpp>
28 #include <boost/static_assert.hpp>
32 #include <boost/mpl/if.hpp>
33 
34 namespace boost
35 {
36  class any
37  {
38  public: // structors
39 
41  : content(0)
42  {
43  }
44 
45  template<typename ValueType>
46  any(const ValueType & value)
47  : content(new holder<
49  >(value))
50  {
51  }
52 
53  any(const any & other)
54  : content(other.content ? other.content->clone() : 0)
55  {
56  }
57 
58 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
59  // Move constructor
61  : content(other.content)
62  {
63  other.content = 0;
64  }
65 
66  // Perfect forwarding of ValueType
67  template<typename ValueType>
68  any(ValueType&& value
69  , typename boost::disable_if<boost::is_same<any&, ValueType> >::type* = 0 // disable if value has type `any&`
70  , typename boost::disable_if<boost::is_const<ValueType> >::type* = 0) // disable if value has type `const ValueType&&`
71  : content(new holder< typename decay<ValueType>::type >(static_cast<ValueType&&>(value)))
72  {
73  }
74 #endif
75 
77  {
78  delete content;
79  }
80 
81  public: // modifiers
82 
84  {
85  std::swap(content, rhs.content);
86  return *this;
87  }
88 
89 
90 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
91  template<typename ValueType>
92  any & operator=(const ValueType & rhs)
93  {
94  any(rhs).swap(*this);
95  return *this;
96  }
97 
98  any & operator=(any rhs)
99  {
100  any(rhs).swap(*this);
101  return *this;
102  }
103 
104 #else
105  any & operator=(const any& rhs)
106  {
107  any(rhs).swap(*this);
108  return *this;
109  }
110 
111  // move assignement
113  {
114  rhs.swap(*this);
115  any().swap(rhs);
116  return *this;
117  }
118 
119  // Perfect forwarding of ValueType
120  template <class ValueType>
121  any & operator=(ValueType&& rhs)
122  {
123  any(static_cast<ValueType&&>(rhs)).swap(*this);
124  return *this;
125  }
126 #endif
127 
128  public: // queries
129 
130  bool empty() const BOOST_NOEXCEPT
131  {
132  return !content;
133  }
134 
136  {
137  any().swap(*this);
138  }
139 
141  {
142  return content ? content->type() : boost::typeindex::type_id<void>().type_info();
143  }
144 
145 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
146  private: // types
147 #else
148  public: // types (public so any_cast can be non-friend)
149 #endif
150 
152  {
153  public: // structors
154 
155  virtual ~placeholder()
156  {
157  }
158 
159  public: // queries
160 
161  virtual const boost::typeindex::type_info& type() const BOOST_NOEXCEPT = 0;
162 
163  virtual placeholder * clone() const = 0;
164 
165  };
166 
167  template<typename ValueType>
168  class holder : public placeholder
169  {
170  public: // structors
171 
172  holder(const ValueType & value)
173  : held(value)
174  {
175  }
176 
177 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
178  holder(ValueType&& value)
179  : held(static_cast< ValueType&& >(value))
180  {
181  }
182 #endif
183  public: // queries
184 
186  {
187  return boost::typeindex::type_id<ValueType>().type_info();
188  }
189 
190  virtual placeholder * clone() const
191  {
192  return new holder(held);
193  }
194 
195  public: // representation
196 
197  ValueType held;
198 
199  private: // intentionally left unimplemented
200  holder & operator=(const holder &);
201  };
202 
203 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
204 
205  private: // representation
206 
207  template<typename ValueType>
208  friend ValueType * any_cast(any *) BOOST_NOEXCEPT;
209 
210  template<typename ValueType>
211  friend ValueType * unsafe_any_cast(any *) BOOST_NOEXCEPT;
212 
213 #else
214 
215  public: // representation (public so any_cast can be non-friend)
216 
217 #endif
218 
220 
221  };
222 
223  inline void swap(any & lhs, any & rhs) BOOST_NOEXCEPT
224  {
225  lhs.swap(rhs);
226  }
227 
229 #ifndef BOOST_NO_RTTI
230  public std::bad_cast
231 #else
232  public std::exception
233 #endif
234  {
235  public:
236  virtual const char * what() const BOOST_NOEXCEPT_OR_NOTHROW
237  {
238  return "boost::bad_any_cast: "
239  "failed conversion using boost::any_cast";
240  }
241  };
242 
243  template<typename ValueType>
244  ValueType * any_cast(any * operand) BOOST_NOEXCEPT
245  {
246  return operand && operand->type() == boost::typeindex::type_id<ValueType>()
247  ? &static_cast<any::holder<BOOST_DEDUCED_TYPENAME remove_cv<ValueType>::type> *>(operand->content)->held
248  : 0;
249  }
250 
251  template<typename ValueType>
252  inline const ValueType * any_cast(const any * operand) BOOST_NOEXCEPT
253  {
254  return any_cast<ValueType>(const_cast<any *>(operand));
255  }
256 
257  template<typename ValueType>
258  ValueType any_cast(any & operand)
259  {
261 
262 
263  nonref * result = any_cast<nonref>(&operand);
264  if(!result)
266 
267  // Attempt to avoid construction of a temporary object in cases when
268  // `ValueType` is not a reference. Example:
269  // `static_cast<std::string>(*result);`
270  // which is equal to `std::string(*result);`
273  ValueType,
275  >::type ref_type;
276 
277  return static_cast<ref_type>(*result);
278  }
279 
280  template<typename ValueType>
281  inline ValueType any_cast(const any & operand)
282  {
284  return any_cast<const nonref &>(const_cast<any &>(operand));
285  }
286 
287 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
288  template<typename ValueType>
289  inline ValueType any_cast(any&& operand)
290  {
292  boost::is_rvalue_reference<ValueType&&>::value /*true if ValueType is rvalue or just a value*/
294  "boost::any_cast shall not be used for getting nonconst references to temporary objects"
295  );
296  return any_cast<ValueType>(operand);
297  }
298 #endif
299 
300 
301  // Note: The "unsafe" versions of any_cast are not part of the
302  // public interface and may be removed at any time. They are
303  // required where we know what type is stored in the any and can't
304  // use typeid() comparison, e.g., when our types may travel across
305  // different shared libraries.
306  template<typename ValueType>
307  inline ValueType * unsafe_any_cast(any * operand) BOOST_NOEXCEPT
308  {
309  return &static_cast<any::holder<ValueType> *>(operand->content)->held;
310  }
311 
312  template<typename ValueType>
313  inline const ValueType * unsafe_any_cast(const any * operand) BOOST_NOEXCEPT
314  {
315  return unsafe_any_cast<ValueType>(const_cast<any *>(operand));
316  }
317 }
318 
319 // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
320 //
321 // Distributed under the Boost Software License, Version 1.0. (See
322 // accompanying file LICENSE_1_0.txt or copy at
323 // http://www.boost.org/LICENSE_1_0.txt)
324 
325 #endif
boost::swap
void swap(any &lhs, any &rhs) BOOST_NOEXCEPT
Definition: any.hpp:223
boost::any::any
any(any &&other) BOOST_NOEXCEPT
Definition: any.hpp:60
remove_reference.hpp
boost::any::swap
any & swap(any &rhs) BOOST_NOEXCEPT
Definition: any.hpp:83
BOOST_STATIC_ASSERT_MSG
#define BOOST_STATIC_ASSERT_MSG(...)
Definition: static_assert.hpp:31
boost::any::holder::holder
holder(const ValueType &value)
Definition: any.hpp:172
config.hpp
boost::any::holder::holder
holder(ValueType &&value)
Definition: any.hpp:178
boost::any::any
any(ValueType &&value, typename boost::disable_if< boost::is_same< any &, ValueType > >::type *=0, typename boost::disable_if< boost::is_const< ValueType > >::type *=0)
Definition: any.hpp:68
boost::any::holder::held
ValueType held
Definition: any.hpp:197
boost::any::operator=
any & operator=(any &&rhs) BOOST_NOEXCEPT
Definition: any.hpp:112
boost::any::operator=
any & operator=(const any &rhs)
Definition: any.hpp:105
static_assert.hpp
remove_cv.hpp
boost::any
Definition: any.hpp:36
boost::type
Definition: type.hpp:14
boost::throw_exception
BOOST_NORETURN void throw_exception(E const &e)
Definition: throw_exception.hpp:62
boost
BOOST_MOVE_USE_STANDARD_LIBRARY_MOVE.
boost::any::operator=
any & operator=(ValueType &&rhs)
Definition: any.hpp:121
is_reference.hpp
boost::is_same
Definition: type_traits/is_same.hpp:29
boost::is_rvalue_reference
Definition: is_rvalue_reference.hpp:17
boost::any::clear
void clear() BOOST_NOEXCEPT
Definition: any.hpp:135
boost::any::placeholder::~placeholder
virtual ~placeholder()
Definition: any.hpp:155
boost::any::any
any() BOOST_NOEXCEPT
Definition: any.hpp:40
boost::disable_if
Definition: core/enable_if.hpp:64
boost::any::placeholder::type
virtual const boost::typeindex::type_info & type() const BOOST_NOEXCEPT=0
boost::typeindex::type_info
type_index::type_info_t type_info
Definition: type_index.hpp:144
boost::any::content
placeholder * content
Definition: any.hpp:219
BOOST_NOEXCEPT
#define BOOST_NOEXCEPT
Definition: suffix.hpp:938
boost::any::placeholder
Definition: any.hpp:151
boost::any::~any
~any() BOOST_NOEXCEPT
Definition: any.hpp:76
add_reference.hpp
boost::bad_any_cast::what
virtual const char * what() const BOOST_NOEXCEPT_OR_NOTHROW
Definition: any.hpp:236
boost::is_const
Definition: is_const.hpp:37
enable_if.hpp
is_same.hpp
boost::is_reference
Definition: is_reference.hpp:20
is_const.hpp
BOOST_DEDUCED_TYPENAME
#define BOOST_DEDUCED_TYPENAME
Definition: suffix.hpp:467
boost::add_reference::type
boost::detail::add_reference_impl< T >::type type
Definition: add_reference.hpp:42
boost::bad_any_cast
Definition: any.hpp:228
decay.hpp
boost::remove_reference::type
boost::detail::remove_rvalue_ref< T >::type type
Definition: remove_reference.hpp:38
boost::any::unsafe_any_cast
friend ValueType * unsafe_any_cast(any *) BOOST_NOEXCEPT
Definition: any.hpp:307
boost::any::holder
Definition: any.hpp:168
boost::mpl::if_
Definition: dmc/basic_bind.hpp:374
boost::decay
Definition: decay.hpp:32
boost::any::any
any(const ValueType &value)
Definition: any.hpp:46
boost::remove_cv
Definition: remove_cv.hpp:21
boost::any::holder::clone
virtual placeholder * clone() const
Definition: any.hpp:190
boost::any::any_cast
friend ValueType * any_cast(any *) BOOST_NOEXCEPT
Definition: any.hpp:244
boost::any::type
const boost::typeindex::type_info & type() const BOOST_NOEXCEPT
Definition: any.hpp:140
throw_exception.hpp
BOOST_SYMBOL_VISIBLE
#define BOOST_SYMBOL_VISIBLE
Definition: clang.hpp:100
boost::any::any
any(const any &other)
Definition: any.hpp:53
boost::any::holder::type
virtual const boost::typeindex::type_info & type() const BOOST_NOEXCEPT
Definition: any.hpp:185
boost::any::placeholder::clone
virtual placeholder * clone() const =0
type_index.hpp
Includes minimal set of headers required to use the Boost.TypeIndex library.
BOOST_NOEXCEPT_OR_NOTHROW
#define BOOST_NOEXCEPT_OR_NOTHROW
Definition: suffix.hpp:939
boost::any::empty
bool empty() const BOOST_NOEXCEPT
Definition: any.hpp:130
if.hpp


sick_visionary_ros
Author(s): SICK AG TechSupport 3D Snapshot
autogenerated on Thu Feb 8 2024 03:36:31