From 4f18ae106a216a465713a0c7be78105419146881 Mon Sep 17 00:00:00 2001 From: Dimitri van Heesch Date: Sun, 2 Aug 2020 14:24:52 +0200 Subject: Modernize code for the resource manager --- src/CMakeLists.txt | 4 ++-- src/res2cc_cmd.py | 7 ++----- src/resourcemgr.cpp | 33 ++++++++++++++++----------------- src/resourcemgr.h | 7 +++++-- 4 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8b11a2d..c99ee9f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -87,7 +87,7 @@ file(GLOB RESOURCES ${CMAKE_SOURCE_DIR}/templates/*/*) add_custom_command( COMMENT "Generating ${GENERATED_SRC}/resources.cpp" COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/res2cc_cmd.py ${CMAKE_SOURCE_DIR}/templates ${GENERATED_SRC}/resources.cpp - DEPENDS ${RESOURCES} + DEPENDS ${RESOURCES} ${CMAKE_CURRENT_LIST_DIR}/res2cc_cmd.py OUTPUT ${GENERATED_SRC}/resources.cpp ) set_source_files_properties(${GENERATED_SRC}/resources.cpp PROPERTIES GENERATED 1) @@ -95,7 +95,7 @@ set_source_files_properties(${GENERATED_SRC}/resources.cpp PROPERTIES GENERATED # layout_default.xml add_custom_command( COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/to_c_cmd.py < ${CMAKE_CURRENT_LIST_DIR}/layout_default.xml > ${GENERATED_SRC}/layout_default.xml.h - DEPENDS ${CMAKE_CURRENT_LIST_DIR}/layout_default.xml + DEPENDS ${CMAKE_CURRENT_LIST_DIR}/layout_default.xml ${CMAKE_CURRENT_LIST_DIR}/to_c_cmd.py OUTPUT ${GENERATED_SRC}/layout_default.xml.h ) set_source_files_properties(${GENERATED_SRC}/layout_default.xml.h PROPERTIES GENERATED 1) diff --git a/src/res2cc_cmd.py b/src/res2cc_cmd.py index 268ae86..f6321f6 100755 --- a/src/res2cc_cmd.py +++ b/src/res2cc_cmd.py @@ -116,13 +116,10 @@ def main(): print("#include \"resourcemgr.h\"\n",file=outputFile) for f in files: f.writeContents(outputFile) - print("static Resource resourceDir[] =",file=outputFile) - print("{",file=outputFile) + print("void initResources() { ResourceMgr::instance().registerResources({",file=outputFile) for f in files: f.writeDirEntry(outputFile) - print("};",file=outputFile) - print("static int resourceDir_len = %s;" % len(files), file=outputFile) - print("void initResources() { ResourceMgr::instance().registerResources(resourceDir,resourceDir_len); }",file=outputFile) + print("});}",file=outputFile) if __name__ == '__main__': main() diff --git a/src/resourcemgr.cpp b/src/resourcemgr.cpp index 6d2946c..36ce30d 100644 --- a/src/resourcemgr.cpp +++ b/src/resourcemgr.cpp @@ -12,7 +12,8 @@ * input used in their production; they are not affected by this license. * */ -#include + +#include #include #include #include @@ -28,8 +29,7 @@ class ResourceMgr::Private { public: - Private() : resources(257) {} - QDict resources; + std::map resources; }; ResourceMgr &ResourceMgr::instance() @@ -38,37 +38,34 @@ ResourceMgr &ResourceMgr::instance() return theInstance; } -ResourceMgr::ResourceMgr() +ResourceMgr::ResourceMgr() : p(std::make_unique()) { - p = new Private; } ResourceMgr::~ResourceMgr() { - delete p; } -void ResourceMgr::registerResources(const Resource resources[],int numResources) +void ResourceMgr::registerResources(std::initializer_list resources) { - for (int i=0;iresources.insert(resources[i].name,&resources[i]); + p->resources.insert({res.name,res}); } } bool ResourceMgr::writeCategory(const char *categoryName,const char *targetDir) const { - QDictIterator it(p->resources); - const Resource *res; - for (it.toFirst();(res=it.current());++it) + for (auto &kv : p->resources) { - if (qstrcmp(res->category,categoryName)==0) + Resource &res = kv.second; + if (qstrcmp(res.category,categoryName)==0) { - QCString pathName = QCString(targetDir)+"/"+res->name; + QCString pathName = QCString(targetDir)+"/"+res.name; QFile f(pathName); - if (!f.open(IO_WriteOnly) || f.writeBlock((const char *)res->data,res->size)!=res->size) + if (!f.open(IO_WriteOnly) || f.writeBlock((const char *)res.data,res.size)!=res.size) { - err("Failed to write resource '%s' to directory '%s'\n",res->name,targetDir); + err("Failed to write resource '%s' to directory '%s'\n",res.name,targetDir); return FALSE; } } @@ -178,7 +175,9 @@ bool ResourceMgr::copyResource(const char *name,const char *targetDir) const const Resource *ResourceMgr::get(const char *name) const { - return p->resources.find(name); + auto it = p->resources.find(name); + if (it!=p->resources.end()) return &it->second; + return 0; } QCString ResourceMgr::getAsString(const char *name) const diff --git a/src/resourcemgr.h b/src/resourcemgr.h index 1959ec4..2d7ad9b 100644 --- a/src/resourcemgr.h +++ b/src/resourcemgr.h @@ -15,6 +15,9 @@ #ifndef RESOURCEMGR_H #define RESOURCEMGR_H +#include +#include + #include /** @brief Compiled resource */ @@ -36,7 +39,7 @@ class ResourceMgr static ResourceMgr &instance(); /** Registers an array of resources */ - void registerResources(const Resource resources[],int numResources); + void registerResources(std::initializer_list resources); /** Writes all resource belonging to a given category to a given target directory */ bool writeCategory(const char *categoryName,const char *targetDir) const; @@ -57,7 +60,7 @@ class ResourceMgr ResourceMgr(); ~ResourceMgr(); class Private; - Private *p; + std::unique_ptr p; }; #endif -- cgit v0.12