propagate_const 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. // <experimental/propagate_const> -*- C++ -*-
  2. // Copyright (C) 2015-2018 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 experimental/propagate_const
  21. * This is a TS C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_EXPERIMENTAL_PROPAGATE_CONST
  24. #define _GLIBCXX_EXPERIMENTAL_PROPAGATE_CONST 1
  25. #pragma GCC system_header
  26. #if __cplusplus >= 201402L
  27. #include <type_traits>
  28. #include <bits/functional_hash.h>
  29. #include <bits/move.h>
  30. #include <bits/stl_function.h>
  31. #include <experimental/bits/lfts_config.h>
  32. namespace std _GLIBCXX_VISIBILITY(default)
  33. {
  34. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  35. namespace experimental
  36. {
  37. inline namespace fundamentals_v2
  38. {
  39. /**
  40. * @defgroup propagate_const Const-propagating wrapper
  41. * @ingroup experimental
  42. *
  43. * A const-propagating wrapper that propagates const to pointer-like members,
  44. * as described in n4388 "A Proposal to Add a Const-Propagating Wrapper
  45. * to the Standard Library".
  46. *
  47. * @{
  48. */
  49. /// Const-propagating wrapper.
  50. template <typename _Tp>
  51. class propagate_const
  52. {
  53. public:
  54. typedef remove_reference_t<decltype(*std::declval<_Tp&>())> element_type;
  55. private:
  56. template <typename _Up>
  57. struct __is_propagate_const : false_type
  58. { };
  59. template <typename _Up>
  60. struct __is_propagate_const<propagate_const<_Up>> : true_type
  61. { };
  62. template <typename _Up>
  63. friend constexpr const _Up&
  64. get_underlying(const propagate_const<_Up>& __pt) noexcept;
  65. template <typename _Up>
  66. friend constexpr _Up&
  67. get_underlying(propagate_const<_Up>& __pt) noexcept;
  68. template <typename _Up>
  69. static constexpr element_type*
  70. __to_raw_pointer(_Up* __u)
  71. { return __u; }
  72. template <typename _Up>
  73. static constexpr element_type*
  74. __to_raw_pointer(_Up& __u)
  75. { return __u.get(); }
  76. template <typename _Up>
  77. static constexpr const element_type*
  78. __to_raw_pointer(const _Up* __u)
  79. { return __u; }
  80. template <typename _Up>
  81. static constexpr const element_type*
  82. __to_raw_pointer(const _Up& __u)
  83. { return __u.get(); }
  84. public:
  85. static_assert(__and_<is_object<typename remove_pointer<_Tp>::type>,
  86. __not_<is_array<_Tp>>,
  87. __or_<is_class<_Tp>, is_pointer<_Tp>>>::value,
  88. "propagate_const requires a class or a pointer to an"
  89. " object type");
  90. // [propagate_const.ctor], constructors
  91. constexpr propagate_const() = default;
  92. propagate_const(const propagate_const& __p) = delete;
  93. constexpr propagate_const(propagate_const&& __p) = default;
  94. template <typename _Up, typename
  95. enable_if<__and_<is_constructible<_Tp, _Up&&>,
  96. is_convertible<_Up&&, _Tp>>::value, bool
  97. >::type=true>
  98. constexpr propagate_const(propagate_const<_Up>&& __pu)
  99. : _M_t(std::move(get_underlying(__pu)))
  100. {}
  101. template <typename _Up, typename
  102. enable_if<__and_<is_constructible<_Tp, _Up&&>,
  103. __not_<is_convertible<_Up&&, _Tp>>>::value,
  104. bool>::type=false>
  105. constexpr explicit propagate_const(propagate_const<_Up>&& __pu)
  106. : _M_t(std::move(get_underlying(__pu)))
  107. {}
  108. template <typename _Up, typename
  109. enable_if<__and_<is_constructible<_Tp, _Up&&>,
  110. is_convertible<_Up&&, _Tp>,
  111. __not_<__is_propagate_const<
  112. typename decay<_Up>::type>>
  113. >::value, bool>::type=true>
  114. constexpr propagate_const(_Up&& __u)
  115. : _M_t(std::forward<_Up>(__u))
  116. {}
  117. template <typename _Up, typename
  118. enable_if<__and_<is_constructible<_Tp, _Up&&>,
  119. __not_<is_convertible<_Up&&, _Tp>>,
  120. __not_<__is_propagate_const<
  121. typename decay<_Up>::type>>
  122. >::value, bool>::type=false>
  123. constexpr explicit propagate_const(_Up&& __u)
  124. : _M_t(std::forward<_Up>(__u))
  125. {}
  126. // [propagate_const.assignment], assignment
  127. propagate_const& operator=(const propagate_const& __p) = delete;
  128. constexpr propagate_const& operator=(propagate_const&& __p) = default;
  129. template <typename _Up, typename =
  130. typename enable_if<is_convertible<_Up&&, _Tp>::value>::type>
  131. constexpr propagate_const& operator=(propagate_const<_Up>&& __pu)
  132. {
  133. _M_t = std::move(get_underlying(__pu));
  134. return *this;
  135. }
  136. template <typename _Up, typename =
  137. typename enable_if<__and_<is_convertible<_Up&&, _Tp>,
  138. __not_<__is_propagate_const<
  139. typename decay<_Up>::type>>
  140. >::value>::type>
  141. constexpr propagate_const& operator=(_Up&& __u)
  142. {
  143. _M_t = std::forward<_Up>(__u);
  144. return *this;
  145. }
  146. // [propagate_const.const_observers], const observers
  147. explicit constexpr operator bool() const
  148. {
  149. return bool(_M_t);
  150. }
  151. constexpr const element_type* operator->() const
  152. {
  153. return get();
  154. }
  155. template <typename _Up = _Tp,
  156. typename enable_if<__or_<is_pointer<_Up>,
  157. is_convertible<_Up,
  158. const element_type*>
  159. >::value, bool>::type = true>
  160. constexpr operator const element_type*() const
  161. {
  162. return get();
  163. }
  164. constexpr const element_type& operator*() const
  165. {
  166. return *get();
  167. }
  168. constexpr const element_type* get() const
  169. {
  170. return __to_raw_pointer(_M_t);
  171. }
  172. // [propagate_const.non_const_observers], non-const observers
  173. constexpr element_type* operator->()
  174. {
  175. return get();
  176. }
  177. template <typename _Up = _Tp,
  178. typename enable_if<__or_<is_pointer<_Up>,
  179. is_convertible<_Up,
  180. const element_type*>
  181. >::value, bool>::type = true>
  182. constexpr operator element_type*()
  183. {
  184. return get();
  185. }
  186. constexpr element_type& operator*()
  187. {
  188. return *get();
  189. }
  190. constexpr element_type* get()
  191. {
  192. return __to_raw_pointer(_M_t);
  193. }
  194. // [propagate_const.modifiers], modifiers
  195. constexpr void
  196. swap(propagate_const& __pt) noexcept(__is_nothrow_swappable<_Tp>::value)
  197. {
  198. using std::swap;
  199. swap(_M_t, get_underlying(__pt));
  200. }
  201. private:
  202. _Tp _M_t;
  203. };
  204. // [propagate_const.relational], relational operators
  205. template <typename _Tp>
  206. constexpr bool
  207. operator==(const propagate_const<_Tp>& __pt, nullptr_t)
  208. {
  209. return get_underlying(__pt) == nullptr;
  210. }
  211. template <typename _Tp>
  212. constexpr bool
  213. operator==(nullptr_t, const propagate_const<_Tp>& __pu)
  214. {
  215. return nullptr == get_underlying(__pu);
  216. }
  217. template <typename _Tp>
  218. constexpr bool
  219. operator!=(const propagate_const<_Tp>& __pt, nullptr_t)
  220. {
  221. return get_underlying(__pt) != nullptr;
  222. }
  223. template <typename _Tp>
  224. constexpr bool operator!=(nullptr_t, const propagate_const<_Tp>& __pu)
  225. {
  226. return nullptr != get_underlying(__pu);
  227. }
  228. template <typename _Tp, typename _Up>
  229. constexpr bool
  230. operator==(const propagate_const<_Tp>& __pt,
  231. const propagate_const<_Up>& __pu)
  232. {
  233. return get_underlying(__pt) == get_underlying(__pu);
  234. }
  235. template <typename _Tp, typename _Up>
  236. constexpr bool
  237. operator!=(const propagate_const<_Tp>& __pt,
  238. const propagate_const<_Up>& __pu)
  239. {
  240. return get_underlying(__pt) != get_underlying(__pu);
  241. }
  242. template <typename _Tp, typename _Up>
  243. constexpr bool
  244. operator<(const propagate_const<_Tp>& __pt,
  245. const propagate_const<_Up>& __pu)
  246. {
  247. return get_underlying(__pt) < get_underlying(__pu);
  248. }
  249. template <typename _Tp, typename _Up>
  250. constexpr bool
  251. operator>(const propagate_const<_Tp>& __pt,
  252. const propagate_const<_Up>& __pu)
  253. {
  254. return get_underlying(__pt) > get_underlying(__pu);
  255. }
  256. template <typename _Tp, typename _Up>
  257. constexpr bool
  258. operator<=(const propagate_const<_Tp>& __pt,
  259. const propagate_const<_Up>& __pu)
  260. {
  261. return get_underlying(__pt) <= get_underlying(__pu);
  262. }
  263. template <typename _Tp, typename _Up>
  264. constexpr bool
  265. operator>=(const propagate_const<_Tp>& __pt,
  266. const propagate_const<_Up>& __pu)
  267. {
  268. return get_underlying(__pt) >= get_underlying(__pu);
  269. }
  270. template <typename _Tp, typename _Up>
  271. constexpr bool
  272. operator==(const propagate_const<_Tp>& __pt, const _Up& __u)
  273. {
  274. return get_underlying(__pt) == __u;
  275. }
  276. template <typename _Tp, typename _Up>
  277. constexpr bool
  278. operator!=(const propagate_const<_Tp>& __pt, const _Up& __u)
  279. {
  280. return get_underlying(__pt) != __u;
  281. }
  282. template <typename _Tp, typename _Up>
  283. constexpr bool
  284. operator<(const propagate_const<_Tp>& __pt, const _Up& __u)
  285. {
  286. return get_underlying(__pt) < __u;
  287. }
  288. template <typename _Tp, typename _Up>
  289. constexpr bool
  290. operator>(const propagate_const<_Tp>& __pt, const _Up& __u)
  291. {
  292. return get_underlying(__pt) > __u;
  293. }
  294. template <typename _Tp, typename _Up>
  295. constexpr bool
  296. operator<=(const propagate_const<_Tp>& __pt, const _Up& __u)
  297. {
  298. return get_underlying(__pt) <= __u;
  299. }
  300. template <typename _Tp, typename _Up>
  301. constexpr bool
  302. operator>=(const propagate_const<_Tp>& __pt, const _Up& __u)
  303. {
  304. return get_underlying(__pt) >= __u;
  305. }
  306. template <typename _Tp, typename _Up>
  307. constexpr bool
  308. operator==(const _Tp& __t, const propagate_const<_Up>& __pu)
  309. {
  310. return __t == get_underlying(__pu);
  311. }
  312. template <typename _Tp, typename _Up>
  313. constexpr bool
  314. operator!=(const _Tp& __t, const propagate_const<_Up>& __pu)
  315. {
  316. return __t != get_underlying(__pu);
  317. }
  318. template <typename _Tp, typename _Up>
  319. constexpr bool
  320. operator<(const _Tp& __t, const propagate_const<_Up>& __pu)
  321. {
  322. return __t < get_underlying(__pu);
  323. }
  324. template <typename _Tp, typename _Up>
  325. constexpr bool
  326. operator>(const _Tp& __t, const propagate_const<_Up>& __pu)
  327. {
  328. return __t > get_underlying(__pu);
  329. }
  330. template <typename _Tp, typename _Up>
  331. constexpr bool
  332. operator<=(const _Tp& __t, const propagate_const<_Up>& __pu)
  333. {
  334. return __t <= get_underlying(__pu);
  335. }
  336. template <typename _Tp, typename _Up>
  337. constexpr bool
  338. operator>=(const _Tp& __t, const propagate_const<_Up>& __pu)
  339. {
  340. return __t >= get_underlying(__pu);
  341. }
  342. // [propagate_const.algorithms], specialized algorithms
  343. template <typename _Tp>
  344. constexpr void
  345. swap(propagate_const<_Tp>& __pt, propagate_const<_Tp>& __pt2)
  346. noexcept(__is_nothrow_swappable<_Tp>::value)
  347. {
  348. __pt.swap(__pt2);
  349. }
  350. // [propagate_const.underlying], underlying pointer access
  351. template <typename _Tp>
  352. constexpr const _Tp&
  353. get_underlying(const propagate_const<_Tp>& __pt) noexcept
  354. {
  355. return __pt._M_t;
  356. }
  357. template <typename _Tp>
  358. constexpr _Tp&
  359. get_underlying(propagate_const<_Tp>& __pt) noexcept
  360. {
  361. return __pt._M_t;
  362. }
  363. // @} group propagate_const
  364. } // namespace fundamentals_v2
  365. } // namespace experimental
  366. // [propagate_const.hash], hash support
  367. template <typename _Tp>
  368. struct hash<experimental::propagate_const<_Tp>>
  369. {
  370. using result_type = size_t;
  371. using argument_type = experimental::propagate_const<_Tp>;
  372. size_t
  373. operator()(const experimental::propagate_const<_Tp>& __t) const
  374. noexcept(noexcept(hash<_Tp>{}(get_underlying(__t))))
  375. {
  376. return hash<_Tp>{}(get_underlying(__t));
  377. }
  378. };
  379. // [propagate_const.comparison_function_objects], comparison function objects
  380. template <typename _Tp>
  381. struct equal_to<experimental::propagate_const<_Tp>>
  382. {
  383. constexpr bool
  384. operator()(const experimental::propagate_const<_Tp>& __x,
  385. const experimental::propagate_const<_Tp>& __y) const
  386. {
  387. return equal_to<_Tp>{}(get_underlying(__x), get_underlying(__y));
  388. }
  389. typedef experimental::propagate_const<_Tp> first_argument_type;
  390. typedef experimental::propagate_const<_Tp> second_argument_type;
  391. typedef bool result_type;
  392. };
  393. template <typename _Tp>
  394. struct not_equal_to<experimental::propagate_const<_Tp>>
  395. {
  396. constexpr bool
  397. operator()(const experimental::propagate_const<_Tp>& __x,
  398. const experimental::propagate_const<_Tp>& __y) const
  399. {
  400. return not_equal_to<_Tp>{}(get_underlying(__x), get_underlying(__y));
  401. }
  402. typedef experimental::propagate_const<_Tp> first_argument_type;
  403. typedef experimental::propagate_const<_Tp> second_argument_type;
  404. typedef bool result_type;
  405. };
  406. template <typename _Tp>
  407. struct less<experimental::propagate_const<_Tp>>
  408. {
  409. constexpr bool
  410. operator()(const experimental::propagate_const<_Tp>& __x,
  411. const experimental::propagate_const<_Tp>& __y) const
  412. {
  413. return less<_Tp>{}(get_underlying(__x), get_underlying(__y));
  414. }
  415. typedef experimental::propagate_const<_Tp> first_argument_type;
  416. typedef experimental::propagate_const<_Tp> second_argument_type;
  417. typedef bool result_type;
  418. };
  419. template <typename _Tp>
  420. struct greater<experimental::propagate_const<_Tp>>
  421. {
  422. constexpr bool
  423. operator()(const experimental::propagate_const<_Tp>& __x,
  424. const experimental::propagate_const<_Tp>& __y) const
  425. {
  426. return greater<_Tp>{}(get_underlying(__x), get_underlying(__y));
  427. }
  428. typedef experimental::propagate_const<_Tp> first_argument_type;
  429. typedef experimental::propagate_const<_Tp> second_argument_type;
  430. typedef bool result_type;
  431. };
  432. template <typename _Tp>
  433. struct less_equal<experimental::propagate_const<_Tp>>
  434. {
  435. constexpr bool
  436. operator()(const experimental::propagate_const<_Tp>& __x,
  437. const experimental::propagate_const<_Tp>& __y) const
  438. {
  439. return less_equal<_Tp>{}(get_underlying(__x), get_underlying(__y));
  440. }
  441. typedef experimental::propagate_const<_Tp> first_argument_type;
  442. typedef experimental::propagate_const<_Tp> second_argument_type;
  443. typedef bool result_type;
  444. };
  445. template <typename _Tp>
  446. struct greater_equal<experimental::propagate_const<_Tp>>
  447. {
  448. constexpr bool
  449. operator()(const experimental::propagate_const<_Tp>& __x,
  450. const experimental::propagate_const<_Tp>& __y) const
  451. {
  452. return greater_equal<_Tp>{}(get_underlying(__x), get_underlying(__y));
  453. }
  454. typedef experimental::propagate_const<_Tp> first_argument_type;
  455. typedef experimental::propagate_const<_Tp> second_argument_type;
  456. typedef bool result_type;
  457. };
  458. _GLIBCXX_END_NAMESPACE_VERSION
  459. } // namespace std
  460. #endif // C++14
  461. #endif // _GLIBCXX_EXPERIMENTAL_PROPAGATE_CONST