xref: /aosp_15_r20/external/cronet/base/apple/owned_objc.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2023 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_OWNED_OBJC_H_
6 #define BASE_APPLE_OWNED_OBJC_H_
7 
8 #include <memory>
9 
10 #include "base/base_export.h"
11 #include "build/build_config.h"
12 
13 // This file defines wrappers to allow C++ code to hold references to
14 // Objective-C objects (either strong or weak) without being Objective-C++ code
15 // themselves.
16 //
17 // WHEN NOT TO USE:
18 // - Do not use these for pure Objective-C++ code. For that code, simply use
19 //   Objective-C types as normal.
20 // - Do not use as a member variable in an Objective-C++ class where the header
21 //   is included from C++ files. Use the pimpl idiom instead:
22 //   https://chromium.googlesource.com/chromium/src/+/main/docs/mac/mixing_cpp_and_objc.md
23 //
24 // Use these wrappers only in the situation where C++ code is passing
25 // Objective-C framework objects around, instead of using double-declaration.
26 
27 #if __OBJC__
28 
29 #define GENERATE_STRONG_OBJC_TYPE(name) @class name;
30 #define GENERATE_STRONG_OBJC_PROTOCOL(name) @protocol name;
31 #define GENERATE_WEAK_OBJC_TYPE(name) @class name;
32 #define GENERATE_WEAK_OBJC_PROTOCOL(name) @protocol name;
33 
34 #include "base/apple/owned_objc_types.h"
35 
36 #undef GENERATE_STRONG_OBJC_TYPE
37 #undef GENERATE_STRONG_OBJC_PROTOCOL
38 #undef GENERATE_WEAK_OBJC_TYPE
39 #undef GENERATE_WEAK_OBJC_PROTOCOL
40 
41 #endif  // __OBJC__
42 
43 // Define this class two ways: the full-fledged way that allows Objective-C code
44 // to fully construct and access the inner Objective-C object, and a
45 // C++-compatible way that does not expose any Objective-C code and only allows
46 // default construction and validity checking.
47 #if __OBJC__
48 #define OWNED_TYPE_DECL_OBJC_ADDITIONS(classname, objctype) \
49   explicit classname(objctype obj);                         \
50   objctype Get() const;
51 #else
52 #define OWNED_TYPE_DECL_OBJC_ADDITIONS(classname, objctype)
53 #endif  // __OBJC__
54 
55 #define OWNED_OBJC_DECL(classname, objctype)                 \
56   namespace base::apple {                                    \
57   class BASE_EXPORT classname {                              \
58    public:                                                   \
59     /* Default-construct in a null state. */                 \
60     classname();                                             \
61     ~classname();                                            \
62     classname(const classname&);                             \
63     classname& operator=(const classname&);                  \
64     /* Returns whether the object contains a valid object.*/ \
65     explicit operator bool() const;                          \
66     /* Comparisons. */                                       \
67     bool operator==(const classname& other) const;           \
68     bool operator!=(const classname& other) const;           \
69     /* Objective-C-only constructor and getter. */           \
70     OWNED_TYPE_DECL_OBJC_ADDITIONS(classname, objctype)      \
71                                                              \
72    private:                                                  \
73     struct ObjCStorage;                                      \
74     std::unique_ptr<ObjCStorage> objc_storage_;              \
75   };                                                         \
76   }  // namespace base::apple
77 
78 #define GENERATE_STRONG_OBJC_TYPE(name) OWNED_OBJC_DECL(Owned##name, name*)
79 #define GENERATE_STRONG_OBJC_PROTOCOL(name) \
80   OWNED_OBJC_DECL(Owned##name, id<name>)
81 #define GENERATE_WEAK_OBJC_TYPE(name) OWNED_OBJC_DECL(Weak##name, name*)
82 #define GENERATE_WEAK_OBJC_PROTOCOL(name) OWNED_OBJC_DECL(Weak##name, id<name>)
83 
84 #include "base/apple/owned_objc_types.h"
85 
86 #undef GENERATE_STRONG_OBJC_TYPE
87 #undef GENERATE_STRONG_OBJC_PROTOCOL
88 #undef GENERATE_WEAK_OBJC_TYPE
89 #undef GENERATE_WEAK_OBJC_PROTOCOL
90 #undef OWNED_OBJC_DECL
91 #undef OWNED_TYPE_DECL_OBJC_ADDITIONS
92 
93 #endif  // BASE_APPLE_OWNED_OBJC_H_
94