Home | History | Annotate | Download | only in memory
      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 <tuple>
      9 #include <type_traits>
     10 
     11 #include "base/memory/ref_counted.h"
     12 
     13 // It is dangerous to post a task with a T* argument where T is a subtype of
     14 // RefCounted(Base|ThreadSafeBase), since by the time the parameter is used, the
     15 // object may already have been deleted since it was not held with a
     16 // scoped_refptr. Example: http://crbug.com/27191
     17 // The following set of traits are designed to generate a compile error
     18 // whenever this antipattern is attempted.
     19 
     20 namespace base {
     21 
     22 // This is a base internal implementation file used by task.h and callback.h.
     23 // Not for public consumption, so we wrap it in namespace internal.
     24 namespace internal {
     25 
     26 template <typename T>
     27 struct NeedsScopedRefptrButGetsRawPtr {
     28   enum {
     29     // Human readable translation: you needed to be a scoped_refptr if you are a
     30     // raw pointer type and are convertible to a RefCounted(Base|ThreadSafeBase)
     31     // type.
     32     value = (std::is_pointer<T>::value &&
     33              (std::is_convertible<T, subtle::RefCountedBase*>::value ||
     34               std::is_convertible<T, subtle::RefCountedThreadSafeBase*>::value))
     35   };
     36 };
     37 
     38 template <typename Params>
     39 struct ParamsUseScopedRefptrCorrectly {
     40   enum { value = 0 };
     41 };
     42 
     43 template <>
     44 struct ParamsUseScopedRefptrCorrectly<std::tuple<>> {
     45   enum { value = 1 };
     46 };
     47 
     48 template <typename Head, typename... Tail>
     49 struct ParamsUseScopedRefptrCorrectly<std::tuple<Head, Tail...>> {
     50   enum { value = !NeedsScopedRefptrButGetsRawPtr<Head>::value &&
     51                   ParamsUseScopedRefptrCorrectly<std::tuple<Tail...>>::value };
     52 };
     53 
     54 }  // namespace internal
     55 
     56 }  // namespace base
     57 
     58 #endif  // BASE_MEMORY_RAW_SCOPED_REFPTR_MISMATCH_CHECKER_H_
     59