Commit 70354074 authored by Tobias WEBER's avatar Tobias WEBER
Browse files

started with un/serialisation

parent d71947db
......@@ -124,6 +124,9 @@ public:
virtual std::shared_ptr<Symbol> copy() const = 0;
virtual void print(std::ostream& ostr) const = 0;
virtual std::string serialise() const = 0;
static std::shared_ptr<Symbol> unserialise(const std::string &str);
static std::shared_ptr<Symbol> uminus(const Symbol &sym2);
static std::shared_ptr<Symbol> add(const Symbol &sym1, const Symbol &sym2);
static std::shared_ptr<Symbol> sub(const Symbol &sym1, const Symbol &sym2);
......@@ -157,6 +160,7 @@ public:
virtual std::shared_ptr<Symbol> copy() const override { return std::make_shared<SymbolReal>(m_val); }
virtual void print(std::ostream& ostr) const override { ostr << GetValue(); }
virtual std::string serialise() const override;
};
......@@ -176,6 +180,7 @@ public:
virtual std::shared_ptr<Symbol> copy() const override { return std::make_shared<SymbolString>(m_val); }
virtual void print(std::ostream& ostr) const override { ostr << GetValue(); }
virtual std::string serialise() const override;
};
......@@ -212,6 +217,8 @@ public:
}
if(!m_islist) ostr << " ]";
}
virtual std::string serialise() const override;
};
......@@ -231,6 +238,7 @@ public:
virtual std::shared_ptr<Symbol> copy() const override { return std::make_shared<SymbolDataset>(m_val); }
virtual void print(std::ostream& ostr) const override { ostr << "&lt;Dataset&gt;"; }
virtual std::string serialise() const override;
};
......
......@@ -353,3 +353,68 @@ std::shared_ptr<Symbol> Symbol::pow(const Symbol &sym1, const Symbol &sym2)
}
// ----------------------------------------------------------------------------
/**
* string representation of real
*/
std::string SymbolReal::serialise() const
{
std::ostringstream ostr;
ostr.precision(std::numeric_limits<t_real>::digits10);
ostr << Symbol::get_type_name(*this) << ":" << GetValue();
return ostr.str();
}
/**
* string representation of string (trivial)
*/
std::string SymbolString::serialise() const
{
return Symbol::get_type_name(*this) + ":" + GetValue();
}
/**
* string representation of list
*/
std::string SymbolList::serialise() const
{
std::ostringstream ostr;
ostr.precision(std::numeric_limits<t_real>::digits10);
ostr << Symbol::get_type_name(*this) << ":" << "###[";
bool firstelem = true;
for(const auto &elem : m_val)
{
if(!firstelem) ostr << "###, ";
ostr << elem->serialise();
firstelem = false;
}
ostr << "###]";
return ostr.str();
}
/**
* string representation of dataset
*/
std::string SymbolDataset::serialise() const
{
std::ostringstream ostr;
ostr.precision(8);
return ostr.str();
}
/**
* re-construct a symbol from a string representation
*/
std::shared_ptr<Symbol> Symbol::unserialise(const std::string &str)
{
return nullptr;
}
......@@ -245,12 +245,15 @@ void MainWnd::SaveFileAs()
*/
bool MainWnd::OpenFile(const QString &file)
{
static const std::string basename = "in20/";
if(file=="" || !QFile::exists(file))
return false;
// load xml
tl::Prop<std::string> prop;
prop.SetSeparator('/');
if(!prop.Load(file.toStdString(), tl::PropType::XML))
{
QMessageBox::critical(this, "Error", "Could not open session.");
......@@ -258,9 +261,9 @@ bool MainWnd::OpenFile(const QString &file)
}
// check format and version
auto optFmt = prop.QueryOpt<std::string>("in20/format");
auto optVer = prop.QueryOpt<std::string>("in20/version");
auto optTime = prop.QueryOpt<t_real>("in20/timestamp");
auto optFmt = prop.QueryOpt<std::string>(basename + "format");
auto optVer = prop.QueryOpt<std::string>(basename + "version");
auto optTime = prop.QueryOpt<t_real>(basename + "timestamp");
if(!optFmt || *optFmt != "session")
{
QMessageBox::critical(this, "Error", "Not a session file. Ignoring.");
......@@ -270,7 +273,9 @@ bool MainWnd::OpenFile(const QString &file)
print_out("Loading session file version ", *optVer, ", dated ", tl::epoch_to_str(*optTime), ".");
// TODO: get properties
// TODO: clear old workspace and load saved workspace variables
m_pWS->GetWidget()->GetWorkspace()->clear();
m_pWS->GetWidget()->LoadWorkspace(basename, prop);
SetCurrentFile(file);
......@@ -284,21 +289,25 @@ bool MainWnd::OpenFile(const QString &file)
*/
bool MainWnd::SaveFile(const QString &file)
{
static const std::string basename = "in20/";
if(file=="")
return false;
std::unordered_map<std::string, std::string> sessionmap;
// set format and version
sessionmap["in20/format"] = "session";
sessionmap["in20/version"] = PROGRAM_VERSION;
sessionmap["in20/timestamp"] = tl::var_to_str(tl::epoch<t_real>());
sessionmap[basename + "format"] = "session";
sessionmap[basename + "version"] = PROGRAM_VERSION;
sessionmap[basename + "timestamp"] = tl::var_to_str(tl::epoch<t_real>());
// TODO: set properties to sessionmap
// save workspace variables
m_pWS->GetWidget()->SaveWorkspace(basename, sessionmap);
tl::Prop<std::string> prop;
prop.SetSeparator('/');
prop.Add(sessionmap);
if(!prop.Save(file.toStdString(), tl::PropType::XML))
......
......@@ -12,9 +12,9 @@
#include <QtWidgets/QPushButton>
#include <QtWidgets/QFileDialog>
#include "tlibs/string/string.h"
#include "globals.h"
using t_real = t_real_dat;
......@@ -226,6 +226,59 @@ bool WorkSpaceWidget::eventFilter(QObject *pObj, QEvent *pEvt)
return QObject::eventFilter(pObj, pEvt);
}
/**
* load workspace variables from a property tree
*/
bool WorkSpaceWidget::LoadWorkspace(const std::string &basename, const tl::Prop<std::string> &prop)
{
// iterate over save variables
std::size_t varnum = 0;
while(true)
{
const std::string keyName = basename + "workspace/var_" + tl::var_to_str(varnum) + "/name";
const std::string keyValue = basename + "workspace/var_" + tl::var_to_str(varnum) + "/value";
++varnum;
auto key = prop.QueryOpt<std::string>(keyName);
auto val = prop.QueryOpt<std::string>(keyValue);
if(!key || !val) break; // no more variables?
// unserialise symbol from value string
auto sym = Symbol::unserialise(*val);
if(!sym)
{
print_err("Cannot unserialise variable \"", *key, "\".");
continue;
}
m_workspace.insert(std::make_pair(*key, sym));
}
return true;
}
/**
* save workspace variables to a map
*/
bool WorkSpaceWidget::SaveWorkspace(const std::string &basename, std::unordered_map<std::string, std::string> &map) const
{
std::size_t varnum = 0;
for(const auto &pair : m_workspace)
{
if(!pair.second)
continue;
map[basename + "workspace/var_" + tl::var_to_str(varnum) + "/name"] = pair.first;
map[basename + "workspace/var_" + tl::var_to_str(varnum) + "/value"] = pair.second->serialise();
++varnum;
}
return true;
}
// ----------------------------------------------------------------------------
......
......@@ -20,6 +20,7 @@
#include "data.h"
#include "tools/cli/cliparser.h"
#include "tlibs/file/prop.h"
......@@ -41,6 +42,9 @@ public:
std::map<std::string, std::shared_ptr<Symbol>>* GetWorkspace() { return &m_workspace; }
bool LoadWorkspace(const std::string &basename, const tl::Prop<std::string> &prop);
bool SaveWorkspace(const std::string &basename, std::unordered_map<std::string, std::string> &map) const;
protected:
void ItemSelected(QListWidgetItem* pCur);
void ItemDoubleClicked(QListWidgetItem* pCur);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment