From 27a2dd4fce2d529cd1d96aa5ddce1e74b4f54a2f Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 7 Apr 2017 02:48:12 +0300 Subject: Move resourcemanager from class to namespace. --- src/resources/resourcemanager/resourcemanager.h | 155 ++++++++++-------------- 1 file changed, 63 insertions(+), 92 deletions(-) (limited to 'src/resources/resourcemanager/resourcemanager.h') diff --git a/src/resources/resourcemanager/resourcemanager.h b/src/resources/resourcemanager/resourcemanager.h index 6fff83df0..f279b2b7c 100644 --- a/src/resources/resourcemanager/resourcemanager.h +++ b/src/resources/resourcemanager/resourcemanager.h @@ -23,8 +23,6 @@ #ifndef RESOURCES_RESOURCEMANAGER_RESOURCEMANAGER_H #define RESOURCES_RESOURCEMANAGER_RESOURCEMANAGER_H -#include "resources/memorycounter.h" - #include #include @@ -35,124 +33,97 @@ class Resource; struct SDL_Surface; struct SDL_RWops; -/** - * A class for loading and managing resources. - */ -class ResourceManager final : public MemoryCounter +namespace ResourceManager { - friend class Resource; - - public: - typedef Resource *(*loader)(SDL_RWops *rw, - const std::string &name); - typedef Resource *(&generator)(const void *const data); - - ResourceManager(); - - A_DELETE_COPY(ResourceManager) - - /** - * Destructor. Cleans up remaining resources, warning about resources - * that were still referenced. - */ - ~ResourceManager(); + typedef Resource *(*loader)(SDL_RWops *rw, + const std::string &name); + typedef Resource *(&generator)(const void *const data); - /** - * Creates a resource and adds it to the resource map. - * - * @param idPath The resource identifier path. - * @param fun A function for generating the resource. - * @param data Extra parameters for the generator. - * @return A valid resource or NULL if the resource could - * not be generated. - */ - Resource *get(const std::string &idPath, - generator fun, - const void *const data) A_WARN_UNUSED; + typedef std::map Resources; + typedef Resources::iterator ResourceIterator; + typedef Resources::const_iterator ResourceCIterator; - Resource *getFromCache(const std::string &idPath) A_WARN_UNUSED; + /** + * Cleans up remaining resources, warning about resources + * that were still referenced. + */ + void deleteResourceManager(); - Resource *getFromCache(const std::string &filename, - const int variant) A_WARN_UNUSED; + /** + * Creates a resource and adds it to the resource map. + * + * @param idPath The resource identifier path. + * @param fun A function for generating the resource. + * @param data Extra parameters for the generator. + * @return A valid resource or NULL if the resource could + * not be generated. + */ + Resource *get(const std::string &idPath, + generator fun, + const void *const data) A_WARN_UNUSED; - bool addResource(const std::string &idPath, - Resource *const resource); + Resource *getFromCache(const std::string &idPath) A_WARN_UNUSED; - /** - * Releases a resource, placing it in the set of orphaned resources. - */ - void release(Resource *const res); + Resource *getFromCache(const std::string &filename, + const int variant) A_WARN_UNUSED; - void clearDeleted(const bool full = true); + bool addResource(const std::string &idPath, + Resource *const resource); - void decRefDelete(Resource *const res); + /** + * Releases a resource, placing it in the set of orphaned resources. + */ + void release(Resource *const res); - /** - * Move resource to deleted resources list. - */ - void moveToDeleted(Resource *const res); + void clearDeleted(const bool full = true); - void scheduleDelete(SDL_Surface *const surface); + void decRefDelete(Resource *const res); - void clearScheduled(); + /** + * Move resource to deleted resources list. + */ + void moveToDeleted(Resource *const res); - /** - * Deletes the class instance if it exists. - */ - static void deleteInstance(); + void scheduleDelete(SDL_Surface *const surface); - int size() const noexcept2 A_WARN_UNUSED - { return CAST_S32(mResources.size()); } + void clearScheduled(); - typedef std::map Resources; - typedef Resources::iterator ResourceIterator; - typedef Resources::const_iterator ResourceCIterator; + /** + * Deletes the class instance if it exists. + */ + void deleteInstance(); -#ifdef DEBUG_DUMP_LEAKS - Resources* getResources() A_WARN_UNUSED - { return &mResources; } + int size() noexcept2 A_WARN_UNUSED; - Resources* getOrphanedResources() A_WARN_UNUSED - { return &mOrphanedResources; } -#endif // DEBUG_DUMP_LEAKS +#if defined(DEBUG_DUMP_LEAKS) || defined(UNITTESTS) + Resources &getResources() A_WARN_UNUSED; - bool cleanOrphans(const bool always = false); + Resources &getOrphanedResources() A_WARN_UNUSED; - void cleanProtected(); + const std::set &getDeletedResources() A_WARN_UNUSED; +#endif // defined(DEBUG_DUMP_LEAKS) || defined(UNITTESTS) - bool isInCache(const std::string &idPath) const A_WARN_UNUSED; + bool cleanOrphans(const bool always = false); - Resource *getTempResource(const std::string &idPath) A_WARN_UNUSED; + void cleanProtected(); - void clearCache(); + bool isInCache(const std::string &idPath) A_WARN_UNUSED; - int calcMemoryLocal() const override final; + Resource *getTempResource(const std::string &idPath) A_WARN_UNUSED; - int calcMemoryChilds(const int level) const override final; + void clearCache(); - std::string getCounterName() const override final - { return "ResourceManager"; } + int calcMemoryLocal(); - static void init(); + int calcMemoryChilds(const int level); -#ifndef UNITTESTS - private: -#endif // UNITTESTS - /** - * Deletes the resource after logging a cleanup message. - */ - static void cleanUp(Resource *const resource); + int calcMemory(const int level); - static void logResource(const Resource *const res); + void cleanUp(Resource *const resource); - std::set deletedSurfaces; - Resources mResources; - Resources mOrphanedResources; - std::set mDeletedResources; - time_t mOldestOrphan; - bool mDestruction; -}; + void logResource(const Resource *const res); -extern ResourceManager *resourceManager; + void logResources(const std::string &msg); +} // namespace ResourceManager #endif // RESOURCES_RESOURCEMANAGER_RESOURCEMANAGER_H -- cgit v1.2.3-70-g09d2