76 #ifndef _util_ref_ref_h
77 #define _util_ref_ref_h
83 #include <util/ref/identity.h>
90 #ifndef REF_CHECK_STACK
91 # define REF_CHECK_STACK 0
96 #ifndef REF_CHECK_MAX_NREF
97 # define REF_CHECK_MAX_NREF 0
99 #ifndef REF_CHECK_MIN_NREF
100 # define REF_CHECK_MIN_NREF 0
105 #ifndef REF_CHECK_STACK
106 #define REF_CHECK_STACK 0
109 #ifndef REF_CHECK_STACK
110 #define REF_CHECK_STACK 0
118 #ifndef REF_CHECK_MAX_NREF
119 #define REF_CHECK_MAX_NREF 1
122 #ifndef REF_CHECK_MIN_NREF
123 #define REF_CHECK_MIN_NREF 1
126 #ifndef REF_USE_LOCKS
127 # if HAVE_STHREAD || HAVE_CREATETHREAD || HAVE_PTHREAD
128 # define REF_USE_LOCKS 1
132 #ifndef REF_ALWAYS_USE_LOCKS
133 # define REF_ALWAYS_USE_LOCKS 1
138 #ifndef HAVE_SBRK_DEC
139 extern "C" void * sbrk(ssize_t);
141 #define DO_REF_CHECK_STACK(p) (((void*) (p) > sbrk(0)) && (p)->managed())
142 #else // REF_CHECK_STACK
143 #define DO_REF_CHECK_STACK(p) (0)
144 #endif // REF_CHECK_STACK
147 #define DO_REF_UNMANAGE(p) ((p)->unmanage())
149 #define DO_REF_UNMANAGE(p)
153 #define __REF_LOCK__(p) p->lock_ptr()
154 #define __REF_UNLOCK__(p) p->unlock_ptr()
155 #if REF_ALWAYS_USE_LOCKS
156 #define __REF_INITLOCK__() use_locks(true)
158 #define __REF_INITLOCK__() ref_lock_ = 0xff
161 #define __REF_LOCK__(p)
162 #define __REF_UNLOCK__(p)
163 #define __REF_INITLOCK__()
168 typedef unsigned long refcount_t;
197 # define REF_MAX_NREF (UINT_MAX - 1)
198 # define REF_MANAGED_CODE UINT_MAX
200 # define REF_MAX_NREF UINT_MAX
202 unsigned int _reference_count_;
204 unsigned char ref_lock_;
207 void error(
const char*)
const;
208 void too_many_refs()
const;
209 void not_enough_refs()
const;
236 if (!managed())
return 1;
238 return _reference_count_;
244 if (!managed())
return 1;
247 # if REF_CHECK_MAX_NREF
248 if (_reference_count_ >= REF_MAX_NREF) too_many_refs();
251 refcount_t r = _reference_count_;
252 __REF_UNLOCK__(
this);
259 if (!managed())
return 1;
262 # if REF_CHECK_MIN_NREF
263 if (_reference_count_ == 0) not_enough_refs();
266 refcount_t r = _reference_count_;
267 __REF_UNLOCK__(
this);
272 int managed()
const {
273 return _reference_count_ != REF_MANAGED_CODE;
281 _reference_count_ = REF_MANAGED_CODE;
284 int managed()
const {
return 1; }
295 void warn (
const char * msg)
const;
304 void ref_info(std::ostream& os)
const;
305 void check_pointer()
const;
387 operator T*()
const {
return p; }
393 int null()
const {
return p == 0; }
400 template <
class A>
int operator>=(
const Ref<A>&a)
const
402 template <
class A>
int operator<=(
const Ref<A>&a)
const
403 {
return le(p,a.pointer()); }
404 template <
class A>
int operator>(
const Ref<A>&a)
const
405 {
return gt(p,a.pointer()); }
406 template <
class A>
int operator<(
const Ref<A>&a)
const
407 {
return lt(p,a.pointer()); }
408 template <
class A>
int operator!=(
const Ref<A>&a)
const
409 {
return ne(p,a.pointer()); }
413 return eq(p,a.p)?0:((lt(p,a.p)?-1:1));
419 int ref = dereference(p);
467 T* cr = dynamic_cast<T*>(a);
482 if (DO_REF_CHECK_STACK(cr)) {
494 if (p && p->nreference() <= 0) {
Identity gives objects a unique identity and ordering relationship relative to all other objects.
Definition: identity.h:89
void warn_bad_ref_count() const
Called when the reference count is corrupted.
int operator==(const Ref< A > &a) const
A variety of ordering and equivalence operators are provided using the Identity class.
Definition: ref.h:398
void warn_ref_to_stack() const
Called when stack data is referenced.
int compare(const Ref< T > &a) const
Compare two objects returning -1, 0, or 1.
Definition: ref.h:412
void warn_skip_stack_delete() const
Called when the deletion of stack data is skipped.
A template class that maintains references counts.
Definition: ref.h:332
Ref(const Ref< T > &a)
Create a reference to the object referred to by a.
Definition: ref.h:347
Ref(const Ref< A > &a)
Create a reference to the object referred to by a.
Definition: ref.h:355
Ref< T > & operator<<(const RefBase &a)
Assignment to the object that a references using dynamic_cast.
Definition: ref.h:454
Ref< T > & operator=(T *cr)
Assignment to cr.
Definition: ref.h:473
T * pointer() const
Returns a pointer the reference counted object.
Definition: ref.h:383
T * operator->() const
Returns the reference counted object.
Definition: ref.h:381
T & operator*() const
Returns a C++ reference to the reference counted object.
Definition: ref.h:390
void clear()
Refer to the null object.
Definition: ref.h:416
Provides a few utility routines common to all Ref template instantiations.
Definition: ref.h:292
int unlock_ptr() const
Unlock this object.
void ref_info(RefCount *p, std::ostream &os) const
Print information about the reference.
refcount_t nreference() const
Return the reference count.
Definition: ref.h:234
Ref(T *a)
Create a reference to the object a.
Definition: ref.h:339
int lock_ptr() const
Lock this object.
void assign_pointer(T *cr)
Assignment to cr.
Definition: ref.h:479
void unmanage()
Turn off the reference counting mechanism for this object.
Definition: ref.h:280
virtual RefCount * parentpointer() const =0
Returns the DescribedClass pointer for the contained object.
refcount_t dereference()
Decrement the reference count and return the new count.
Definition: ref.h:257
void require_nonnull() const
Requires that a nonnull reference is held.
void use_locks(bool inVal)
start and stop using locks on this object
Ref()
Create a reference to a null object.
Definition: ref.h:337
~Ref()
Create a reference to the object a.
Definition: ref.h:375
void ref_info(std::ostream &os) const
Print information about the reference to os.
Definition: ref.h:499
Ref< T > & operator=(const Ref< A > &c)
Assignment to c.
Definition: ref.h:440
int nonnull() const
Return !null().
Definition: ref.h:395
void warn(const char *s) const
Print a warning concerning the reference.
Definition: ref.h:504
The base class for all reference counted objects.
Definition: ref.h:194
void check_pointer() const
Check the validity of the pointer.
Definition: ref.h:492
Ref< T > & operator=(const Ref< T > &c)
Assignment to c.
Definition: ref.h:426
void warn(const char *msg) const
Print a warning message.
RefCount * parentpointer() const
Implements the parentpointer pure virtual in the base class.
Definition: ref.h:385
refcount_t reference()
Increment the reference count and return the new count.
Definition: ref.h:242
Generated at Sun Jan 26 2020 23:33:04 for MPQC
2.3.1 using the documentation package Doxygen
1.8.16.