From 7b6eca1067be630c6fa914b3202f8070b09e2213 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Fri, 14 Jan 2022 22:58:16 +0100 Subject: [PATCH] Serialize and deserialize. --- CMakeLists.txt | 2 +- include/characteristic/adresse.hpp | 32 +++------------ include/characteristic/chance.hpp | 22 +++++----- include/characteristic/characteristic.hpp | 50 +++++++++++++++++++++++ include/characteristic/endurance.hpp | 29 ++++++------- include/characteristic/habilete.hpp | 21 ++++++---- 6 files changed, 96 insertions(+), 60 deletions(-) create mode 100644 include/characteristic/characteristic.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e012e0f..09b0d16 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ set(SOURCE_HEADERS include/characteristic/endurance.hpp include/characteristic/chance.hpp include/gui/menu/menu.hpp - include/gui/menu/menu_data.hpp) + include/gui/menu/menu_data.hpp include/characteristic/characteristic.hpp) set(SOURCE_FILES src/imgui/imgui.cpp diff --git a/include/characteristic/adresse.hpp b/include/characteristic/adresse.hpp index 0dda21e..ec85225 100644 --- a/include/characteristic/adresse.hpp +++ b/include/characteristic/adresse.hpp @@ -1,45 +1,25 @@ #ifndef BILLYSHEET_ADRESSE_HPP #define BILLYSHEET_ADRESSE_HPP -#include +#include "characteristic.hpp" #include using json = nlohmann::json; namespace character::characteristic { - class Adresse final { - private: - const std::uint32_t base{ 1 }; - std::uint32_t carac{ 0 }; - std::uint32_t materiel{ 0 }; - std::uint32_t additional{ 0 }; - + class Adresse final : public Characteristic { public: - Adresse() noexcept = default; + Adresse() noexcept: Characteristic(1, 0, 0, 0) {} - ~Adresse() noexcept = default; - - [[nodiscard]] std::uint32_t get_base() const { return base; } - - [[nodiscard]] std::uint32_t get_carac() const { return carac; } - - [[nodiscard]] std::uint32_t get_materiel() const { return materiel; } - - [[nodiscard]] std::uint32_t get_additional() const { return additional; } + ~Adresse() noexcept final = default; friend void from_json(const json &j, Adresse &adresse) { - const_cast(adresse.base) = j.at("base"); - adresse.carac = j.at("carac"); - adresse.materiel =j.at("materiel"); - adresse.additional =j.at("additional"); + nlohmann::from_json(j, static_cast(adresse)); } }; void to_json(json &j, const Adresse &adresse) { - j.emplace("base", adresse.get_base()); - j.emplace("carac", adresse.get_carac()); - j.emplace("materiel", adresse.get_materiel()); - j.emplace("additional", adresse.get_additional()); + to_json(j, static_cast(adresse)); } } diff --git a/include/characteristic/chance.hpp b/include/characteristic/chance.hpp index 22ff7ec..e7eb28c 100644 --- a/include/characteristic/chance.hpp +++ b/include/characteristic/chance.hpp @@ -1,21 +1,23 @@ #ifndef BILLYSHEET_CHANCE_HPP #define BILLYSHEET_CHANCE_HPP -#include +#include "characteristic.hpp" namespace character::characteristic { - class Chance final { - private: - const std::uint32_t base{ 3 }; - std::uint32_t carac{ 0 }; - std::uint32_t materiel{ 0 }; - std::uint32_t additional{ 0 }; - + class Chance final : public Characteristic { public: - Chance() noexcept = default; + Chance() noexcept: Characteristic(3, 0, 0, 0) {} - ~Chance() noexcept = default; + ~Chance() noexcept final = default; + + friend void from_json(const json &j, Chance &chance) { + nlohmann::from_json(j, static_cast(chance)); + } }; + + void to_json(json &j, const Chance &chance) { + to_json(j, static_cast(chance)); + } } diff --git a/include/characteristic/characteristic.hpp b/include/characteristic/characteristic.hpp new file mode 100644 index 0000000..d3fadc2 --- /dev/null +++ b/include/characteristic/characteristic.hpp @@ -0,0 +1,50 @@ +#ifndef BILLYSHEET_CHARACTERISTIC_HPP +#define BILLYSHEET_CHARACTERISTIC_HPP + +#include +#include + +using json = nlohmann::json; + +namespace character::characteristic { + class Characteristic { + protected: + const std::uint32_t base{ 0 }; + std::uint32_t carac{ 0 }; + std::uint32_t materiel{ 0 }; + std::uint32_t additional{ 0 }; + + public: + Characteristic() noexcept = default; + + Characteristic(const std::uint32_t base, const std::uint32_t carac, const std::uint32_t materiel, const std::uint32_t additional) : + base(base), carac(carac), materiel(materiel), additional(additional) {} + + virtual ~Characteristic() noexcept = default; + + [[nodiscard]] std::uint32_t get_base() const { return base; } + + [[nodiscard]] std::uint32_t get_carac() const { return carac; } + + [[nodiscard]] std::uint32_t get_materiel() const { return materiel; } + + [[nodiscard]] std::uint32_t get_additional() const { return additional; } + + friend void from_json(const json &j, Characteristic &charac) { + const_cast(charac.base) = j.at("base"); + charac.carac = j.at("carac"); + charac.materiel = j.at("materiel"); + charac.additional = j.at("additional"); + } + }; + + void to_json(json &j, const Characteristic &charac) { + j.emplace("base", charac.get_base()); + j.emplace("carac", charac.get_carac()); + j.emplace("materiel", charac.get_materiel()); + j.emplace("additional", charac.get_additional()); + } +} + + +#endif //BILLYSHEET_CHARACTERISTIC_HPP diff --git a/include/characteristic/endurance.hpp b/include/characteristic/endurance.hpp index d696e9c..c012aaa 100644 --- a/include/characteristic/endurance.hpp +++ b/include/characteristic/endurance.hpp @@ -1,27 +1,28 @@ -// -// Created by postaron on 10/01/2022. -// - #ifndef BILLYSHEET_ENDURANCE_HPP #define BILLYSHEET_ENDURANCE_HPP -#include +#include "characteristic.hpp" +#include + +using json = nlohmann::json; namespace character::characteristic { - class Endurance final { - private: - const std::uint32_t base{ 2 }; - std::uint32_t carac{ 0 }; - std::uint32_t materiel{ 0 }; - std::uint32_t additional{ 0 }; - + class Endurance final : public Characteristic { public: - Endurance() noexcept = default; + Endurance() noexcept: Characteristic(2, 0, 0, 0) {}; - ~Endurance() noexcept = default; + ~Endurance() noexcept final = default; [[nodiscard]] std::uint32_t get_max_lp() const noexcept; + + friend void from_json(const json &j, Endurance &endurance) { + nlohmann::from_json(j, static_cast(endurance)); + } }; + + void to_json(json &j, const Endurance &endurance) { + to_json(j, static_cast(endurance)); + } } diff --git a/include/characteristic/habilete.hpp b/include/characteristic/habilete.hpp index 6caffd2..0efd63f 100644 --- a/include/characteristic/habilete.hpp +++ b/include/characteristic/habilete.hpp @@ -2,20 +2,23 @@ #define BILLYSHEET_HABILETE_HPP #include +#include "characteristic.hpp" namespace character::characteristic { - class Habilete final { - private: - const std::uint32_t base{ 2 }; - std::uint32_t carac{ 0 }; - std::uint32_t materiel{ 0 }; - std::uint32_t additional{ 0 }; - + class Habilete final : public Characteristic { public: - Habilete() noexcept = default; + Habilete() noexcept: Characteristic(2, 0, 0, 0) {}; - ~Habilete() noexcept = default; + ~Habilete() noexcept final = default; + + friend void from_json(const json &j, Habilete &habilete) { + nlohmann::from_json(j, static_cast(habilete)); + } }; + + void to_json(json &j, const Habilete &habilete) { + to_json(j, static_cast(habilete)); + } }