diff options
author | qsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-27 09:07:18 +0000 |
---|---|---|
committer | qsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-27 09:07:18 +0000 |
commit | d299b5058510cec176ae34feb4585797f22b30a2 (patch) | |
tree | 08214957c1040cee40f58462500c76a0f1e910ab /base | |
parent | cf6c2100d85624efd69f74afecdfaf50450866ad (diff) | |
download | chromium_src-d299b5058510cec176ae34feb4585797f22b30a2.zip chromium_src-d299b5058510cec176ae34feb4585797f22b30a2.tar.gz chromium_src-d299b5058510cec176ae34feb4585797f22b30a2.tar.bz2 |
Adding a scoped_nsprotocol
This allow to easily own protocol. The syntax is:
scoped_nsobject<id<MyProtocol> > object;
BUG= none
TEST= none
Review URL: http://codereview.chromium.org/9290046
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@119419 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/memory/scoped_nsobject.h | 65 |
1 files changed, 64 insertions, 1 deletions
diff --git a/base/memory/scoped_nsobject.h b/base/memory/scoped_nsobject.h index 65e35d5..c438ee7 100644 --- a/base/memory/scoped_nsobject.h +++ b/base/memory/scoped_nsobject.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -160,4 +160,67 @@ class scoped_nsobject<NSAutoreleasePool> { DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); }; +// Equivalent of scoped_nsobject for a id<protocol>. This is exactly the same +// class as scoped_nsobject, except that it doesn't handle any pointer +// transformation. +template<typename NST> +class scoped_nsprotocol { + public: + explicit scoped_nsprotocol(NST object = nil) : object_(object) { + } + + ~scoped_nsprotocol() { + [object_ release]; + } + + void reset(NST object = nil) { + [object_ release]; + object_ = object; + } + + bool operator==(NST that) const { return object_ == that; } + bool operator!=(NST that) const { return object_ != that; } + + operator NST() const { + return object_; + } + + NST get() const { + return object_; + } + + void swap(scoped_nsprotocol& that) { + NST temp = that.object_; + that.object_ = object_; + object_ = temp; + } + + NST release() WARN_UNUSED_RESULT { + NST temp = object_; + object_ = nil; + return temp; + } + + private: + NST object_; + + DISALLOW_COPY_AND_ASSIGN(scoped_nsprotocol); +}; + +// Free functions +template <class C> +void swap(scoped_nsprotocol<C>& p1, scoped_nsprotocol<C>& p2) { + p1.swap(p2); +} + +template <class C> +bool operator==(C p1, const scoped_nsprotocol<C>& p2) { + return p1 == p2.get(); +} + +template <class C> +bool operator!=(C p1, const scoped_nsprotocol<C>& p2) { + return p1 != p2.get(); +} + #endif // BASE_MEMORY_SCOPED_NSOBJECT_H_ |