1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_MEMORY_RAW_SCOPED_REFPTR_MISMATCH_CHECKER_H_
6 #define BASE_MEMORY_RAW_SCOPED_REFPTR_MISMATCH_CHECKER_H_
7 
8 #include <type_traits>
9 
10 #include "base/template_util.h"
11 
12 // It is dangerous to post a task with a T* argument where T is a subtype of
13 // RefCounted(Base|ThreadSafeBase), since by the time the parameter is used, the
14 // object may already have been deleted since it was not held with a
15 // scoped_refptr. Example: http://crbug.com/27191
16 // The following set of traits are designed to generate a compile error
17 // whenever this antipattern is attempted.
18 
19 namespace base {
20 
21 // This is a base internal implementation file used by task.h and callback.h.
22 // Not for public consumption, so we wrap it in namespace internal.
23 namespace internal {
24 
25 template <typename T, typename = void>
26 struct IsRefCountedType : std::false_type {};
27 
28 template <typename T>
29 struct IsRefCountedType<T,
30                         void_t<decltype(std::declval<T*>()->AddRef()),
31                                decltype(std::declval<T*>()->Release())>>
32     : std::true_type {};
33 
34 // Human readable translation: you needed to be a scoped_refptr if you are a raw
35 // pointer type and are convertible to a RefCounted(Base|ThreadSafeBase) type.
36 template <typename T>
37 struct NeedsScopedRefptrButGetsRawPtr
38     : conjunction<std::is_pointer<T>,
39                   IsRefCountedType<std::remove_pointer_t<T>>> {
40   static_assert(!std::is_reference<T>::value,
41                 "NeedsScopedRefptrButGetsRawPtr requires non-reference type.");
42 };
43 
44 }  // namespace internal
45 
46 }  // namespace base
47 
48 #endif  // BASE_MEMORY_RAW_SCOPED_REFPTR_MISMATCH_CHECKER_H_
49