| 1 |
#ifndef IPATH_H_ |
| 2 |
#define IPATH_H_ |
| 3 |
|
| 4 |
#include "inode.h" |
| 5 |
#include <vector> |
| 6 |
|
| 7 |
namespace scene { |
| 8 |
|
| 9 |
/** greebo: This is the base structure used as unique to scenegraph elements. |
| 10 |
* |
| 11 |
* It extends the functionality of std::vector to mimick |
| 12 |
* the interface of a std::stack (push(), top(), pop()) and |
| 13 |
* provides an additional parent() method that allows |
| 14 |
* to retrieve the element right below the top() element. |
| 15 |
* |
| 16 |
* The parent() method is a convenience method helping to insert |
| 17 |
* scenegraph elements at the right place without complicated |
| 18 |
* popping() and pushing(). |
| 19 |
* |
| 20 |
* Note: Normally, deriving from STL containers is not a good idea, due to |
| 21 |
* the non-virtual destructors (all data members in the subclasses |
| 22 |
* wouldn't get destroyed). In this case it is ok to derive from |
| 23 |
* std::vector, because no additional data members are introduced. |
| 24 |
*/ |
| 25 |
class Path : |
| 26 |
public std::vector<INodePtr> |
| 27 |
{ |
| 28 |
public: |
| 29 |
// Default constructor |
| 30 |
Path() |
| 31 |
{} |
| 32 |
|
| 33 |
// Constructor taking an initial element. |
| 34 |
// The stack is starting with one top element. |
| 35 |
Path(const INodePtr& initialElement) { |
| 36 |
push(initialElement); |
| 37 |
} |
| 38 |
|
| 39 |
// Destructor |
| 40 |
virtual ~Path() |
| 41 |
{} |
| 42 |
|
| 43 |
// Accessor method to retrieve the last element inserted. |
| 44 |
INodePtr& top() { |
| 45 |
return back(); |
| 46 |
} |
| 47 |
|
| 48 |
// Accessor method to retrieve the last element inserted. |
| 49 |
const INodePtr& top() const { |
| 50 |
return back(); |
| 51 |
} |
| 52 |
|
| 53 |
// Accessor method to retrieve the element below the last inserted. |
| 54 |
INodePtr& parent() { |
| 55 |
return *(std::vector<INodePtr>::end()-2); |
| 56 |
} |
| 57 |
|
| 58 |
// Accessor method to retrieve the element below the last inserted. |
| 59 |
const INodePtr& parent() const { |
| 60 |
return *(std::vector<INodePtr>::end()-2); |
| 61 |
} |
| 62 |
|
| 63 |
// Add an element to the stack, this becomes the top() element |
| 64 |
void push(const INodePtr& newElement) { |
| 65 |
push_back(newElement); |
| 66 |
} |
| 67 |
|
| 68 |
// Remove the topmost element, the size of the stack is reduced by 1. |
| 69 |
void pop() { |
| 70 |
pop_back(); |
| 71 |
} |
| 72 |
}; // class Path |
| 73 |
|
| 74 |
} // namespace scene |
| 75 |
|
| 76 |
#endif /*IPATH_H_*/ |