OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimReferenced.h
Go to the documentation of this file.
1 /* -*-c++-*- libossim - Copyright (C) since 2004 Garrett Potts
2  * LICENSE: MIT
3  * Author: Garrett Potts
4 */
5 #ifndef ossimReferenced_HEADER
6 #define ossimReferenced_HEADER
8 
9 #include <atomic>
10 #include <mutex>
11 
24 {
25  public:
27  : m_refCount(0)
28  {}
29 
31  : m_refCount(0)
32  {}
33  inline ossimReferenced& operator = (const ossimReferenced&) { return *this; }
34 
35 
40  inline void ref() const;
41 
48  inline void unref() const;
49 
58  inline void unref_nodelete() const
59  {
60  // std::lock_guard<std::mutex> lock(theRefMutex);
61  --m_refCount;
62  }
63 
67  inline int referenceCount() const { return m_refCount.load(); }
68 
69 
70  protected:
71  virtual ~ossimReferenced();
72 
73  private:
74  mutable std::atomic_int m_refCount;
75 };
76 
77 inline void ossimReferenced::ref() const
78 {
79  ++m_refCount;
80 }
81 
82 inline void ossimReferenced::unref() const
83 {
84  bool needDelete = false;
85  {
86  // fetch_sub should return the value before subtraction.
87  // so if 1 was before the subtraction that means it should
88  // be zero after the subtraction so we will test <=1
89  needDelete = m_refCount.fetch_sub(1) <= 1;
90  }
91 
92  if (needDelete)
93  {
94  delete this;
95  }
96 }
97 
98 #endif
#define OSSIMDLLEXPORT
ossimReferenced allows for shared object ref counting if the reference count ever gets to 0 or less i...
void ref() const
increment the reference count by one, indicating that this object has another pointer which is refere...
int referenceCount() const
std::atomic_int m_refCount
void unref() const
decrement the reference count by one, indicating that a pointer to this object is referencing it...
ossimReferenced(const ossimReferenced &)
void unref_nodelete() const
decrement the reference count by one, indicating that a pointer to this object is referencing it...