speciesgen/src/starbound/frames.cpp

145 lines
4.1 KiB
C++
Raw Normal View History

2023-03-27 13:36:18 +00:00
/*
speciesgen
Copyright (C) 2022 prisixia
This file is part of speciesgen.
speciesgen is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
speciesgen is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with speciesgen. If not, see <https://www.gnu.org/licenses/>.
*/
#include "starbound/frames.h"
namespace Starbound {
namespace Frames {
//----------------------------------------------------------------------
// Grid
//----------------------------------------------------------------------
Grid::Grid(const std::vector<std::vector<std::optional<std::string>>> names,
const std::tuple<uint32_t, uint32_t> size,
const std::tuple<uint32_t, uint32_t> dimensions)
: m_size(size), m_dimensions(dimensions), m_names(names) {}
std::tuple<uint32_t, uint32_t> Grid::GetSize() const { return m_size; }
std::tuple<uint32_t, uint32_t> Grid::GetDimensions() const {
return m_dimensions;
}
std::vector<std::vector<std::optional<std::string>>> Grid::GetNames() const {
return m_names;
}
void Grid::SetSize(const std::tuple<uint32_t, uint32_t> size) { m_size = size; }
void Grid::SetDimensions(const std::tuple<uint32_t, uint32_t> dimensions) {
m_dimensions = dimensions;
}
void Grid::SetNames(
const std::vector<std::vector<std::optional<std::string>>> names) {
m_names = names;
}
void Grid::Serialize(const std::filesystem::path path) const {
std::ofstream file(path);
rapidjson::BasicOStreamWrapper wrapper(file);
rapidjson::PrettyWriter writer(wrapper);
writer.StartObject();
this->Serialize(writer);
writer.EndObject();
}
template <typename Writer> void Grid::Serialize(Writer &writer) const {
const std::tuple<uint32_t, uint32_t> size = m_size;
const std::tuple<uint32_t, uint32_t> dimensions = m_dimensions;
const std::vector<std::vector<std::optional<std::string>>> names = m_names;
writer.Key(RAPIDJSON_STRINGIFY(size));
writer.StartArray();
writer.Uint(std::get<0>(size));
writer.Uint(std::get<1>(size));
writer.EndArray();
writer.Key(RAPIDJSON_STRINGIFY(dimensions));
writer.StartArray();
writer.Uint(std::get<0>(dimensions));
writer.Uint(std::get<1>(dimensions));
writer.EndArray();
writer.Key(RAPIDJSON_STRINGIFY(names));
writer.StartArray();
for (auto &vec : names) {
writer.StartArray();
for (auto &name : vec) {
if (name.has_value())
writer.String(name.value().c_str());
else
writer.Null();
}
writer.EndArray();
}
writer.EndArray();
}
//----------------------------------------------------------------------
// Frames
//----------------------------------------------------------------------
Frames::Frames(const std::unordered_map<std::string, std::string> aliases,
const Grid grid)
: m_aliases(aliases), m_grid(grid) {}
std::unordered_map<std::string, std::string> Frames::GetAliases() const {
return m_aliases;
}
Grid Frames::GetGrid() const { return m_grid; }
void Frames::SetAliases(
const std::unordered_map<std::string, std::string> aliases) {
m_aliases = aliases;
}
void Frames::SetGrid(const Grid grid) { m_grid = grid; }
void Frames::Serialize(const std::filesystem::path path) const {
std::ofstream file(path);
rapidjson::BasicOStreamWrapper wrapper(file);
rapidjson::PrettyWriter writer(wrapper);
writer.StartObject();
this->Serialize(writer);
writer.EndObject();
}
template <typename Writer> void Frames::Serialize(Writer &writer) const {
const std::unordered_map<std::string, std::string> aliases = m_aliases;
const Grid grid = m_grid;
writer.Key(RAPIDJSON_STRINGIFY(aliases));
writer.StartObject();
for (auto &it : aliases) {
writer.Key(it.first.c_str());
writer.String(it.second.c_str());
}
writer.EndObject();
writer.Key(RAPIDJSON_STRINGIFY(grid));
writer.StartObject();
grid.Serialize(writer);
writer.EndObject();
}
} // namespace Frames
} // namespace Starbound