mirror of
https://github.com/gwm17/Specter.git
synced 2024-11-22 18:28:52 -05:00
Add Variable to serialization. Fix some Serializer jank. Change save file extension to .yaml to reflect actual file type.
This commit is contained in:
parent
74e6c1d122
commit
6c5b7d17a7
|
@ -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<std::mutex> guard(m_managerMutex);
|
||||
auto iter = m_varMap.find(variable);
|
||||
if (iter != m_varMap.end())
|
||||
{
|
||||
return iter->second->value;
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
std::vector<std::string> SpectrumManager::GetListOfVariables()
|
||||
{
|
||||
std::scoped_lock<std::mutex> guard(m_managerMutex);
|
||||
|
|
|
@ -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<std::string> GetListOfVariables();
|
||||
/********************/
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
namespace Specter {
|
||||
|
||||
static void SerializeCut(YAML::Emitter& output, const std::shared_ptr<SpectrumManager>& 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<SpectrumManager>& 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<HistogramArgs>& histoList, const std::vector<CutArgs>& 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<std::string>());
|
||||
manager->BindVariable(tempVar);
|
||||
tempVar.SetValue(var["Value"].as<double>());
|
||||
}
|
||||
}
|
||||
|
||||
SPEC_INFO("Successfully loaded data from {0}", m_filename);
|
||||
}
|
||||
|
|
|
@ -26,8 +26,8 @@ namespace Specter {
|
|||
SpectrumSerializer(const std::string& filepath);
|
||||
~SpectrumSerializer();
|
||||
|
||||
void SerializeData(const SpectrumManager::Ref& manager, const std::vector<HistogramArgs>& histoList, const std::vector<CutArgs>& 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; }
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user