1 // Copyright (c) 2012 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_REF_OBJECT
6 #define LIBRARIES_SDK_UTIL_REF_OBJECT
11 #include "sdk_util/atomicops.h"
20 * A reference counted object. RefObjects should only be handled by ScopedRef
23 * When the object is first created, it has a reference count of zero. It's
24 * first incremented when it gets assigned to a ScopedRef. When the last
25 * ScopedRef is reset or destroyed the object will get released.
37 * RefCount returns an instantaneous snapshot of the RefCount, which may
38 * change immediately after it is fetched. This is only stable when all
39 * pointers to the object are scoped pointers (ScopedRef), and the value
40 * is one implying the current thread is the only one, with visibility to
43 int RefCount() const { return ref_count_
; }
46 virtual ~RefObject() {}
48 // Override to clean up object when last reference is released.
49 virtual void Destroy() {}
52 AtomicAddFetch(&ref_count_
, 1);
56 Atomic32 val
= AtomicAddFetch(&ref_count_
, -1);
68 friend class ScopedRefBase
;
71 } // namespace sdk_util
73 #endif // LIBRARIES_SDK_UTIL_REF_OBJECT