From 510a3ddea9ca77a72005efbcade1bab5c329b395 Mon Sep 17 00:00:00 2001 From: Alexey Fedoseev Date: Thu, 9 May 2024 16:46:15 +0300 Subject: [PATCH] extend Element API --- cyberiadamlpp.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++- cyberiadamlpp.h | 8 +++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/cyberiadamlpp.cpp b/cyberiadamlpp.cpp index 2c8943c..8ac2989 100644 --- a/cyberiadamlpp.cpp +++ b/cyberiadamlpp.cpp @@ -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(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(); diff --git a/cyberiadamlpp.h b/cyberiadamlpp.h index 1ac999a..f8237fc 100644 --- a/cyberiadamlpp.h +++ b/cyberiadamlpp.h @@ -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); @@ -439,7 +445,7 @@ namespace Cyberiada { const std::list& get_actions() const { return actions; } std::list& get_actions() { return actions; } void add_action(const Action& a); - + virtual CyberiadaNode* to_node() const; protected: