• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 template <typename T>
35 struct NeedsScopedRefptrButGetsRawPtr {
36   static_assert(!std::is_reference<T>::value,
37                 "NeedsScopedRefptrButGetsRawPtr requires non-reference type.");
38 
39   enum {
40     // Human readable translation: you needed to be a scoped_refptr if you are a
41     // raw pointer type and are convertible to a RefCounted(Base|ThreadSafeBase)
42     // type.
43     value = std::is_pointer<T>::value &&
44             IsRefCountedType<std::remove_pointer_t<T>>::value
45   };
46 };
47 
48 }  // namespace internal
49 
50 }  // namespace base
51 
52 #endif  // BASE_MEMORY_RAW_SCOPED_REFPTR_MISMATCH_CHECKER_H_
53