From 97bdc17651717fedae0d987e54f22ba21a03f086 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Fri, 23 Dec 2016 14:27:58 +0100 Subject: [PATCH] All static compilation with several generated resource code. If there are several uses of the compile_resource script it will have several definition of getResource function. So, define a custum getResource function per resources "pack" and add a define to have a nice API. A developer must take care of not include two generated .h in the same compilation unit as there will be a redefine error. The best way to avoid this is to always include the generated .h in the c(pp) file and never in a header. If a compilation unit need to use two pack at the same time, we have to undef 'getResource' and use the real getResource_* methods. --- scripts/compile_resources.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/compile_resources.py b/scripts/compile_resources.py index 0586183bc..257a114a9 100755 --- a/scripts/compile_resources.py +++ b/scripts/compile_resources.py @@ -79,7 +79,7 @@ master_c_template = """//This file is automaically generated. Do not modify it. #include #include #include -#include "{basename}" +#include "{include_file}" class ResourceNotFound : public std::runtime_error {{ public: @@ -101,7 +101,7 @@ static std::string init_resource(const char* name, const unsigned char* content, (std::istreambuf_iterator() )); }} -const std::string& getResource(const std::string& name) {{ +const std::string& getResource_{basename}(const std::string& name) {{ {RESOURCES_GETTER} throw ResourceNotFound("Resource not found."); }} @@ -114,7 +114,8 @@ def gen_c_file(resources, basename): return master_c_template.format( RESOURCES="\n\n".join(r.dump_impl() for r in resources), RESOURCES_GETTER="\n\n".join(r.dump_getter() for r in resources), - basename=basename + include_file=basename, + basename=to_identifier(basename) ) @@ -129,7 +130,9 @@ namespace RESOURCE {{ {RESOURCES} }}; -const std::string& getResource(const std::string& name); +const std::string& getResource_{basename}(const std::string& name); + +#define getResource(a) (getResource_{basename}(a)) #endif // KIWIX_{BASENAME} @@ -138,7 +141,8 @@ const std::string& getResource(const std::string& name); def gen_h_file(resources, basename): return master_h_template.format( RESOURCES="\n ".join(r.dump_decl() for r in resources), - BASENAME=basename.upper() + BASENAME=basename.upper(), + basename=basename, ) if __name__ == "__main__":