Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3198)

Unified Diff: base/mac/scoped_typeref.h

Issue 1565803002: Sync Mac scopers with upstream Chromium (Closed) Base URL: https://chromium.googlesource.com/chromium/mini_chromium@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/mac/scoped_nsobject.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/mac/scoped_typeref.h
diff --git a/base/mac/scoped_typeref.h b/base/mac/scoped_typeref.h
new file mode 100644
index 0000000000000000000000000000000000000000..9f00b1a2160bda2024728369baa7f8e6a6b60e5f
--- /dev/null
+++ b/base/mac/scoped_typeref.h
@@ -0,0 +1,86 @@
+// Copyright 2014 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 MINI_CHROMIUM_BASE_MAC_SCOPED_TYPEREF_H_
+#define MINI_CHROMIUM_BASE_MAC_SCOPED_TYPEREF_H_
+
+#include "base/compiler_specific.h"
+#include "base/logging.h"
+#include "base/memory/scoped_policy.h"
+
+namespace base {
+
+template <typename T>
+struct ScopedTypeRefTraits;
+
+template <typename T, typename Traits = ScopedTypeRefTraits<T>>
+class ScopedTypeRef {
+ public:
+ typedef T element_type;
+
+ ScopedTypeRef(
+ T object = Traits::InvalidValue(),
+ base::scoped_policy::OwnershipPolicy policy = base::scoped_policy::ASSUME)
+ : object_(object) {
+ if (object_ && policy == base::scoped_policy::RETAIN)
+ object_ = Traits::Retain(object_);
+ }
+
+ ScopedTypeRef(const ScopedTypeRef<T, Traits>& that) : object_(that.object_) {
+ if (object_)
+ object_ = Traits::Retain(object_);
+ }
+
+ ~ScopedTypeRef() {
+ if (object_)
+ Traits::Release(object_);
+ }
+
+ ScopedTypeRef& operator=(const ScopedTypeRef<T, Traits>& that) {
+ reset(that.get(), base::scoped_policy::RETAIN);
+ return *this;
+ }
+
+ T* InitializeInto() WARN_UNUSED_RESULT {
+ DCHECK(!object_);
+ return &object_;
+ }
+
+ void reset(T object = Traits::InvalidValue(),
+ base::scoped_policy::OwnershipPolicy policy =
+ base::scoped_policy::ASSUME) {
+ if (object && policy == base::scoped_policy::RETAIN)
+ object = Traits::Retain(object);
+ if (object_)
+ Traits::Release(object_);
+ object_ = object;
+ }
+
+ bool operator==(T that) const { return object_ == that; }
+
+ bool operator!=(T that) const { return object_ != that; }
+
+ operator T() const { return object_; }
+
+ T get() const { return object_; }
+
+ void swap(ScopedTypeRef& that) {
+ T temp = that.object_;
+ that.object_ = object_;
+ object_ = temp;
+ }
+
+ T release() WARN_UNUSED_RESULT {
+ T temp = object_;
+ object_ = Traits::InvalidValue();
+ return temp;
+ }
+
+ private:
+ T object_;
+};
+
+} // namespace base
+
+#endif // MINI_CHROMIUM_BASE_MAC_SCOPED_TYPEREF_H_
« no previous file with comments | « base/mac/scoped_nsobject.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698