cast.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // <cast.h> -*- C++ -*-
  2. // Copyright (C) 2008, 2009 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. #ifndef _CAST_H
  21. #define _CAST_H 1
  22. _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx);
  23. /**
  24. * These functions are here to allow containers to support non standard
  25. * pointer types. For normal pointers, these resolve to the use of the
  26. * standard cast operation. For other types the functions will perform
  27. * the apprpriate cast to/from the custom pointer class so long as that
  28. * class meets the following conditions:
  29. * 1) has a typedef element_type which names tehe type it points to.
  30. * 2) has a get() const method which returns element_type*.
  31. * 3) has a constructor which can take one element_type* argument.
  32. */
  33. /**
  34. * This type supports the semantics of the pointer cast operators (below.)
  35. */
  36. template<typename _ToType>
  37. struct _Caster
  38. { typedef typename _ToType::element_type* type; };
  39. template<typename _ToType>
  40. struct _Caster<_ToType*>
  41. { typedef _ToType* type; };
  42. /**
  43. * Casting operations for cases where _FromType is not a standard pointer.
  44. * _ToType can be a standard or non-standard pointer. Given that _FromType
  45. * is not a pointer, it must have a get() method that returns the standard
  46. * pointer equivalent of the address it points to, and must have an
  47. * element_type typedef which names the type it points to.
  48. */
  49. template<typename _ToType, typename _FromType>
  50. inline _ToType
  51. __static_pointer_cast(const _FromType& __arg)
  52. { return _ToType(static_cast<typename _Caster<_ToType>::
  53. type>(__arg.get())); }
  54. template<typename _ToType, typename _FromType>
  55. inline _ToType
  56. __dynamic_pointer_cast(const _FromType& __arg)
  57. { return _ToType(dynamic_cast<typename _Caster<_ToType>::
  58. type>(__arg.get())); }
  59. template<typename _ToType, typename _FromType>
  60. inline _ToType
  61. __const_pointer_cast(const _FromType& __arg)
  62. { return _ToType(const_cast<typename _Caster<_ToType>::
  63. type>(__arg.get())); }
  64. template<typename _ToType, typename _FromType>
  65. inline _ToType
  66. __reinterpret_pointer_cast(const _FromType& __arg)
  67. { return _ToType(reinterpret_cast<typename _Caster<_ToType>::
  68. type>(__arg.get())); }
  69. /**
  70. * Casting operations for cases where _FromType is a standard pointer.
  71. * _ToType can be a standard or non-standard pointer.
  72. */
  73. template<typename _ToType, typename _FromType>
  74. inline _ToType
  75. __static_pointer_cast(_FromType* __arg)
  76. { return _ToType(static_cast<typename _Caster<_ToType>::
  77. type>(__arg)); }
  78. template<typename _ToType, typename _FromType>
  79. inline _ToType
  80. __dynamic_pointer_cast(_FromType* __arg)
  81. { return _ToType(dynamic_cast<typename _Caster<_ToType>::
  82. type>(__arg)); }
  83. template<typename _ToType, typename _FromType>
  84. inline _ToType
  85. __const_pointer_cast(_FromType* __arg)
  86. { return _ToType(const_cast<typename _Caster<_ToType>::
  87. type>(__arg)); }
  88. template<typename _ToType, typename _FromType>
  89. inline _ToType
  90. __reinterpret_pointer_cast(_FromType* __arg)
  91. { return _ToType(reinterpret_cast<typename _Caster<_ToType>::
  92. type>(__arg)); }
  93. _GLIBCXX_END_NAMESPACE
  94. #endif // _CAST_H