X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FMippleton.hh;h=7ad17823a45862cfe7eae2b7f0d03148c34db692;hp=95d3f21577e6e9d444e56cf6a4fb33f50e421b73;hb=7e898e8ec0ff716e2fc722b883a626a6c346f107;hpb=c2321281bf12a7efaedde930422c7ddbc92080d4 diff --git a/src/Moof/Mippleton.hh b/src/Moof/Mippleton.hh index 95d3f21..7ad1782 100644 --- a/src/Moof/Mippleton.hh +++ b/src/Moof/Mippleton.hh @@ -38,9 +38,12 @@ * after the last interested code releases its hold on the object. */ -#include #include +#include + +#include + namespace Mf { @@ -48,57 +51,64 @@ namespace Mf { template class Mippleton { - typedef std::pair ptr_value_t; - typedef std::pair ptr_map_pair_t; - typedef std::map ptr_map_t; + typedef std::pair PtrValue; + typedef stlplus::hash PtrMap; - static ptr_map_t ptrs_; - std::string name_; + static PtrMap ptrs_; + std::string name_; -public: - explicit Mippleton(const std::string& name) : name_(name) {} - - inline const std::string& getName() const + static T* retain(const std::string& name) { - return name_; - } - - inline static T* retain(const std::string& name) - { - typename ptr_map_t::iterator it; + typename PtrMap::iterator it = ptrs_.find(name); - if ((it = ptrs_.find(name)) != ptrs_.end()) + if (it != ptrs_.end()) { - (*it).second.first++; + ++((*it).second.first); return (*it).second.second; } else { T* newObj = new T(name); - ptrs_.insert(ptr_map_pair_t(name, ptr_value_t(1, newObj))); + ptrs_.insert(std::make_pair(name, std::make_pair(1, newObj))); return newObj; } } - inline static void releaseByName(const std::string& name) + static void release(T* obj) + { + releaseByName(obj->name_); + } + + static void releaseByName(const std::string& name) { - typename ptr_map_t::iterator it; + typename PtrMap::iterator it; - if ((it = ptrs_.find(name)) != ptrs_.end() && -(*it).second.first == 0) + if ((it = ptrs_.find(name)) != ptrs_.end() && --(*it).second.first == 0) { delete (*it).second.second; - ptrs_.erase(it); + ptrs_.erase((*it).first); } } - inline static void release(T* obj) +public: + + explicit Mippleton(const std::string& name) : + name_(name) {} + + const std::string& getName() const + { + return name_; + } + + static boost::shared_ptr getInstance(const std::string& name) { - releaseByName(obj->getName()); + return boost::shared_ptr(retain(name), &release); } }; template -std::map > Mippleton::ptrs_; +stlplus::hash< std::string,std::pair,getHash > + Mippleton::ptrs_; } // namespace Mf