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_
10 #include "sdk_util/macros.h"
11 #include "sdk_util/ref_object.h"
17 ScopedRefBase() : ptr_(NULL
) {}
18 ~ScopedRefBase() { reset(NULL
); }
20 void reset(RefObject
* obj
) {
35 class ScopedRef
: public ScopedRefBase
{
38 ScopedRef(const ScopedRef
& ptr
) { reset(ptr
.get()); }
39 explicit ScopedRef(T
* ptr
) { reset(ptr
); }
41 ScopedRef
& operator=(const ScopedRef
& ptr
) {
47 ScopedRef(const ScopedRef
<U
>& ptr
) {
52 ScopedRef
& operator=(const ScopedRef
<U
>& ptr
) {
57 void reset(T
* obj
= NULL
) { ScopedRefBase::reset(obj
); }
58 T
* get() const { return static_cast<T
*>(ptr_
); }
61 bool operator==(const ScopedRef
<U
>& p
) const {
62 return get() == p
.get();
66 bool operator!=(const ScopedRef
<U
>& p
) const {
67 return get() != p
.get();
71 T
& operator*() const { return *get(); }
72 T
* operator->() const { return get(); }
75 typedef void (ScopedRef::*bool_as_func_ptr
)() const;
76 void bool_as_func_impl() const {};
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_