1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkWeakPointerBase.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 "vtkWeakPointerBase.h"
//----------------------------------------------------------------------------
class vtkWeakPointerBaseToObjectBaseFriendship
{
public:
static void AddWeakPointer(vtkObjectBase *r, vtkWeakPointerBase *p);
static void RemoveWeakPointer(vtkObjectBase *r, vtkWeakPointerBase *p);
};
//----------------------------------------------------------------------------
void vtkWeakPointerBaseToObjectBaseFriendship::AddWeakPointer(
vtkObjectBase *r, vtkWeakPointerBase *p)
{
if (r)
{
vtkWeakPointerBase **l = r->WeakPointers;
if (l == 0)
{
// create a new list if none exists
l = new vtkWeakPointerBase *[2];
l[0] = p;
l[1] = 0;
r->WeakPointers = l;
}
else
{
size_t n = 0;
while (l[n] != 0) { n++; }
// if n+1 is a power of two, double the list size
if ((n & (n+1)) == 0)
{
vtkWeakPointerBase **t = l;
l = new vtkWeakPointerBase *[(n+1)*2];
for (size_t i = 0; i < n; i++)
{
l[i] = t[i];
}
delete [] t;
r->WeakPointers = l;
}
// make sure list is null-terminated
l[n++] = p;
l[n] = 0;
}
}
}
//----------------------------------------------------------------------------
void vtkWeakPointerBaseToObjectBaseFriendship::RemoveWeakPointer(
vtkObjectBase *r, vtkWeakPointerBase *p)
{
if (r)
{
vtkWeakPointerBase **l = r->WeakPointers;
if (l != 0)
{
size_t i = 0;
while (l[i] != 0 && l[i] != p)
{
i++;
}
while (l[i] != 0)
{
l[i] = l[i+1];
i++;
}
if (l[0] == 0)
{
delete [] l;
r->WeakPointers = 0;
}
}
}
}
//----------------------------------------------------------------------------
vtkWeakPointerBase::vtkWeakPointerBase(vtkObjectBase* r) :
Object(r)
{
vtkWeakPointerBaseToObjectBaseFriendship::AddWeakPointer(r, this);
}
//----------------------------------------------------------------------------
vtkWeakPointerBase::vtkWeakPointerBase(const vtkWeakPointerBase& r) :
Object(r.Object)
{
vtkWeakPointerBaseToObjectBaseFriendship::AddWeakPointer(r.Object, this);
}
//----------------------------------------------------------------------------
vtkWeakPointerBase::~vtkWeakPointerBase()
{
vtkWeakPointerBaseToObjectBaseFriendship::RemoveWeakPointer(
this->Object, this);
this->Object = 0;
}
//----------------------------------------------------------------------------
vtkWeakPointerBase&
vtkWeakPointerBase::operator=(vtkObjectBase* r)
{
if (this->Object != r)
{
vtkWeakPointerBaseToObjectBaseFriendship::RemoveWeakPointer(
this->Object, this);
this->Object = r;
vtkWeakPointerBaseToObjectBaseFriendship::AddWeakPointer(
this->Object, this);
}
return *this;
}
//----------------------------------------------------------------------------
vtkWeakPointerBase&
vtkWeakPointerBase::operator=(const vtkWeakPointerBase& r)
{
if (this != &r)
{
if (this->Object != r.Object)
{
vtkWeakPointerBaseToObjectBaseFriendship::RemoveWeakPointer(
this->Object, this);
this->Object = r.Object;
vtkWeakPointerBaseToObjectBaseFriendship::AddWeakPointer(
this->Object, this);
}
}
return *this;
}
//----------------------------------------------------------------------------
ostream& operator << (ostream& os, const vtkWeakPointerBase& p)
{
// Just print the pointer value into the stream.
return os << static_cast<void*>(p.GetPointer());
}
|