X-Git-Url: http://git.home-dn.net/?p=manu%2Fsuphp.git;a=blobdiff_plain;f=src%2FSmartPtr.hpp;h=88a16b7a0c3e94bf8e44a3e1654bc50b9e6881b8;hp=04926d571897ed5f26d53deaa5d78cf1a4bc71c0;hb=59c5bff8d9aef402c868519d13398ba4eb2dddb7;hpb=47bcf8ba77fa8011f9be728c23dbe6915d70251b diff --git a/src/SmartPtr.hpp b/src/SmartPtr.hpp index 04926d5..88a16b7 100644 --- a/src/SmartPtr.hpp +++ b/src/SmartPtr.hpp @@ -37,7 +37,7 @@ namespace suPHP { template class SmartPtr { private: - static std::map counter; + static std::map *counter; T* ptr; void increment(T* key_ptr); @@ -106,23 +106,32 @@ namespace suPHP { bool operator==(const SmartPtr& ref); }; - + template - std::map suPHP::SmartPtr::counter; + std::map *suPHP::SmartPtr::counter; template suPHP::SmartPtr::SmartPtr() { + 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); } 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); @@ -132,6 +141,10 @@ namespace suPHP { 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; + } } template @@ -168,14 +181,14 @@ namespace suPHP { if (obj_ptr == NULL) return NULL; - int& c = SmartPtr::counter.find(obj_ptr)->second; + 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); + SmartPtr::counter->erase(obj_ptr); } this->ptr = NULL; return obj_ptr; @@ -195,14 +208,14 @@ namespace suPHP { if (key_ptr == NULL) return; - if (SmartPtr::counter.find(key_ptr) - != SmartPtr::counter.end()) { - (SmartPtr::counter.find(key_ptr)->second)++; + 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); + SmartPtr::counter->insert(p); } } @@ -211,11 +224,11 @@ namespace suPHP { if (key_ptr == NULL) return; - int& c = SmartPtr::counter.find(key_ptr)->second; + int& c = SmartPtr::counter->find(key_ptr)->second; c--; if (c < 1) { delete key_ptr; - SmartPtr::counter.erase(key_ptr); + SmartPtr::counter->erase(key_ptr); } }