PropagateConst.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <functional>
  18. #include <type_traits>
  19. #include <utility>
  20. #include <folly/Traits.h>
  21. #include <folly/Utility.h>
  22. namespace folly {
  23. template <typename Pointer>
  24. class propagate_const;
  25. template <typename Pointer>
  26. constexpr Pointer& get_underlying(propagate_const<Pointer>& obj) {
  27. return obj.pointer_;
  28. }
  29. template <typename Pointer>
  30. constexpr Pointer const& get_underlying(propagate_const<Pointer> const& obj) {
  31. return obj.pointer_;
  32. }
  33. namespace detail {
  34. template <typename>
  35. struct is_propagate_const : std::false_type {};
  36. template <typename Pointer>
  37. struct is_propagate_const<propagate_const<Pointer>> : std::true_type {};
  38. template <typename T>
  39. using is_decay_propagate_const = is_propagate_const<_t<std::decay<T>>>;
  40. namespace propagate_const_adl {
  41. using std::swap;
  42. template <typename T>
  43. auto adl_swap(T& a, T& b) noexcept(noexcept(swap(a, b)))
  44. -> decltype(swap(a, b)) {
  45. swap(a, b);
  46. }
  47. } // namespace propagate_const_adl
  48. } // namespace detail
  49. // mimic: std::experimental::propagate_const, C++ Library Fundamentals TS v2
  50. template <typename Pointer>
  51. class propagate_const {
  52. public:
  53. using element_type =
  54. _t<std::remove_reference<decltype(*std::declval<Pointer&>())>>;
  55. constexpr propagate_const() = default;
  56. FOLLY_CPP14_CONSTEXPR propagate_const(propagate_const&&) = default;
  57. propagate_const(propagate_const const&) = delete;
  58. template <
  59. typename OtherPointer,
  60. _t<std::enable_if<
  61. std::is_constructible<Pointer, OtherPointer&&>::value &&
  62. !std::is_convertible<OtherPointer&&, Pointer>::value,
  63. int>> = 0>
  64. constexpr explicit propagate_const(propagate_const<OtherPointer>&& other)
  65. : pointer_(static_cast<OtherPointer&&>(other.pointer_)) {}
  66. template <
  67. typename OtherPointer,
  68. _t<std::enable_if<
  69. std::is_constructible<Pointer, OtherPointer&&>::value &&
  70. std::is_convertible<OtherPointer&&, Pointer>::value,
  71. int>> = 0>
  72. constexpr propagate_const(propagate_const<OtherPointer>&& other)
  73. : pointer_(static_cast<OtherPointer&&>(other.pointer_)) {}
  74. template <
  75. typename OtherPointer,
  76. _t<std::enable_if<
  77. !detail::is_decay_propagate_const<OtherPointer>::value &&
  78. std::is_constructible<Pointer, OtherPointer&&>::value &&
  79. !std::is_convertible<OtherPointer&&, Pointer>::value,
  80. int>> = 0>
  81. constexpr explicit propagate_const(OtherPointer&& other)
  82. : pointer_(static_cast<OtherPointer&&>(other)) {}
  83. template <
  84. typename OtherPointer,
  85. _t<std::enable_if<
  86. !detail::is_decay_propagate_const<OtherPointer>::value &&
  87. std::is_constructible<Pointer, OtherPointer&&>::value &&
  88. std::is_convertible<OtherPointer&&, Pointer>::value,
  89. int>> = 0>
  90. constexpr propagate_const(OtherPointer&& other)
  91. : pointer_(static_cast<OtherPointer&&>(other)) {}
  92. FOLLY_CPP14_CONSTEXPR propagate_const& operator=(propagate_const&&) = default;
  93. propagate_const& operator=(propagate_const const&) = delete;
  94. template <
  95. typename OtherPointer,
  96. typename = _t<
  97. std::enable_if<std::is_convertible<OtherPointer&&, Pointer>::value>>>
  98. FOLLY_CPP14_CONSTEXPR propagate_const& operator=(
  99. propagate_const<OtherPointer>&& other) {
  100. pointer_ = static_cast<OtherPointer&&>(other.pointer_);
  101. }
  102. template <
  103. typename OtherPointer,
  104. typename = _t<std::enable_if<
  105. !detail::is_decay_propagate_const<OtherPointer>::value &&
  106. std::is_convertible<OtherPointer&&, Pointer>::value>>>
  107. FOLLY_CPP14_CONSTEXPR propagate_const& operator=(OtherPointer&& other) {
  108. pointer_ = static_cast<OtherPointer&&>(other);
  109. return *this;
  110. }
  111. FOLLY_CPP14_CONSTEXPR void swap(propagate_const& other) noexcept(
  112. noexcept(detail::propagate_const_adl::adl_swap(
  113. std::declval<Pointer&>(),
  114. other.pointer_))) {
  115. detail::propagate_const_adl::adl_swap(pointer_, other.pointer_);
  116. }
  117. FOLLY_CPP14_CONSTEXPR element_type* get() {
  118. return get_(pointer_);
  119. }
  120. constexpr element_type const* get() const {
  121. return get_(pointer_);
  122. }
  123. constexpr explicit operator bool() const {
  124. return static_cast<bool>(pointer_);
  125. }
  126. FOLLY_CPP14_CONSTEXPR element_type& operator*() {
  127. return *get();
  128. }
  129. constexpr element_type const& operator*() const {
  130. return *get();
  131. }
  132. FOLLY_CPP14_CONSTEXPR element_type* operator->() {
  133. return get();
  134. }
  135. constexpr element_type const* operator->() const {
  136. return get();
  137. }
  138. template <
  139. typename OtherPointer = Pointer,
  140. typename = _t<std::enable_if<
  141. std::is_pointer<OtherPointer>::value ||
  142. std::is_convertible<OtherPointer, element_type*>::value>>>
  143. FOLLY_CPP14_CONSTEXPR operator element_type*() {
  144. return get();
  145. }
  146. template <
  147. typename OtherPointer = Pointer,
  148. typename = _t<std::enable_if<
  149. std::is_pointer<OtherPointer>::value ||
  150. std::is_convertible<OtherPointer, element_type const*>::value>>>
  151. constexpr operator element_type const*() const {
  152. return get();
  153. }
  154. private:
  155. friend Pointer& get_underlying<>(propagate_const&);
  156. friend Pointer const& get_underlying<>(propagate_const const&);
  157. template <typename OtherPointer>
  158. friend class propagate_const;
  159. template <typename T>
  160. constexpr static T* get_(T* t) {
  161. return t;
  162. }
  163. template <typename T>
  164. constexpr static auto get_(T& t) -> decltype(t.get()) {
  165. return t.get();
  166. }
  167. Pointer pointer_;
  168. };
  169. template <typename Pointer>
  170. FOLLY_CPP14_CONSTEXPR void swap(
  171. propagate_const<Pointer>& a,
  172. propagate_const<Pointer>& b) noexcept(noexcept(a.swap(b))) {
  173. a.swap(b);
  174. }
  175. template <typename Pointer>
  176. constexpr bool operator==(propagate_const<Pointer> const& a, std::nullptr_t) {
  177. return get_underlying(a) == nullptr;
  178. }
  179. template <typename Pointer>
  180. constexpr bool operator==(std::nullptr_t, propagate_const<Pointer> const& a) {
  181. return nullptr == get_underlying(a);
  182. }
  183. template <typename Pointer>
  184. constexpr bool operator!=(propagate_const<Pointer> const& a, std::nullptr_t) {
  185. return get_underlying(a) != nullptr;
  186. }
  187. template <typename Pointer>
  188. constexpr bool operator!=(std::nullptr_t, propagate_const<Pointer> const& a) {
  189. return nullptr != get_underlying(a);
  190. }
  191. template <typename Pointer>
  192. constexpr bool operator==(
  193. propagate_const<Pointer> const& a,
  194. propagate_const<Pointer> const& b) {
  195. return get_underlying(a) == get_underlying(b);
  196. }
  197. template <typename Pointer>
  198. constexpr bool operator!=(
  199. propagate_const<Pointer> const& a,
  200. propagate_const<Pointer> const& b) {
  201. return get_underlying(a) != get_underlying(b);
  202. }
  203. template <typename Pointer>
  204. constexpr bool operator<(
  205. propagate_const<Pointer> const& a,
  206. propagate_const<Pointer> const& b) {
  207. return get_underlying(a) < get_underlying(b);
  208. }
  209. template <typename Pointer>
  210. constexpr bool operator<=(
  211. propagate_const<Pointer> const& a,
  212. propagate_const<Pointer> const& b) {
  213. return get_underlying(a) <= get_underlying(b);
  214. }
  215. template <typename Pointer>
  216. constexpr bool operator>(
  217. propagate_const<Pointer> const& a,
  218. propagate_const<Pointer> const& b) {
  219. return get_underlying(a) > get_underlying(b);
  220. }
  221. template <typename Pointer>
  222. constexpr bool operator>=(
  223. propagate_const<Pointer> const& a,
  224. propagate_const<Pointer> const& b) {
  225. return get_underlying(a) >= get_underlying(b);
  226. }
  227. // Note: contrary to the specification, the heterogeneous comparison operators
  228. // only participate in overload resolution when the equivalent heterogeneous
  229. // comparison operators on the underlying pointers, as returned by invocation
  230. // of get_underlying, would also participate in overload resolution.
  231. template <typename Pointer, typename Other>
  232. constexpr auto operator==(propagate_const<Pointer> const& a, Other const& b)
  233. -> decltype(get_underlying(a) == b, false) {
  234. return get_underlying(a) == b;
  235. }
  236. template <typename Pointer, typename Other>
  237. constexpr auto operator!=(propagate_const<Pointer> const& a, Other const& b)
  238. -> decltype(get_underlying(a) != b, false) {
  239. return get_underlying(a) != b;
  240. }
  241. template <typename Pointer, typename Other>
  242. constexpr auto operator<(propagate_const<Pointer> const& a, Other const& b)
  243. -> decltype(get_underlying(a) < b, false) {
  244. return get_underlying(a) < b;
  245. }
  246. template <typename Pointer, typename Other>
  247. constexpr auto operator<=(propagate_const<Pointer> const& a, Other const& b)
  248. -> decltype(get_underlying(a) <= b, false) {
  249. return get_underlying(a) <= b;
  250. }
  251. template <typename Pointer, typename Other>
  252. constexpr auto operator>(propagate_const<Pointer> const& a, Other const& b)
  253. -> decltype(get_underlying(a) > b, false) {
  254. return get_underlying(a) > b;
  255. }
  256. template <typename Pointer, typename Other>
  257. constexpr auto operator>=(propagate_const<Pointer> const& a, Other const& b)
  258. -> decltype(get_underlying(a) >= b, false) {
  259. return get_underlying(a) >= b;
  260. }
  261. template <typename Other, typename Pointer>
  262. constexpr auto operator==(Other const& a, propagate_const<Pointer> const& b)
  263. -> decltype(a == get_underlying(b), false) {
  264. return a == get_underlying(b);
  265. }
  266. template <typename Other, typename Pointer>
  267. constexpr auto operator!=(Other const& a, propagate_const<Pointer> const& b)
  268. -> decltype(a != get_underlying(b), false) {
  269. return a != get_underlying(b);
  270. }
  271. template <typename Other, typename Pointer>
  272. constexpr auto operator<(Other const& a, propagate_const<Pointer> const& b)
  273. -> decltype(a < get_underlying(b), false) {
  274. return a < get_underlying(b);
  275. }
  276. template <typename Other, typename Pointer>
  277. constexpr auto operator<=(Other const& a, propagate_const<Pointer> const& b)
  278. -> decltype(a <= get_underlying(b), false) {
  279. return a <= get_underlying(b);
  280. }
  281. template <typename Other, typename Pointer>
  282. constexpr auto operator>(Other const& a, propagate_const<Pointer> const& b)
  283. -> decltype(a > get_underlying(b), false) {
  284. return a > get_underlying(b);
  285. }
  286. template <typename Other, typename Pointer>
  287. constexpr auto operator>=(Other const& a, propagate_const<Pointer> const& b)
  288. -> decltype(a >= get_underlying(b), false) {
  289. return a >= get_underlying(b);
  290. }
  291. } // namespace folly
  292. namespace std {
  293. template <typename Pointer>
  294. struct hash<folly::propagate_const<Pointer>> : private hash<Pointer> {
  295. using hash<Pointer>::hash;
  296. size_t operator()(folly::propagate_const<Pointer> const& obj) const {
  297. return hash<Pointer>::operator()(folly::get_underlying(obj));
  298. }
  299. };
  300. template <typename Pointer>
  301. struct equal_to<folly::propagate_const<Pointer>> : private equal_to<Pointer> {
  302. using equal_to<Pointer>::equal_to;
  303. constexpr bool operator()(
  304. folly::propagate_const<Pointer> const& a,
  305. folly::propagate_const<Pointer> const& b) {
  306. return equal_to<Pointer>::operator()(
  307. folly::get_underlying(a), folly::get_underlying(b));
  308. }
  309. };
  310. template <typename Pointer>
  311. struct not_equal_to<folly::propagate_const<Pointer>>
  312. : private not_equal_to<Pointer> {
  313. using not_equal_to<Pointer>::not_equal_to;
  314. constexpr bool operator()(
  315. folly::propagate_const<Pointer> const& a,
  316. folly::propagate_const<Pointer> const& b) {
  317. return not_equal_to<Pointer>::operator()(
  318. folly::get_underlying(a), folly::get_underlying(b));
  319. }
  320. };
  321. template <typename Pointer>
  322. struct less<folly::propagate_const<Pointer>> : private less<Pointer> {
  323. using less<Pointer>::less;
  324. constexpr bool operator()(
  325. folly::propagate_const<Pointer> const& a,
  326. folly::propagate_const<Pointer> const& b) {
  327. return less<Pointer>::operator()(
  328. folly::get_underlying(a), folly::get_underlying(b));
  329. }
  330. };
  331. template <typename Pointer>
  332. struct greater<folly::propagate_const<Pointer>> : private greater<Pointer> {
  333. using greater<Pointer>::greater;
  334. constexpr bool operator()(
  335. folly::propagate_const<Pointer> const& a,
  336. folly::propagate_const<Pointer> const& b) {
  337. return greater<Pointer>::operator()(
  338. folly::get_underlying(a), folly::get_underlying(b));
  339. }
  340. };
  341. template <typename Pointer>
  342. struct less_equal<folly::propagate_const<Pointer>>
  343. : private less_equal<Pointer> {
  344. using less_equal<Pointer>::less_equal;
  345. constexpr bool operator()(
  346. folly::propagate_const<Pointer> const& a,
  347. folly::propagate_const<Pointer> const& b) {
  348. return less_equal<Pointer>::operator()(
  349. folly::get_underlying(a), folly::get_underlying(b));
  350. }
  351. };
  352. template <typename Pointer>
  353. struct greater_equal<folly::propagate_const<Pointer>>
  354. : private greater_equal<Pointer> {
  355. using greater_equal<Pointer>::greater_equal;
  356. constexpr bool operator()(
  357. folly::propagate_const<Pointer> const& a,
  358. folly::propagate_const<Pointer> const& b) {
  359. return greater_equal<Pointer>::operator()(
  360. folly::get_underlying(a), folly::get_underlying(b));
  361. }
  362. };
  363. } // namespace std