~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/core/scoped_nsobject.h

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
4
 
 
5
#ifndef BASE_MEMORY_SCOPED_NSOBJECT_H_
 
6
#define BASE_MEMORY_SCOPED_NSOBJECT_H_
 
7
 
 
8
#import <Foundation/Foundation.h>
 
9
 
 
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
 
13
// familiar.
 
14
//
 
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
 
19
// -retain.
 
20
//
 
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 {
 
28
 public:
 
29
  explicit scoped_nsobject(NST* object = nil)
 
30
      : object_(object) {
 
31
  }
 
32
 
 
33
  ~scoped_nsobject() {
 
34
    [object_ release];
 
35
  }
 
36
 
 
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.
 
43
    [object_ release];
 
44
    object_ = object;
 
45
  }
 
46
 
 
47
  bool operator==(NST* that) const { return object_ == that; }
 
48
  bool operator!=(NST* that) const { return object_ != that; }
 
49
 
 
50
  operator NST*() const {
 
51
    return object_;
 
52
  }
 
53
 
 
54
  NST* get() const {
 
55
    return object_;
 
56
  }
 
57
 
 
58
  void swap(scoped_nsobject& that) {
 
59
    NST* temp = that.object_;
 
60
    that.object_ = object_;
 
61
    object_ = temp;
 
62
  }
 
63
 
 
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)) {
 
68
    NST* temp = object_;
 
69
    object_ = nil;
 
70
    return temp;
 
71
  }
 
72
 
 
73
 private:
 
74
  NST* object_;
 
75
 
 
76
  Q_DISABLE_COPY(scoped_nsobject);
 
77
};
 
78
 
 
79
// Free functions
 
80
template <class C>
 
81
void swap(scoped_nsobject<C>& p1, scoped_nsobject<C>& p2) {
 
82
  p1.swap(p2);
 
83
}
 
84
 
 
85
template <class C>
 
86
bool operator==(C* p1, const scoped_nsobject<C>& p2) {
 
87
  return p1 == p2.get();
 
88
}
 
89
 
 
90
template <class C>
 
91
bool operator!=(C* p1, const scoped_nsobject<C>& p2) {
 
92
  return p1 != p2.get();
 
93
}
 
94
 
 
95
 
 
96
// Specialization to make scoped_nsobject<id> work.
 
97
template<>
 
98
class scoped_nsobject<id> {
 
99
 public:
 
100
  explicit scoped_nsobject(id object = nil)
 
101
      : object_(object) {
 
102
  }
 
103
 
 
104
  ~scoped_nsobject() {
 
105
    [object_ release];
 
106
  }
 
107
 
 
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.
 
114
    [object_ release];
 
115
    object_ = object;
 
116
  }
 
117
 
 
118
  bool operator==(id that) const { return object_ == that; }
 
119
  bool operator!=(id that) const { return object_ != that; }
 
120
 
 
121
  operator id() const {
 
122
    return object_;
 
123
  }
 
124
 
 
125
  id get() const {
 
126
    return object_;
 
127
  }
 
128
 
 
129
  void swap(scoped_nsobject& that) {
 
130
    id temp = that.object_;
 
131
    that.object_ = object_;
 
132
    object_ = temp;
 
133
  }
 
134
 
 
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)) {
 
139
    id temp = object_;
 
140
    object_ = nil;
 
141
    return temp;
 
142
  }
 
143
 
 
144
 private:
 
145
  id object_;
 
146
 
 
147
  Q_DISABLE_COPY(scoped_nsobject);
 
148
};
 
149
 
 
150
// Do not use scoped_nsobject for NSAutoreleasePools, use
 
151
// ScopedNSAutoreleasePool instead. This is a compile time check. See details
 
152
// at top of header.
 
153
template<>
 
154
class scoped_nsobject<NSAutoreleasePool> {
 
155
 private:
 
156
  explicit scoped_nsobject(NSAutoreleasePool* object = nil);
 
157
  Q_DISABLE_COPY(scoped_nsobject);
 
158
};
 
159
 
 
160
#endif  // BASE_MEMORY_SCOPED_NSOBJECT_H_