xref: /aosp_15_r20/external/cronet/base/apple/scoped_dispatch_object.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 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.
4 
5 #ifndef BASE_APPLE_SCOPED_DISPATCH_OBJECT_H_
6 #define BASE_APPLE_SCOPED_DISPATCH_OBJECT_H_
7 
8 #include <dispatch/dispatch.h>
9 
10 #include "base/apple/scoped_typeref.h"
11 
12 #if __OBJC__
13 // In Objective-C ARC, dispatch types are Objective-C types, and must be managed
14 // as such with __strong, etc. This header file must not be included in
15 // Objective-C code, nor may it be allowed to be recursively included. Use the
16 // pimpl pattern to isolate its use in a pure C++ file if needed.
17 #error Do not use this file, or allow it to be included, in Objective-C code.
18 #endif
19 
20 namespace base::apple {
21 
22 namespace internal {
23 
24 template <typename T>
25 struct ScopedDispatchObjectTraits {
InvalidValueScopedDispatchObjectTraits26   static constexpr T InvalidValue() { return nullptr; }
RetainScopedDispatchObjectTraits27   static T Retain(T object) {
28     dispatch_retain(object);
29     return object;
30   }
ReleaseScopedDispatchObjectTraits31   static void Release(T object) { dispatch_release(object); }
32 };
33 
34 }  // namespace internal
35 
36 template <typename T>
37 using ScopedDispatchObject =
38     ScopedTypeRef<T, internal::ScopedDispatchObjectTraits<T>>;
39 
40 }  // namespace base::apple
41 
42 #endif  // BASE_APPLE_SCOPED_DISPATCH_OBJECT_H_
43