summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDimitri van Heesch <doxygen@gmail.com>2020-08-02 12:24:52 (GMT)
committerDimitri van Heesch <doxygen@gmail.com>2020-08-02 12:24:52 (GMT)
commit4f18ae106a216a465713a0c7be78105419146881 (patch)
treedb61b0cb3a8e97d0f68eb86d86593f17996cf8f0
parent713a6167733e660dcdfd48a36134576aaa8b80d3 (diff)
downloadDoxygen-4f18ae106a216a465713a0c7be78105419146881.zip
Doxygen-4f18ae106a216a465713a0c7be78105419146881.tar.gz
Doxygen-4f18ae106a216a465713a0c7be78105419146881.tar.bz2
Modernize code for the resource manager
-rw-r--r--src/CMakeLists.txt4
-rwxr-xr-xsrc/res2cc_cmd.py7
-rw-r--r--src/resourcemgr.cpp33
-rw-r--r--src/resourcemgr.h7
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 <qdict.h>
+
+#include <map>
#include <qfile.h>
#include <qcstring.h>
#include <qglobal.h>
@@ -28,8 +29,7 @@
class ResourceMgr::Private
{
public:
- Private() : resources(257) {}
- QDict<Resource> resources;
+ std::map<std::string,Resource> resources;
};
ResourceMgr &ResourceMgr::instance()
@@ -38,37 +38,34 @@ ResourceMgr &ResourceMgr::instance()
return theInstance;
}
-ResourceMgr::ResourceMgr()
+ResourceMgr::ResourceMgr() : p(std::make_unique<Private>())
{
- p = new Private;
}
ResourceMgr::~ResourceMgr()
{
- delete p;
}
-void ResourceMgr::registerResources(const Resource resources[],int numResources)
+void ResourceMgr::registerResources(std::initializer_list<Resource> resources)
{
- for (int i=0;i<numResources;i++)
+ for (auto &res : resources)
{
- p->resources.insert(resources[i].name,&resources[i]);
+ p->resources.insert({res.name,res});
}
}
bool ResourceMgr::writeCategory(const char *categoryName,const char *targetDir) const
{
- QDictIterator<Resource> 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 <memory>
+#include <initializer_list>
+
#include <qcstring.h>
/** @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<Resource> 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<Private> p;
};
#endif