/*! ========================================================================
** Extended Template Library
-**
-** $Id: _ref_count.h,v 1.1.1.1 2005/01/04 01:31:48 darco Exp $
+**
+** $Id$
**
** Copyright (c) 2002 Robert B. Quattlebaum Jr.
**
_ETL_BEGIN_NAMESPACE
class weak_reference_counter;
-
+
// ========================================================================
/*! \class reference_counter _ref_count.h ETL/ref_count
** \brief Reference counter
public:
reference_counter(const bool &x=true):counter_(x?new int(1):0) { }
-
+
reference_counter(const reference_counter &x):counter_(x.counter_)
{ if(counter_) (*counter_)++; }
reference_counter(const weak_reference_counter &x);
-
+
~reference_counter() { detach(); }
-
+
reference_counter& operator=(const reference_counter &rhs)
{
detach();
if(counter_)
{
assert(*counter_>0);
- (*counter_)++;
+ (*counter_)++;
}
- return *this;
+ return *this;
}
void detach()
if(!--(*counter_))
delete counter_;
counter_=0;
- }
- }
-
+ }
+ }
+
void reset()
{
detach();
- counter_=new int(1);
+ counter_=new int(1);
}
-
+
int count()const { return counter_?*counter_:0; }
bool unique()const { return counter_?*counter_==1:0; }
-
+
operator int()const { return count(); }
}; // END of class reference_counter
int* counter_;
public:
weak_reference_counter():counter_(0) { }
-
+
weak_reference_counter(const weak_reference_counter &x):counter_(x.counter_) { }
weak_reference_counter(const reference_counter &x):counter_(x.counter_) { }
-
+
~weak_reference_counter() { }
-
+
weak_reference_counter& operator=(const reference_counter &rhs)
{
counter_=rhs.counter_;
return *this;
}
- void detach() { counter_=0; }
-
+ void detach() { counter_=0; }
+
int count()const { return counter_?*counter_:0; }
bool unique()const { return counter_?*counter_==1:0; }
-
+
operator int()const { return count(); }
}; // END of class weak_reference_counter