Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / native_client_sdk / src / libraries / sdk_util / scoped_ref.h
blobb3b8f5b41a5cba09693f4b818d5a61743a79c10e
1 // Copyright 2013 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 LIBRARIES_SDK_UTIL_SCOPED_REF_H_
6 #define LIBRARIES_SDK_UTIL_SCOPED_REF_H_
8 #include <stdlib.h>
10 #include "sdk_util/macros.h"
11 #include "sdk_util/ref_object.h"
13 namespace sdk_util {
15 class ScopedRefBase {
16 protected:
17 ScopedRefBase() : ptr_(NULL) {}
18 ~ScopedRefBase() { reset(NULL); }
20 void reset(RefObject* obj) {
21 if (obj) {
22 obj->Acquire();
24 if (ptr_) {
25 ptr_->Release();
27 ptr_ = obj;
30 protected:
31 RefObject* ptr_;
34 template <class T>
35 class ScopedRef : public ScopedRefBase {
36 public:
37 ScopedRef() {}
38 ScopedRef(const ScopedRef& ptr) { reset(ptr.get()); }
39 explicit ScopedRef(T* ptr) { reset(ptr); }
41 ScopedRef& operator=(const ScopedRef& ptr) {
42 reset(ptr.get());
43 return *this;
46 template <typename U>
47 ScopedRef(const ScopedRef<U>& ptr) {
48 reset(ptr.get());
51 template <typename U>
52 ScopedRef& operator=(const ScopedRef<U>& ptr) {
53 reset(ptr.get());
54 return *this;
57 void reset(T* obj = NULL) { ScopedRefBase::reset(obj); }
58 T* get() const { return static_cast<T*>(ptr_); }
60 template <typename U>
61 bool operator==(const ScopedRef<U>& p) const {
62 return get() == p.get();
65 template <typename U>
66 bool operator!=(const ScopedRef<U>& p) const {
67 return get() != p.get();
70 public:
71 T& operator*() const { return *get(); }
72 T* operator->() const { return get(); }
74 private:
75 typedef void (ScopedRef::*bool_as_func_ptr)() const;
76 void bool_as_func_impl() const {};
78 public:
79 operator bool_as_func_ptr() const {
80 return (ptr_ != NULL) ? &ScopedRef::bool_as_func_impl : 0;
84 template <typename U, typename T>
85 ScopedRef<U> static_scoped_ref_cast(const ScopedRef<T>& ptr) {
86 return ScopedRef<U>(static_cast<U*>(ptr.get()));
89 } // namespace sdk_util
91 #endif // LIBRARIES_SDK_UTIL_SCOPED_REF_H_