libstdc++
any
Go to the documentation of this file.
1 // <experimental/any> -*- C++ -*-
2 
3 // Copyright (C) 2014-2015 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 #include <experimental/lfts_config.h>
43 
44 namespace std _GLIBCXX_VISIBILITY(default)
45 {
46 namespace experimental
47 {
48 inline namespace fundamentals_v1
49 {
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
51 
52  /**
53  * @defgroup any Type-safe container of any type
54  * @ingroup experimental
55  *
56  * A type-safe container for single values of value types, as
57  * described in n3804 "Any Library Proposal (Revision 3)".
58  *
59  * @{
60  */
61 
62 #define __cpp_lib_experimental_any 201411
63 
64  /**
65  * @brief Exception class thrown by a failed @c any_cast
66  * @ingroup exceptions
67  */
68  class bad_any_cast : public bad_cast
69  {
70  public:
71  virtual const char* what() const noexcept { return "bad any_cast"; }
72  };
73 
74  [[gnu::noreturn]] inline void __throw_bad_any_cast()
75  {
76 #if __cpp_exceptions
77  throw bad_any_cast{};
78 #else
79  __builtin_abort();
80 #endif
81  }
82 
83  /**
84  * @brief A type-safe container of any type.
85  *
86  * An @c any object's state is either empty or it stores a contained object
87  * of CopyConstructible type.
88  */
89  class any
90  {
91  // Holds either pointer to a heap object or the contained object itself.
92  union _Storage
93  {
94  void* _M_ptr;
95  std::aligned_storage<sizeof(_M_ptr), sizeof(_M_ptr)>::type _M_buffer;
96  };
97 
98  template<typename _Tp, typename _Safe = is_trivially_copyable<_Tp>,
99  bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))>
100  using _Internal = std::integral_constant<bool, _Safe::value && _Fits>;
101 
102  template<typename _Tp>
103  struct _Manager_internal; // uses small-object optimization
104 
105  template<typename _Tp>
106  struct _Manager_external; // creates contained object on the heap
107 
108  template<typename _Tp>
109  using _Manager = conditional_t<_Internal<_Tp>::value,
110  _Manager_internal<_Tp>,
111  _Manager_external<_Tp>>;
112 
113  template<typename _Tp, typename _Decayed = decay_t<_Tp>>
114  using _Decay = enable_if_t<!is_same<_Decayed, any>::value, _Decayed>;
115 
116  public:
117  // construct/destruct
118 
119  /// Default constructor, creates an empty object.
120  any() noexcept : _M_manager(nullptr) { }
121 
122  /// Copy constructor, copies the state of @p __other
123  any(const any& __other) : _M_manager(__other._M_manager)
124  {
125  if (!__other.empty())
126  {
127  _Arg __arg;
128  __arg._M_any = this;
129  _M_manager(_Op_clone, &__other, &__arg);
130  }
131  }
132 
133  /**
134  * @brief Move constructor, transfer the state from @p __other
135  *
136  * @post @c __other.empty() (not guaranteed for other implementations)
137  */
138  any(any&& __other) noexcept
139  : _M_manager(__other._M_manager),
140  _M_storage(__other._M_storage)
141  { __other._M_manager = nullptr; }
142 
143  /// Construct with a copy of @p __value as the contained object.
144  template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
145  typename _Mgr = _Manager<_Tp>>
146  any(_ValueType&& __value)
147  : _M_manager(&_Mgr::_S_manage),
148  _M_storage(_Mgr::_S_create(std::forward<_ValueType>(__value)))
149  {
150  static_assert(is_copy_constructible<_Tp>::value,
151  "The contained object must be CopyConstructible");
152  }
153 
154  /// Destructor, calls @c clear()
155  ~any() { clear(); }
156 
157  // assignments
158 
159  /// Copy the state of
160  any& operator=(const any& __rhs)
161  {
162  any(__rhs).swap(*this);
163  return *this;
164  }
165 
166  /**
167  * @brief Move assignment operator
168  *
169  * @post @c __rhs.empty() (not guaranteed for other implementations)
170  */
171  any& operator=(any&& __rhs) noexcept
172  {
173  any(std::move(__rhs)).swap(*this);
174  return *this;
175  }
176 
177  /// Store a copy of @p __rhs as the contained object.
178  template<typename _ValueType>
179  any& operator=(_ValueType&& __rhs)
180  {
181  any(std::forward<_ValueType>(__rhs)).swap(*this);
182  return *this;
183  }
184 
185  // modifiers
186 
187  /// If not empty, destroy the contained object.
188  void clear() noexcept
189  {
190  if (!empty())
191  {
192  _M_manager(_Op_destroy, this, nullptr);
193  _M_manager = nullptr;
194  }
195  }
196 
197  /// Exchange state with another object.
198  void swap(any& __rhs) noexcept
199  {
200  std::swap(_M_manager, __rhs._M_manager);
201  std::swap(_M_storage, __rhs._M_storage);
202  }
203 
204  // observers
205 
206  /// Reports whether there is a contained object or not.
207  bool empty() const noexcept { return _M_manager == nullptr; }
208 
209 #if __cpp_rtti
210  /// The @c typeid of the contained object, or @c typeid(void) if empty.
211  const type_info& type() const noexcept
212  {
213  if (empty())
214  return typeid(void);
215  _Arg __arg;
216  _M_manager(_Op_get_type_info, this, &__arg);
217  return *__arg._M_typeinfo;
218  }
219 #endif
220 
221  template<typename _Tp>
222  static constexpr bool __is_valid_cast()
223  { return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
224 
225  private:
226  enum _Op { _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy };
227 
228  union _Arg
229  {
230  void* _M_obj;
231  const std::type_info* _M_typeinfo;
232  any* _M_any;
233  };
234 
235  void (*_M_manager)(_Op, const any*, _Arg*);
236  _Storage _M_storage;
237 
238  template<typename _Tp>
239  friend void* __any_caster(const any* __any)
240  {
241  if (__any->_M_manager != &_Manager<decay_t<_Tp>>::_S_manage)
242  return nullptr;
243  _Arg __arg;
244  __any->_M_manager(_Op_access, __any, &__arg);
245  return __arg._M_obj;
246  }
247 
248  // Manage in-place contained object.
249  template<typename _Tp>
250  struct _Manager_internal
251  {
252  static void
253  _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
254 
255  template<typename _Up>
256  static _Storage
257  _S_create(_Up&& __value)
258  {
259  _Storage __storage;
260  void* __addr = &__storage._M_buffer;
261  ::new (__addr) _Tp(std::forward<_Up>(__value));
262  return __storage;
263  }
264 
265  template<typename _Alloc, typename _Up>
266  static _Storage
267  _S_alloc(const _Alloc&, _Up&& __value)
268  {
269  return _S_create(std::forward<_Up>(__value));
270  }
271  };
272 
273  // Manage external contained object.
274  template<typename _Tp>
275  struct _Manager_external
276  {
277  static void
278  _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
279 
280  template<typename _Up>
281  static _Storage
282  _S_create(_Up&& __value)
283  {
284  _Storage __storage;
285  __storage._M_ptr = new _Tp(std::forward<_Up>(__value));
286  return __storage;
287  }
288  };
289  };
290 
291  /// Exchange the states of two @c any objects.
292  inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); }
293 
294  /**
295  * @brief Access the contained object.
296  *
297  * @tparam _ValueType A const-reference or CopyConstructible type.
298  * @param __any The object to access.
299  * @return The contained object.
300  * @throw bad_any_cast If <code>
301  * __any.type() != typeid(remove_reference_t<_ValueType>)
302  * </code>
303  */
304  template<typename _ValueType>
305  inline _ValueType any_cast(const any& __any)
306  {
307  static_assert(any::__is_valid_cast<_ValueType>(),
308  "Template argument must be a reference or CopyConstructible type");
309  auto __p = any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any);
310  if (__p)
311  return *__p;
312  __throw_bad_any_cast();
313  }
314 
315  /**
316  * @brief Access the contained object.
317  *
318  * @tparam _ValueType A reference or CopyConstructible type.
319  * @param __any The object to access.
320  * @return The contained object.
321  * @throw bad_any_cast If <code>
322  * __any.type() != typeid(remove_reference_t<_ValueType>)
323  * </code>
324  *
325  * @{
326  */
327  template<typename _ValueType>
328  inline _ValueType any_cast(any& __any)
329  {
330  static_assert(any::__is_valid_cast<_ValueType>(),
331  "Template argument must be a reference or CopyConstructible type");
332  auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
333  if (__p)
334  return *__p;
335  __throw_bad_any_cast();
336  }
337 
338  template<typename _ValueType>
339  inline _ValueType any_cast(any&& __any)
340  {
341  static_assert(any::__is_valid_cast<_ValueType>(),
342  "Template argument must be a reference or CopyConstructible type");
343  auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
344  if (__p)
345  return *__p;
346  __throw_bad_any_cast();
347  }
348  // @}
349 
350  /**
351  * @brief Access the contained object.
352  *
353  * @tparam _ValueType The type of the contained object.
354  * @param __any A pointer to the object to access.
355  * @return The address of the contained object if <code>
356  * __any != nullptr && __any.type() == typeid(_ValueType)
357  * </code>, otherwise a null pointer.
358  *
359  * @{
360  */
361  template<typename _ValueType>
362  inline const _ValueType* any_cast(const any* __any) noexcept
363  {
364  if (__any)
365  return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
366  return nullptr;
367  }
368 
369  template<typename _ValueType>
370  inline _ValueType* any_cast(any* __any) noexcept
371  {
372  if (__any)
373  return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
374  return nullptr;
375  }
376  // @}
377 
378  template<typename _Tp>
379  void
380  any::_Manager_internal<_Tp>::
381  _S_manage(_Op __which, const any* __any, _Arg* __arg)
382  {
383  // The contained object is in _M_storage._M_buffer
384  auto __ptr = reinterpret_cast<const _Tp*>(&__any->_M_storage._M_buffer);
385  switch (__which)
386  {
387  case _Op_access:
388  __arg->_M_obj = const_cast<_Tp*>(__ptr);
389  break;
390  case _Op_get_type_info:
391 #if __cpp_rtti
392  __arg->_M_typeinfo = &typeid(_Tp);
393 #endif
394  break;
395  case _Op_clone:
396  ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
397  break;
398  case _Op_destroy:
399  __ptr->~_Tp();
400  break;
401  }
402  }
403 
404  template<typename _Tp>
405  void
406  any::_Manager_external<_Tp>::
407  _S_manage(_Op __which, const any* __any, _Arg* __arg)
408  {
409  // The contained object is *_M_storage._M_ptr
410  auto __ptr = static_cast<const _Tp*>(__any->_M_storage._M_ptr);
411  switch (__which)
412  {
413  case _Op_access:
414  __arg->_M_obj = const_cast<_Tp*>(__ptr);
415  break;
416  case _Op_get_type_info:
417 #if __cpp_rtti
418  __arg->_M_typeinfo = &typeid(_Tp);
419 #endif
420  break;
421  case _Op_clone:
422  __arg->_M_any->_M_storage._M_ptr = new _Tp(*__ptr);
423  break;
424  case _Op_destroy:
425  delete __ptr;
426  break;
427  }
428  }
429 
430  // @} group any
431 _GLIBCXX_END_NAMESPACE_VERSION
432 } // namespace fundamentals_v1
433 } // namespace experimental
434 } // namespace std
435 
436 #endif // C++14
437 
438 #endif // _GLIBCXX_EXPERIMENTAL_ANY