Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shareable.h
Go to the documentation of this file.
1 
18 #ifndef ION_BASE_SHAREABLE_H_
19 #define ION_BASE_SHAREABLE_H_
20 
21 #include "base/macros.h"
22 #include "ion/base/logging.h"
23 #include "ion/port/atomic.h"
24 
25 namespace ion {
26 namespace base {
27 
31 class Shareable {
32  public:
34  int GetRefCount() const { return ref_count_; }
35 
36  protected:
37  Shareable(): ref_count_(0) {}
38 
41  virtual ~Shareable() { DCHECK_EQ(ref_count_.load(), 0); }
42 
43  private:
46  void IncrementRefCount() const { ++ref_count_; }
47  void DecrementRefCount() const {
48  const int new_count = --ref_count_;
49  DCHECK_GE(new_count, 0);
50  if (new_count == 0) {
51  OnZeroRefCount();
52  }
53  }
54 
55  virtual void OnZeroRefCount() const { delete this; }
56 
59  mutable std::atomic<int> ref_count_;
60 
62  template <typename T> friend class SharedPtr;
63  friend class WeakReferent;
64 
65  DISALLOW_COPY_AND_ASSIGN(Shareable);
66 };
67 
68 } // namespace base
69 } // namespace ion
70 
71 #endif // ION_BASE_SHAREABLE_H_
virtual ~Shareable()
The destructor is protected because all instances should be managed through SharedPtr.
Definition: shareable.h:41
Shareable is an abstract base class for any object that can be shared via the SharedPtr class...
Definition: shareable.h:31
int GetRefCount() const
GetRefCount() is part of the interface necessary for SharedPtr.
Definition: shareable.h:34
#define DCHECK_GE(val1, val2)
Definition: logging.h:336
Copyright 2016 Google Inc.
Abstract base class that inherits from Referent, and adds the ability for instances to be referenced ...
Definition: weakreferent.h:40
#define DCHECK_EQ(val1, val2)
Definition: logging.h:332
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60