Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions fml/platform/darwin/scoped_nsobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ class scoped_nsprotocol
scoped_policy::OwnershipPolicy::kRetain) {}
#endif

// NOLINTNEXTLINE(google-explicit-constructor)
scoped_nsprotocol(const scoped_nsprotocol<NST>& that)
: ScopedTypeRef<NST, Traits>(that) {}

template <typename NSR>
explicit scoped_nsprotocol(const scoped_nsprotocol<NSR>& that_as_subclass)
: ScopedTypeRef<NST, Traits>(that_as_subclass) {}

// NOLINTNEXTLINE(google-explicit-constructor)
scoped_nsprotocol(scoped_nsprotocol<NST>&& that)
: ScopedTypeRef<NST, Traits>(std::move(that)) {}

Expand Down Expand Up @@ -161,13 +163,15 @@ class scoped_nsobject : public scoped_nsprotocol<NST*> {
: scoped_nsprotocol<NST*>(object) {}
#endif

// NOLINTNEXTLINE(google-explicit-constructor)
scoped_nsobject(const scoped_nsobject<NST>& that)
: scoped_nsprotocol<NST*>(that) {}

template <typename NSR>
explicit scoped_nsobject(const scoped_nsobject<NSR>& that_as_subclass)
: scoped_nsprotocol<NST*>(that_as_subclass) {}

// NOLINTNEXTLINE(google-explicit-constructor)
scoped_nsobject(scoped_nsobject<NST>&& that)
: scoped_nsprotocol<NST*>(std::move(that)) {}

Expand Down Expand Up @@ -210,13 +214,15 @@ class scoped_nsobject<id> : public scoped_nsprotocol<id> {
: scoped_nsprotocol<id>(object) {}
#endif

// NOLINTNEXTLINE(google-explicit-constructor)
scoped_nsobject(const scoped_nsobject<id>& that)
: scoped_nsprotocol<id>(that) {}

template <typename NSR>
explicit scoped_nsobject(const scoped_nsobject<NSR>& that_as_subclass)
: scoped_nsprotocol<id>(that_as_subclass) {}

// NOLINTNEXTLINE(google-explicit-constructor)
scoped_nsobject(scoped_nsobject<id>&& that)
: scoped_nsprotocol<id>(std::move(that)) {}

Expand Down
24 changes: 17 additions & 7 deletions fml/platform/darwin/scoped_typeref.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,36 @@ class ScopedTypeRef {
__unsafe_unretained T object = Traits::InvalidValue(),
fml::scoped_policy::OwnershipPolicy policy = fml::scoped_policy::kAssume)
: object_(object) {
if (object_ && policy == fml::scoped_policy::kRetain)
if (object_ && policy == fml::scoped_policy::kRetain) {
object_ = Traits::Retain(object_);
}
}

// NOLINTNEXTLINE(google-explicit-constructor)
ScopedTypeRef(const ScopedTypeRef<T, Traits>& that) : object_(that.object_) {
if (object_)
if (object_) {
object_ = Traits::Retain(object_);
}
}

// This allows passing an object to a function that takes its superclass.
template <typename R, typename RTraits>
explicit ScopedTypeRef(const ScopedTypeRef<R, RTraits>& that_as_subclass)
: object_(that_as_subclass.get()) {
if (object_)
if (object_) {
object_ = Traits::Retain(object_);
}
}

// NOLINTNEXTLINE(google-explicit-constructor)
ScopedTypeRef(ScopedTypeRef<T, Traits>&& that) : object_(that.object_) {
that.object_ = Traits::InvalidValue();
}

~ScopedTypeRef() {
if (object_)
if (object_) {
Traits::Release(object_);
}
}

ScopedTypeRef& operator=(const ScopedTypeRef<T, Traits>& that) {
Expand All @@ -98,17 +104,20 @@ class ScopedTypeRef {
void reset(__unsafe_unretained T object = Traits::InvalidValue(),
fml::scoped_policy::OwnershipPolicy policy =
fml::scoped_policy::kAssume) {
if (object && policy == fml::scoped_policy::kRetain)
if (object && policy == fml::scoped_policy::kRetain) {
object = Traits::Retain(object);
if (object_)
}
if (object_) {
Traits::Release(object_);
}
object_ = object;
}

bool operator==(__unsafe_unretained T that) const { return object_ == that; }

bool operator!=(__unsafe_unretained T that) const { return object_ != that; }

// NOLINTNEXTLINE(google-explicit-constructor)
operator T() const __attribute((ns_returns_not_retained)) { return object_; }

T get() const __attribute((ns_returns_not_retained)) { return object_; }
Expand All @@ -119,10 +128,11 @@ class ScopedTypeRef {
object_ = temp;
}

protected:
// ScopedTypeRef<>::release() is like std::unique_ptr<>::release. It is NOT
// a wrapper for Release(). To force a ScopedTypeRef<> object to call
// Release(), use ScopedTypeRef<>::reset().
T release() __attribute((ns_returns_not_retained)) WARN_UNUSED_RESULT {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If want to use [[nodiscard]] instead of the macro, presumably we should remove the part of this PR that adds the macro to our codebase and replace any other usage as well?

[[nodiscard]] T release() __attribute((ns_returns_not_retained)) {
__unsafe_unretained T temp = object_;
object_ = Traits::InvalidValue();
return temp;
Expand Down