deserialize from toml on its way
This commit is contained in:
@@ -1,24 +1,31 @@
|
||||
#ifndef MHACHEARMOR_H
|
||||
#define MHACHEARMOR_H
|
||||
#ifndef MHACHE_ARMOR_H
|
||||
#define MHACHE_ARMOR_H
|
||||
|
||||
#include <string>
|
||||
#include <array>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
#include <toml++/toml.h>
|
||||
#include "mhache_skill.h"
|
||||
#include "mhache_tomltools.h"
|
||||
|
||||
class MHacheSkill;
|
||||
|
||||
class MHacheArmor {
|
||||
private:
|
||||
struct Piece {
|
||||
std::string _name;
|
||||
std::vector<unsigned char> _slots;
|
||||
std::vector<std::pair<MHacheSkill*, unsigned char>> _skills;
|
||||
|
||||
MHacheArmor* _armor;
|
||||
struct Skill {
|
||||
MHacheSkill* _skill;
|
||||
unsigned char _level;
|
||||
};
|
||||
|
||||
private:
|
||||
std::string _name;
|
||||
struct Piece {
|
||||
std::string _name;
|
||||
std::vector<unsigned char> _slots;
|
||||
std::vector<Skill> _skills;
|
||||
MHacheArmor* _armor;
|
||||
};
|
||||
|
||||
std::string _name;
|
||||
std::array<char, 5> _resistances;
|
||||
|
||||
Piece _head;
|
||||
@@ -27,8 +34,33 @@ private:
|
||||
Piece _waist;
|
||||
Piece _legs;
|
||||
|
||||
friend class MHacheTomlTools;
|
||||
|
||||
private:
|
||||
MHacheArmor() = default;
|
||||
|
||||
MHacheArmor(const MHacheArmor&) = delete;
|
||||
MHacheArmor(MHacheArmor&&) = delete;
|
||||
MHacheArmor& operator=(const MHacheArmor&) = delete;
|
||||
MHacheArmor& operator=(MHacheArmor&&) = delete;
|
||||
|
||||
public:
|
||||
static MHacheArmor* BuildFromTOML(const toml::table& armor);
|
||||
~MHacheArmor() = default;
|
||||
|
||||
bool operator < (const MHacheArmor&) const;
|
||||
bool operator == (const MHacheArmor&) const;
|
||||
|
||||
const std::string& name() const;
|
||||
};
|
||||
|
||||
// Gives std::hash namespace a new template definition for our type:
|
||||
template<>
|
||||
struct std::hash<MHacheArmor>
|
||||
{
|
||||
std::size_t operator()(const MHacheArmor& armor) const noexcept
|
||||
{
|
||||
return std::hash<std::string>{}(armor.name());
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef MHACHERESEARCHER_H
|
||||
#define MHACHERESEARCHER_H
|
||||
#ifndef MHACHE_RESEARCHER_H
|
||||
#define MHACHE_RESEARCHER_H
|
||||
|
||||
#include <set>
|
||||
#include <unordered_set>
|
||||
#include <memory>
|
||||
#include <filesystem>
|
||||
|
||||
@@ -10,8 +10,47 @@
|
||||
|
||||
class MHacheResearcher {
|
||||
private:
|
||||
std::set<std::unique_ptr<MHacheSkill>> _skills;
|
||||
std::set<std::unique_ptr<MHacheArmor>> _armors;
|
||||
template <typename T>
|
||||
struct PtrEqualOperator {
|
||||
bool operator() (const T* a, const T* b) const
|
||||
{
|
||||
return ((a && b) && (*a == *b));
|
||||
}
|
||||
|
||||
bool operator() (const std::unique_ptr<T>& a, const std::unique_ptr<T>& b) const
|
||||
{
|
||||
return (a.get() == b.get());
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct PtrLessOperator {
|
||||
bool operator() (const T* a, const T* b) const
|
||||
{
|
||||
return ((a && b) && (*a < *b));
|
||||
}
|
||||
|
||||
bool operator() (const std::unique_ptr<T>& a, const std::unique_ptr<T>& b) const
|
||||
{
|
||||
return (*this)(a.get(), b.get());
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct PtrHashOperator {
|
||||
size_t operator() (const T* a) const
|
||||
{
|
||||
return (a ? std::hash<T>{}(*a) : 0);
|
||||
}
|
||||
|
||||
size_t operator() (const std::unique_ptr<T>& a) const
|
||||
{
|
||||
return (*this)(a.get());
|
||||
}
|
||||
};
|
||||
|
||||
std::unordered_set<std::unique_ptr<MHacheSkill>, PtrHashOperator<MHacheSkill>, PtrEqualOperator<MHacheSkill>> _skills;
|
||||
std::unordered_set<std::unique_ptr<MHacheArmor>, PtrHashOperator<MHacheArmor>, PtrEqualOperator<MHacheArmor>> _armors;
|
||||
|
||||
private:
|
||||
MHacheResearcher() = default;
|
||||
|
||||
@@ -1,13 +1,45 @@
|
||||
#ifndef MHACHESKILL
|
||||
#define MHACHESKILL
|
||||
#ifndef MHACHE_SKILL_H
|
||||
#define MHACHE_SKILL_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
#include "mhache_tomltools.h"
|
||||
|
||||
class MHacheSkill {
|
||||
private:
|
||||
std::string _name;
|
||||
unsigned char _level;
|
||||
const unsigned char _levels;
|
||||
std::string _name;
|
||||
std::string _description;
|
||||
|
||||
std::vector<std::string> _levels;
|
||||
|
||||
private:
|
||||
MHacheSkill() = delete;
|
||||
MHacheSkill(const MHacheSkill&) = delete;
|
||||
MHacheSkill(MHacheSkill&&) = delete;
|
||||
MHacheSkill& operator=(MHacheSkill&) = delete;
|
||||
MHacheSkill& operator=(MHacheSkill&&) = delete;
|
||||
|
||||
public:
|
||||
MHacheSkill(const std::string& name, const std::string& description, const std::vector<std::string>& levels);
|
||||
~MHacheSkill() = default;
|
||||
|
||||
bool operator < (const MHacheSkill&) const;
|
||||
bool operator == (const MHacheSkill&) const;
|
||||
|
||||
const std::string& name() const;
|
||||
const std::string& description() const;
|
||||
const std::vector<std::string>& levels() const;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct std::hash<MHacheSkill>
|
||||
{
|
||||
std::size_t operator()(const MHacheSkill& skill) const noexcept
|
||||
{
|
||||
return std::hash<std::string>{}(skill.name());
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
32
include/mhache_tomltools.h
Normal file
32
include/mhache_tomltools.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef MHACHE_TOMLTOOLS_H
|
||||
#define MHACHE_TOMLTOOLS_H
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <filesystem>
|
||||
|
||||
#include <toml++/toml.h>
|
||||
|
||||
#include "mhache_skill.h"
|
||||
#include "mhache_armor.h"
|
||||
|
||||
class MHacheSkill;
|
||||
class MHacheArmor;
|
||||
|
||||
/*
|
||||
* Pure static class of services.
|
||||
*
|
||||
*/
|
||||
class MHacheTomlTools {
|
||||
private:
|
||||
MHacheTomlTools() = delete;
|
||||
~MHacheTomlTools() = delete;
|
||||
|
||||
public:
|
||||
static toml::table parse_root_table(const std::filesystem::path& file_dot_toml);
|
||||
|
||||
static std::unique_ptr<MHacheSkill> make_skill(const toml::const_table_iterator::value_type& skill);
|
||||
static std::unique_ptr<MHacheArmor> make_armor(const toml::const_table_iterator::value_type& armor);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef MHACHEWINDOW_H
|
||||
#define MHACHEWINDOW_H
|
||||
#ifndef MHACHE_WINDOW_H
|
||||
#define MHACHE_WINDOW_H
|
||||
|
||||
#include <csignal>
|
||||
#include <GLFW/glfw3.h>
|
||||
@@ -22,7 +22,6 @@ private:
|
||||
|
||||
private:
|
||||
static void callback_signals(int signal);
|
||||
|
||||
static void callback_glfw_error(int error, const char* description);
|
||||
static void callback_glfw_keyboard(GLFWwindow* window, int key, int scancode, int action, int mods);
|
||||
|
||||
|
||||
@@ -3,10 +3,11 @@ find_package(OpenGL REQUIRED)
|
||||
add_executable(
|
||||
${PROJECT_NAME}
|
||||
main.cpp
|
||||
mhache_armor.cpp
|
||||
mhache_skill.cpp
|
||||
mhache_window.cpp
|
||||
mhache_armor.cpp
|
||||
mhache_tomltools.cpp
|
||||
mhache_researcher.cpp
|
||||
mhache_window.cpp
|
||||
)
|
||||
|
||||
target_compile_options(
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#include "mhache_armor.h"
|
||||
|
||||
bool
|
||||
MHacheArmor::operator< (const MHacheArmor& other) const
|
||||
{
|
||||
return _name < other._name;
|
||||
}
|
||||
|
||||
const std::string&
|
||||
MHacheArmor::name() const
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <filesystem>
|
||||
|
||||
#include <toml++/toml.h>
|
||||
#include "mhache_tomltools.h"
|
||||
|
||||
MHacheResearcher& MHacheResearcher::instance()
|
||||
{
|
||||
@@ -15,37 +16,38 @@ MHacheResearcher& MHacheResearcher::instance()
|
||||
bool
|
||||
MHacheResearcher::load_armors(const std::filesystem::path& armors_dot_toml)
|
||||
{
|
||||
toml::parse_result result = toml::parse_file(armors_dot_toml.u8string());
|
||||
|
||||
if (!result) {
|
||||
std::cerr << "Parsing of TOML file" << result.error().source().path << " failed." << std::endl;
|
||||
std::cerr << "-> " << result.error().description() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// do something
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
MHacheResearcher::load_skills(const std::filesystem::path& skills_dot_toml)
|
||||
{
|
||||
toml::parse_result result = toml::parse_file(skills_dot_toml.u8string());
|
||||
|
||||
if (!result) {
|
||||
std::cerr << "Parsing of TOML file" << result.error().source().path << " failed." << std::endl;
|
||||
std::cerr << "-> " << result.error().description() << std::endl;
|
||||
const toml::table table_root = MHacheTomlTools::parse_root_table(skills_dot_toml);
|
||||
if (0 == table_root.size())
|
||||
return false;
|
||||
}
|
||||
|
||||
const toml::table root = std::move(result.table());
|
||||
const auto node_skills = table_root["skills"];
|
||||
if (!(node_skills.is_homogeneous() && node_skills.is_table()))
|
||||
return false;
|
||||
|
||||
const auto nv_skills = root["skills"];
|
||||
if (!(nv_skills.is_table() && nv_skills.is_homogeneous()))
|
||||
const toml::table* table_skills = node_skills.as_table();
|
||||
if (!table_skills)
|
||||
return false;
|
||||
|
||||
const toml::table& skills = *(nv_skills.as_table());
|
||||
for (const auto& it_skill: *table_skills) {
|
||||
std::unique_ptr<MHacheSkill> mhache_skill = MHacheTomlTools::make_skill(it_skill);
|
||||
|
||||
skills.for_each([](const toml::key& skill_name, const toml::table& skill){
|
||||
std::cerr << skill_name << ": " << skill["description"] << std::endl;
|
||||
});
|
||||
if (mhache_skill)
|
||||
_skills.insert(std::move(mhache_skill));
|
||||
}
|
||||
|
||||
for (const auto& s: _skills) {
|
||||
std::cerr << s->name() << ": " << s->description() << std::endl;
|
||||
|
||||
for (const auto& l: s->levels()) {
|
||||
std::cerr << " --| " << l << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1 +1,37 @@
|
||||
#include "mhache_skill.h"
|
||||
#include "mhache_skill.h"
|
||||
|
||||
MHacheSkill::MHacheSkill(const std::string& name, const std::string& description, const std::vector<std::string>& levels)
|
||||
: _name(name)
|
||||
, _description(description)
|
||||
, _levels(levels)
|
||||
{}
|
||||
|
||||
bool
|
||||
MHacheSkill::operator< (const MHacheSkill& other) const
|
||||
{
|
||||
return _name < other._name;
|
||||
}
|
||||
|
||||
bool
|
||||
MHacheSkill::operator== (const MHacheSkill& other) const
|
||||
{
|
||||
return _name == other._name;
|
||||
}
|
||||
|
||||
const std::string&
|
||||
MHacheSkill::name() const
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
|
||||
const std::string&
|
||||
MHacheSkill::description() const
|
||||
{
|
||||
return _description;
|
||||
}
|
||||
|
||||
const std::vector<std::string>&
|
||||
MHacheSkill::levels() const
|
||||
{
|
||||
return _levels;
|
||||
}
|
||||
|
||||
56
src/mhache_tomltools.cpp
Normal file
56
src/mhache_tomltools.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "mhache_tomltools.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::unique_ptr<MHacheSkill>
|
||||
MHacheTomlTools::make_skill(const toml::const_table_iterator::value_type& skill)
|
||||
{
|
||||
const toml::node& node = skill.second;
|
||||
|
||||
const toml::table* members = node.as_table();
|
||||
if (!members)
|
||||
return nullptr;
|
||||
|
||||
const auto description = members->get_as<std::string>("description");
|
||||
if (!description)
|
||||
return nullptr;
|
||||
|
||||
const auto levels = members->get_as<toml::array>("levels");
|
||||
if (!levels)
|
||||
return nullptr;
|
||||
|
||||
std::vector<std::string> skill_levels;
|
||||
|
||||
for (const auto& level: *levels) {
|
||||
const auto level_description = level.as_string();
|
||||
|
||||
if (level_description)
|
||||
skill_levels.push_back(level_description->get());
|
||||
}
|
||||
|
||||
if (skill_levels.empty())
|
||||
return nullptr;
|
||||
|
||||
return std::make_unique<MHacheSkill>(std::string(skill.first.str()), description->get(), skill_levels);
|
||||
}
|
||||
|
||||
std::unique_ptr<MHacheArmor>
|
||||
MHacheTomlTools::make_armor(const toml::const_table_iterator::value_type& armor)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
toml::table
|
||||
MHacheTomlTools::parse_root_table(const std::filesystem::path& file_dot_toml)
|
||||
{
|
||||
const toml::parse_result result = toml::parse_file(file_dot_toml.u8string());
|
||||
|
||||
if (!result) {
|
||||
std::cerr << "Parsing of TOML file << " << file_dot_toml << " failed." << std::endl;
|
||||
std::cerr << "-> " << result.error().description() << std::endl;
|
||||
}
|
||||
|
||||
return std::move(result.table());
|
||||
}
|
||||
Reference in New Issue
Block a user