Double visitor?

This commit is contained in:
Vincent Stumpf 2023-06-10 06:41:25 +00:00
parent 28383ee332
commit 95ee5d04e6
6 changed files with 66 additions and 23 deletions

View File

@ -1,15 +1,41 @@
#include "skill.hpp" #include "skill.hpp"
#include "skills.hpp" #include "skills.hpp"
#include "swordsman/bash.hpp" // #include "swordsman/bash.hpp"
#include "swordsman/provoke.hpp" // #include "swordsman/provoke.hpp"
#include "skilllist.hpp"
int main() { int main() {
Bash bash;
Provoke provoke;
bash.castend_damage_id(); constexpr int skill_id = SM_BASH;
provoke.castend_nodamage_id();
const auto &sk = skill_db.at(static_cast<e_skill>(skill_id));
const SkillImpl sk2 = Bash{};
std::visit([](auto &skill) {
skill.getSkillID();
skill.castend_damage_id(); // error
// skill.hpp:19:26: error: const class Provoke has no member named castendDamageId; did you mean castendNoDamageId?
// 19 | return as_underlying().castendDamageId();
// | ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
// | castendNoDamageId
}, sk2);
for (auto &it : skill_db) {
std::visit([](auto &skill) {
skill.getSkillID();
// skill.castend_damage_id();
}, it.second);
}
return 0; return 0;
} }

View File

@ -15,16 +15,20 @@ constexpr int MAX_SKILL_LEVEL = 13;
template <typename T> template <typename T>
class Skill { class Skill {
public: public:
int castend_damage_id() { int castend_damage_id() const {
return as_underlying().castend_damage_id(); return as_underlying().castendDamageId();
}; };
int castend_nodamage_id() { int castend_nodamage_id() const {
return as_underlying().castend_nodamage_id(); return as_underlying().castendNoDamageId();
}; };
int castend_pos2() { int castend_pos2() const {
return as_underlying().castend_pos2(); return as_underlying().castendPos2();
}; };
uint16_t getSkillID() const {
return nameid;
}
protected: protected:
explicit Skill(e_skill skillid) : nameid(static_cast<uint16_t>(skillid)) {}; explicit Skill(e_skill skillid) : nameid(static_cast<uint16_t>(skillid)) {};
private: private:
@ -38,6 +42,10 @@ private:
inline T& as_underlying() { inline T& as_underlying() {
return static_cast<T&>(*this); return static_cast<T&>(*this);
} }
inline const T& as_underlying() const {
return static_cast<const T&>(*this);
}
}; };

View File

@ -0,0 +1,15 @@
#include <variant>
#include "skill.hpp"
#include "skills.hpp"
#include "swordsman/bash.hpp"
#include "swordsman/provoke.hpp"
using SkillImpl = std::variant<Bash, Provoke>;
std::unordered_map<e_skill, SkillImpl> skill_db = {
{ e_skill::SM_BASH, Bash{} },
{ e_skill::SM_PROVOKE, Provoke{} }
};

View File

@ -5,13 +5,7 @@
#define MAP_SKILLS_HPP #define MAP_SKILLS_HPP
#include <unordered_map> #include <unordered_map>
#include "skill.hpp"
enum e_skill;
template <typename T>
class Skill;
inline std::unordered_map<e_skill, Skill> skills;
/// List of Skills /// List of Skills
enum e_skill { enum e_skill {

View File

@ -5,9 +5,9 @@
#include "../skills.hpp" #include "../skills.hpp"
class Bash : Skill<Bash> { class Bash : public Skill<Bash> {
public: public:
int castend_damage_id() { int castendDamageId() const {
return 0; return 0;
}; };

View File

@ -4,9 +4,9 @@
#include "../skill.hpp" #include "../skill.hpp"
class Provoke : Skill<Provoke> { class Provoke : public Skill<Provoke> {
public: public:
int castend_nodamage_id() { int castendNoDamageId() const {
return 0; return 0;
}; };