Code: Select all
GraphIterator specialNode = std::find_if(GraphIterator(myScene), GraphIterator(), &isSpecial);
Code: Select all
GraphIterator specialNode = std::find_if(GraphIterator(myScene), GraphIterator(), &isSpecial);
Code: Select all
Index: irrList.h
===================================================================
--- irrList.h (revision 1815)
+++ irrList.h (working copy)
@@ -114,8 +114,8 @@
bool operator ==(const Iterator& other) const { return Current == other.Current; }
bool operator !=(const Iterator& other) const { return Current != other.Current; }
- const T & operator * () { return Current->Element; }
- const T * operator ->() { return &Current->Element; }
+ const T & operator * () const { return Current->Element; }
+ const T * operator ->() const { return &Current->Element; }
ConstIterator & operator =(const Iterator & iterator) { Current = iterator.Current; return *this; }
Code: Select all
#include "irrlicht.h"
#include "boost/iterator/iterator_facade.hpp"
#include <stack>
class SceneIterator : public boost::iterator_facade
< SceneIterator
, irr::scene::ISceneNode*
, boost::forward_traversal_tag
, irr::scene::ISceneNode*
>
{
public:
SceneIterator() {}
explicit SceneIterator(irr::scene::ISceneManager* scene) {
beginChildren(scene->getRootSceneNode());
}
// Non-standard iterator member, handy for pretty printing.
std::size_t depth() const {
return iterStack.size();
}
private: // functions for iterator_facade:
friend class boost::iterator_core_access;
void increment() {
// preorder depth first traversal (i.e. parent first)
if (!beginChildren(*iterStack.top())) {
nextSibling();
}
}
bool equal(SceneIterator const& other) const {
return iterStack == other.iterStack && currentEnd == other.currentEnd;
}
irr::scene::ISceneNode* dereference() const {
return *iterStack.top();
}
private: // tree walking implementation:
typedef irr::core::list<irr::scene::ISceneNode*>::ConstIterator ChildIter;
bool beginChildren(irr::scene::ISceneNode* parent) {
if (parent->getChildren().empty()) {
return false;
}
else {
iterStack.push(parent->getChildren().begin());
currentEnd = parent->getChildren().end();
return true;
}
}
void nextSibling() {
++iterStack.top();
while (!iterStack.empty() && iterStack.top() == currentEnd) {
iterStack.pop();
if (!iterStack.empty()) {
currentEnd = (*iterStack.top())->getParent()->getChildren().end();
++iterStack.top();
}
else {
currentEnd = ChildIter();
}
}
}
std::stack<ChildIter> iterStack;
ChildIter currentEnd;
};
Code: Select all
// default-constructed iterator is the "end".
SceneIterator end;
// pretty print the tree (using i.depth())
for (SceneIterator i=SceneIterator(sceneManager); i!=end; ++i) {
std::cout << std::string(i.depth(), '\t') << "'" << (*i)->getName() << "'" << std::endl;
}
// find the first debug node (with some boost::bind magic)
SceneIterator i = std::find_if(SceneIterator(sceneManager), end, boost::bind(&ISceneNode::isDebugObject, _1));
if (i != end) std::cout << (*i)->getName() << std::endl;