Commit d6610d60 authored by Neel Goyal's avatar Neel Goyal Committed by Facebook GitHub Bot

import or backport reinterpret_pointer_cast

Summary: Import or backport `std::reinterpret_pointer_cast` into folly. The relevant guard is the use of the C++17 language and, if feature-test macros are exported, `__cpp_lib_shared_ptr_arrays >= 201611`.

Reviewed By: yfeldblum

Differential Revision: D29433489

fbshipit-source-id: 92596d05f5057cff4c65283711b4ed6778d20758
parent 1583ff06
......@@ -29,6 +29,7 @@
#include <folly/ConstexprMath.h>
#include <folly/Likely.h>
#include <folly/Portability.h>
#include <folly/Traits.h>
#include <folly/Utility.h>
#include <folly/functional/Invoke.h>
......@@ -473,6 +474,21 @@ inline erased_unique_ptr empty_erased_unique_ptr() {
return {nullptr, nullptr};
}
// reinterpret_pointer_cast
//
// import or backport
#if FOLLY_CPLUSPLUS >= 201703L && __cpp_lib_shared_ptr_arrays >= 201611
using std::reinterpret_pointer_cast;
#else
template <typename T, typename U>
std::shared_ptr<T> reinterpret_pointer_cast(
const std::shared_ptr<U>& r) noexcept {
auto p =
reinterpret_cast<typename std::shared_ptr<T>::element_type*>(r.get());
return std::shared_ptr<T>{r, p};
}
#endif
/**
* SysAllocator
*
......
......@@ -17,6 +17,7 @@
#include <memory>
#include <utility>
#include <folly/Memory.h>
#include <folly/Portability.h>
#include <folly/lang/Exception.h>
......@@ -495,13 +496,13 @@ not_null_shared_ptr<T> const_pointer_cast(not_null_shared_ptr<U>&& r) {
template <typename T, typename U>
not_null_shared_ptr<T> reinterpret_pointer_cast(
const not_null_shared_ptr<U>& r) {
auto p = std::reinterpret_pointer_cast<T, U>(r.unwrap());
auto p = folly::reinterpret_pointer_cast<T, U>(r.unwrap());
return not_null_shared_ptr<T>(
std::move(p), detail::secret_guaranteed_not_null::get());
}
template <typename T, typename U>
not_null_shared_ptr<T> reinterpret_pointer_cast(not_null_shared_ptr<U>&& r) {
auto p = std::reinterpret_pointer_cast<T, U>(std::move(r).unwrap());
auto p = folly::reinterpret_pointer_cast<T, U>(std::move(r).unwrap());
return not_null_shared_ptr<T>(
std::move(p), detail::secret_guaranteed_not_null::get());
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment