Skip to content

Commit

Permalink
save on background thread
Browse files Browse the repository at this point in the history
  • Loading branch information
thorstink committed Dec 30, 2024
1 parent 2c3927a commit d900e53
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 33 deletions.
3 changes: 1 addition & 2 deletions symmetri/gui/menu_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ void draw_menu_bar(const model::ViewModel &vm) {
file_dialog.Open();
}
if (ImGui::MenuItem("Save")) {
rxdispatch::push(
farbart::writeToDisk(std::filesystem::path(vm.active_file)));
rxdispatch::push(farbart::writeToDisk(vm));
}
// Exit...
ImGui::EndMenu();
Expand Down
1 change: 1 addition & 0 deletions symmetri/gui/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ struct ViewModel {
};

using Reducer = std::function<Model(Model &&)>;
using Computer = std::function<Reducer(void)>;

Model initializeModel();

Expand Down
8 changes: 2 additions & 6 deletions symmetri/gui/rxdispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

namespace rxdispatch {

using Update =
std::variant<model::Reducer, std::function<model::Reducer(void)>>;
using Update = std::variant<model::Reducer, model::Computer>;

void unsubscribe();
moodycamel::BlockingConcurrentQueue<Update>& getQueue();
Expand Down Expand Up @@ -43,10 +42,7 @@ inline auto get_events_observable() {
}) |
rpp::operators::subscribe_on(rpp::schedulers::new_thread{}) |
rpp::operators::flat_map(
[](auto value) { return std::visit(VisitPackage(), value); }) |
rpp::operators::tap([](auto&&) {
std::cout << "tap " << std::this_thread::get_id() << std::endl;
});
[](auto&& value) { return std::visit(VisitPackage(), value); });
}

} // namespace rxdispatch
52 changes: 28 additions & 24 deletions symmetri/gui/write_graph_to_disk.cpp
Original file line number Diff line number Diff line change
@@ -1,70 +1,74 @@
#include "write_graph_to_disk.h"

#include <thread>

#include "draw_popups.h"
#include "reducers.h"
#include "tinyxml2/tinyxml2.h"

namespace farbart {
model::Reducer writeToDisk(const std::filesystem::path& path) {
return [=](model::Model&& m_ptr) {
auto& m = *m_ptr.data;
model::Computer writeToDisk(const model::ViewModel& vm) {
return [p_positions = vm.p_positions, t_positions = vm.t_positions,
p_view = vm.p_view, t_view = vm.t_view, &net = vm.net,
path = vm.active_file]() {
using namespace tinyxml2;
XMLDocument doc;
doc.SaveFile(path.c_str());
XMLElement* pnml = doc.NewElement("pnml");
pnml->SetAttribute("xmlns",
"http://www.pnml.org/version-2009/grammar/pnml");
doc.InsertFirstChild(pnml);
auto net = pnml->InsertFirstChild(doc.NewElement("net"));
auto root = net->InsertFirstChild(doc.NewElement("page"));
auto xmlnet = pnml->InsertFirstChild(doc.NewElement("net"));
auto root = xmlnet->InsertFirstChild(doc.NewElement("page"));

for (auto idx : m.p_view) {
for (auto idx : p_view) {
XMLElement* child = doc.NewElement("place");
XMLElement* s_child = doc.NewElement("graphics");
XMLElement* s_s_child = doc.NewElement("position");
child->SetAttribute("id", m.net.place[idx].c_str());
s_s_child->SetAttribute("x", m.p_positions[idx].x);
s_s_child->SetAttribute("y", m.p_positions[idx].y);
child->SetAttribute("id", net.place[idx].c_str());
s_s_child->SetAttribute("x", p_positions[idx].x);
s_s_child->SetAttribute("y", p_positions[idx].y);
s_child->InsertEndChild(s_s_child);
child->InsertEndChild(s_child);
root->InsertEndChild(child);
}

for (auto idx : m.t_view) {
for (auto idx : t_view) {
XMLElement* child = doc.NewElement("transition");
XMLElement* s_child = doc.NewElement("graphics");
XMLElement* s_s_child = doc.NewElement("position");
child->SetAttribute("id", m.net.transition[idx].c_str());
s_s_child->SetAttribute("x", m.t_positions[idx].x);
s_s_child->SetAttribute("y", m.t_positions[idx].y);
child->SetAttribute("id", net.transition[idx].c_str());
s_s_child->SetAttribute("x", t_positions[idx].x);
s_s_child->SetAttribute("y", t_positions[idx].y);
s_child->InsertEndChild(s_s_child);
child->InsertEndChild(s_child);
root->InsertEndChild(child);

for (const auto& token : m.net.input_n[idx]) {
for (const auto& token : net.input_n[idx]) {
XMLElement* child = doc.NewElement("arc");
child->SetAttribute("source",
m.net.place[std::get<size_t>(token)].c_str());
child->SetAttribute("target", m.net.transition[idx].c_str());
net.place[std::get<size_t>(token)].c_str());
child->SetAttribute("target", net.transition[idx].c_str());
child->SetAttribute(
"color",
std::string(std::get<symmetri::Token>(token).toString()).c_str());
root->InsertEndChild(child);
}
for (const auto& token : m.net.output_n[idx]) {
for (const auto& token : net.output_n[idx]) {
XMLElement* child = doc.NewElement("arc");
child->SetAttribute("source", m.net.transition[idx].c_str());
child->SetAttribute("source", net.transition[idx].c_str());
child->SetAttribute("target",
m.net.place[std::get<size_t>(token)].c_str());
net.place[std::get<size_t>(token)].c_str());
child->SetAttribute(
"color",
std::string(std::get<symmetri::Token>(token).toString()).c_str());
root->InsertEndChild(child);
}
}

doc.SaveFile(path.c_str());

m.active_file = path;
return m_ptr;
return [path](model::Model&& m_ptr) {
m_ptr.data->active_file = path.c_str();
return m_ptr;
};
};
}
} // namespace farbart
3 changes: 2 additions & 1 deletion symmetri/gui/write_graph_to_disk.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
#include "model.h"

namespace farbart {
model::Reducer writeToDisk(const std::filesystem::path& path);
model::Computer writeToDisk(const model::ViewModel &vm);

} // namespace farbart

0 comments on commit d900e53

Please sign in to comment.