libstdc++
any
Go to the documentation of this file.
1 // <experimental/any> -*- C++ -*-
2 
3 // Copyright (C) 2014-2016 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file experimental/any
26  * This is a TS C++ Library header.
27  */
28 
29 #ifndef _GLIBCXX_EXPERIMENTAL_ANY
30 #define _GLIBCXX_EXPERIMENTAL_ANY 1
31 
32 #pragma GCC system_header
33 
34 #if __cplusplus <= 201103L
35 # include <bits/c++14_warning.h>
36 #else
37 
38 #include <typeinfo>
39 #include <new>
40 #include <utility>
41 #include <type_traits>
42 
43 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45 namespace experimental
46 {
47 inline namespace fundamentals_v1
48 {
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 
51  /**
52  * @defgroup any Type-safe container of any type
53  * @ingroup experimental
54  *
55  * A type-safe container for single values of value types, as
56  * described in n3804 "Any Library Proposal (Revision 3)".
57  *
58  * @{
59  */
60 
61 #define __cpp_lib_experimental_any 201411
62 
63  /**
64  * @brief Exception class thrown by a failed @c any_cast
65  * @ingroup exceptions
66  */
67  class bad_any_cast : public bad_cast
68  {
69  public:
70  virtual const char* what() const noexcept { return "bad any_cast"; }
71  };
72 
73  [[gnu::noreturn]] inline void __throw_bad_any_cast()
74  {
75 #if __cpp_exceptions
76  throw bad_any_cast{};
77 #else
78  __builtin_abort();
79 #endif
80  }
81 
82  /**
83  * @brief A type-safe container of any type.
84  *
85  * An @c any object's state is either empty or it stores a contained object
86  * of CopyConstructible type.
87  */
88  class any
89  {
90  // Holds either pointer to a heap object or the contained object itself.
91  union _Storage
92  {
93  // This constructor intentionally doesn't initialize anything.
94  _Storage() = default;
95 
96  // Prevent trivial copies of this type, buffer might hold a non-POD.
97  _Storage(const _Storage&) = delete;
98  _Storage& operator=(const _Storage&) = delete;
99 
100  void* _M_ptr;
101  aligned_storage<sizeof(_M_ptr), alignof(void*)>::type _M_buffer;
102  };
103 
104  template<typename _Tp, typename _Safe = is_nothrow_move_constructible<_Tp>,
105  bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))
106  && (alignof(_Tp) <= alignof(_Storage))>
107  using _Internal = std::integral_constant<bool, _Safe::value && _Fits>;
108 
109  template<typename _Tp>
110  struct _Manager_internal; // uses small-object optimization
111 
112  template<typename _Tp>
113  struct _Manager_external; // creates contained object on the heap
114 
115  template<typename _Tp>
116  using _Manager = conditional_t<_Internal<_Tp>::value,
117  _Manager_internal<_Tp>,
118  _Manager_external<_Tp>>;
119 
120  template<typename _Tp, typename _Decayed = decay_t<_Tp>>
121  using _Decay = enable_if_t<!is_same<_Decayed, any>::value, _Decayed>;
122 
123  public:
124  // construct/destruct
125 
126  /// Default constructor, creates an empty object.
127  any() noexcept : _M_manager(nullptr) { }
128 
129  /// Copy constructor, copies the state of @p __other
130  any(const any& __other)
131  {
132  if (__other.empty())
133  _M_manager = nullptr;
134  else
135  {
136  _Arg __arg;
137  __arg._M_any = this;
138  __other._M_manager(_Op_clone, &__other, &__arg);
139  }
140  }
141 
142  /**
143  * @brief Move constructor, transfer the state from @p __other
144  *
145  * @post @c __other.empty() (this postcondition is a GNU extension)
146  */
147  any(any&& __other) noexcept
148  {
149  if (__other.empty())
150  _M_manager = nullptr;
151  else
152  {
153  _Arg __arg;
154  __arg._M_any = this;
155  __other._M_manager(_Op_xfer, &__other, &__arg);
156  }
157  }
158 
159  /// Construct with a copy of @p __value as the contained object.
160  template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
161  typename _Mgr = _Manager<_Tp>,
162  typename enable_if<is_constructible<_Tp, _ValueType&&>::value,
163  bool>::type = true>
164  any(_ValueType&& __value)
165  : _M_manager(&_Mgr::_S_manage)
166  {
167  _Mgr::_S_create(_M_storage, std::forward<_ValueType>(__value));
168  static_assert(is_copy_constructible<_Tp>::value,
169  "The contained object must be CopyConstructible");
170  }
171 
172  /// Construct with a copy of @p __value as the contained object.
173  template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
174  typename _Mgr = _Manager<_Tp>,
175  typename enable_if<!is_constructible<_Tp, _ValueType&&>::value,
176  bool>::type = false>
177  any(_ValueType&& __value)
178  : _M_manager(&_Mgr::_S_manage)
179  {
180  _Mgr::_S_create(_M_storage, __value);
181  static_assert(is_copy_constructible<_Tp>::value,
182  "The contained object must be CopyConstructible");
183  }
184 
185  /// Destructor, calls @c clear()
186  ~any() { clear(); }
187 
188  // assignments
189 
190  /// Copy the state of another object.
191  any& operator=(const any& __rhs)
192  {
193  if (__rhs.empty())
194  clear();
195  else if (this != &__rhs)
196  {
197  if (!empty())
198  _M_manager(_Op_destroy, this, nullptr);
199  _Arg __arg;
200  __arg._M_any = this;
201  __rhs._M_manager(_Op_clone, &__rhs, &__arg);
202  }
203  return *this;
204  }
205 
206  /**
207  * @brief Move assignment operator
208  *
209  * @post @c __rhs.empty() (not guaranteed for other implementations)
210  */
211  any& operator=(any&& __rhs) noexcept
212  {
213  if (__rhs.empty())
214  clear();
215  else if (this != &__rhs)
216  {
217  if (!empty())
218  _M_manager(_Op_destroy, this, nullptr);
219  _Arg __arg;
220  __arg._M_any = this;
221  __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
222  }
223  return *this;
224  }
225 
226  /// Store a copy of @p __rhs as the contained object.
227  template<typename _ValueType>
228  enable_if_t<!is_same<any, decay_t<_ValueType>>::value, any&>
229  operator=(_ValueType&& __rhs)
230  {
231  *this = any(std::forward<_ValueType>(__rhs));
232  return *this;
233  }
234 
235  // modifiers
236 
237  /// If not empty, destroy the contained object.
238  void clear() noexcept
239  {
240  if (!empty())
241  {
242  _M_manager(_Op_destroy, this, nullptr);
243  _M_manager = nullptr;
244  }
245  }
246 
247  /// Exchange state with another object.
248  void swap(any& __rhs) noexcept
249  {
250  if (empty() && __rhs.empty())
251  return;
252 
253  if (!empty() && !__rhs.empty())
254  {
255  if (this == &__rhs)
256  return;
257 
258  any __tmp;
259  _Arg __arg;
260  __arg._M_any = &__tmp;
261  __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
262  __arg._M_any = &__rhs;
263  _M_manager(_Op_xfer, this, &__arg);
264  __arg._M_any = this;
265  __tmp._M_manager(_Op_xfer, &__tmp, &__arg);
266  }
267  else
268  {
269  any* __empty = empty() ? this : &__rhs;
270  any* __full = empty() ? &__rhs : this;
271  _Arg __arg;
272  __arg._M_any = __empty;
273  __full->_M_manager(_Op_xfer, __full, &__arg);
274  }
275  }
276 
277  // observers
278 
279  /// Reports whether there is a contained object or not.
280  bool empty() const noexcept { return _M_manager == nullptr; }
281 
282 #if __cpp_rtti
283  /// The @c typeid of the contained object, or @c typeid(void) if empty.
284  const type_info& type() const noexcept
285  {
286  if (empty())
287  return typeid(void);
288  _Arg __arg;
289  _M_manager(_Op_get_type_info, this, &__arg);
290  return *__arg._M_typeinfo;
291  }
292 #endif
293 
294  template<typename _Tp>
295  static constexpr bool __is_valid_cast()
296  { return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
297 
298  private:
299  enum _Op {
300  _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy, _Op_xfer
301  };
302 
303  union _Arg
304  {
305  void* _M_obj;
306  const std::type_info* _M_typeinfo;
307  any* _M_any;
308  };
309 
310  void (*_M_manager)(_Op, const any*, _Arg*);
311  _Storage _M_storage;
312 
313  template<typename _Tp>
314  friend void* __any_caster(const any* __any);
315 
316  // Manage in-place contained object.
317  template<typename _Tp>
318  struct _Manager_internal
319  {
320  static void
321  _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
322 
323  template<typename _Up>
324  static void
325  _S_create(_Storage& __storage, _Up&& __value)
326  {
327  void* __addr = &__storage._M_buffer;
328  ::new (__addr) _Tp(std::forward<_Up>(__value));
329  }
330  };
331 
332  // Manage external contained object.
333  template<typename _Tp>
334  struct _Manager_external
335  {
336  static void
337  _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
338 
339  template<typename _Up>
340  static void
341  _S_create(_Storage& __storage, _Up&& __value)
342  {
343  __storage._M_ptr = new _Tp(std::forward<_Up>(__value));
344  }
345  };
346  };
347 
348  /// Exchange the states of two @c any objects.
349  inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); }
350 
351  /**
352  * @brief Access the contained object.
353  *
354  * @tparam _ValueType A const-reference or CopyConstructible type.
355  * @param __any The object to access.
356  * @return The contained object.
357  * @throw bad_any_cast If <code>
358  * __any.type() != typeid(remove_reference_t<_ValueType>)
359  * </code>
360  */
361  template<typename _ValueType>
362  inline _ValueType any_cast(const any& __any)
363  {
364  static_assert(any::__is_valid_cast<_ValueType>(),
365  "Template argument must be a reference or CopyConstructible type");
366  auto __p = any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any);
367  if (__p)
368  return *__p;
369  __throw_bad_any_cast();
370  }
371 
372  /**
373  * @brief Access the contained object.
374  *
375  * @tparam _ValueType A reference or CopyConstructible type.
376  * @param __any The object to access.
377  * @return The contained object.
378  * @throw bad_any_cast If <code>
379  * __any.type() != typeid(remove_reference_t<_ValueType>)
380  * </code>
381  *
382  * @{
383  */
384  template<typename _ValueType>
385  inline _ValueType any_cast(any& __any)
386  {
387  static_assert(any::__is_valid_cast<_ValueType>(),
388  "Template argument must be a reference or CopyConstructible type");
389  auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
390  if (__p)
391  return *__p;
392  __throw_bad_any_cast();
393  }
394 
395  template<typename _ValueType,
396  typename enable_if<!is_move_constructible<_ValueType>::value
398  bool>::type = true>
399  inline _ValueType any_cast(any&& __any)
400  {
401  static_assert(any::__is_valid_cast<_ValueType>(),
402  "Template argument must be a reference or CopyConstructible type");
403  auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
404  if (__p)
405  return *__p;
406  __throw_bad_any_cast();
407  }
408 
409  template<typename _ValueType,
410  typename enable_if<is_move_constructible<_ValueType>::value
412  bool>::type = false>
413  inline _ValueType any_cast(any&& __any)
414  {
415  static_assert(any::__is_valid_cast<_ValueType>(),
416  "Template argument must be a reference or CopyConstructible type");
417  auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
418  if (__p)
419  return std::move(*__p);
420  __throw_bad_any_cast();
421  }
422  // @}
423 
424  template<typename _Tp>
425  void* __any_caster(const any* __any)
426  {
427  if (__any->_M_manager != &any::_Manager<decay_t<_Tp>>::_S_manage)
428  return nullptr;
429  any::_Arg __arg;
430  __any->_M_manager(any::_Op_access, __any, &__arg);
431  return __arg._M_obj;
432  }
433 
434  /**
435  * @brief Access the contained object.
436  *
437  * @tparam _ValueType The type of the contained object.
438  * @param __any A pointer to the object to access.
439  * @return The address of the contained object if <code>
440  * __any != nullptr && __any.type() == typeid(_ValueType)
441  * </code>, otherwise a null pointer.
442  *
443  * @{
444  */
445  template<typename _ValueType>
446  inline const _ValueType* any_cast(const any* __any) noexcept
447  {
448  if (__any)
449  return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
450  return nullptr;
451  }
452 
453  template<typename _ValueType>
454  inline _ValueType* any_cast(any* __any) noexcept
455  {
456  if (__any)
457  return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
458  return nullptr;
459  }
460  // @}
461 
462  template<typename _Tp>
463  void
464  any::_Manager_internal<_Tp>::
465  _S_manage(_Op __which, const any* __any, _Arg* __arg)
466  {
467  // The contained object is in _M_storage._M_buffer
468  auto __ptr = reinterpret_cast<const _Tp*>(&__any->_M_storage._M_buffer);
469  switch (__which)
470  {
471  case _Op_access:
472  __arg->_M_obj = const_cast<_Tp*>(__ptr);
473  break;
474  case _Op_get_type_info:
475 #if __cpp_rtti
476  __arg->_M_typeinfo = &typeid(_Tp);
477 #endif
478  break;
479  case _Op_clone:
480  ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
481  __arg->_M_any->_M_manager = __any->_M_manager;
482  break;
483  case _Op_destroy:
484  __ptr->~_Tp();
485  break;
486  case _Op_xfer:
487  ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
488  __ptr->~_Tp();
489  __arg->_M_any->_M_manager = __any->_M_manager;
490  const_cast<any*>(__any)->_M_manager = nullptr;
491  break;
492  }
493  }
494 
495  template<typename _Tp>
496  void
497  any::_Manager_external<_Tp>::
498  _S_manage(_Op __which, const any* __any, _Arg* __arg)
499  {
500  // The contained object is *_M_storage._M_ptr
501  auto __ptr = static_cast<const _Tp*>(__any->_M_storage._M_ptr);
502  switch (__which)
503  {
504  case _Op_access:
505  __arg->_M_obj = const_cast<_Tp*>(__ptr);
506  break;
507  case _Op_get_type_info:
508 #if __cpp_rtti
509  __arg->_M_typeinfo = &typeid(_Tp);
510 #endif
511  break;
512  case _Op_clone:
513  __arg->_M_any->_M_storage._M_ptr = new _Tp(*__ptr);
514  __arg->_M_any->_M_manager = __any->_M_manager;
515  break;
516  case _Op_destroy:
517  delete __ptr;
518  break;
519  case _Op_xfer:
520  __arg->_M_any->_M_storage._M_ptr = __any->_M_storage._M_ptr;
521  __arg->_M_any->_M_manager = __any->_M_manager;
522  const_cast<any*>(__any)->_M_manager = nullptr;
523  break;
524  }
525  }
526 
527  // @} group any
528 _GLIBCXX_END_NAMESPACE_VERSION
529 } // namespace fundamentals_v1
530 } // namespace experimental
531 } // namespace std
532 
533 #endif // C++14
534 
535 #endif // _GLIBCXX_EXPERIMENTAL_ANY
is_lvalue_reference
Definition: type_traits:376
~any()
Destructor, calls clear()
Definition: any:186
Exception class thrown by a failed any_cast.
Definition: any:67
Part of RTTI.
Definition: typeinfo:88
any & operator=(any &&__rhs) noexcept
Move assignment operator.
Definition: any:211
const type_info & type() const noexcept
The typeid of the contained object, or typeid(void) if empty.
Definition: any:284
bool empty() const noexcept
Reports whether there is a contained object or not.
Definition: any:280
A type-safe container of any type.
Definition: any:88
any(const any &__other)
Copy constructor, copies the state of __other.
Definition: any:130
Thrown during incorrect typecasting.If you attempt an invalid dynamic_cast expression, an instance of this class (or something derived from this class) is thrown.
Definition: typeinfo:187
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition: move.h:76
_ValueType any_cast(const any &__any)
Access the contained object.
Definition: any:362
any & operator=(const any &__rhs)
Copy the state of another object.
Definition: any:191
virtual const char * what() const noexcept
Definition: any:70
void clear() noexcept
If not empty, destroy the contained object.
Definition: any:238
any(_ValueType &&__value)
Construct with a copy of __value as the contained object.
Definition: any:164
any(any &&__other) noexcept
Move constructor, transfer the state from __other.
Definition: any:147
enable_if_t<!is_same< any, decay_t< _ValueType > >::value, any & > operator=(_ValueType &&__rhs)
Store a copy of __rhs as the contained object.
Definition: any:229
any() noexcept
Default constructor, creates an empty object.
Definition: any:127
void swap(any &__rhs) noexcept
Exchange state with another object.
Definition: any:248