utility 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // <utility> -*- C++ -*-
  2. // Copyright (C) 2001-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. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996,1997
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file include/utility
  46. * This is a Standard C++ Library header.
  47. */
  48. #ifndef _GLIBCXX_UTILITY
  49. #define _GLIBCXX_UTILITY 1
  50. #pragma GCC system_header
  51. /**
  52. * @defgroup utilities Utilities
  53. *
  54. * Components deemed generally useful. Includes pair, tuple,
  55. * forward/move helpers, ratio, function object, metaprogramming and
  56. * type traits, time, date, and memory functions.
  57. */
  58. #include <bits/c++config.h>
  59. #include <bits/stl_relops.h>
  60. #include <bits/stl_pair.h>
  61. #if __cplusplus >= 201103L
  62. #include <bits/move.h>
  63. #include <initializer_list>
  64. namespace std _GLIBCXX_VISIBILITY(default)
  65. {
  66. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  67. template<class _Tp>
  68. class tuple_size;
  69. template<std::size_t _Int, class _Tp>
  70. class tuple_element;
  71. // Various functions which give std::pair a tuple-like interface.
  72. /// Partial specialization for std::pair
  73. template<class _Tp1, class _Tp2>
  74. struct tuple_size<std::pair<_Tp1, _Tp2>>
  75. : public integral_constant<std::size_t, 2> { };
  76. /// Partial specialization for std::pair
  77. template<class _Tp1, class _Tp2>
  78. struct tuple_element<0, std::pair<_Tp1, _Tp2>>
  79. { typedef _Tp1 type; };
  80. /// Partial specialization for std::pair
  81. template<class _Tp1, class _Tp2>
  82. struct tuple_element<1, std::pair<_Tp1, _Tp2>>
  83. { typedef _Tp2 type; };
  84. template<std::size_t _Int>
  85. struct __pair_get;
  86. template<>
  87. struct __pair_get<0>
  88. {
  89. template<typename _Tp1, typename _Tp2>
  90. static constexpr _Tp1&
  91. __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
  92. { return __pair.first; }
  93. template<typename _Tp1, typename _Tp2>
  94. static constexpr _Tp1&&
  95. __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
  96. { return std::forward<_Tp1>(__pair.first); }
  97. template<typename _Tp1, typename _Tp2>
  98. static constexpr const _Tp1&
  99. __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
  100. { return __pair.first; }
  101. };
  102. template<>
  103. struct __pair_get<1>
  104. {
  105. template<typename _Tp1, typename _Tp2>
  106. static constexpr _Tp2&
  107. __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
  108. { return __pair.second; }
  109. template<typename _Tp1, typename _Tp2>
  110. static constexpr _Tp2&&
  111. __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
  112. { return std::forward<_Tp2>(__pair.second); }
  113. template<typename _Tp1, typename _Tp2>
  114. static constexpr const _Tp2&
  115. __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
  116. { return __pair.second; }
  117. };
  118. template<std::size_t _Int, class _Tp1, class _Tp2>
  119. constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
  120. get(std::pair<_Tp1, _Tp2>& __in) noexcept
  121. { return __pair_get<_Int>::__get(__in); }
  122. template<std::size_t _Int, class _Tp1, class _Tp2>
  123. constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&&
  124. get(std::pair<_Tp1, _Tp2>&& __in) noexcept
  125. { return __pair_get<_Int>::__move_get(std::move(__in)); }
  126. template<std::size_t _Int, class _Tp1, class _Tp2>
  127. constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
  128. get(const std::pair<_Tp1, _Tp2>& __in) noexcept
  129. { return __pair_get<_Int>::__const_get(__in); }
  130. #if __cplusplus > 201103L
  131. #define __cpp_lib_tuples_by_type 201304
  132. template <typename _Tp, typename _Up>
  133. constexpr _Tp&
  134. get(pair<_Tp, _Up>& __p) noexcept
  135. { return __p.first; }
  136. template <typename _Tp, typename _Up>
  137. constexpr const _Tp&
  138. get(const pair<_Tp, _Up>& __p) noexcept
  139. { return __p.first; }
  140. template <typename _Tp, typename _Up>
  141. constexpr _Tp&&
  142. get(pair<_Tp, _Up>&& __p) noexcept
  143. { return std::move(__p.first); }
  144. template <typename _Tp, typename _Up>
  145. constexpr _Tp&
  146. get(pair<_Up, _Tp>& __p) noexcept
  147. { return __p.second; }
  148. template <typename _Tp, typename _Up>
  149. constexpr const _Tp&
  150. get(const pair<_Up, _Tp>& __p) noexcept
  151. { return __p.second; }
  152. template <typename _Tp, typename _Up>
  153. constexpr _Tp&&
  154. get(pair<_Up, _Tp>&& __p) noexcept
  155. { return std::move(__p.second); }
  156. #define __cpp_lib_exchange_function 201304
  157. /// Assign @p __new_val to @p __obj and return its previous value.
  158. template <typename _Tp, typename _Up = _Tp>
  159. inline _Tp
  160. exchange(_Tp& __obj, _Up&& __new_val)
  161. { return std::__exchange(__obj, std::forward<_Up>(__new_val)); }
  162. #endif
  163. // Stores a tuple of indices. Used by tuple and pair, and by bind() to
  164. // extract the elements in a tuple.
  165. template<size_t... _Indexes>
  166. struct _Index_tuple
  167. {
  168. typedef _Index_tuple<_Indexes..., sizeof...(_Indexes)> __next;
  169. };
  170. // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
  171. template<size_t _Num>
  172. struct _Build_index_tuple
  173. {
  174. typedef typename _Build_index_tuple<_Num - 1>::__type::__next __type;
  175. };
  176. template<>
  177. struct _Build_index_tuple<0>
  178. {
  179. typedef _Index_tuple<> __type;
  180. };
  181. #if __cplusplus > 201103L
  182. #define __cpp_lib_integer_sequence 201304
  183. /// Class template integer_sequence
  184. template<typename _Tp, _Tp... _Idx>
  185. struct integer_sequence
  186. {
  187. typedef _Tp value_type;
  188. static constexpr size_t size() { return sizeof...(_Idx); }
  189. };
  190. template<typename _Tp, _Tp _Num,
  191. typename _ISeq = typename _Build_index_tuple<_Num>::__type>
  192. struct _Make_integer_sequence;
  193. template<typename _Tp, _Tp _Num, size_t... _Idx>
  194. struct _Make_integer_sequence<_Tp, _Num, _Index_tuple<_Idx...>>
  195. {
  196. static_assert( _Num >= 0,
  197. "Cannot make integer sequence of negative length" );
  198. typedef integer_sequence<_Tp, static_cast<_Tp>(_Idx)...> __type;
  199. };
  200. /// Alias template make_integer_sequence
  201. template<typename _Tp, _Tp _Num>
  202. using make_integer_sequence
  203. = typename _Make_integer_sequence<_Tp, _Num>::__type;
  204. /// Alias template index_sequence
  205. template<size_t... _Idx>
  206. using index_sequence = integer_sequence<size_t, _Idx...>;
  207. /// Alias template make_index_sequence
  208. template<size_t _Num>
  209. using make_index_sequence = make_integer_sequence<size_t, _Num>;
  210. /// Alias template index_sequence_for
  211. template<typename... _Types>
  212. using index_sequence_for = make_index_sequence<sizeof...(_Types)>;
  213. #endif
  214. _GLIBCXX_END_NAMESPACE_VERSION
  215. } // namespace
  216. #endif
  217. #endif /* _GLIBCXX_UTILITY */