diff options
Diffstat (limited to 'core/src/Model/Template')
-rw-r--r-- | core/src/Model/Template/TableTemplate.cpp | 84 | ||||
-rw-r--r-- | core/src/Model/Template/TableTemplate.hpp | 15 | ||||
-rw-r--r-- | core/src/Model/Template/Template.hpp | 2 | ||||
-rw-r--r-- | core/src/Model/Template/Template_Main.cpp | 32 |
4 files changed, 68 insertions, 65 deletions
diff --git a/core/src/Model/Template/TableTemplate.cpp b/core/src/Model/Template/TableTemplate.cpp index a5745c7..16cd333 100644 --- a/core/src/Model/Template/TableTemplate.cpp +++ b/core/src/Model/Template/TableTemplate.cpp @@ -27,22 +27,29 @@ bool TableCell::IsMergedCell() const return PrimaryCellLocation.x == -1 || PrimaryCellLocation.y == -1; } -template <class TProxy> -void TableCell::OperateIOProxy(TProxy& proxy) +template <class TTableCell, class TStream> +void OperateStreamForTableCell(TTableCell& cell, TStream& proxy) { - proxy.template ObjectAdapted<DataStreamAdapters::String>(Content); - proxy.Object(Location); - proxy.Object(PrimaryCellLocation); - proxy.Value(SpanX); - proxy.Value(SpanY); - proxy.Value(HorizontalAlignment); - proxy.Value(VerticalAlignment); - proxy.Value(Type); - proxy.Value(DataId); + proxy.template ObjectAdapted<DataStreamAdapters::String>(cell.Content); + proxy.Object(cell.Location); + proxy.Object(cell.PrimaryCellLocation); + proxy.Value(cell.SpanX); + proxy.Value(cell.SpanY); + proxy.Enum(cell.HorizontalAlignment); + proxy.Enum(cell.VerticalAlignment); + proxy.Enum(cell.Type); + proxy.Value(cell.DataId); } -template void TableCell::OperateIOProxy(InputDataStream& adapter); -template void TableCell::OperateIOProxy(OutputDataStream& adapter); +void TableCell::ReadFromDataStream(InputDataStream& stream) +{ + ::OperateStreamForTableCell(*this, stream); +} + +void TableCell::WriteToDataStream(OutputDataStream& stream) const +{ + ::OperateStreamForTableCell(*this, stream); +} Vec2i TableArrayGroup::GetLeftCell() const { @@ -86,16 +93,23 @@ bool TableArrayGroup::UpdateCellName(std::string_view oldName, std::string_view return ::UpdateElementName(mName2Cell, oldName, newName); } -template <class TProxy> -void TableArrayGroup::OperateIOProxy(TProxy& proxy) +template <class TTableArrayGroup, class TStream> +void OperateStreamForTableArrayGroup(TTableArrayGroup& group, TStream& stream) +{ + stream.Value(group.Row); + stream.Value(group.LeftCell); + stream.Value(group.RightCell); +} + +void TableArrayGroup::ReadFromDataStream(InputDataStream& stream) { - proxy.Value(Row); - proxy.Value(LeftCell); - proxy.Value(RightCell); + ::OperateStreamForTableArrayGroup(*this, stream); } -template void TableArrayGroup::OperateIOProxy(InputDataStream& adapter); -template void TableArrayGroup::OperateIOProxy(OutputDataStream& adapter); +void TableArrayGroup::WriteToDataStream(OutputDataStream& stream) const +{ + ::OperateStreamForTableArrayGroup(*this, stream); +} TableInstantiationParameters::TableInstantiationParameters(const TableTemplate& table) : mTable{ &table } @@ -549,23 +563,27 @@ lxw_worksheet* TableTemplate::InstantiateToExcelWorksheet(lxw_workbook* workbook return worksheet; } -void TableTemplate::ReadFromDataStream(InputDataStream& stream) +class TableTemplate::Private { - OperateIOProxy(stream); -} +public: + template <class TTableTemplate, class TProxy> + static void OperateStream(TTableTemplate& table, TProxy& proxy) + { + proxy.template ObjectAdapted<DataStreamAdapters::Vector<>>(table.mColumnWidths); + proxy.template ObjectAdapted<DataStreamAdapters::Vector<>>(table.mRowHeights); + proxy.template ObjectAdapted<DataStreamAdapters::Vector<>>(table.mCells); + proxy.template ObjectAdapted<DataStreamAdapters::Vector<>>(table.mArrayGroups); + proxy.template ObjectAdapted<DataStreamAdapters::TslArrayMap<>>(table.mName2Parameters); + proxy.template ObjectAdapted<DataStreamAdapters::TslArrayMap<>>(table.mName2ArrayGroups); + } +}; -void TableTemplate::WriteToDataStream(OutputDataStream& stream) +void TableTemplate::ReadFromDataStream(InputDataStream& stream) { - OperateIOProxy(stream); + Private::OperateStream(*this, stream); } -template <class TProxy> -void TableTemplate::OperateIOProxy(TProxy& proxy) +void TableTemplate::WriteToDataStream(OutputDataStream& stream) const { - proxy.template ObjectAdapted<DataStreamAdapters::Vector<>>(mColumnWidths); - proxy.template ObjectAdapted<DataStreamAdapters::Vector<>>(mRowHeights); - proxy.template ObjectAdapted<DataStreamAdapters::Vector<>>(mCells); - proxy.template ObjectAdapted<DataStreamAdapters::Vector<>>(mArrayGroups); - proxy.template ObjectAdapted<DataStreamAdapters::TslArrayMap<>>(mName2Parameters); - proxy.template ObjectAdapted<DataStreamAdapters::TslArrayMap<>>(mName2ArrayGroups); + Private::OperateStream(*this, stream); } diff --git a/core/src/Model/Template/TableTemplate.hpp b/core/src/Model/Template/TableTemplate.hpp index c6617b0..8771867 100644 --- a/core/src/Model/Template/TableTemplate.hpp +++ b/core/src/Model/Template/TableTemplate.hpp @@ -54,8 +54,8 @@ public: /// Return whether this cell is a part of a merged range or not. Includes the primary cell. bool IsMergedCell() const; - template <class TProxy> - void OperateIOProxy(TProxy& proxy); + void ReadFromDataStream(InputDataStream& stream); + void WriteToDataStream(OutputDataStream& stream) const; }; // TODO support reverse (bottom to top) filling order @@ -109,8 +109,8 @@ public: Vec2i FindCell(std::string_view name); bool UpdateCellName(std::string_view oldName, std::string_view newName); - template <class TProxy> - void OperateIOProxy(TProxy& proxy); + void ReadFromDataStream(InputDataStream& stream); + void WriteToDataStream(OutputDataStream& stream) const; }; // Forward declaration of libxlsxwriter structs @@ -148,6 +148,7 @@ class TableTemplate : public Template { friend class TableSingleParamsIter; friend class TableArrayGroupsIter; + class Private; private: /// Map from parameter name to index of the parameter cell (stored in mCells). @@ -218,9 +219,5 @@ public: lxw_worksheet* InstantiateToExcelWorksheet(lxw_workbook* workbook, const TableInstantiationParameters& params) const; void ReadFromDataStream(InputDataStream& stream) override; - void WriteToDataStream(OutputDataStream& stream) override; - -private: - template <class TProxy> - void OperateIOProxy(TProxy& proxy); + void WriteToDataStream(OutputDataStream& stream) const override; }; diff --git a/core/src/Model/Template/Template.hpp b/core/src/Model/Template/Template.hpp index c6b93e2..061cc07 100644 --- a/core/src/Model/Template/Template.hpp +++ b/core/src/Model/Template/Template.hpp @@ -36,7 +36,7 @@ public: Kind GetKind() const; virtual void ReadFromDataStream(InputDataStream& stream) = 0; - virtual void WriteToDataStream(OutputDataStream& stream) = 0; + virtual void WriteToDataStream(OutputDataStream& stream) const = 0; }; class TemplateAssetList final : public AssetListTyped<Template> diff --git a/core/src/Model/Template/Template_Main.cpp b/core/src/Model/Template/Template_Main.cpp index 19803aa..49187d3 100644 --- a/core/src/Model/Template/Template_Main.cpp +++ b/core/src/Model/Template/Template_Main.cpp @@ -4,6 +4,8 @@ #include "Model/Project.hpp" #include "UI/UI.hpp" #include "Utils/I18n.hpp" +#include "Utils/IO/DataStream.hpp" +#include "Utils/IO/FileStream.hpp" #include "Utils/UUID.hpp" #include <imgui.h> @@ -60,15 +62,11 @@ fs::path TemplateAssetList::RetrievePathFromAsset(const SavedAsset& asset) const bool TemplateAssetList::SaveInstance(const SavedAsset& assetInfo, const Asset* asset) const { auto path = RetrievePathFromAsset(assetInfo); + auto stream = OutputDataStream(OutputFileStream(path, OutputFileStream::TruncateFile)); - std::ofstream ofs(path, std::ios::binary); - if (!ofs) return false; - ofs << (uint64_t)assetInfo.Name.size(); - ofs << assetInfo.Name; - ofs << static_cast<Template::Kind>(assetInfo.Payload); - + stream.WriteObject(assetInfo); if (auto tmpl = static_cast<const Template*>(asset)) { - tmpl->WriteTo(ofs); + stream.WriteObject(*tmpl); } return true; @@ -76,24 +74,14 @@ bool TemplateAssetList::SaveInstance(const SavedAsset& assetInfo, const Asset* a static std::unique_ptr<Template> LoadTemplateFromFile(const fs::path& path) { - std::ifstream ifs(path, std::ios::binary); - if (!ifs) return nullptr; - - // TODO fix serialization - uint64_t nameSize; - ifs >> nameSize; - ifs.seekg(nameSize, std::ios::end); + auto stream = InputDataStream(InputFileStream(path)); - uint32_t iKind; - ifs >> iKind; + SavedAsset assetInfo; + stream.ReadObject(assetInfo); - auto kind = static_cast<Template::Kind>(iKind); + auto kind = static_cast<Template::Kind>(assetInfo.Payload); auto tmpl = Template::CreateByKind(kind); - - auto res = tmpl->ReadFrom(ifs); - if (res != Template::RR_Success) { - return nullptr; - } + stream.ReadObject(*tmpl); return tmpl; } |