00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef PVLE_AUTO_DELETE_CHILD_CALLBACK
00029 #define PVLE_AUTO_DELETE_CHILD_CALLBACK
00030
00031 #include <osg/Group>
00032 #include <osg/NodeCallback>
00033
00038 template <class T>
00039 class AutoDeleteChildCallback : public osg::NodeCallback {
00040 public:
00042 AutoDeleteChildCallback();
00044 AutoDeleteChildCallback(const T & reference);
00045
00046 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
00047
00048 protected:
00049 std::string libraryName;
00050 std::string className;
00051 };
00052
00053
00054 template <class T>
00055 AutoDeleteChildCallback<T>::AutoDeleteChildCallback() {
00056 osg::ref_ptr<osg::Node> reference = new T;
00057 libraryName = reference->libraryName();
00058 className = reference->className();
00059 }
00060
00061 template <class T>
00062 AutoDeleteChildCallback<T>::AutoDeleteChildCallback(const T & reference)
00063 : libraryName(reference.libraryName()), className(reference.className())
00064 {
00065 }
00066
00067
00068 template <class T>
00069 void AutoDeleteChildCallback<T>::operator()(osg::Node* node, osg::NodeVisitor* nv) {
00070
00071 osg::Group * pGroup = node->asGroup();
00072 if (pGroup) {
00073 for(int i=static_cast<int>(pGroup->getNumChildren())-1; i>=0; --i) {
00074 osg::Node* pChild = pGroup->getChild(i);
00075 if (pChild->className() == className && pChild->libraryName() == libraryName) {
00076
00077 #ifdef _DEBUG
00078 if (dynamic_cast<T *>(pChild) != pChild) throw "AutoDeleteChildCallback cast error";
00079 #endif
00080
00081 if (static_cast<T *>(pChild)->autoDelete())
00082 pGroup->removeChildren(i, 1);
00083 }
00084 }
00085 }
00086 traverse(node, nv);
00087 }
00088
00089 #endif // PVLE_AUTO_DELETE_CHILD_CALLBACK