10std::string
joinLines(
const std::vector<std::string>& lines) {
12 for (
const auto& line : lines) {
18void NumericTemplateManager::generateTemplateImpl(
const std::string&,
19 const std::string& outputPath) {
20 namespace fs = std::filesystem;
24 const fs::path helper_cpp_src = mgr->getBaseHelperFileName(
"cpp");
25 const fs::path helper_h_src = mgr->getBaseHelperFileName(
"h");
26 const fs::path helper_cpp_dst = mgr->getHelperFileName(
"cpp");
27 const fs::path helper_h_dst = mgr->getHelperFileName(
"h");
29 fs::create_directories(helper_cpp_dst.parent_path());
30 fs::create_directories(helper_h_dst.parent_path());
33 fs::copy_file(helper_cpp_src, helper_cpp_dst, fs::copy_options::overwrite_existing);
34 fs::copy_file(helper_h_src, helper_h_dst, fs::copy_options::overwrite_existing);
37 const fs::path templatePath = mgr->getNumGeneratedFileName();
38 std::ifstream templateFile(templatePath);
40 const fs::path paramPath = mgr->getParamFileName();
41 fs::create_directories(paramPath.parent_path());
42 const fs::path paramTmpPath = paramPath.string() +
".tmp";
44 std::ofstream paramFile(paramTmpPath, std::ios::trunc);
46 throw std::runtime_error(
"Cannot write MARTY parameter file: " + paramTmpPath.string());
48 std::unordered_map<std::string, double> params =
numModifier->get_params();
52 throw std::runtime_error(
"Failed while writing MARTY parameter file: " + paramTmpPath.string());
55 std::error_code paramEc;
56 fs::rename(paramTmpPath, paramPath, paramEc);
58 std::error_code cleanupEc;
59 fs::remove(paramTmpPath, cleanupEc);
60 throw std::runtime_error(
"Cannot atomically publish MARTY parameter file: " + paramEc.message());
63 const fs::path outPathFs = outputPath;
64 fs::create_directories(outPathFs.parent_path());
70 const fs::path tmpPath = outPathFs.string() +
".tmp";
71 std::ofstream outputFile(tmpPath);
72 if (!outputFile)
return;
74 outputFile <<
"//42\n";
79 fs::copy_file(tmpPath, outPathFs, fs::copy_options::overwrite_existing);
82void NonNumericTemplateManager::generateTemplateImpl(
const std::string& templateName,
const std::string& outputPath) {
83 std::string templatePath =
templatesDir +
"/" + templateName +
".cpp";
84 std::ifstream templateFile(templatePath);
93 std::ofstream outputFile(outputPath);
99 std::getline(templateFile, line);
102 outputFile <<
"//42" <<
"\n";
104 while (std::getline(templateFile, line)) {
109 outputFile << line <<
"\n";
115 std::unordered_set<InterpretedParam> deps;
116 for (
auto& [k, v] :
numModifier->get_interpreted_param_map()) {
124 std::ifstream file(path);
126 if (!file.is_open()) {
135 for (std::size_t line_number = 0;
136 line_number < 16 && std::getline(file, line);
138 if (line.find(
"//42") != std::string::npos) {
Manages file and directory names for generated MARTY code and assets.
Declares the helper used to prepare numeric MARTY models and parameter files.
std::string joinLines(const std::vector< std::string > &lines)
Declares managers for generating (numeric and non-numeric) model templates.
static std::shared_ptr< FileNameManager > getInstance(const std::string &wilson="", const std::string &model="")
Retrieves a FileNameManager for a given (wilson, model) pair.
std::string wilson
Wilson basis name.
std::string templatesDir
Directory containing the template sources.
std::unique_ptr< GeneralNumModelModifier > numModifier
Optional modifier for numeric templates.
bool already_generated(const std::string &path)
Checks whether a file was already generated by this system.
std::unordered_set< InterpretedParam > get_dependencies()
Returns the set of interpreted parameters used by the numeric modifier.
std::unique_ptr< ModelModifier > modelModifier
Optional modifier for non-numeric templates.
std::string model
Model name.