Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9d8af6af authored by Steven Moreland's avatar Steven Moreland
Browse files

RefBase: disallow make_shared, make_unique

It's risky to mix multiple-ownership types. Taken from SharedRefBase.

Bug: 232557259
Test: N/A
Change-Id: Ic0dbd6d11e44fa9db87c4f9b1776d4989cbf9f56
parent 8874ad63
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
@@ -779,6 +779,40 @@ void move_backward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {

}  // namespace android

namespace libutilsinternal {
template <typename T, typename = void>
struct is_complete_type : std::false_type {};

template <typename T>
struct is_complete_type<T, decltype(void(sizeof(T)))> : std::true_type {};
}  // namespace libutilsinternal

namespace std {

// Define `RefBase` specific versions of `std::make_shared` and
// `std::make_unique` to block people from using them. Using them to allocate
// `RefBase` objects results in double ownership. Use
// `sp<T>::make(...)` instead.
//
// Note: We exclude incomplete types because `std::is_base_of` is undefined in
// that case.

template <typename T, typename... Args,
          typename std::enable_if<libutilsinternal::is_complete_type<T>::value, bool>::value = true,
          typename std::enable_if<std::is_base_of<android::RefBase, T>::value, bool>::value = true>
shared_ptr<T> make_shared(Args...) {  // SEE COMMENT ABOVE.
    static_assert(!std::is_base_of<android::RefBase, T>::value, "Must use RefBase with sp<>");
}

template <typename T, typename... Args,
          typename std::enable_if<libutilsinternal::is_complete_type<T>::value, bool>::value = true,
          typename std::enable_if<std::is_base_of<android::RefBase, T>::value, bool>::value = true>
unique_ptr<T> make_unique(Args...) {  // SEE COMMENT ABOVE.
    static_assert(!std::is_base_of<android::RefBase, T>::value, "Must use RefBase with sp<>");
}

}  // namespace std

// ---------------------------------------------------------------------------

#endif // ANDROID_REF_BASE_H