Serialize and deserialize.

This commit is contained in:
Pcornat 2022-01-14 22:58:16 +01:00
parent e762ab05c6
commit 7b6eca1067
Signed by: Pcornat
GPG Key ID: 2F3932FF46D9ECA0
6 changed files with 96 additions and 60 deletions

View File

@ -28,7 +28,7 @@ set(SOURCE_HEADERS
include/characteristic/endurance.hpp include/characteristic/endurance.hpp
include/characteristic/chance.hpp include/characteristic/chance.hpp
include/gui/menu/menu.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 set(SOURCE_FILES
src/imgui/imgui.cpp src/imgui/imgui.cpp

View File

@ -1,45 +1,25 @@
#ifndef BILLYSHEET_ADRESSE_HPP #ifndef BILLYSHEET_ADRESSE_HPP
#define BILLYSHEET_ADRESSE_HPP #define BILLYSHEET_ADRESSE_HPP
#include <cstdint> #include "characteristic.hpp"
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using json = nlohmann::json;
namespace character::characteristic { namespace character::characteristic {
class Adresse final { class Adresse final : public Characteristic {
private:
const std::uint32_t base{ 1 };
std::uint32_t carac{ 0 };
std::uint32_t materiel{ 0 };
std::uint32_t additional{ 0 };
public: public:
Adresse() noexcept = default; Adresse() noexcept: Characteristic(1, 0, 0, 0) {}
~Adresse() noexcept = default; ~Adresse() noexcept final = 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, Adresse &adresse) { friend void from_json(const json &j, Adresse &adresse) {
const_cast<std::uint32_t&>(adresse.base) = j.at("base"); nlohmann::from_json(j, static_cast<Characteristic &>(adresse));
adresse.carac = j.at("carac");
adresse.materiel =j.at("materiel");
adresse.additional =j.at("additional");
} }
}; };
void to_json(json &j, const Adresse &adresse) { void to_json(json &j, const Adresse &adresse) {
j.emplace("base", adresse.get_base()); to_json(j, static_cast<Characteristic>(adresse));
j.emplace("carac", adresse.get_carac());
j.emplace("materiel", adresse.get_materiel());
j.emplace("additional", adresse.get_additional());
} }
} }

View File

@ -1,21 +1,23 @@
#ifndef BILLYSHEET_CHANCE_HPP #ifndef BILLYSHEET_CHANCE_HPP
#define BILLYSHEET_CHANCE_HPP #define BILLYSHEET_CHANCE_HPP
#include <cstdint> #include "characteristic.hpp"
namespace character::characteristic { namespace character::characteristic {
class Chance final { class Chance final : public Characteristic {
private:
const std::uint32_t base{ 3 };
std::uint32_t carac{ 0 };
std::uint32_t materiel{ 0 };
std::uint32_t additional{ 0 };
public: 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<Characteristic &>(chance));
}
}; };
void to_json(json &j, const Chance &chance) {
to_json(j, static_cast<Characteristic>(chance));
}
} }

View File

@ -0,0 +1,50 @@
#ifndef BILLYSHEET_CHARACTERISTIC_HPP
#define BILLYSHEET_CHARACTERISTIC_HPP
#include <cstdint>
#include <nlohmann/json.hpp>
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<std::uint32_t &>(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

View File

@ -1,27 +1,28 @@
//
// Created by postaron on 10/01/2022.
//
#ifndef BILLYSHEET_ENDURANCE_HPP #ifndef BILLYSHEET_ENDURANCE_HPP
#define BILLYSHEET_ENDURANCE_HPP #define BILLYSHEET_ENDURANCE_HPP
#include <cstdint> #include "characteristic.hpp"
#include <nlohmann/json.hpp>
using json = nlohmann::json;
namespace character::characteristic { namespace character::characteristic {
class Endurance final { class Endurance final : public Characteristic {
private:
const std::uint32_t base{ 2 };
std::uint32_t carac{ 0 };
std::uint32_t materiel{ 0 };
std::uint32_t additional{ 0 };
public: 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; [[nodiscard]] std::uint32_t get_max_lp() const noexcept;
friend void from_json(const json &j, Endurance &endurance) {
nlohmann::from_json(j, static_cast<Characteristic &>(endurance));
}
}; };
void to_json(json &j, const Endurance &endurance) {
to_json(j, static_cast<Characteristic>(endurance));
}
} }

View File

@ -2,20 +2,23 @@
#define BILLYSHEET_HABILETE_HPP #define BILLYSHEET_HABILETE_HPP
#include <cstdint> #include <cstdint>
#include "characteristic.hpp"
namespace character::characteristic { namespace character::characteristic {
class Habilete final { class Habilete final : public Characteristic {
private:
const std::uint32_t base{ 2 };
std::uint32_t carac{ 0 };
std::uint32_t materiel{ 0 };
std::uint32_t additional{ 0 };
public: 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<Characteristic &>(habilete));
}
}; };
void to_json(json &j, const Habilete &habilete) {
to_json(j, static_cast<Characteristic>(habilete));
}
} }