diff options
author | Your Name <you@example.com> | 2021-05-20 17:00:53 -0400 |
---|---|---|
committer | Your Name <you@example.com> | 2021-05-20 17:00:53 -0400 |
commit | b27700a7e0b281ece3dea23060c17e0cae28715d (patch) | |
tree | ef13e98281dd0183c4fb1e32cdf371ea1f6c1794 | |
parent | be88609c825e18201f240415fe74a31c1a789484 (diff) | |
download | dmtool-b27700a7e0b281ece3dea23060c17e0cae28715d.tar.gz dmtool-b27700a7e0b281ece3dea23060c17e0cae28715d.tar.bz2 dmtool-b27700a7e0b281ece3dea23060c17e0cae28715d.zip |
Reduced exposure of implementation details
-rw-r--r-- | src/armor.cc | 22 | ||||
-rw-r--r-- | src/armor.h | 25 | ||||
-rw-r--r-- | src/defines.h | 6 | ||||
-rw-r--r-- | src/entry.cc | 27 | ||||
-rw-r--r-- | src/entry.h | 32 | ||||
-rw-r--r-- | src/spell.cc | 22 | ||||
-rw-r--r-- | src/spell.h | 28 | ||||
-rw-r--r-- | src/spellcasting.cc | 21 | ||||
-rw-r--r-- | src/spellcasting.h | 19 | ||||
-rw-r--r-- | src/weapon.cc | 24 | ||||
-rw-r--r-- | src/weapon.h | 28 |
11 files changed, 175 insertions, 79 deletions
diff --git a/src/armor.cc b/src/armor.cc index d7e7e57..3a429b3 100644 --- a/src/armor.cc +++ b/src/armor.cc @@ -8,6 +8,27 @@ using namespace std; namespace entry{ + struct armorImpl { + int ac; + std::string armor_type; + int strength; + bool disadvantage; + int cost; + double weight; + }; + NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(armorImpl, ac, armor_type, strength, disadvantage, cost, weight); + + NLOHMANN_FRIEND_DEFS(Item, Armor, data); + + Armor::Armor() : data(new armorImpl()) {} + + int Armor::getACBonus(void) const {return data->ac;} + std::string Armor::getArmorType(void) const {return data->armor_type;} + int Armor::getStrRequirement(void) const {return data->strength;} + bool Armor::stealthDisadvantage(void) const {return data->disadvantage;} + int Armor::getCost(void) const {return data->cost;} + double Armor::getWeight(void) const {return data->weight;} + string getTextHelper(const Armor& a, string dexBonusLight, string dexBonusMedium) { stringstream text; text << "AC: " << a.getACBonus(); @@ -30,7 +51,6 @@ namespace entry{ return getTextHelper(*this, "", ""); } - string Armor::getText(const creature::Creature& c) const { stringstream text; text << getName() << " (" << getType() << "): "; diff --git a/src/armor.h b/src/armor.h index 4351a24..c8bdb87 100644 --- a/src/armor.h +++ b/src/armor.h @@ -3,28 +3,23 @@ #include <nlohmann/json.hpp> namespace entry { - class Armor; + struct armorImpl; class Armor : public Item , public Substantial { public: - int getACBonus(void) const {return ac;} - std::string getArmorType(void) const {return armor_type;} - int getStrRequirement(void) const {return strength;} - bool stealthDisadvantage(void) const {return disadvantage;} - int getCost(void) const {return cost;} - double getWeight(void) const {return weight;} + Armor(); + int getACBonus(void) const; + std::string getArmorType(void) const; + int getStrRequirement(void) const; + bool stealthDisadvantage(void) const; + int getCost(void) const; + double getWeight(void) const; virtual std::string getText() const override; virtual std::string getText(const creature::Creature& c) const override; - NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE(Item, Armor, ac, armor_type, strength, disadvantage, cost, weight); - + NLOHMANN_FRIEND_DECLARES(Armor); private: - int ac; - std::string armor_type; - int strength; - bool disadvantage; - int cost; - double weight; + std::shared_ptr<armorImpl> data; }; } diff --git a/src/defines.h b/src/defines.h index 78ab770..f55a56f 100644 --- a/src/defines.h +++ b/src/defines.h @@ -6,8 +6,12 @@ #define NLOHMANN_FRIEND_DECLARES(Type) \ friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t); \ friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t); \ - virtual nlohmann::json serialize(void) const override { nlohmann::json ret; to_json(ret, *this); return ret; } + virtual nlohmann::json serialize(void) const { nlohmann::json ret; to_json(ret, *this); return ret; } #define NLOHMANN_FRIEND_DEFS(Super, Derived, PImpl) \ void to_json(nlohmann::json& nlohmann_json_j, const Derived& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<Super>(nlohmann_json_t)); nlohmann::to_json(nlohmann_json_j, *nlohmann_json_t.PImpl); } \ void from_json(const nlohmann::json& nlohmann_json_j, Derived& nlohmann_json_t) { nlohmann::from_json(nlohmann_json_j, static_cast<Super&>(nlohmann_json_t)); nlohmann::from_json(nlohmann_json_j, *nlohmann_json_t.PImpl); } + +#define NLOHMANN_FRIEND_DEFS_BASE(Type, PImpl) \ + void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, *nlohmann_json_t.PImpl); } \ + void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { nlohmann::from_json(nlohmann_json_j, *nlohmann_json_t.PImpl); } diff --git a/src/entry.cc b/src/entry.cc index 77cf144..663a40a 100644 --- a/src/entry.cc +++ b/src/entry.cc @@ -20,4 +20,31 @@ namespace entry { } throw std::invalid_argument("Invalid entry: " + std::string(data["entry"])); } + + struct entryImpl { + std::string entry; + std::string name; + std::string type; + std::string text; + }; + NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(entryImpl, entry, name, type, text); + + NLOHMANN_FRIEND_DEFS_BASE(Entry, data); + + Entry::Entry() : data(new entryImpl()) {} + + Entry::Entry(const std::string& entry, const std::string& name, const std::string& type, const std::string& text) : data(new entryImpl()) { + data->entry = entry; + data->name = name; + data->type = type; + data->text = text; + } + + std::string Entry::getName(void) const {return data->name;} + std::string Entry::getType(void) const {return data->type;} + std::string Entry::getText(void) const {return data->text;} + std::string Entry::getText(const creature::Creature& c) const { + return getName() + " (" + getType() + "): " + getText(); + } + void Entry::setText(const std::string& t) {data->text = t;} } diff --git a/src/entry.h b/src/entry.h index 9e00798..abfe831 100644 --- a/src/entry.h +++ b/src/entry.h @@ -8,34 +8,26 @@ namespace creature { } namespace entry { + struct entryImpl; + class Entry { public: - Entry() {} + Entry(); // Also can be created programmatically - Entry(const std::string& entry, const std::string& name, const std::string& type, const std::string& text) : entry(entry), name(name), type(type), text(text) {} + Entry(const std::string& entry, const std::string& name, const std::string& type, const std::string& text); static std::shared_ptr<Entry> create(const nlohmann::json& data); virtual ~Entry() {} - std::string getName(void) const {return name;} - std::string getType(void) const {return type;} - virtual std::string getText(void) const {return text;} - void setText(std::string t) {text = t;} + std::string getName(void) const; + std::string getType(void) const; + virtual std::string getText(void) const; + virtual std::string getText(const creature::Creature& c) const; + void setText(const std::string& t); virtual void init(void) {} - virtual nlohmann::json serialize(void) const { - nlohmann::json ret; - to_json(ret, *this); - return ret; - } - virtual std::string getText(const creature::Creature& c) const { - return getName() + " (" + getType() + "): " + getText(); - } - - NLOHMANN_DEFINE_TYPE_INTRUSIVE(Entry, entry, name, type, text); + + NLOHMANN_FRIEND_DECLARES(Entry); private: - std::string entry; - std::string name; - std::string type; - std::string text; + std::shared_ptr<entryImpl> data; }; } diff --git a/src/spell.cc b/src/spell.cc index 0294956..83ec9da 100644 --- a/src/spell.cc +++ b/src/spell.cc @@ -6,6 +6,28 @@ using namespace std; namespace entry { + struct spellImpl { + int level; + std::vector<std::string> classes; + std::string casting_time; + std::string range; + std::string components; + std::string duration; + }; + NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(spellImpl, level, classes, casting_time, range, components, duration); + + NLOHMANN_FRIEND_DEFS(Entry, Spell, data); + + Spell::Spell() : data(new spellImpl()) {} + + int Spell::getLevel(void) const {return data->level;} + std::string Spell::getSchool(void) const {return getType();} + std::vector<std::string> Spell::getClasses(void) const {return data->classes;} + std::string Spell::getCastingTime(void) const {return data->casting_time;} + std::string Spell::getRange(void) const {return data->range;} + std::string Spell::getComponents(void) const {return data->components;} + std::string Spell::getDuration(void) const {return data->duration;} + string Spell::getText() const { stringstream text; text << utils::toOrdinal(getLevel()) << " level " << getSchool() << " spell." << endl; diff --git a/src/spell.h b/src/spell.h index 1a560f2..2d9b24d 100644 --- a/src/spell.h +++ b/src/spell.h @@ -3,29 +3,25 @@ #include <nlohmann/json.hpp> namespace entry { + struct spellImpl; + class Spell : public Entry { public: - Spell() {} + Spell(); virtual ~Spell() {} - int getLevel(void) const {return level;} - std::string getSchool(void) const {return getType();} - std::vector<std::string> getClasses(void) const {return classes;} - std::string getCastingTime(void) const {return casting_time;} - std::string getRange(void) const {return range;} - std::string getComponents(void) const {return components;} - std::string getDuration(void) const {return duration;} + int getLevel(void) const; + std::string getSchool(void) const; + std::vector<std::string> getClasses(void) const; + std::string getCastingTime(void) const; + std::string getRange(void) const; + std::string getComponents(void) const; + std::string getDuration(void) const; std::string getText(void) const override; - NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE(Entry, Spell, level, classes, casting_time, range, components, duration); - + NLOHMANN_FRIEND_DECLARES(Spell); private: - int level; - std::vector<std::string> classes; - std::string casting_time; - std::string range; - std::string components; - std::string duration; + std::shared_ptr<spellImpl> data; }; } diff --git a/src/spellcasting.cc b/src/spellcasting.cc index 6315446..d969c5e 100644 --- a/src/spellcasting.cc +++ b/src/spellcasting.cc @@ -26,6 +26,27 @@ namespace entry { return shared_ptr<SlotLevel>(new SlotLevel(data)); } + struct spellcastingImpl { + bool innate; + rules::Ability spellcasting_ability; + std::vector<std::shared_ptr<SlotLevel>> levels; + }; + + NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(spellcastingImpl, innate, spellcasting_ability, levels); + + NLOHMANN_FRIEND_DEFS(Feature, Spellcasting, data); + + Spellcasting::Spellcasting() : Feature("spellcasting", "spells", ""), data(new spellcastingImpl()) { + data->innate = false; + data->spellcasting_ability = rules::Ability::Int(); + } + + bool Spellcasting::isInnate(void) const {return data->innate;} + rules::Ability Spellcasting::getAbility(void) const {return data->spellcasting_ability;} + void Spellcasting::setAbility(const rules::Ability& ability) {data->spellcasting_ability = ability;} + const std::vector<std::shared_ptr<SlotLevel>>& Spellcasting::getSlotLevels(void) const {return data->levels;} + void Spellcasting::addSlotLevel(void) {data->levels.push_back(std::shared_ptr<SlotLevel>(new SlotLevel()));} + vector<shared_ptr<Spell>> Spellcasting::getSpells() const { vector<shared_ptr<Spell>> ret; for(auto sl : getSlotLevels()) { diff --git a/src/spellcasting.h b/src/spellcasting.h index aa3b3e8..902f860 100644 --- a/src/spellcasting.h +++ b/src/spellcasting.h @@ -10,6 +10,7 @@ typedef nlohmann::json json; namespace entry { struct SlotLevel; + struct spellcastingImpl; void to_json(nlohmann::json& j, const SlotLevel& sl); void from_json(const nlohmann::json& j, SlotLevel& sl); @@ -28,20 +29,18 @@ namespace entry { class Spellcasting : public Feature { public: - Spellcasting() : Feature("spellcasting", "spells", ""), innate(false), spellcasting_ability("int") {} - bool isInnate(void) const {return innate;} - rules::Ability getAbility(void) const {return spellcasting_ability;} - void setAbility(const rules::Ability& ability) {spellcasting_ability = ability;} - const std::vector<std::shared_ptr<SlotLevel>>& getSlotLevels(void) const {return levels;} - void addSlotLevel(void) {levels.push_back(std::shared_ptr<SlotLevel>(new SlotLevel()));} + Spellcasting(); + bool isInnate(void) const; + rules::Ability getAbility(void) const; + void setAbility(const rules::Ability& ability); + const std::vector<std::shared_ptr<SlotLevel>>& getSlotLevels(void) const; + void addSlotLevel(void); std::vector<std::shared_ptr<Spell>> getSpells(void) const; virtual std::string getText(const creature::Creature& c) const; - NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE(Feature, Spellcasting, innate, spellcasting_ability, levels); + NLOHMANN_FRIEND_DECLARES(Spellcasting); private: - bool innate; - rules::Ability spellcasting_ability; - std::vector<std::shared_ptr<SlotLevel>> levels; + std::shared_ptr<spellcastingImpl> data; }; } diff --git a/src/weapon.cc b/src/weapon.cc index 0c4dc29..053dbc1 100644 --- a/src/weapon.cc +++ b/src/weapon.cc @@ -9,6 +9,30 @@ using namespace std; namespace entry { + struct weaponImpl { + std::vector<Damage> damage; + std::set<std::string> properties; + std::string weapon_type; + std::pair<int, int> range; + int reach; + int cost; + double weight; + }; + + NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(weaponImpl, damage, properties, weapon_type, range, reach, cost, weight); + + NLOHMANN_FRIEND_DEFS(Item, Weapon, data); + + Weapon::Weapon() : data(new weaponImpl()) {} + + std::vector<Damage> Weapon::getDamage(void) const {return data->damage;} + std::set<std::string> Weapon::getProperties(void) const {return data->properties;} + std::string Weapon::getWeaponType(void) const {return data->weapon_type;} + std::pair<int, int> Weapon::getRange(void) const {return data->range;} + int Weapon::getReach(void) const {return data->reach;} + int Weapon::getCost(void) const {return data->cost;} + double Weapon::getWeight(void) const {return data->weight;} + string getTextHelper(const Weapon& w, string toHitBonus, string damageBonus) { stringstream text; text << "+" << toHitBonus << " to hit, "; diff --git a/src/weapon.h b/src/weapon.h index da6700c..707edd9 100644 --- a/src/weapon.h +++ b/src/weapon.h @@ -11,6 +11,7 @@ namespace creature { namespace entry { class Weapon; class Damage; + struct weaponImpl; std::vector<rules::Ability> getAbilityOptions(const Weapon& w); std::vector<Damage> rollDmg(const Weapon& w, bool versatile=false); @@ -27,26 +28,21 @@ namespace entry { class Weapon : public Item, public Substantial { public: - std::vector<Damage> getDamage(void) const {return damage;} - std::set<std::string> getProperties(void) const {return properties;} - std::string getWeaponType(void) const {return weapon_type;} - std::pair<int, int> getRange(void) const {return range;} - int getReach(void) const {return reach;} - int getCost(void) const {return cost;} - double getWeight(void) const {return weight;} + Weapon(); + + std::vector<Damage> getDamage(void) const; + std::set<std::string> getProperties(void) const; + std::string getWeaponType(void) const; + std::pair<int, int> getRange(void) const; + int getReach(void) const; + int getCost(void) const; + double getWeight(void) const; virtual std::string getText() const override; virtual std::string getText(const creature::Creature& c) const override; - NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE(Item, Weapon, damage, properties, weapon_type, range, reach, cost, weight); - + NLOHMANN_FRIEND_DECLARES(Weapon); private: - std::vector<Damage> damage; - std::set<std::string> properties; - std::string weapon_type; - std::pair<int, int> range; - int reach; - int cost; - double weight; + std::shared_ptr<weaponImpl> data; }; } |