myMemReleaser

Introduction

In glNormalViewer, we showed how to define a new processor. In this example we demonstrate the use of virtuality in processor coding. Here we want to release the memory, but the default behavior of the processor is not specific enough. When a node is walked we want to print its name, and count the number of visited nodes. To do it, we define the class 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:

Deriving MemReleaserAbstractVisitor

The name of the node is an attribute of 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.

StateVariables

To count the nodes, we have to increment a counter stored in X3DTK::MyMemReleaserStateVariables. Notice that X3DTK::MyMemReleaserStateVariables does not derive from X3DTK::MemReleaserStateVariables.

Deriving MemReleaser

We derive the processor X3DTK::MemReleaser in X3DTK::MyMemReleaser. We have then to plug X3DTK::MyMemReleaserAbstractVisitor to X3DTK::MyMemReleaser in the constructor.

Code

MyMemReleaserAbstractVisitor.h

#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

MyMemReleaserAbstractVisitor.cpp

#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...
}

}

MyMemReleaser.h

#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

MyMemReleaser.cpp

#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;
}

}

main.cpp

#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;
}

Generated on Fri Aug 27 13:16:26 2004 for X3DToolKit by doxygen 1.3.6