X3DTK::MyMemReleaser
by deriving X3DTK::MemReleaser
, an existing X3D processor which deletes the X3DTK::X3DAbstractNode
nodes and we redefine the appropriate methods. For more details about the scene graph API, see the page.We want here to print the name of the nodes deleted.
Some important functions and classes:
X3DTK::X3DAbstractNode
, so we only need to redefine the function enterX3DAbstractNode
of X3DTK::MyMemReleaserAbstractVisitor
. We also add the definition of an enterCylinder function to show the validation of functions. X3DTK::X3D::Cylinder
doesn't belong to the Abstract component, so there will be a warning message.X3DTK::MyMemReleaserStateVariables
. Notice that X3DTK::MyMemReleaserStateVariables
does not derive from X3DTK::MemReleaserStateVariables
.X3DTK::MemReleaser
in X3DTK::MyMemReleaser
. We have then to plug X3DTK::MyMemReleaserAbstractVisitor
to X3DTK::MyMemReleaser
in the constructor.#ifndef MYMEMRELEASERABSTRACTVISITOR_H #define MYMEMRELEASERABSTRACTVISITOR_H #include <X3DTK/X3D/scenegraph.h> #include <X3DTK/memreleaser.h> namespace X3DTK { // Visitor for the Abstract component of the MemReleaser processor. class MyMemReleaserAbstractVisitor : public MemReleaserAbstractVisitor { public: MyMemReleaserAbstractVisitor(); static void enterX3DAbstractNode(X3DAbstractNode *N); static void enterCylinder(X3D::Cylinder *C); }; } #endif
#include "MyMemReleaserAbstractVisitor.h" #include "MyMemReleaserStateVariables.h" #include <iostream> using namespace std; namespace X3DTK { MyMemReleaserAbstractVisitor::MyMemReleaserAbstractVisitor() : MemReleaserAbstractVisitor() { // Defines the new enter function for the X3DAbstractNode. define(Recorder<X3DAbstractNode>::getEnterFunction(&MyMemReleaserAbstractVisitor::enterX3DAbstractNode)); // Defines the new enter function for the Cylinder. This method will not be recorded // because X3D::Cylinder doesn't belong to the Abstract component. define(Recorder<X3D::Cylinder>::getEnterFunction(&MyMemReleaserAbstractVisitor::enterCylinder)); } void MyMemReleaserAbstractVisitor::enterX3DAbstractNode(X3DAbstractNode *N) { // Prints the name of the node. cout << N->getTypeName() << " released" << endl; // Call to the super class method. MemReleaserAbstractVisitor::enterX3DAbstractNode(N); Singleton<MyMemReleaserStateVariables>::getInstance()->addNode(); } void MyMemReleaserAbstractVisitor::enterCylinder(X3D::Cylinder *) { // empty because it will never be called... } }
#ifndef MYMEMRELEASER_H #define MYMEMRELEASER_H #include <X3DTK/memreleaser.h> namespace X3DTK { // Class deriving MemReleaser. class MyMemReleaser : public MemReleaser { public: MyMemReleaser(); virtual void release(SFNode N, bool verbose = false); }; } #endif
#include "MyMemReleaser.h" #include "MyMemReleaserAbstractVisitor.h" #include "MyMemReleaserStateVariables.h" #include <iostream> using namespace std; namespace X3DTK { MyMemReleaser::MyMemReleaser() : MemReleaser() { // Sets the Abstract visitor for MyMemReleaser processor. setComponentVisitor(new MyMemReleaserAbstractVisitor()); } void MyMemReleaser::release(SFNode N, bool verbose) { // initializing the state variables. Singleton<MyMemReleaserStateVariables>::getInstance()->init(); // call to super-class method. MemReleaser::release(N, verbose); // print the number of nodes. cout << endl << Singleton<MyMemReleaserStateVariables>::getInstance()->getNodeNumber() << " nodes visited" << endl; } }
#include "MyMemReleaser.h" #include <X3DTK/X3D/scenegraph.h> #include <iostream> using namespace X3DTK; using namespace std; int main(int argc, char *argv[]) { if (argc <= 1) { cerr << "usage: mymemreleaser input" << endl; exit(0); } // Default loader for the X3D Nodes. X3D::Loader *loader = Singleton<X3D::Loader>::getInstance(); // Instanciation of GraphTester. MemReleaser *releaser = Singleton<MyMemReleaser>::getInstance(); // Loads the scene. X3D::Scene *s = loader->load(argv[1]); // Releases the memory. releaser->release(s); // removes the instance. Singleton<MyMemReleaser>::removeInstance(); Singleton<X3D::Loader>::removeInstance(); return 1; }