#ifndef BILLYSHEET_CHARACTERISTIC_HPP #define BILLYSHEET_CHARACTERISTIC_HPP #include #include using json = nlohmann::json; namespace character::characteristic { class Characteristic { protected: using defaultValue = std::numeric_limits; mutable std::int32_t total{ defaultValue::max() }; 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) noexcept: base(base), carac(carac), materiel(materiel), additional(additional) { (void) get_total(); } Characteristic(const Characteristic &charac) noexcept = default; Characteristic &operator=(const Characteristic &charac) noexcept { const_cast(base) = charac.base; carac = charac.carac; materiel = charac.materiel; additional = charac.additional; total = charac.get_total(); return *this; } 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; } [[nodiscard]] std::size_t get_total() const noexcept { if (total == defaultValue::max()) { total = static_cast(base + carac + materiel + additional); if (total < 0) { total = 0; } } return total; } friend void from_json(const json &j, Characteristic &charac) { const_cast(charac.base) = j.at("base").get(); charac.carac = j.at("carac").get(); charac.materiel = j.at("materiel").get(); charac.additional = j.at("additional").get(); charac.total = j.at("total").get(); } }; static void to_json(json &j, const Characteristic &charac) { j["base"] = charac.get_base(); j["carac"] = charac.get_carac(); j["materiel"] = charac.get_materiel(); j["additional"] = charac.get_additional(); j["total"] = charac.get_total(); } } #endif //BILLYSHEET_CHARACTERISTIC_HPP