X-Git-Url: http://git.home-dn.net/?p=manu%2Fsuphp.git;a=blobdiff_plain;f=src%2FSmartPtr.hpp;h=1223b5c6ace69b657fb3063f37263835472d2abf;hp=88a16b7a0c3e94bf8e44a3e1654bc50b9e6881b8;hb=849f4a7977b5780eacae8fad7a078d05f4a615ee;hpb=b0eaa3f1fbf491fdc8a3044cf20754f14254338a diff --git a/src/SmartPtr.hpp b/src/SmartPtr.hpp index 88a16b7..1223b5c 100644 --- a/src/SmartPtr.hpp +++ b/src/SmartPtr.hpp @@ -37,74 +37,74 @@ namespace suPHP { template class SmartPtr { private: - static std::map *counter; - T* ptr; - - void increment(T* key_ptr); - void decrement(T* key_ptr); - + static std::map *counter; + T* ptr; + + void increment(T* key_ptr); + void decrement(T* key_ptr); + public: - /** - * Constructor using NULL pointer - */ - SmartPtr(); - - /** - * Constructor taking T* as argument - */ - SmartPtr(T* obj_ptr); - - /** - * Copy constructor - */ - SmartPtr(const SmartPtr& ref); - - /** - * Destructor - */ - ~SmartPtr(); - - /** - * Copy operator - */ - const SmartPtr& operator=(const SmartPtr& ref); - - /** - * Dereference operator. - * Returns reference to object hold by smart pointer - */ - T& operator*() const throw(PointerException); - - /** - * Member access operator - */ - T* operator->() const throw(PointerException); - - /** - * Returns underlying pointer - */ - T* get() const; - - /** - * Returns underlying pointer and releases it - * from management by the SmartPtr. - * Throws an exception if underlying pointer is - * hold by more than one SmartPtr. - */ - T* release() throw (PointerException); - - /** - * Resets SmartPtr to point to another object - */ - void reset(T* obj_ptr); - - /** - * Compares to pointers. - * Returns true, if both point to the same object, - * false otherwise - */ - bool operator==(const SmartPtr& ref); - + /** + * Constructor using NULL pointer + */ + SmartPtr(); + + /** + * Constructor taking T* as argument + */ + SmartPtr(T* obj_ptr); + + /** + * Copy constructor + */ + SmartPtr(const SmartPtr& ref); + + /** + * Destructor + */ + ~SmartPtr(); + + /** + * Copy operator + */ + const SmartPtr& operator=(const SmartPtr& ref); + + /** + * Dereference operator. + * Returns reference to object hold by smart pointer + */ + T& operator*() const throw(PointerException); + + /** + * Member access operator + */ + T* operator->() const throw(PointerException); + + /** + * Returns underlying pointer + */ + T* get() const; + + /** + * Returns underlying pointer and releases it + * from management by the SmartPtr. + * Throws an exception if underlying pointer is + * hold by more than one SmartPtr. + */ + T* release() throw (PointerException); + + /** + * Resets SmartPtr to point to another object + */ + void reset(T* obj_ptr); + + /** + * Compares to pointers. + * Returns true, if both point to the same object, + * false otherwise + */ + bool operator==(const SmartPtr& ref); + }; template @@ -112,132 +112,132 @@ namespace suPHP { template suPHP::SmartPtr::SmartPtr() { - if (SmartPtr::counter == NULL) { - SmartPtr::counter = new std::map; - } - this->ptr = NULL; + if (SmartPtr::counter == NULL) { + SmartPtr::counter = new std::map; + } + this->ptr = NULL; } template suPHP::SmartPtr::SmartPtr(T* obj_ptr) { - if (SmartPtr::counter == NULL) { - SmartPtr::counter = new std::map; - } - this->ptr = obj_ptr; - this->increment(obj_ptr); + if (SmartPtr::counter == NULL) { + SmartPtr::counter = new std::map; + } + this->ptr = obj_ptr; + this->increment(obj_ptr); } template suPHP::SmartPtr::SmartPtr(const SmartPtr& ref) { - if (SmartPtr::counter == NULL) { - SmartPtr::counter = new std::map; - } - this->ptr = ref.ptr; - if (ref.ptr != NULL) - this->increment(ref.ptr); + if (SmartPtr::counter == NULL) { + SmartPtr::counter = new std::map; + } + this->ptr = ref.ptr; + if (ref.ptr != NULL) + this->increment(ref.ptr); } template suPHP::SmartPtr::~SmartPtr() { - if (this->ptr != NULL) - this->decrement(this->ptr); - if (SmartPtr::counter != NULL && SmartPtr::counter->size() == 0) { - delete SmartPtr::counter; - SmartPtr::counter = NULL; - } + if (this->ptr != NULL) + this->decrement(this->ptr); + if (SmartPtr::counter != NULL && SmartPtr::counter->size() == 0) { + delete SmartPtr::counter; + SmartPtr::counter = NULL; + } } template const SmartPtr& suPHP::SmartPtr::operator=( - const SmartPtr& ref) { - this.reset(ref.ptr); - return *this; + const SmartPtr& ref) { + this.reset(ref.ptr); + return *this; } template T& suPHP::SmartPtr::operator*() const throw (PointerException) { - if (this->ptr == NULL) - throw PointerException("Cannot dereference NULL pointer", - __FILE__, __LINE__); - return *(this->ptr); + if (this->ptr == NULL) + throw PointerException("Cannot dereference NULL pointer", + __FILE__, __LINE__); + return *(this->ptr); } template T* suPHP::SmartPtr::operator->() const throw (PointerException) { - if (this->ptr == NULL) - throw PointerException("Cannot access member of NULL pointer", - __FILE__, __LINE__); - return this->ptr; + if (this->ptr == NULL) + throw PointerException("Cannot access member of NULL pointer", + __FILE__, __LINE__); + return this->ptr; } template T* suPHP::SmartPtr::get() const { - return this->ptr; + return this->ptr; } template T* suPHP::SmartPtr::release() throw (PointerException) { - T* obj_ptr = this->ptr; - if (obj_ptr == NULL) - return NULL; - - int& c = SmartPtr::counter->find(obj_ptr)->second; - - if (c > 1) { - throw PointerException( - "Cannot release object hold by more than one SmartPointer.", - __FILE__, __LINE__); - } else { - SmartPtr::counter->erase(obj_ptr); - } - this->ptr = NULL; - return obj_ptr; + T* obj_ptr = this->ptr; + if (obj_ptr == NULL) + return NULL; + + int& c = SmartPtr::counter->find(obj_ptr)->second; + + if (c > 1) { + throw PointerException( + "Cannot release object hold by more than one SmartPointer.", + __FILE__, __LINE__); + } else { + SmartPtr::counter->erase(obj_ptr); + } + this->ptr = NULL; + return obj_ptr; } template void suPHP::SmartPtr::reset(T* obj_ptr) { - if (obj_ptr != this->ptr) { - this->decrement(this->ptr); - this->ptr = obj_ptr; - this->increment(obj_ptr); - } + if (obj_ptr != this->ptr) { + this->decrement(this->ptr); + this->ptr = obj_ptr; + this->increment(obj_ptr); + } } template void suPHP::SmartPtr::increment(T* key_ptr) { - if (key_ptr == NULL) - return; - - if (SmartPtr::counter->find(key_ptr) - != SmartPtr::counter->end()) { - (SmartPtr::counter->find(key_ptr)->second)++; - } else { - std::pair p; - p.first = key_ptr; - p.second = 1; - SmartPtr::counter->insert(p); - } + if (key_ptr == NULL) + return; + + if (SmartPtr::counter->find(key_ptr) + != SmartPtr::counter->end()) { + (SmartPtr::counter->find(key_ptr)->second)++; + } else { + std::pair p; + p.first = key_ptr; + p.second = 1; + SmartPtr::counter->insert(p); + } } template void suPHP::SmartPtr::decrement(T* key_ptr) { - if (key_ptr == NULL) - return; - - int& c = SmartPtr::counter->find(key_ptr)->second; - c--; - if (c < 1) { - delete key_ptr; - SmartPtr::counter->erase(key_ptr); - } + if (key_ptr == NULL) + return; + + int& c = SmartPtr::counter->find(key_ptr)->second; + c--; + if (c < 1) { + delete key_ptr; + SmartPtr::counter->erase(key_ptr); + } } template bool suPHP::SmartPtr::operator==(const SmartPtr& ref) { - if (this->get() == ref.get()) - return true; - else - return false; + if (this->get() == ref.get()) + return true; + else + return false; } };