1
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
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_MEMORY_SCOPED_NSOBJECT_H_
6
#define BASE_MEMORY_SCOPED_NSOBJECT_H_
8
#import <Foundation/Foundation.h>
10
// scoped_nsobject<> is patterned after scoped_ptr<>, but maintains ownership
11
// of an NSObject subclass object. Style deviations here are solely for
12
// compatibility with scoped_ptr<>'s interface, with which everyone is already
15
// When scoped_nsobject<> takes ownership of an object (in the constructor or
16
// in reset()), it takes over the caller's existing ownership claim. The
17
// caller must own the object it gives to scoped_nsobject<>, and relinquishes
18
// an ownership claim to that object. scoped_nsobject<> does not call
21
// scoped_nsobject<> is not to be used for NSAutoreleasePools. For
22
// NSAutoreleasePools use ScopedNSAutoreleasePool from
23
// scoped_nsautorelease_pool.h instead.
24
// We check for bad uses of scoped_nsobject and NSAutoreleasePool at compile
25
// time with a template specialization (see below).
26
template<typename NST>
27
class scoped_nsobject {
29
explicit scoped_nsobject(NST* object = nil)
37
void reset(NST* object = nil) {
38
// We intentionally do not check that object != object_ as the caller must
39
// already have an ownership claim over whatever it gives to
40
// scoped_nsobject and ScopedCFTypeRef, whether it's in the constructor or
41
// in a call to reset(). In either case, it relinquishes that claim and
42
// the scoper assumes it.
47
bool operator==(NST* that) const { return object_ == that; }
48
bool operator!=(NST* that) const { return object_ != that; }
50
operator NST*() const {
58
void swap(scoped_nsobject& that) {
59
NST* temp = that.object_;
60
that.object_ = object_;
64
// scoped_nsobject<>::release() is like scoped_ptr<>::release. It is NOT
65
// a wrapper for [object_ release]. To force a scoped_nsobject<> object to
66
// call [object_ release], use scoped_nsobject<>::reset().
67
NST* release() __attribute__((warn_unused_result)) {
76
Q_DISABLE_COPY(scoped_nsobject);
81
void swap(scoped_nsobject<C>& p1, scoped_nsobject<C>& p2) {
86
bool operator==(C* p1, const scoped_nsobject<C>& p2) {
87
return p1 == p2.get();
91
bool operator!=(C* p1, const scoped_nsobject<C>& p2) {
92
return p1 != p2.get();
96
// Specialization to make scoped_nsobject<id> work.
98
class scoped_nsobject<id> {
100
explicit scoped_nsobject(id object = nil)
108
void reset(id object = nil) {
109
// We intentionally do not check that object != object_ as the caller must
110
// already have an ownership claim over whatever it gives to
111
// scoped_nsobject and ScopedCFTypeRef, whether it's in the constructor or
112
// in a call to reset(). In either case, it relinquishes that claim and
113
// the scoper assumes it.
118
bool operator==(id that) const { return object_ == that; }
119
bool operator!=(id that) const { return object_ != that; }
121
operator id() const {
129
void swap(scoped_nsobject& that) {
130
id temp = that.object_;
131
that.object_ = object_;
135
// scoped_nsobject<>::release() is like scoped_ptr<>::release. It is NOT
136
// a wrapper for [object_ release]. To force a scoped_nsobject<> object to
137
// call [object_ release], use scoped_nsobject<>::reset().
138
id release() __attribute__((warn_unused_result)) {
147
Q_DISABLE_COPY(scoped_nsobject);
150
// Do not use scoped_nsobject for NSAutoreleasePools, use
151
// ScopedNSAutoreleasePool instead. This is a compile time check. See details
154
class scoped_nsobject<NSAutoreleasePool> {
156
explicit scoped_nsobject(NSAutoreleasePool* object = nil);
157
Q_DISABLE_COPY(scoped_nsobject);
160
#endif // BASE_MEMORY_SCOPED_NSOBJECT_H_