2 """Returns the last element of the name, without any of the qualifying
4 return nname.split(
":")[-1]
6def generator(module_class_name, fq_interface_name, fq_impl_names, help_names):
7 """Generates the text for a module support file."""
8 interface_name = denamespace(fq_interface_name)
11 print(f
"#include \"include/{module_class_name}.hpp\"")
14 for fq_impl_name
in fq_impl_names:
15 print(f
"#include \"include/{denamespace(fq_impl_name)}.hpp\"")
19 print(
"#include <string>")
22 print(
"namespace Module {")
25 for fq_impl_name
in fq_impl_names:
26 print(f
"const std::string {denamespace(fq_impl_name).upper()} = \"{fq_impl_name}\";")
31 print(f
"Module<{fq_interface_name}>::map Module<{fq_interface_name}>::functionMap" +
" = {")
32 for fq_impl_name
in fq_impl_names:
33 print(f
" {{ {denamespace(fq_impl_name).upper()}, newImpl<{fq_interface_name}, {fq_impl_name}> }},")
39 print(f
"Module<{fq_interface_name}>::fn Module<{fq_interface_name}>::spf = functionMap.at({denamespace(fq_impl_names[0]).upper()});")
41 print(f
"std::unique_ptr<{fq_interface_name}> Module<{fq_interface_name}>::staticInstance")
42 print(f
"= std::move(newImpl<{fq_interface_name}, {fq_impl_names[0]}>());")
47 print(f
"std::string Module<{fq_interface_name}>::moduleName(){{ return \"{fq_interface_name}\"; }}")
55 for fq_impl_name
in fq_impl_names:
56 impl_names_uc.append(denamespace(fq_impl_name).upper())
58 print(f
"template<> HelpMap& getHelpRecursive<{fq_interface_name}>(HelpMap& map, bool getAll)")
60 print(
" const std::string& pfx = Nextsim::ConfiguredModule::MODULE_PREFIX;")
61 print(f
" map[pfx].push_back({{ pfx + \".\" + Module<{fq_interface_name}>::moduleName(), ConfigType::MODULE,")
62 impl_namelist_uc =
", ".join(impl_names_uc)
63 print(f
" {{ {impl_namelist_uc} }}, {impl_names_uc[0]}, \"\",")
64 print(
" \"MODULE DESCRIPTION HERE\" });")
65 for help_name
in help_names:
66 print(f
" {help_name}::getHelpRecursive(map, getAll);")
71 print(f
"{fq_interface_name}& getImplementation<{fq_interface_name}>()")
73 print(f
" return getImplTemplate<{fq_interface_name}, {module_class_name}>();")
77 print(f
"void setImplementation<{fq_interface_name}>(const std::string& implName)")
79 print(f
" setImplTemplate<{module_class_name}>(implName);")
83 print(f
"std::unique_ptr<{fq_interface_name}> getInstance()")
85 print(f
" return getInstTemplate<{fq_interface_name}, {module_class_name}>();")
88 print(f
"{module_class_name}::Constructor {module_class_name}::ctor;")
89 print(f
"{module_class_name}::Constructor::Constructor()")
91 print(f
" addToConfiguredModules<{fq_interface_name}, {module_class_name}>();")
94 print(
"} /* namespace Module */")
96if __name__ ==
"__main__":
99 parser = argparse.ArgumentParser(description =
"Write out the text for a Nextsim module class source file.",
100 epilog =
"Suffix the interface or any implementation name with an asterisk (*) to include a call to getHelpRecursive().")
101 parser.add_argument(
"impl", metavar =
"impls", nargs =
'*', default =
None, help =
"Fully qualified name of the implementation classes.")
102 parser.add_argument(
"--interface", dest =
"interface", required =
True, help =
"Fully qualified name of the interface class.")
103 parser.add_argument(
"--module-prefix", dest =
"modulepfx", help =
"Name of the module, will be suffixed by 'Module'.")
104 args = parser.parse_args()
106 if (len(args.impl) == 0):
113 interface_name = args.interface
114 if interface_name[-1] ==
"*":
115 interface_name = interface_name[:-1]
116 help_names.append(interface_name)
117 for impl_name
in args.impl:
118 if impl_name[-1] ==
"*":
119 impl_names.append(impl_name[:-1])
120 help_names.append(impl_names[-1])
122 impl_names.append(impl_name)
125 if args.modulepfx
is None:
126 iface = denamespace(interface_name)
127 if (iface[0] ==
"I")
and iface[1].isupper():
128 modulepfx = iface[1:]
130 modulepfx = denamespace(interface_name)
131 modulepfx +=
"Module"
133 modulepfx = denamespace(args.modulepfx)
136 generator(modulepfx, interface_name, impl_names, help_names)