#ifndef TEMP_PATHED_RALLOCATOR#define TEMP_PATHED_RALLOCATOR#include #include #include #include #include // TPathedAllocator// Implements a template class for an allocator/manager for string-pathed// resource loading.// Useful for images, textures, meshes, etc... which can be shared among// many objects, and which should only be loaded once.template <class T>class TPathedAllocator{ public: typedef typename std::map >::iterator TIter; TPathedAllocator() { }; ~TPathedAllocator() { }; boost::shared_ptr getResource(const char *n) { std::string name=std::string(n); TIter iter; iter = m_map.find(name); if(iter == m_map.end()) { // Resource not found, create new boost::shared_ptr temp(new T(n)); boost::weak_ptr tempweak(temp); m_map[name]=tempweak; return temp; } if((*iter).second.expired()) { // Resource expired boost::shared_ptr temp(new T(n)); boost::weak_ptr tempweak(temp); m_map[name]=tempweak; return temp; } // Get here, resource valid and not expired boost::shared_ptr temp((*iter).second); return temp; }; template void iterateObjects(_Function f) { TIter ti= m_map.begin(); while(ti != m_map.end()) { if(!(*ti).second.expired()) { boost::shared_ptr temp((*ti).second); f(temp); } ++ti; } }; protected: std::map > m_map;};#endif
I perform resource construction via a const char * because many types of resource allocation calls may end up bound to Lua, and this simplifies things.