AJA NTV2 SDK  17.1.1.1245
NTV2 SDK 17.1.1.1245
ajarefptr.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
8 #if !defined (__AJAREFPTR__)
9  #define __AJAREFPTR__
10 
11 #include "../system/atomic.h"
12 
19  template <class TRef>
20  class Referent
21  {
22  // Instance Methods
23  public:
24  // Constructor
25  Referent (TRef * ptr);
26 
27  // Reference Count Management
28  void AddRef () throw ();
29  void RemoveRef () throw ();
30 
31  // Access To Underlying Object
32  TRef * get () const throw();
33 
34  // Instance Data
35  private:
36  uint32_t m_nCount; // Reference count
37  TRef * m_pointer; // Pointer to underlying object I refer to
38 
39  }; // Referent
40 
41 
42  template <class TRef>
44  : m_nCount (1),
45  m_pointer (ptr)
46  {
47  }
48 
49 
50  template <class TRef>
51  void Referent <TRef>::AddRef (void) throw ()
52  {
53  AJAAtomic::Increment(&m_nCount);
54  }
55 
56 
57  template <class TRef>
59  {
60  if (m_nCount > 0)
61  {
62  if (AJAAtomic::Decrement(&m_nCount) == 0)
63  {
64  delete m_pointer;
65  m_pointer = 0;
66  delete this;
67  }
68  }
69  }
70 
71 
72  template <class TRef>
73  TRef * Referent <TRef>::get (void) const throw ()
74  {
75  return m_pointer;
76  }
77 
78 
79 
88  template <class TRef>
89  class AJARefPtr
90  {
91  public:
92  typedef TRef element_type;
93 
94  // Construction & Destruction
95  explicit AJARefPtr (TRef * ptr = NULL) throw ();
96  AJARefPtr (const AJARefPtr<TRef>& obToCopy ) throw ();
97  ~AJARefPtr () throw ();
98 
99  // Assignment
100  AJARefPtr<TRef>& operator = (const AJARefPtr<TRef>& inRHS) throw ();
101  AJARefPtr<TRef>& operator = (TRef * pobRHS) throw ();
102 
103  // Comparison
104  bool operator == (const AJARefPtr< TRef > & inRHS) const throw ();
105  bool operator != (const AJARefPtr< TRef > & inRHS) const throw ();
106  bool operator < (const AJARefPtr< TRef > & inRHS) const throw ();
107 
108  // Dereferencing
109  TRef & operator * () const throw ();
110  TRef * operator -> () const throw ();
111  TRef * get () const throw ();
112 
113  // Testing
114  operator bool () const throw ();
115 
116  private:
117  Referent <element_type> * m_pRef; // My referent
118 
119  }; // AJARefPtr
120 
121 
122  template <class TRef>
123  AJARefPtr <TRef>::AJARefPtr (TRef * ptr) throw () : m_pRef (new Referent <element_type> (ptr))
124  {
125  }
126 
127 
128  template <class TRef>
129  AJARefPtr <TRef>::AJARefPtr (const AJARefPtr <TRef> & obToCopy) throw () : m_pRef (obToCopy.m_pRef)
130  {
131  m_pRef->AddRef ();
132  }
133 
134 
135  template <class TRef>
137  {
138  if (*this != inRHS)
139  {
140  if (m_pRef)
141  m_pRef->RemoveRef ();
142  m_pRef = inRHS.m_pRef;
143  m_pRef->AddRef ();
144  }
145  return *this;
146  }
147 
148 
149  template <class TRef>
150  AJARefPtr <TRef> & AJARefPtr <TRef>::operator = (TRef * pobRHS) throw ()
151  {
152  if (pobRHS != get ())
153  {
154  if (m_pRef)
155  m_pRef->RemoveRef ();
156  m_pRef = new Referent <element_type> (pobRHS);
157  }
158  return *this;
159  }
160 
161 
162  template <class TRef>
163  AJARefPtr <TRef>::~AJARefPtr () throw ()
164  {
165  m_pRef->RemoveRef ();
166  }
167 
168 
169  template <class TRef>
170  bool AJARefPtr <TRef>::operator == (const AJARefPtr <TRef> & inRHS) const throw ()
171  {
172  return get() == inRHS.get();
173  }
174 
175 
176  template <class TRef>
177  bool AJARefPtr <TRef>::operator != (const AJARefPtr <TRef> & inRHS) const throw ()
178  {
179  return get() != inRHS.get ();
180  }
181 
182 
183  template <class TRef>
184  bool AJARefPtr <TRef>::operator < (const AJARefPtr <TRef> & inRHS) const throw ()
185  {
186  return get() < inRHS.get ();
187  }
188 
189 
190  template <class TRef>
191  TRef & AJARefPtr <TRef>::operator * () const throw ()
192  {
193  return *(m_pRef->get());
194  }
195 
196 
197  template <class TRef>
198  TRef * AJARefPtr <TRef>::operator -> () const throw ()
199  {
200  return m_pRef ? m_pRef->get () : NULL;
201  }
202 
203 
204  template <class TRef>
205  TRef * AJARefPtr <TRef>::get () const throw ()
206  {
207  return m_pRef ? m_pRef->get () : NULL;
208  }
209 
210 
211  template <class TRef>
212  AJARefPtr <TRef>::operator bool () const throw ()
213  {
214  return m_pRef && m_pRef->get () != 0;
215  }
216 
217 #endif // __AJAREFPTR__
Referent::Referent
Referent(TRef *ptr)
Definition: ajarefptr.h:43
AJARefPtr::AJARefPtr
AJARefPtr(TRef *ptr=NULL)
Definition: ajarefptr.h:123
NULL
#define NULL
Definition: ntv2caption608types.h:19
Referent::AddRef
void AddRef()
Definition: ajarefptr.h:51
AJARefPtr
I am a reference-counted pointer template class. I am intended to be a proxy for an underlying object...
Definition: ajarefptr.h:89
AJARefPtr::~AJARefPtr
~AJARefPtr()
Definition: ajarefptr.h:163
Referent
I am the referent object that maintains the reference count and the pointer to the underlying object ...
Definition: ajarefptr.h:20
nlohmann::json_abiNLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON_v3_11_NLOHMANN_JSON_VERSION_PATCH::detail::void
j template void())
Definition: json.hpp:4893
Referent::RemoveRef
void RemoveRef()
Definition: ajarefptr.h:58
AJARefPtr::get
TRef * get() const
Definition: ajarefptr.h:205
AJARefPtr::element_type
TRef element_type
Definition: ajarefptr.h:92
AJAAtomic::Increment
static int32_t Increment(int32_t volatile *pTarget)
Definition: atomic.cpp:82
nlohmann::json_abiNLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON_v3_11_NLOHMANN_JSON_VERSION_PATCH::detail::get
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())
Definition: json.hpp:5342
CNTV2CaptionEncoder708
Definition: ntv2captionencoder708.h:586
AJAAtomic::Decrement
static int32_t Decrement(int32_t volatile *pTarget)
Definition: atomic.cpp:95
Referent::get
TRef * get() const
Definition: ajarefptr.h:73