diff --git a/libutils/include/utils/RefBase.h b/libutils/include/utils/RefBase.h index e07f57415..f3acd6f01 100644 --- a/libutils/include/utils/RefBase.h +++ b/libutils/include/utils/RefBase.h @@ -779,6 +779,40 @@ void move_backward_type(wp* d, wp const* s, size_t n) { } // namespace android +namespace libutilsinternal { +template +struct is_complete_type : std::false_type {}; + +template +struct is_complete_type : 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::make(...)` instead. +// +// Note: We exclude incomplete types because `std::is_base_of` is undefined in +// that case. + +template ::value, bool>::value = true, + typename std::enable_if::value, bool>::value = true> +shared_ptr make_shared(Args...) { // SEE COMMENT ABOVE. + static_assert(!std::is_base_of::value, "Must use RefBase with sp<>"); +} + +template ::value, bool>::value = true, + typename std::enable_if::value, bool>::value = true> +unique_ptr make_unique(Args...) { // SEE COMMENT ABOVE. + static_assert(!std::is_base_of::value, "Must use RefBase with sp<>"); +} + +} // namespace std + // --------------------------------------------------------------------------- #endif // ANDROID_REF_BASE_H