schema progress

This commit is contained in:
Sascha Kühl
2025-02-04 15:19:01 +01:00
parent 6a5974337d
commit f01e9ff87f
17 changed files with 416 additions and 276 deletions
+17
View File
@@ -0,0 +1,17 @@
#ifndef OBJECT_PTR_HPP
#define OBJECT_PTR_HPP
namespace matador::object {
template <typename Type>
class object_ptr {
public:
Type* operator->() { return ptr; };
Type& operator*() { return *ptr; };
private:
Type* ptr{nullptr};
};
}
#endif //OBJECT_PTR_HPP
+38 -10
View File
@@ -18,7 +18,10 @@ class schema {
public:
typedef const_schema_node_iterator const_iterator; /**< Shortcut for the list const iterator. */
schema();
/**
* Creates an empty schema
*/
explicit schema( const std::string& name = "");
template <typename Type>
utils::result<void, utils::error> attach(const std::string name, const std::string &parent = "") {
@@ -36,6 +39,11 @@ public:
utils::result<void, utils::error> attach(const std::string name) {
auto node = schema_node::make_node<Type>(*this, name);
auto result = attach_node(node, std::type_index(typeid(ParentType)));
if (!result) {
return utils::failure(result.err());
}
return utils::ok<void>();
}
@@ -53,26 +61,46 @@ public:
*/
[[nodiscard]] const_iterator end() const;
/**
* Returns true if the schema contains
* no schema nodes.
*
* @return True if schema is empty
*/
[[nodiscard]] bool empty() const;
/**
* Returns the current number of schema node.
*
* @return Number of schema nodes
*/
[[nodiscard]] size_t size() const;
/**
* Returns the name of the schema.
*
* @return The name of the schema
*/
[[nodiscard]] std::string name() 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>;
using t_node_map = std::unordered_map<std::string, node_ptr>;
using t_type_index_node_map = std::unordered_map<std::type_index, node_ptr>;
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;
[[nodiscard]] utils::result<std::shared_ptr<schema_node>, utils::error> attach_node(const std::shared_ptr<schema_node> &node,
const std::string &parent);
[[nodiscard]] utils::result<std::shared_ptr<schema_node>, utils::error> attach_node(const std::shared_ptr<schema_node> &node,
const std::type_index &type_index);
[[nodiscard]] utils::result<std::shared_ptr<schema_node>, utils::error> find_node(const std::string &name) const;
[[nodiscard]] utils::result<std::shared_ptr<schema_node>, utils::error> find_node(const std::type_index &type_index) const;
void push_back_child(const node_ptr &parent, const node_ptr &child);
[[nodiscard]] bool has_node(const std::type_index& index, const std::string &name) const;
bool has_node(const std::type_index& index, const std::string &name) const;
static void push_back_child(const node_ptr &parent, const node_ptr &child);
private:
std::string name_;
std::shared_ptr<schema_node> root_;
t_node_map node_map_;
+5
View File
@@ -12,6 +12,8 @@ class schema;
class schema_node final {
public:
using node_ptr = std::shared_ptr<schema_node>;
template < typename Type >
static std::shared_ptr<schema_node> make_node(schema& tree, const std::string& name) {
return std::shared_ptr<schema_node>(new schema_node(tree, name, static_cast<Type*>(nullptr)));
@@ -41,6 +43,9 @@ public:
*/
void insert(const std::shared_ptr<schema_node> &child);
[[nodiscard]] node_ptr next() const;
[[nodiscard]] node_ptr prev() const;
private:
explicit schema_node(schema& tree);
template < typename Type >
@@ -81,7 +81,7 @@ public:
*
* @return Returns iterator before incrementing.
*/
const_schema_node_iterator operator++(int);
const_schema_node_iterator operator++( int );
/**
* Pre increments the iterator