cast.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // <cast.h> -*- C++ -*-
  2. // Copyright (C) 2008-2015 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file ext/cast.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{ext/pointer.h}
  23. */
  24. #ifndef _GLIBCXX_CAST_H
  25. #define _GLIBCXX_CAST_H 1
  26. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  27. {
  28. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  29. /**
  30. * These functions are here to allow containers to support non standard
  31. * pointer types. For normal pointers, these resolve to the use of the
  32. * standard cast operation. For other types the functions will perform
  33. * the appropriate cast to/from the custom pointer class so long as that
  34. * class meets the following conditions:
  35. * 1) has a typedef element_type which names tehe type it points to.
  36. * 2) has a get() const method which returns element_type*.
  37. * 3) has a constructor which can take one element_type* argument.
  38. */
  39. /**
  40. * This type supports the semantics of the pointer cast operators (below.)
  41. */
  42. template<typename _ToType>
  43. struct _Caster
  44. { typedef typename _ToType::element_type* type; };
  45. template<typename _ToType>
  46. struct _Caster<_ToType*>
  47. { typedef _ToType* type; };
  48. /**
  49. * Casting operations for cases where _FromType is not a standard pointer.
  50. * _ToType can be a standard or non-standard pointer. Given that _FromType
  51. * is not a pointer, it must have a get() method that returns the standard
  52. * pointer equivalent of the address it points to, and must have an
  53. * element_type typedef which names the type it points to.
  54. */
  55. template<typename _ToType, typename _FromType>
  56. inline _ToType
  57. __static_pointer_cast(const _FromType& __arg)
  58. { return _ToType(static_cast<typename _Caster<_ToType>::
  59. type>(__arg.get())); }
  60. template<typename _ToType, typename _FromType>
  61. inline _ToType
  62. __dynamic_pointer_cast(const _FromType& __arg)
  63. { return _ToType(dynamic_cast<typename _Caster<_ToType>::
  64. type>(__arg.get())); }
  65. template<typename _ToType, typename _FromType>
  66. inline _ToType
  67. __const_pointer_cast(const _FromType& __arg)
  68. { return _ToType(const_cast<typename _Caster<_ToType>::
  69. type>(__arg.get())); }
  70. template<typename _ToType, typename _FromType>
  71. inline _ToType
  72. __reinterpret_pointer_cast(const _FromType& __arg)
  73. { return _ToType(reinterpret_cast<typename _Caster<_ToType>::
  74. type>(__arg.get())); }
  75. /**
  76. * Casting operations for cases where _FromType is a standard pointer.
  77. * _ToType can be a standard or non-standard pointer.
  78. */
  79. template<typename _ToType, typename _FromType>
  80. inline _ToType
  81. __static_pointer_cast(_FromType* __arg)
  82. { return _ToType(static_cast<typename _Caster<_ToType>::
  83. type>(__arg)); }
  84. template<typename _ToType, typename _FromType>
  85. inline _ToType
  86. __dynamic_pointer_cast(_FromType* __arg)
  87. { return _ToType(dynamic_cast<typename _Caster<_ToType>::
  88. type>(__arg)); }
  89. template<typename _ToType, typename _FromType>
  90. inline _ToType
  91. __const_pointer_cast(_FromType* __arg)
  92. { return _ToType(const_cast<typename _Caster<_ToType>::
  93. type>(__arg)); }
  94. template<typename _ToType, typename _FromType>
  95. inline _ToType
  96. __reinterpret_pointer_cast(_FromType* __arg)
  97. { return _ToType(reinterpret_cast<typename _Caster<_ToType>::
  98. type>(__arg)); }
  99. _GLIBCXX_END_NAMESPACE_VERSION
  100. } // namespace
  101. #endif // _GLIBCXX_CAST_H