Hello, I have a SceneGraph consisting of a Node:
class Node { public: ... GLvoid addChild(Node* child) noexcept; GLboolean isExistChildren() const noexcept; GLvoid childrenForEach(std::function<GLvoid(Node* child)> callback) noexcept; ... private: Node* mChild = nullptr; Node* mNextNode = nullptr; } GLvoid Node::addChild(Node* child) noexcept { child->mNextNode = mChild; mChild = child; } GLboolean Node::isExistChildren() const noexcept { return mChild != nullptr; } GLvoid Node::childrenForEach(std::function<GLvoid(Node* child)> callback) noexcept { Node* iterator = mChild; while (iterator) { callback(iterator); iterator = iterator->mNextNode; } }
At the moment, the detour is done as follows, and only ha is one level of the graph from the root:
Node* mRootNode = ...; if (mRootNode->isExistChildren()) { mRootNode->childrenForEach([&shader](Node* child) { ... }); }
I would like to write my custom iterator for this, so I don’t want to use callbacks, but I don’t know how to do without reference to the parent node (I don’t want to use extra memory for each node pointer) ... Can someone tell me how best to implement a workaround with minimum memory usage and greatest performance in this case?
Node *
in a class with a stl-like interface and you will have a full iterator ... Shl: judging by the structure, this is not a graph, but a tree ... - Fat-Zer