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.
This commit is contained in:
Matthieu Gautier 2016-12-23 14:27:58 +01:00
parent cba71b4e75
commit 97bdc17651
1 changed files with 9 additions and 5 deletions

View File

@ -79,7 +79,7 @@ master_c_template = """//This file is automaically generated. Do not modify it.
#include <stdlib.h>
#include <fstream>
#include <exception>
#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<char>() ));
}}
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__":