From 6c5b7d17a7f6b653ad1968c97f59f61e4fad724c Mon Sep 17 00:00:00 2001 From: Gordon McCann Date: Tue, 15 Nov 2022 21:15:15 -0500 Subject: [PATCH] Add Variable to serialization. Fix some Serializer jank. Change save file extension to .yaml to reflect actual file type. --- Specter/src/Specter/Core/SpectrumManager.cpp | 12 ++++++ Specter/src/Specter/Core/SpectrumManager.h | 1 + .../src/Specter/Core/SpectrumSerializer.cpp | 37 +++++++++++++++++-- Specter/src/Specter/Core/SpectrumSerializer.h | 4 +- Specter/src/Specter/Editor/EditorLayer.cpp | 4 +- 5 files changed, 50 insertions(+), 8 deletions(-) diff --git a/Specter/src/Specter/Core/SpectrumManager.cpp b/Specter/src/Specter/Core/SpectrumManager.cpp index 14ae940..60a08d8 100644 --- a/Specter/src/Specter/Core/SpectrumManager.cpp +++ b/Specter/src/Specter/Core/SpectrumManager.cpp @@ -390,6 +390,18 @@ namespace Specter { var.m_pdata = m_varMap[var.GetName()]; } + //Only for use with SpectrumSerializer + double SpectrumManager::GetVariableData(const std::string& variable) + { + std::scoped_lock guard(m_managerMutex); + auto iter = m_varMap.find(variable); + if (iter != m_varMap.end()) + { + return iter->second->value; + } + return 0.0; + } + std::vector SpectrumManager::GetListOfVariables() { std::scoped_lock guard(m_managerMutex); diff --git a/Specter/src/Specter/Core/SpectrumManager.h b/Specter/src/Specter/Core/SpectrumManager.h index e812e19..22cda09 100644 --- a/Specter/src/Specter/Core/SpectrumManager.h +++ b/Specter/src/Specter/Core/SpectrumManager.h @@ -81,6 +81,7 @@ namespace Specter { /*Variable Functions*/ void BindVariable(Variable& var); + double GetVariableData(const std::string& variableName); //Only for use with SpectrumSerializer std::vector GetListOfVariables(); /********************/ diff --git a/Specter/src/Specter/Core/SpectrumSerializer.cpp b/Specter/src/Specter/Core/SpectrumSerializer.cpp index 230beb5..db06390 100644 --- a/Specter/src/Specter/Core/SpectrumSerializer.cpp +++ b/Specter/src/Specter/Core/SpectrumSerializer.cpp @@ -18,7 +18,7 @@ namespace Specter { - static void SerializeCut(YAML::Emitter& output, const std::shared_ptr& manager, const CutArgs& args) + static void SerializeCut(YAML::Emitter& output, const SpectrumManager::Ref& manager, const CutArgs& args) { output << YAML::BeginMap; output << YAML::Key << "Cut" << YAML::Value << args.name; @@ -49,7 +49,7 @@ namespace Specter { output << YAML::EndMap; } - static void SerializeHistogram(YAML::Emitter& output, const std::shared_ptr& manager, const HistogramArgs& args) + static void SerializeHistogram(YAML::Emitter& output, const SpectrumManager::Ref& manager, const HistogramArgs& args) { output << YAML::BeginMap; output << YAML::Key << "Histogram" << YAML::Value << args.name; @@ -73,6 +73,14 @@ namespace Specter { } + static void SerializeVariable(YAML::Emitter& output, const SpectrumManager::Ref& manager, const std::string& variable) + { + output << YAML::BeginMap; + output << YAML::Key << "Variable" << YAML::Value << variable; + output << YAML::Key << "Value" << YAML::Value << manager->GetVariableData(variable); + output << YAML::EndMap; + } + SpectrumSerializer::SpectrumSerializer(const std::string& filepath) : m_filename(filepath) { @@ -80,7 +88,7 @@ namespace Specter { SpectrumSerializer::~SpectrumSerializer() {} - void SpectrumSerializer::SerializeData(const SpectrumManager::Ref& manager, const std::vector& histoList, const std::vector& cutList) + void SpectrumSerializer::SerializeData(const SpectrumManager::Ref& manager) { std::ofstream output(m_filename); if (!output.is_open()) @@ -89,8 +97,13 @@ namespace Specter { return; } + auto cutList = manager->GetListOfCuts(); + auto histoList = manager->GetListOfHistograms(); + auto varList = manager->GetListOfVariables(); + YAML::Emitter yamlStream; yamlStream << YAML::BeginMap; + yamlStream << YAML::Key << "Cuts" << YAML::Value << YAML::BeginSeq; for (auto& cut : cutList) { @@ -102,6 +115,12 @@ namespace Specter { { SerializeHistogram(yamlStream, manager, histo); } + yamlStream << YAML::EndSeq; + yamlStream << YAML::Key << "Variables" << YAML::Value << YAML::BeginSeq; + for (auto& var : varList) + { + SerializeVariable(yamlStream, manager, var); + } yamlStream << YAML::EndSeq << YAML::EndMap; output << yamlStream.c_str(); @@ -109,7 +128,7 @@ namespace Specter { output.close(); } - void SpectrumSerializer::DeserializeData(const SpectrumManager::Ref& manager) + void SpectrumSerializer::DeserializeData(SpectrumManager::Ref& manager) { manager->RemoveAllSpectra(); //When loading in, we remove all extant data, to avoid any potential collisions. @@ -177,6 +196,16 @@ namespace Specter { } } } + auto vars = data["Variables"]; + if (vars) + { + for (const auto& var : vars) + { + Variable tempVar(var["Variable"].as()); + manager->BindVariable(tempVar); + tempVar.SetValue(var["Value"].as()); + } + } SPEC_INFO("Successfully loaded data from {0}", m_filename); } diff --git a/Specter/src/Specter/Core/SpectrumSerializer.h b/Specter/src/Specter/Core/SpectrumSerializer.h index 86ccf6a..f20a09f 100644 --- a/Specter/src/Specter/Core/SpectrumSerializer.h +++ b/Specter/src/Specter/Core/SpectrumSerializer.h @@ -26,8 +26,8 @@ namespace Specter { SpectrumSerializer(const std::string& filepath); ~SpectrumSerializer(); - void SerializeData(const SpectrumManager::Ref& manager, const std::vector& histoList, const std::vector& cutList); - void DeserializeData(const SpectrumManager::Ref& manager); + void SerializeData(const SpectrumManager::Ref& manager); + void DeserializeData(SpectrumManager::Ref& manager); inline const std::string& GetFilename() { return m_filename; } diff --git a/Specter/src/Specter/Editor/EditorLayer.cpp b/Specter/src/Specter/Editor/EditorLayer.cpp index 3c5b4af..97d3cc3 100644 --- a/Specter/src/Specter/Editor/EditorLayer.cpp +++ b/Specter/src/Specter/Editor/EditorLayer.cpp @@ -197,7 +197,7 @@ namespace Specter { } //Render all of our sub-windows, dialogs, panels, etc - auto fd_result = m_fileDialog.RenderFileDialog(".spec"); + auto fd_result = m_fileDialog.RenderFileDialog(".yaml"); if (!fd_result.first.empty()) { switch (fd_result.second) @@ -214,7 +214,7 @@ namespace Specter { { SPEC_INFO("Found a Save File! {0}", fd_result.first); SpectrumSerializer serializer(fd_result.first); - serializer.SerializeData(m_manager, m_histoList, m_cutList); + serializer.SerializeData(m_manager); break; } }