xref: /aosp_15_r20/external/cronet/base/apple/owned_objc.mm (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#include "base/apple/owned_objc.h"
6
7#include <MacTypes.h>  // For nil, to avoid having to bring in frameworks.
8
9#include "build/build_config.h"
10
11#define OWNED_OBJC_IMPL(classname, objctype, ownership)                      \
12  namespace base::apple {                                                    \
13  struct classname::ObjCStorage {                                            \
14    objctype ownership obj;                                                  \
15  };                                                                         \
16  classname::classname() : objc_storage_(std::make_unique<ObjCStorage>()) {} \
17  classname::~classname() = default;                                         \
18  classname::classname(objctype obj) : classname() {                         \
19    objc_storage_->obj = obj;                                                \
20  }                                                                          \
21  classname::classname(const classname& other) : classname() {               \
22    objc_storage_->obj = other.objc_storage_->obj;                           \
23  }                                                                          \
24  classname& classname::operator=(const classname& other) {                  \
25    objc_storage_->obj = other.objc_storage_->obj;                           \
26    return *this;                                                            \
27  }                                                                          \
28  classname::operator bool() const {                                         \
29    return objc_storage_->obj != nil;                                        \
30  }                                                                          \
31  bool classname::operator==(const classname& other) const {                 \
32    return objc_storage_->obj == other.objc_storage_->obj;                   \
33  }                                                                          \
34  bool classname::operator!=(const classname& other) const {                 \
35    return !this->operator==(other);                                         \
36  }                                                                          \
37  objctype classname::Get() const {                                          \
38    return objc_storage_->obj;                                               \
39  }                                                                          \
40  }  // namespace base::apple
41
42#define GENERATE_STRONG_OBJC_TYPE(name) \
43  OWNED_OBJC_IMPL(Owned##name, name*, __strong)
44#define GENERATE_STRONG_OBJC_PROTOCOL(name) \
45  OWNED_OBJC_IMPL(Owned##name, id<name>, __strong)
46#define GENERATE_WEAK_OBJC_TYPE(name) OWNED_OBJC_IMPL(Weak##name, name*, __weak)
47#define GENERATE_WEAK_OBJC_PROTOCOL(name) \
48  OWNED_OBJC_IMPL(Weak##name, id<name>, __weak)
49
50#include "base/apple/owned_objc_types.h"
51
52#undef GENERATE_STRONG_OBJC_TYPE
53#undef GENERATE_STRONG_OBJC_PROTOCOL
54#undef GENERATE_WEAK_OBJC_TYPE
55#undef GENERATE_WEAK_OBJC_PROTOCOL
56#undef OWNED_OBJC_IMPL
57