[vtk-developers] Data structures in VTK (Implementation)

Andy Cedilnik Andy.Cedilnik at kitware.com
Wed Feb 20 11:36:33 EST 2002


Hello!

Before I get flamed and people start throwing tomatoes at me,
let me explain that these containers are to be used inside the
VTK classes and not in the public interface of the classes.
They are all templated, so wrappers would die right away.

Second thing is that STL is not a solution because of several
reasons that were described in depth on this list, but mainly
because STL is not yet portable enough.

Third thing is that containers cannot be subclassed off of
vtkObject, because eventually the linked list in vtkObject
and other constructs can be replaced with templated container.
This would improve reliability of it, because it would be
implemented on one place only. That said, they are
reference counted.

Last thing, vtkContainers should be able to host any kind of
objects not just vtkObject. If you look through VTK source
code, you will find several simple implementations of dynamic
arrays, linked lists, etc, and some of them host objects of
class that is not a subclass of vtkObject.

Ok, we worked for a while and here is an interface to the
abstract class vtkContainter:

class VTK_COMMON_EXPORT vtkContainer
{
public:
   // Description:
   // Return the number of items currently held in this container. This
   // different from GetSize which is provided for some containers. GetSize
   // will return how many items the container can currently hold.
   virtual unsigned long GetNumberOfItems() = 0;

   // Description:
   // Removes all items from the container.
   virtual void RemoveAllItems() = 0;

   // Description:
   // The counterpart to New(), Delete simply calls UnRegister to lower the
   // reference count by one. It is no different than calling UnRegister.
   void Delete() { this->UnRegister(); }

   // Description:
   // Increase the reference count of this container.
   void Register();
   void Register(vtkObject *) { this->Register(); }

   // Description:
   // Decrease the reference count (release by another object). This has
   // the same effect as invoking Delete() (i.e., it reduces the reference
   // count by 1).
   void UnRegister();
   void UnRegister(vtkObject *) { this->UnRegister(); }

protected:
   unsigned long ReferenceCount;
   vtkContainer() { this->ReferenceCount = 1;};
   virtual ~vtkContainer() {};
};

As an example, here is an class definition of vtkVector:

template <class T>
class VTK_COMMON_EXPORT vtkVector : public vtkContainer
{
public:
   static vtkVector<T> *New() { return new vtkVector<T>(); }

   // Description:
   // Append an Item to the end of the vector
   unsigned long AppendItem(T a);

   // Description:
   // Remove an Item from the vector
   unsigned long RemoveItem(unsigned long id);

   // Description:
   // Return an item that was previously added to this vector.
   int GetItem(unsigned long id, T& ret);

   // Description:
   // Find an item in the vector. Return one if it was found, zero if it was
   // not found. The location of the item is returned in res.
   int Find(T a, unsigned long &res);

   // Description:
   // Return the number of items currently held in this container. This
   // different from GetSize which is provided for some containers. GetSize
   // will return how many items the container can currently hold.
   virtual unsigned long GetNumberOfItems() { return this->NumberOfItems; }

   // Description:
   // Returns the number of items the container can currently hold.
   virtual unsigned long GetSize() { return this->Size; }

   // Description:
   // Removes all items from the container.
   virtual void RemoveAllItems();

protected:
   vtkVector();
   ~vtkVector();
   unsigned long NumberOfItems;
   unsigned long Size;
   T *Array;
};

They were both written by Ken.
Please comment.
I am in the process of including them in the VTK.


				Andy Cedilnik




More information about the vtk-developers mailing list