query/source/core/object/schema_node.cpp

71 lines
2.4 KiB
C++

#include <utility>
#include "matador/object/schema_node.hpp"
namespace matador::object {
schema_node::schema_node(schema &tree)
: schema_(tree)
, info_(std::make_unique<null_info>(*this, object_definition{})) {
}
schema_node::schema_node(schema &tree, std::string name, std::unique_ptr<basic_object_info> &&info)
: schema_(tree)
, info_(std::move(info))
// , info_(std::make_unique<object_info<Type>>(*this, object_definition(attribute_definition_generator::generate<Type>(schema_))))
, first_child_(std::shared_ptr<schema_node>(new schema_node(tree)))
, last_child_(std::shared_ptr<schema_node>(new schema_node(tree)))
, name_(std::move(name)) {
first_child_->next_sibling_ = last_child_;
last_child_->previous_sibling_ = first_child_;
}
std::shared_ptr<schema_node> schema_node::make_node(schema &tree, const std::string &name, std::unique_ptr<basic_object_info> &&info) {
return std::shared_ptr<schema_node>(new schema_node(tree, name, std::move(info)));
}
std::string schema_node::name() const {
return name_;
}
std::type_index schema_node::type_index() const {
return info_->type_index();
}
const basic_object_info &schema_node::basic_info() const {
return *info_;
}
schema_node::node_ptr schema_node::next() const {
// if we have a child, child is the next iterator to return
// (if we don't do iterate over the siblings)
if (first_child_ && first_child_->next_sibling_ != last_child_) {
return first_child_->next_sibling_;
}
// if there is no child, we check for sibling
// if there is a sibling, this is our next iterator to return
// if not, we go back to the parent
auto *node = this;
while (node->parent_ && node->next_sibling_ == node->parent_->last_child_) {
node = node->parent_.get();
}
return node->parent_ ? node->next_sibling_ : node->last_child_;
}
schema_node::node_ptr schema_node::prev() const {
// if node has a previous sibling, we set it
// as our next iterator. then we check if there
// are last children. if so, we set the last-last
// child as our iterator
if (previous_sibling_ && previous_sibling_->previous_sibling_) {
auto node = previous_sibling_;
while (node->last_child_ && node->first_child_->next_sibling_ != node->last_child_) {
node = node->last_child_->previous_sibling_;
}
return node;
}
// if there is no previous sibling, our next iterator
// is the parent of the node
return parent_->parent_ ? parent_ : parent_->first_child_->next_sibling_;
}
}