Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F126232
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/util/pointer.h b/util/pointer.h
index 735e0a94..2f614fb1 100644
--- a/util/pointer.h
+++ b/util/pointer.h
@@ -1,170 +1,174 @@
#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 = temp_data;
count = temp_count;
return *this;
}
ReferenceCount & operator=(Data * what){
release();
count = new int;
*count = 1;
data = what;
return *this;
}
/* use this if you need to convert between classes in a heirarchy.
* Base
* Child: public Base
* ReferenceCount<Base> base;
* ReferenceCount<Child> child;
* base = child; is illegal but
* base = child.convert<Base>(); is ok
*/
template <class Convert>
ReferenceCount<Convert> convert() const {
ReferenceCount<Convert> what;
what.count = count;
*what.count += 1;
what.data = (Convert*) data;
return what;
}
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 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;
}
}
/* this has to be public to make the convert() function work but pretend
* its private!
*/
public:
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
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Jun 11, 11:00 AM (3 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
68602
Default Alt Text
(3 KB)
Attached To
Mode
R75 R-Tech1
Attached
Detach File
Event Timeline