2 #include "xmlrpc-c/girerr.hpp"
4 #include "xmlrpc-c/girmem.hpp"
7 using namespace girmem;
14 autoObject::incref() {
15 pthread_mutex_lock(&this->refcountLock);
17 pthread_mutex_unlock(&this->refcountLock);
23 autoObject::decref(bool * const unreferencedP) {
25 if (this->refcount == 0)
26 throw(error("Decrementing ref count of unreferenced object"));
27 pthread_mutex_lock(&this->refcountLock);
29 *unreferencedP = (this->refcount == 0);
30 pthread_mutex_unlock(&this->refcountLock);
35 autoObject::autoObject() {
38 rc = pthread_mutex_init(&this->refcountLock, NULL);
41 throw(error("Unable to initialize pthread mutex"));
48 autoObject::~autoObject() {
49 if (this->refcount > 0)
50 throw(error("Destroying referenced object"));
54 rc = pthread_mutex_destroy(&this->refcountLock);
57 throw(error("Unable to destroy pthread mutex"));
62 autoObjectPtr::autoObjectPtr() : objectP(NULL) {}
66 autoObjectPtr::autoObjectPtr(autoObject * const objectP) {
68 // Note: When someone attempts to use this constructor with a null
69 // argument, it's normally because a 'new' of the autoObject
70 // failed, before calling the autoObject's constructor, thus
71 // generating a null pointer.
73 // E.g. the following code, where the system is out of memory:
75 // class client : public autoObject { ... }
76 // class clientPtr : public autoObjectPtr { ... }
77 // clientPtr clientP(new client);
80 throw(error("Object creation failed; trying to create autoObjectPtr "
81 "ith a null autoObject pointer"));
83 this->objectP = objectP;
89 autoObjectPtr::autoObjectPtr(autoObjectPtr const& autoObjectPtr) {
92 this->objectP = autoObjectPtr.objectP;
94 this->objectP->incref();
99 autoObjectPtr::~autoObjectPtr() {
107 autoObjectPtr::point(autoObject * const objectP) {
109 if (this->objectP != NULL)
110 throw(error("Already pointing"));
111 this->objectP = objectP;
118 autoObjectPtr::unpoint() {
122 this->objectP->decref(&dead);
124 delete(this->objectP);
131 autoObjectPtr::operator=(autoObjectPtr const& autoObjectPtr) {
133 if (this->objectP != NULL)
134 throw(error("Already pointing"));
135 this->objectP = autoObjectPtr.objectP;
136 this->objectP->incref();
144 autoObjectPtr::operator->() const {
145 return this->objectP;
151 autoObjectPtr::get() const {
153 return this->objectP;