From d13358b71ec15085f2638fd9c3fc634df62dfc94 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 9 May 2021 13:56:46 -0400 Subject: Reduced dependency on json-related hacks --- src/rules.h | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) (limited to 'src/rules.h') diff --git a/src/rules.h b/src/rules.h index c1b130d..83d471f 100644 --- a/src/rules.h +++ b/src/rules.h @@ -10,23 +10,34 @@ namespace rules { - class Ability : public Jsonable { + class RuleItem { + public: + friend void to_json(nlohmann::json& j, const RuleItem& ri) { + j = ri.payload; + } + friend void from_json(const nlohmann::json& j, RuleItem& ri) { + ri.payload = j; + } + std::string getPayload(void) const {return payload;} + protected: + std::string payload; + }; + + class Ability : public RuleItem { public: std::string getFull() const {return abilities.at(getAbbrev());} - std::string getAbbrev() const {return abbrev;} + std::string getAbbrev() const {return getPayload();} operator std::string() const {return getAbbrev();} - virtual nlohmann::json toJson(void) const { - return getAbbrev(); - } bool operator<(const Ability& rhs) const {return getAbbrev() < rhs.getAbbrev();} bool operator==(const Ability& rhs) const {return getAbbrev() == rhs.getAbbrev();} operator bool() const {return ! getAbbrev().empty();} Ability() {} - explicit Ability(const nlohmann::json& data) : abbrev(data) { + Ability(const std::string& abbrev) { if(! abilities.contains(abbrev)) { throw std::invalid_argument("No such ability: " + abbrev); } + payload = abbrev; } virtual ~Ability() {} @@ -50,19 +61,14 @@ namespace rules { } private: - const std::string abbrev; - static const std::map abilities; }; - class Skill : public Jsonable { + class Skill : public RuleItem { public: - std::string getName() const {return name;} + std::string getName() const {return getPayload();} Ability getAbility() const {return Ability(skill2ability.at(getName()));} operator std::string() const {return getName();} - virtual nlohmann::json toJson(void) const { - return getName(); - } bool operator<(const Skill& rhs) const {return getName() < rhs.getName();} bool operator==(const Skill& rhs) const {return getName() == rhs.getName();} operator bool() const {return ! getName().empty();} @@ -89,10 +95,11 @@ namespace rules { static Skill Persuasion() {return Skill("Persuasion");} Skill() {}; - explicit Skill(const nlohmann::json& data) : name(data) { + Skill(const std::string& name) { if(! skill2ability.contains(name)) { throw std::invalid_argument("No such skill: " + name); } + payload = name; } static Skill fromString(std::string s) { @@ -108,25 +115,21 @@ namespace rules { } private: - const std::string name; - static const std::map skill2ability; }; - class Qualifier : public Jsonable { + class Qualifier : public RuleItem { public: Qualifier() {} - Qualifier(const nlohmann::json& data) : negative(data) { + Qualifier(const std::string& negative) { if(! negative2positive.contains(negative)) { throw std::invalid_argument("No such qualifier: " + negative); } + payload = negative; } - std::string getNegative() const {return negative;} + std::string getNegative() const {return getPayload();} std::string getPositive() const {return negative2positive.at(getNegative());} operator std::string() const {return getNegative();} - virtual nlohmann::json toJson(void) const { - return getNegative(); - } virtual ~Qualifier() {} bool operator==(const Qualifier& rhs) const {return getNegative() == rhs.getNegative();} @@ -135,8 +138,6 @@ namespace rules { static Qualifier Adamantine() {return Qualifier("non-adamantine");} private: - const std::string negative; - static const std::map negative2positive; }; -- cgit v1.2.3