1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_FUNCTIONAL_UNRETAINED_TRAITS_H_
6 #define BASE_FUNCTIONAL_UNRETAINED_TRAITS_H_
8 #include "build/build_config.h"
10 #include <type_traits>
12 // Various opaque system types that should still be usable with the base
13 // callback system. Please keep sorted.
18 struct VkDeviceMemory_T;
21 struct VmaAllocation_T;
22 struct WGPUAdapterImpl;
23 struct fpdf_action_t__;
24 struct fpdf_annotation_t__;
25 struct fpdf_attachment_t__;
26 struct fpdf_bookmark_t__;
27 struct fpdf_document_t__;
28 struct fpdf_form_handle_t__;
30 struct fpdf_structelement_t__;
36 namespace base::internal {
38 // True if `T` is completely defined or false otherwise. Note that this always
39 // returns false for function types.
40 template <typename T, typename = void>
41 inline constexpr bool IsCompleteTypeV = false;
44 inline constexpr bool IsCompleteTypeV<T, std::void_t<decltype(sizeof(T))>> =
47 // Determining whether a type can be used with `Unretained()` requires that `T`
48 // be completely defined. Some system types have an intentionally opaque and
49 // incomplete representation, but should still be usable with `Unretained()`.
50 // The specializations here provide intentional escape hatches for those
53 inline constexpr bool IsIncompleteTypeSafeForUnretained = false;
55 // void* is occasionally used with callbacks; in the future, this may be more
56 // restricted/limited, but allow it for now.
58 inline constexpr bool IsIncompleteTypeSafeForUnretained<void> = true;
60 // Functions have static lifetime and are always safe for use with
62 template <typename R, typename... Args>
63 inline constexpr bool IsIncompleteTypeSafeForUnretained<R(Args...)> = true;
65 // Various opaque system types that should still be usable with the base
66 // callback system. Please keep sorted.
68 inline constexpr bool IsIncompleteTypeSafeForUnretained<ANativeWindow> = true;
70 inline constexpr bool IsIncompleteTypeSafeForUnretained<DBusMessage> = true;
72 inline constexpr bool IsIncompleteTypeSafeForUnretained<HWND__> = true;
74 inline constexpr bool IsIncompleteTypeSafeForUnretained<VkBuffer_T> = true;
76 inline constexpr bool IsIncompleteTypeSafeForUnretained<VkDeviceMemory_T> =
79 inline constexpr bool IsIncompleteTypeSafeForUnretained<VkImage_T> = true;
81 inline constexpr bool IsIncompleteTypeSafeForUnretained<VkSemaphore_T> = true;
83 inline constexpr bool IsIncompleteTypeSafeForUnretained<VmaAllocation_T> = true;
85 inline constexpr bool IsIncompleteTypeSafeForUnretained<WGPUAdapterImpl> = true;
87 inline constexpr bool IsIncompleteTypeSafeForUnretained<fpdf_action_t__> = true;
89 inline constexpr bool IsIncompleteTypeSafeForUnretained<fpdf_annotation_t__> =
92 inline constexpr bool IsIncompleteTypeSafeForUnretained<fpdf_attachment_t__> =
95 inline constexpr bool IsIncompleteTypeSafeForUnretained<fpdf_bookmark_t__> =
98 inline constexpr bool IsIncompleteTypeSafeForUnretained<fpdf_document_t__> =
101 inline constexpr bool IsIncompleteTypeSafeForUnretained<fpdf_form_handle_t__> =
104 inline constexpr bool IsIncompleteTypeSafeForUnretained<fpdf_page_t__> = true;
106 inline constexpr bool
107 IsIncompleteTypeSafeForUnretained<fpdf_structelement_t__> = true;
109 inline constexpr bool IsIncompleteTypeSafeForUnretained<hb_set_t> = true;
111 inline constexpr bool IsIncompleteTypeSafeForUnretained<wl_gpu> = true;
113 inline constexpr bool IsIncompleteTypeSafeForUnretained<wl_shm> = true;
115 inline constexpr bool IsIncompleteTypeSafeForUnretained<wl_surface> = true;
117 template <typename T, typename SFINAE = void>
118 struct TypeSupportsUnretained {
119 // Incrementally enforce the requirement to be completely defined. For now,
120 // limit the failures to:
123 // - non-official code (because these builds don't run as part of the default CQ
124 // and are slower due to PGO and LTO)
125 // - Android, Linux or Windows
127 // to make this easier to land without potentially breaking the tree.
129 // TODO(https://crbug.com/1392872): Enable this on all platforms, then in
130 // official builds, and then in non-test code as well.
131 #if !defined(UNIT_TEST) && !defined(OFFICIAL_BUILD)
132 #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_WIN) || \
133 defined(FORCE_UNRETAINED_COMPLETENESS_CHECKS_FOR_TESTS)
134 static_assert(IsCompleteTypeV<T> ||
135 IsIncompleteTypeSafeForUnretained<std::remove_cv_t<T>>,
136 "T must be fully defined.");
138 #endif // !defined(UNIT_TEST) && !defined(OFFICIAL_BUILD)
140 static constexpr inline bool kValue = true;
143 // Matches against the marker tag created by the `DISALLOW_UNRETAINED()` macro
144 // in //base/functional/disallow_unretained.h.
145 template <typename T>
146 struct TypeSupportsUnretained<T, typename T::DisallowBaseUnretainedMarker> {
147 static constexpr inline bool kValue = false;
150 // True if `T` is annotated with `DISALLOW_UNRETAINED()` and false otherwise.
151 template <typename T>
152 static inline constexpr bool TypeSupportsUnretainedV =
153 TypeSupportsUnretained<T>::kValue;
155 } // namespace base::internal
157 #endif // BASE_FUNCTIONAL_UNRETAINED_TRAITS_H_