OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
NativeModelBundle.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2#pragma once
4#include "FileIO.hpp"
7#include <fstream>
8#include <map>
9#include <iterator>
10
12inline std::map<std::string, std::string> configurations(const SimulationContext& ctx) {
13 std::map<std::string, std::string> result;
14 for (const auto& component : ctx.process_component_specs) {
15 const auto* entry = components::ProcessComponentRegistry::instance().find(component.id);
16 std::string text;
17 if (entry && entry->save) text = entry->save(ctx, component);
18 if (text.empty() && !component.config_path.empty()) {
19 auto path = io::utf8_path(component.resolved_config_path.empty()
20 ? component.config_path : component.resolved_config_path);
21 if (path.is_relative()) path = io::utf8_path(ctx.inp_file_path).parent_path() / path;
22 std::ifstream file(path, std::ios::binary);
23 if (!file) throw std::runtime_error("Cannot preserve component configuration: " + io::path_utf8(path));
24 text.assign(std::istreambuf_iterator<char>(file), {});
25 if (file.bad()) throw std::runtime_error("Cannot finish reading component configuration");
26 }
27 if (!text.empty()) result[component.id] = std::move(text);
28 }
29 return result;
30}
31
33 const std::map<std::string, std::string>& content) {
34 if (content.empty()) return;
35 auto folder = io::utf8_path(ctx.inp_file_path);
36 folder += ".resources";
37 folder /= "components";
38 std::filesystem::create_directories(folder);
39 int index = 0;
40 for (auto& component : ctx.process_component_specs) {
41 const auto found = content.find(component.id);
42 if (found == content.end()) continue;
43 const auto path = std::filesystem::absolute(folder / (std::to_string(index++) + ".cfg"));
44 io::AtomicOutputFile file(path, true);
45 if (!file.stream() || std::fwrite(found->second.data(), 1, found->second.size(), file.stream()) != found->second.size()
46 || !file.commit()) throw std::runtime_error("Cannot restore component configuration: " + file.error());
47 component.config_path = component.resolved_config_path = io::path_utf8(path);
48 }
49}
50}
Checked serialization and publication of a single output file.
Opening files by UTF-8 path on every platform.
Registry + config-file delivery for [PROCESS_COMPONENTS] (Unified Transport suite D-UT8; phases IO1–I...
The central, reentrant simulation context for the new engine.
static ProcessComponentRegistry & instance()
Access the process-global lazily constructed builtin hook registry.
Definition ProcessComponentRegistry.cpp:180
const ProcessComponentEntry * find(const std::string &id) const
Borrow one exact component registry entry.
Definition ProcessComponentRegistry.cpp:192
Own a staging stream and publish it by a checked single-file rename.
Definition AtomicOutputFile.hpp:36
FILE * stream() const
Borrow the writable staging stream.
Definition AtomicOutputFile.hpp:139
const std::string & error() const
Borrow the latest stored file-operation diagnostic.
Definition AtomicOutputFile.hpp:145
bool commit()
Flush, sync, close and attempt to rename staging over the destination.
Definition AtomicOutputFile.hpp:157
std::filesystem::path utf8_path(const std::string &utf8)
Convert borrowed path bytes into an owned native filesystem path.
Definition FileIO.hpp:55
std::string path_utf8(const std::filesystem::path &p, bool generic=false)
Copy a native path into the engine byte-string representation.
Definition FileIO.hpp:99
Definition NativeModelBundle.hpp:11
std::map< std::string, std::string > configurations(const SimulationContext &ctx)
Definition NativeModelBundle.hpp:12
void restoreConfigurations(SimulationContext &ctx, const std::map< std::string, std::string > &content)
Definition NativeModelBundle.hpp:32
Central, reentrant simulation context.
Definition SimulationContext.hpp:390
std::string inp_file_path
Path to the input .inp file (empty if not opened from a file).
Definition SimulationContext.hpp:2039
std::vector< ProcessComponentSpec > process_component_specs
Process-component registrations parsed from [PROCESS_COMPONENTS].
Definition SimulationContext.hpp:955
ComponentConfigSave save
Definition ProcessComponentRegistry.hpp:144