summaryrefslogtreecommitdiffstats
path: root/base/mac
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-17 04:09:06 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-17 04:09:06 +0000
commitdf0ca6c858762b101bf424ff6c0522409fa195fc (patch)
treeefa188eee6972e2c0de358f2d19a2348ce6dcb06 /base/mac
parent73de26a684944782a92f8e6840e1b290c9a424cd (diff)
downloadchromium_src-df0ca6c858762b101bf424ff6c0522409fa195fc.zip
chromium_src-df0ca6c858762b101bf424ff6c0522409fa195fc.tar.gz
chromium_src-df0ca6c858762b101bf424ff6c0522409fa195fc.tar.bz2
Move scoped_cftyperef from base to base/mac, use the new namespace, and name it
properly (scoped_cftyperef -> ScopedCFTypeRef). TEST=it compiles BUG=none Review URL: http://codereview.chromium.org/3855001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@62887 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/mac')
-rw-r--r--base/mac/scoped_cftyperef.h87
1 files changed, 87 insertions, 0 deletions
diff --git a/base/mac/scoped_cftyperef.h b/base/mac/scoped_cftyperef.h
new file mode 100644
index 0000000..9add988
--- /dev/null
+++ b/base/mac/scoped_cftyperef.h
@@ -0,0 +1,87 @@
+// Copyright (c) 2010 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.
+
+#ifndef BASE_MAC_SCOPED_CFTYPEREF_H_
+#define BASE_MAC_SCOPED_CFTYPEREF_H_
+#pragma once
+
+#include <CoreFoundation/CoreFoundation.h>
+
+#include "base/basictypes.h"
+#include "base/compiler_specific.h"
+
+namespace base {
+namespace mac {
+
+// ScopedCFTypeRef<> is patterned after scoped_ptr<>, but maintains ownership
+// of a CoreFoundation object: any object that can be represented as a
+// CFTypeRef. Style deviations here are solely for compatibility with
+// scoped_ptr<>'s interface, with which everyone is already familiar.
+//
+// When ScopedCFTypeRef<> takes ownership of an object (in the constructor or
+// in reset()), it takes over the caller's existing ownership claim. The
+// caller must own the object it gives to ScopedCFTypeRef<>, and relinquishes
+// an ownership claim to that object. ScopedCFTypeRef<> does not call
+// CFRetain().
+template<typename CFT>
+class ScopedCFTypeRef {
+ public:
+ typedef CFT element_type;
+
+ explicit ScopedCFTypeRef(CFT object = NULL)
+ : object_(object) {
+ }
+
+ ~ScopedCFTypeRef() {
+ if (object_)
+ CFRelease(object_);
+ }
+
+ void reset(CFT object = NULL) {
+ if (object_)
+ CFRelease(object_);
+ object_ = object;
+ }
+
+ bool operator==(CFT that) const {
+ return object_ == that;
+ }
+
+ bool operator!=(CFT that) const {
+ return object_ != that;
+ }
+
+ operator CFT() const {
+ return object_;
+ }
+
+ CFT get() const {
+ return object_;
+ }
+
+ void swap(ScopedCFTypeRef& that) {
+ CFT temp = that.object_;
+ that.object_ = object_;
+ object_ = temp;
+ }
+
+ // ScopedCFTypeRef<>::release() is like scoped_ptr<>::release. It is NOT
+ // a wrapper for CFRelease(). To force a ScopedCFTypeRef<> object to call
+ // CFRelease(), use ScopedCFTypeRef<>::reset().
+ CFT release() WARN_UNUSED_RESULT {
+ CFT temp = object_;
+ object_ = NULL;
+ return temp;
+ }
+
+ private:
+ CFT object_;
+
+ DISALLOW_COPY_AND_ASSIGN(ScopedCFTypeRef);
+};
+
+} // namespace mac
+} // namespace base
+
+#endif // BASE_MAC_SCOPED_CFTYPEREF_H_