From 850cecfd7694bcce692f9b7a584f9c3e55892a0c Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Sun, 12 Jun 2011 14:18:58 +0200 Subject: Move more common stuff of (L)ResourceManagers to their base class --- src/FbTk/LResource.cc | 29 ++------------------------ src/FbTk/LResource.hh | 4 ---- src/FbTk/Resource.cc | 56 ++++++++++++++++++++++++++------------------------- src/FbTk/Resource.hh | 32 +++++++++-------------------- 4 files changed, 41 insertions(+), 80 deletions(-) diff --git a/src/FbTk/LResource.cc b/src/FbTk/LResource.cc index fd8ef2f..5842b65 100644 --- a/src/FbTk/LResource.cc +++ b/src/FbTk/LResource.cc @@ -108,8 +108,7 @@ void LResourceManager::addResource(Resource_base &r) { m_l->checkstack(5); lua::stack_sentry s(*m_l); - m_resourcelist.push_back(&r); - m_resourcelist.unique(); + ResourceManager_base::addResource(r); m_l->getfield(lua::REGISTRYINDEX, register_resource); m_l->getfield(lua::GLOBALSINDEX, m_root.c_str()); @@ -133,31 +132,7 @@ void LResourceManager::removeResource(Resource_base &r) { *static_cast(m_l->touserdata(-1)) = NULL; m_l->pop(); - m_resourcelist.remove(&r); -} - -Resource_base *LResourceManager::findResource(const std::string &resname) { - // find resource name - ResourceList::const_iterator i = m_resourcelist.begin(); - ResourceList::const_iterator i_end = m_resourcelist.end(); - for (; i != i_end; ++i) { - if ((*i)->name() == resname || - (*i)->altName() == resname) - return *i; - } - return 0; -} - -const Resource_base *LResourceManager::findResource(const std::string &resname) const { - // find resource name - ResourceList::const_iterator i = m_resourcelist.begin(); - ResourceList::const_iterator i_end = m_resourcelist.end(); - for (; i != i_end; ++i) { - if ((*i)->name() == resname || - (*i)->altName() == resname) - return *i; - } - return 0; + ResourceManager_base::removeResource(r); } } // end namespace FbTk diff --git a/src/FbTk/LResource.hh b/src/FbTk/LResource.hh index 897575b..0853687 100644 --- a/src/FbTk/LResource.hh +++ b/src/FbTk/LResource.hh @@ -43,13 +43,9 @@ public: virtual bool save(const char *filename, const char *); virtual void addResource(Resource_base &r); virtual void removeResource(Resource_base &r); - Resource_base *findResource(const std::string &resname); - const Resource_base *findResource(const std::string &resname) const; private: - typedef std::list ResourceList; - ResourceList m_resourcelist; lua::state *m_l; std::string m_root; }; diff --git a/src/FbTk/Resource.cc b/src/FbTk/Resource.cc index fad4bf3..fb0f595 100644 --- a/src/FbTk/Resource.cc +++ b/src/FbTk/Resource.cc @@ -37,7 +37,34 @@ using std::string; namespace FbTk { -ResourceManager_base::~ResourceManager_base() { + +void ResourceManager_base::addResource(Resource_base &r) { + m_resourcelist.push_back(&r); + m_resourcelist.unique(); +} + +Resource_base *ResourceManager_base::findResource(const string &resname) { + // find resource name + ResourceList::iterator i = m_resourcelist.begin(); + ResourceList::iterator i_end = m_resourcelist.end(); + for (; i != i_end; ++i) { + if ((*i)->name() == resname || + (*i)->altName() == resname) + return *i; + } + return 0; +} + +const Resource_base *ResourceManager_base::findResource(const string &resname) const { + // find resource name + ResourceList::const_iterator i = m_resourcelist.begin(); + ResourceList::const_iterator i_end = m_resourcelist.end(); + for (; i != i_end; ++i) { + if ((*i)->name() == resname || + (*i)->altName() == resname) + return *i; + } + return 0; } string ResourceManager_base::resourceValue(const string &resname) const { @@ -180,30 +207,6 @@ bool ResourceManager::save(const char *filename, const char *mergefilename) { return true; } -Resource_base *ResourceManager::findResource(const string &resname) { - // find resource name - ResourceList::iterator i = m_resourcelist.begin(); - ResourceList::iterator i_end = m_resourcelist.end(); - for (; i != i_end; ++i) { - if ((*i)->name() == resname || - (*i)->altName() == resname) - return *i; - } - return 0; -} - -const Resource_base *ResourceManager::findResource(const string &resname) const { - // find resource name - ResourceList::const_iterator i = m_resourcelist.begin(); - ResourceList::const_iterator i_end = m_resourcelist.end(); - for (; i != i_end; ++i) { - if ((*i)->name() == resname || - (*i)->altName() == resname) - return *i; - } - return 0; -} - ResourceManager &ResourceManager::lock() { ++m_db_lock; // if the lock was zero, then load the database @@ -231,8 +234,7 @@ void ResourceManager::unlock() { // add the resource and load its value void ResourceManager::addResource(Resource_base &r) { - m_resourcelist.push_back(&r); - m_resourcelist.unique(); + ResourceManager_base::addResource(r); // lock ensures that the database is loaded. lock(); diff --git a/src/FbTk/Resource.hh b/src/FbTk/Resource.hh index fb05426..33a6bb2 100644 --- a/src/FbTk/Resource.hh +++ b/src/FbTk/Resource.hh @@ -87,7 +87,7 @@ class ResourceManager_base public: typedef std::list ResourceList; - virtual ~ResourceManager_base(); + virtual ~ResourceManager_base() {} /// Save all resouces registered to this class /// @return true on success @@ -96,17 +96,19 @@ public: /// Add resource to list, only used in Resource - virtual void addResource(Resource_base &r) = 0; + virtual void addResource(Resource_base &r); /// Remove a specific resource, only used in Resource - virtual void removeResource(Resource_base &r) = 0; + virtual void removeResource(Resource_base &r) { + m_resourcelist.remove(&r); + } /// searches for the resource with the resourcename /// @return pointer to resource base on success, else 0. - virtual Resource_base *findResource(const std::string &resourcename) = 0; + Resource_base *findResource(const std::string &resourcename); /// searches for the resource with the resourcename /// @return pointer to resource base on success, else 0. - virtual const Resource_base *findResource(const std::string &resourcename) const = 0; + const Resource_base *findResource(const std::string &resourcename) const; std::string resourceValue(const std::string &resourcename) const; void setResourceValue(const std::string &resourcename, const std::string &value); @@ -118,13 +120,14 @@ public: */ template Resource &getResource(const std::string &resource); + +protected: + ResourceList m_resourcelist; }; class ResourceManager: public ResourceManager_base { public: - typedef std::list ResourceList; - // lock specifies if the database should be opened with one level locked // (useful for constructing inside initial set of constructors) ResourceManager(const char *filename, bool lock_db); @@ -139,22 +142,9 @@ public: virtual bool save(const char *filename, const char *mergefilename=0); - /// Add resource to list, only used in Resource virtual void addResource(Resource_base &r); - /// Remove a specific resource, only used in Resource - virtual void removeResource(Resource_base &r) { - m_resourcelist.remove(&r); - } - - /// searches for the resource with the resourcename - /// @return pointer to resource base on success, else 0. - virtual Resource_base *findResource(const std::string &resourcename); - /// searches for the resource with the resourcename - /// @return pointer to resource base on success, else 0. - virtual const Resource_base *findResource(const std::string &resourcename) const; - // this marks the database as "in use" and will avoid reloading // resources unless it is zero. // It returns this resource manager. Useful for passing to @@ -176,8 +166,6 @@ protected: private: - ResourceList m_resourcelist; - XrmDatabaseHelper *m_database; std::string m_filename; -- cgit v0.11.2