initial matador ng commit
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
#ifndef BASIC_PROTOTYPE_INFO_HPP
|
||||
#define BASIC_PROTOTYPE_INFO_HPP
|
||||
|
||||
#include <typeindex>
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
class schema_node;
|
||||
|
||||
class basic_object_info {
|
||||
public:
|
||||
virtual ~basic_object_info() = default;
|
||||
|
||||
[[nodiscard]] std::type_index type_index() const;
|
||||
|
||||
protected:
|
||||
basic_object_info(schema_node &node, std::type_index type_index);
|
||||
|
||||
protected:
|
||||
schema_node &node_; /**< prototype node of the represented object type */
|
||||
std::type_index type_index_; /**< type index of the represented object type */
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
class object_info final : public basic_object_info {
|
||||
public:
|
||||
explicit object_info(schema_node &node)
|
||||
: basic_object_info(node, typeid(Type)) {}
|
||||
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
struct null_type {};
|
||||
}
|
||||
|
||||
using null_info = object_info<detail::null_type>;
|
||||
|
||||
}
|
||||
|
||||
#endif //BASIC_PROTOTYPE_INFO_HPP
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef OBJECT_ERROR_CODE_HPP
|
||||
#define OBJECT_ERROR_CODE_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <system_error>
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
enum class error_code : uint8_t {
|
||||
OK = 0,
|
||||
NodeNotFound = 1,
|
||||
NodeAlreadyExists = 2,
|
||||
Failure,
|
||||
};
|
||||
|
||||
class object_category_impl final : public std::error_category
|
||||
{
|
||||
public:
|
||||
[[nodiscard]] const char* name() const noexcept override;
|
||||
[[nodiscard]] std::string message(int ev) const override;
|
||||
};
|
||||
|
||||
const std::error_category& object_category();
|
||||
std::error_code make_error_code(error_code e);
|
||||
std::error_condition make_error_condition(error_code e);
|
||||
|
||||
}
|
||||
|
||||
template <>
|
||||
struct std::is_error_code_enum<matador::object::error_code> : true_type {};
|
||||
|
||||
#endif //OBJECT_ERROR_CODE_HPP
|
||||
@@ -0,0 +1,63 @@
|
||||
#ifndef SCHEMA_HPP
|
||||
#define SCHEMA_HPP
|
||||
|
||||
#include "matador/object/error_code.hpp"
|
||||
#include "matador/object/schema_node.hpp"
|
||||
|
||||
#include "matador/utils/result.hpp"
|
||||
#include "matador/utils/error.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
class schema {
|
||||
public:
|
||||
schema();
|
||||
|
||||
template <typename Type>
|
||||
utils::result<void, utils::error> attach(const std::string name, const std::string &parent = "") {
|
||||
auto node = schema_node::make_node<Type>(*this, name);
|
||||
|
||||
attach_node(node, parent);
|
||||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
template <typename Type, typename ParentType>
|
||||
utils::result<void, utils::error> attach(const std::string name) {
|
||||
// auto node = std::make_unique<schema_node>(*this);
|
||||
|
||||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
[[nodiscard]] bool empty() const;
|
||||
|
||||
[[nodiscard]] size_t size() const;
|
||||
|
||||
private:
|
||||
using t_node_map = std::unordered_map<std::string, std::shared_ptr<schema_node>>;
|
||||
// type_index -> [name -> prototype]
|
||||
using t_type_index_node_map = std::unordered_map<std::type_index, t_node_map>;
|
||||
using node_ptr = std::shared_ptr<schema_node>;
|
||||
|
||||
utils::result<std::shared_ptr<schema_node>, utils::error> attach_node(const std::shared_ptr<schema_node> &node,
|
||||
const std::string &parent);
|
||||
utils::result<std::shared_ptr<schema_node>, utils::error> find_parent(const std::string &name) const;
|
||||
utils::result<std::shared_ptr<schema_node>, utils::error> find_node(const std::string &name) const;
|
||||
|
||||
utils::result<std::shared_ptr<schema_node>, utils::error> push_back_child(const node_ptr &parent, const node_ptr &child);
|
||||
|
||||
bool has_node(const std::type_index& index, const std::string &name) const;
|
||||
|
||||
private:
|
||||
std::shared_ptr<schema_node> root_;
|
||||
|
||||
t_node_map node_map_;
|
||||
t_type_index_node_map type_index_node_map_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //SCHEMA_HPP
|
||||
@@ -0,0 +1,75 @@
|
||||
#ifndef SCHEMA_NODE_HPP
|
||||
#define SCHEMA_NODE_HPP
|
||||
|
||||
#include "matador/object/basic_object_info.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
class schema;
|
||||
|
||||
class schema_node final {
|
||||
public:
|
||||
template < typename Type >
|
||||
static std::shared_ptr<schema_node> make_node(schema& tree, const std::string& name) {
|
||||
return std::make_shared<schema_node>(tree, name, static_cast<Type*>(nullptr));
|
||||
}
|
||||
|
||||
schema_node(const schema_node& other) = delete;
|
||||
schema_node(schema_node&& other) = default;
|
||||
schema_node& operator=(const schema_node& other) = delete;
|
||||
schema_node& operator=(schema_node&& other) = delete;
|
||||
~schema_node() = default;
|
||||
|
||||
[[nodiscard]] std::string name() const;
|
||||
[[nodiscard]] std::type_index type_index() const;
|
||||
|
||||
/**
|
||||
* Appends the given prototype node as a sibling
|
||||
* on the same level.
|
||||
*
|
||||
* @param sibling The new sibling node.
|
||||
*/
|
||||
void append(const std::shared_ptr<schema_node> &sibling);
|
||||
|
||||
/**
|
||||
* Inserts the given node to the list of children.
|
||||
*
|
||||
* @param child The child node to add.
|
||||
*/
|
||||
void insert(const std::shared_ptr<schema_node> &child);
|
||||
|
||||
private:
|
||||
explicit schema_node(schema& tree);
|
||||
template < typename Type >
|
||||
schema_node(schema& tree, std::string name, Type *obj)
|
||||
: schema_(tree)
|
||||
, info_(std::make_unique<object_info<Type>>(*this))
|
||||
, first_child_(std::make_shared<schema_node>(tree))
|
||||
, last_child_(std::make_shared<schema_node>(tree))
|
||||
, name_(std::move(name)) {
|
||||
first_child_->next_sibling_ = last_child_;
|
||||
last_child_->previous_sibling_ = first_child_;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
friend schema;
|
||||
|
||||
schema &schema_;
|
||||
std::unique_ptr<basic_object_info> info_;
|
||||
|
||||
std::shared_ptr<schema_node> parent_;
|
||||
std::shared_ptr<schema_node> previous_sibling_;
|
||||
std::shared_ptr<schema_node> next_sibling_;
|
||||
std::shared_ptr<schema_node> first_child_;
|
||||
std::shared_ptr<schema_node> last_child_;
|
||||
|
||||
std::string name_;
|
||||
size_t depth_{0};
|
||||
};
|
||||
|
||||
}
|
||||
#endif //SCHEMA_NODE_HPP
|
||||
Reference in New Issue
Block a user