• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //    * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //    * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //    * Neither the name of Google Inc. nor the name Chromium Embedded
14 // Framework nor the names of its contributors may be used to endorse
15 // or promote products derived from this software without specific prior
16 // written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 // Do not include this header file directly. Use base/cef_callback.h instead.
31 
32 #ifndef CEF_INCLUDE_BASE_INTERNAL_CEF_RAW_SCOPED_REFPTR_MISMATCH_CHECKER_H_
33 #define CEF_INCLUDE_BASE_INTERNAL_CEF_RAW_SCOPED_REFPTR_MISMATCH_CHECKER_H_
34 
35 #include <type_traits>
36 
37 #include "include/base/cef_template_util.h"
38 
39 // It is dangerous to post a task with a T* argument where T is a subtype of
40 // RefCounted(Base|ThreadSafeBase), since by the time the parameter is used, the
41 // object may already have been deleted since it was not held with a
42 // scoped_refptr. Example: http://crbug.com/27191
43 // The following set of traits are designed to generate a compile error
44 // whenever this antipattern is attempted.
45 
46 namespace base {
47 
48 // This is a base internal implementation file used by task.h and callback.h.
49 // Not for public consumption, so we wrap it in namespace internal.
50 namespace internal {
51 
52 template <typename T, typename = void>
53 struct IsRefCountedType : std::false_type {};
54 
55 template <typename T>
56 struct IsRefCountedType<T,
57                         void_t<decltype(std::declval<T*>()->AddRef()),
58                                decltype(std::declval<T*>()->Release())>>
59     : std::true_type {};
60 
61 // Human readable translation: you needed to be a scoped_refptr if you are a raw
62 // pointer type and are convertible to a RefCounted(Base|ThreadSafeBase) type.
63 template <typename T>
64 struct NeedsScopedRefptrButGetsRawPtr
65     : conjunction<std::is_pointer<T>,
66                   IsRefCountedType<std::remove_pointer_t<T>>> {
67   static_assert(!std::is_reference<T>::value,
68                 "NeedsScopedRefptrButGetsRawPtr requires non-reference type.");
69 };
70 
71 }  // namespace internal
72 
73 }  // namespace base
74 
75 #endif  // CEF_INCLUDE_BASE_INTERNAL_CEF_RAW_SCOPED_REFPTR_MISMATCH_CHECKER_H_
76