move.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Move, forward and identity for C++0x + swap -*- C++ -*-
  2. // Copyright (C) 2007, 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. /** @file move.h
  21. * This is an internal header file, included by other library headers.
  22. * You should not attempt to use it directly.
  23. */
  24. #ifndef _MOVE_H
  25. #define _MOVE_H 1
  26. #include <bits/c++config.h>
  27. #include <cstddef>
  28. #include <bits/concept_check.h>
  29. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  30. #include <type_traits>
  31. _GLIBCXX_BEGIN_NAMESPACE(std)
  32. // 20.2.2, forward/move
  33. template<typename _Tp>
  34. struct identity
  35. {
  36. typedef _Tp type;
  37. };
  38. template<typename _Tp>
  39. inline _Tp&&
  40. forward(typename std::identity<_Tp>::type&& __t)
  41. { return __t; }
  42. template<typename _Tp>
  43. inline typename std::remove_reference<_Tp>::type&&
  44. move(_Tp&& __t)
  45. { return __t; }
  46. _GLIBCXX_END_NAMESPACE
  47. #define _GLIBCXX_MOVE(_Tp) std::move(_Tp)
  48. #else
  49. #define _GLIBCXX_MOVE(_Tp) (_Tp)
  50. #endif
  51. _GLIBCXX_BEGIN_NAMESPACE(std)
  52. /**
  53. * @brief Swaps two values.
  54. * @param a A thing of arbitrary type.
  55. * @param b Another thing of arbitrary type.
  56. * @return Nothing.
  57. */
  58. template<typename _Tp>
  59. inline void
  60. swap(_Tp& __a, _Tp& __b)
  61. {
  62. // concept requirements
  63. __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
  64. _Tp __tmp = _GLIBCXX_MOVE(__a);
  65. __a = _GLIBCXX_MOVE(__b);
  66. __b = _GLIBCXX_MOVE(__tmp);
  67. }
  68. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  69. // DR 809. std::swap should be overloaded for array types.
  70. template<typename _Tp, size_t _Nm>
  71. inline void
  72. swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
  73. {
  74. for (size_t __n = 0; __n < _Nm; ++__n)
  75. swap(__a[__n], __b[__n]);
  76. }
  77. _GLIBCXX_END_NAMESPACE
  78. #endif /* _MOVE_H */