Commit f69bfb49 authored by Marcus D. Hanwell's avatar Marcus D. Hanwell Committed by Kitware Robot
Browse files

Merge topic 'vtkNew'

fe9a85b5 STYLE: Minor documentation fixes for vtkNew.
0c390547 STYLE: Added extra documentation on vtkNew use.
decc9508 ENH: Added test using vtkNew in class ivars.
1a608359 ENH: Added vtkNew - scoped new VTK objects.
parents 5f428781 fe9a85b5
......@@ -523,6 +523,7 @@ IF(NOT VTK_INSTALL_NO_DEVELOPMENT)
vtkDenseArray.h
vtkIOStream.h
vtkIOStreamFwd.h
vtkNew.h
vtkSetGet.h
vtkSmartPointer.h
vtkSystemIncludes.h
......
......@@ -18,6 +18,7 @@ CREATE_TEST_SOURCELIST(Tests ${KIT}CxxTests.cxx
TestMath.cxx
TestMatrix3x3.cxx
TestMinimalStandardRandomSequence.cxx
TestNew.cxx
TestObservers.cxx
TestPolynomialSolversUnivariate.cxx
TestSmartPointer.cxx
......@@ -30,7 +31,7 @@ CREATE_TEST_SOURCELIST(Tests ${KIT}CxxTests.cxx
EXTRA_INCLUDE vtkTestDriver.h
)
ADD_EXECUTABLE(${KIT}CxxTests ${Tests})
ADD_EXECUTABLE(${KIT}CxxTests ${Tests} vtkTestNewVar.cxx)
TARGET_LINK_LIBRARIES(${KIT}CxxTests vtkCommon)
SET (TestsToRun ${Tests})
......
/*=========================================================================
Program: Visualization Toolkit
Module: TestSmartPointer.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// .NAME Test of vtkNew.
// .SECTION Description
// Tests instantiations of the vtkNew class template.
#include "vtkDebugLeaks.h"
#include "vtkFloatArray.h"
#include "vtkIntArray.h"
#include "vtkNew.h"
#include "vtkSmartPointer.h"
#include "vtkWeakPointer.h"
#include "vtkTestNewVar.h"
int TestNew(int,char *[])
{
bool error = false;
// This one should be cleaned up when the main function ends.
vtkNew<vtkIntArray> a;
if (a->GetReferenceCount() != 1)
{
error = true;
cerr << "Error, reference count should be 1, was " << a->GetReferenceCount()
<< endl;
}
cout << "vtkNew streaming " << a << endl;
vtkWeakPointer<vtkFloatArray> wf;
// Test scoping, and deletion.
if (wf == 0)
{
vtkNew<vtkFloatArray> f;
wf = f.GetPointer();
}
if (wf != 0)
{
error = true;
cerr << "Error, vtkNew failed to delete the object it contained."
<< endl;
}
// Now test interaction with the smart pointer.
vtkSmartPointer<vtkIntArray> si;
if (si == 0)
{
vtkNew<vtkIntArray> i;
si = i.GetPointer();
}
if (si->GetReferenceCount() != 1)
{
error = true;
cerr << "Error, vtkNew failed to delete the object it contained, "
<< "or the smart pointer failed to increment it. Reference count: "
<< si->GetReferenceCount() << endl;
}
vtkNew<vtkTestNewVar> newVarObj;
if (newVarObj->GetPointsRefCount() != 1)
{
error = true;
cerr << "The mmeber pointer failed to set the correct reference count: "
<< newVarObj->GetPointsRefCount() << endl;
}
vtkSmartPointer<vtkObject> points = newVarObj->GetPoints();
if (points->GetReferenceCount() != 2)
{
error = true;
cerr << "Error, vtkNew failed to keep the object it contained, "
<< "or the smart pointer failed to increment it. Reference count: "
<< points->GetReferenceCount() << endl;
}
return error ? 1 : 0;
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkTestNewVar.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkTestNewVar.h"
#include "vtkPoints2D.h"
#include "vtkObjectFactory.h"
vtkStandardNewMacro(vtkTestNewVar);
vtkTestNewVar::vtkTestNewVar()
{
}
vtkTestNewVar::~vtkTestNewVar()
{
}
vtkIdType vtkTestNewVar::GetPointsRefCount()
{
// Note - this is valid until class destruction and then Delete() will be
// called on the Data object, decrementing its reference count.
return this->Points->GetReferenceCount();
}
vtkObject * vtkTestNewVar::GetPoints()
{
return this->Points.GetPointer();
}
void vtkTestNewVar::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Points: " << endl;
this->Points->PrintSelf(os, indent.GetNextIndent());
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkTestNewVar.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// .NAME Test of vtkNew.
// .SECTION Description
// Tests instantiations of the vtkNew class template.
#ifndef __vtkTestNewVar_h
#define __vtkTestNewVar_h
#include "vtkObject.h"
#include "vtkNew.h"
class vtkPoints2D;
class vtkTestNewVar : public vtkObject
{
public:
static vtkTestNewVar * New();
vtkTypeMacro(vtkTestNewVar, vtkObject)
void PrintSelf(ostream& os, vtkIndent indent);
// Description:
// Get the reference count for the points object.
vtkIdType GetPointsRefCount();
// Description:
// This is just for testing - return the points as a vtkObject so that it can
// be assigned to a vtkSmartPointer without including the vtkPoints2D header
// and defeating part of the point of the test.
vtkObject * GetPoints();
protected:
vtkTestNewVar();
~vtkTestNewVar();
vtkNew<vtkPoints2D> Points;
private:
vtkTestNewVar(const vtkTestNewVar&); // Not implemented.
void operator=(const vtkTestNewVar&); // Not implemented.
};
#endif
/*=========================================================================
Program: Visualization Toolkit
Module: vtkNew.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// .NAME vtkNew - Allocate and hold a VTK object.
// .SECTION Description
// vtkNew is a class template that on construction allocates and
// initializes an instance of its template argument using T::New().
// It assumes ownership of one reference during its lifetime, and calls
// T->Delete() on destruction.
//
// Automatic casting is intentionally unavailable, calling GetPointer() will
// return a raw pointer. Users of this method should ensure that they do not
// return this pointer if the vtkNew will go out of scope without
// incrementing its reference count using vtkSmartPointer or similar.
//
// \code
// vtkNew<vtkClass> a;
// a->SomeMethod();
//
// vtkSmartPointer<vtkClass> b = a.GetPointer();
// b->SomeOtherMethod();
// \endcode
//
// It should be noted that vtkNew is not a drop in replacement for
// vtkSmartPointer as it is not implicitly cast to a pointer in functions
// requiring a pointer. The GetPointer() method must be used, for example,
//
// \code
// vtkNew<vtkRenderer> ren;
// vtkNew<vtkRenderWindow> renWin;
// renWin->AddRenderer(ren.GetPointer());
// vtkNew<vtkRenderWindowInteractor> iren;
// iren->SetRenderWindow(renWin.GetPointer());
// \endcode
//
// .SECTION See Also
// vtkSmartPointer vtkWeakPointer
#ifndef __vtkNew_h
#define __vtkNew_h
#include "vtkIOStream.h"
class vtkObjectBase;
template <class T>
class vtkNew
{
// Description:
// Compile time checking that the class is derived from vtkObjectBase.
void CheckObjectBase(vtkObjectBase*) {}
public:
// Description:
// Create a new T on construction.
vtkNew() : Object(T::New())
{
this->CheckObjectBase(this->Object);
}
// Description:
// Deletes reference to instance of T on destruction.
~vtkNew()
{
T* obj = this->Object;
if (obj)
{
this->Object = 0;
obj->Delete();
}
}
// Description:
// Enable pointer-like dereference syntax. Returns a pointer to the contained
// object.
T* operator->() const
{
return this->Object;
}
// Description:
// Get a raw pointer to the contained object. When using this function be
// careful that the reference count does not drop to 0 when using the pointer
// returned. This will happen when the vtkNew object goes out of
// scope for example.
T* GetPointer() const
{
return this->Object;
}
private:
vtkNew(vtkNew<T> const&); // Not implemented.
void operator=(vtkNew<T> const&); // Not implemented.
T* Object;
};
// Description:
// Streaming operator to print vtkNew like regular pointers.
template <class T>
inline ostream& operator << (ostream& os, const vtkNew<T>& p)
{
return os << p.GetPointer();
}
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment