extend Element API

This commit is contained in:
Alexey Fedoseev
2024-05-09 16:46:15 +03:00
parent 8938d86717
commit 510a3ddea9
2 changed files with 57 additions and 2 deletions

View File

@@ -60,6 +60,17 @@ Element::Element(Element* _parent, ElementType _type, const ID& _id, const Name&
set_name(_name);
}
int Element::index() const
{
if (parent) {
const ElementCollection* collection = static_cast<const ElementCollection*>(parent);
CYB_ASSERT(collection);
return collection->element_index(this);
} else {
return 0;
}
}
void Element::set_name(const Name& n)
{
name = n;
@@ -674,7 +685,7 @@ ElementList ElementCollection::find_elements_by_types(const ElementTypes& types)
size_t ElementCollection::elements_count() const
{
size_t count = 0;
size_t count = 1;
for (ElementList::const_iterator i = children.begin(); i != children.end(); i++) {
count += (*i)->elements_count();
}
@@ -691,6 +702,18 @@ bool ElementCollection::has_initial() const
return false;
}
int ElementCollection::element_index(const Element* e) const
{
CYB_ASSERT(e);
int index = 0;
for (ElementList::const_iterator i = children.begin(); i != children.end(); i++, index++) {
if (*i == e) {
return index;
}
}
return -1;
}
void ElementCollection::add_element(Element* e)
{
CYB_ASSERT(e);
@@ -788,6 +811,32 @@ Element* ElementCollection::first_element()
}
}
const Element* ElementCollection::get_element(int index) const
{
int idx = 0;
if (has_children()) {
for (ElementList::const_iterator i = children.begin(); i != children.end(); i++, idx++) {
if (idx == index) {
return *i;
}
}
}
return NULL;
}
Element* ElementCollection::get_element(int index)
{
int idx = 0;
if (has_children()) {
for (ElementList::iterator i = children.begin(); i != children.end(); i++, idx++) {
if (idx == index) {
return *i;
}
}
}
return NULL;
}
CyberiadaNode* ElementCollection::to_node() const
{
CyberiadaNode* node = Element::to_node();

View File

@@ -81,9 +81,12 @@ namespace Cyberiada {
QualifiedName qualified_name() const;
bool is_root() const { return !parent; }
const Element* get_parent() const { return parent; }
Element* get_parent() { return parent; }
virtual bool has_children() const { return false; }
virtual size_t children_count() const { return 0; }
virtual size_t elements_count() const { return 1; }
virtual int index() const;
virtual bool has_geometry() const = 0;
@@ -280,6 +283,8 @@ namespace Cyberiada {
virtual size_t elements_count() const;
const Element* first_element() const;
Element* first_element();
const Element* get_element(int index) const;
Element* get_element(int index);
const Element* find_element_by_id(const ID& id) const;
Element* find_element_by_id(const ID& id);
ConstElementList find_elements_by_type(ElementType type) const;
@@ -287,6 +292,7 @@ namespace Cyberiada {
ElementList find_elements_by_type(ElementType type);
ElementList find_elements_by_types(const ElementTypes& types);
bool has_initial() const;
virtual int element_index(const Element* e) const;
virtual void add_element(Element* e);
virtual void add_first_element(Element* e);