Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None
diff --git a/util/pointer.h b/util/pointer.h
index bfa3d944..4f3883d5 100644
--- a/util/pointer.h
+++ b/util/pointer.h
@@ -1,135 +1,149 @@
#ifndef _paintown_util_pointer_h
#define _paintown_util_pointer_h
+#include <stdlib.h>
+
namespace Util{
/* Some helpful pointer classes, probably equivalent to stuff in boost
*/
template <class Data>
class ReferenceCount{
public:
ReferenceCount(Data * what = NULL):
count(NULL),
data(what){
count = new int;
*count = 1;
}
ReferenceCount(const ReferenceCount<Data> & him){
data = him.data;
count = him.count;
*count += 1;
}
ReferenceCount & operator=(const ReferenceCount<Data> & him){
+ /* this object might have a link to `him' so if we release first
+ * then `him' will get deleted before we can capture his data.
+ * we need to increase his count first and then release our data
+ * to insure that `him' will still be alive.
+ */
+ int * temp_count = him.count;
+ *temp_count += 1;
+ Data * temp_data = him.data;
release();
- data = him.data;
- count = him.count;
- *count += 1;
+ data = temp_data;
+ count = temp_count;
+
return *this;
}
ReferenceCount & operator=(Data * what){
release();
count = new int;
*count = 1;
data = what;
return *this;
}
+ bool operator!() const {
+ return !this->data;
+ }
+
Data * operator->() const {
return data;
}
Data & operator*() const {
return *data;
}
bool operator==(const ReferenceCount<Data> & him) const {
return data == him.data;
}
bool operator!=(const ReferenceCount<Data> & him) const {
return !(*this == him);
}
bool operator==(const void * what) const {
return data == what;
}
bool operator!=(const void * what) const {
return !(*this == what);
}
virtual ~ReferenceCount(){
release();
}
protected:
void release(){
*count -= 1;
if (*count == 0){
delete data;
delete count;
data = NULL;
count = NULL;
}
}
int * count;
Data * data;
};
/* Initializes its pointer to NULL and deletes the data in the destructor.
* how is this different from the ReferenceCount class above? its basically
* the same thing but only allows one owner at a time.
*/
template <class Data>
class ClassPointer{
public:
ClassPointer():
data(NULL){
}
ClassPointer(Data * him):
data(him){
}
ClassPointer & operator=(Data * him){
if (data != NULL){
delete data;
}
data = him;
return *this;
}
Data & operator*() const {
return *data;
}
bool operator==(const void * what) const {
return data == what;
}
bool operator!=(const void * what) const {
return !(*this == what);
}
Data* operator->() const {
return data;
}
virtual ~ClassPointer(){
delete data;
}
private:
Data* data;
};
}
#endif

File Metadata

Mime Type
text/x-diff
Expires
Thu, Jun 11, 11:14 AM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
68677
Default Alt Text
(3 KB)

Event Timeline