File: vtkInformationExecutivePortVectorKey.cxx

package info (click to toggle)
vtk 5.0.4-1.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 51,084 kB
  • ctags: 70,426
  • sloc: cpp: 524,166; ansic: 220,276; tcl: 43,377; python: 14,037; perl: 3,102; java: 1,436; yacc: 1,033; sh: 339; lex: 248; makefile: 197; asm: 154
file content (320 lines) | stat: -rw-r--r-- 10,865 bytes parent folder | download | duplicates (2)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    $RCSfile: vtkInformationExecutivePortVectorKey.cxx,v $

  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 "vtkInformationExecutivePortVectorKey.h"

#include "vtkExecutive.h"
#include "vtkGarbageCollector.h"
#include "vtkInformation.h"

#include <vtkstd/algorithm>
#include <vtkstd/vector>


// should the pipeline be double or singly linked (referenced) list, single
// make garbage collecting easier but results in a weak reference.
#define VTK_USE_SINGLE_REF 1

vtkCxxRevisionMacro(vtkInformationExecutivePortVectorKey, "$Revision: 1.7.4.1 $");

//----------------------------------------------------------------------------
vtkInformationExecutivePortVectorKey::vtkInformationExecutivePortVectorKey(const char* name, const char* location):
  vtkInformationKey(name, location)
{
  vtkFilteringInformationKeyManager::Register(this);
}

//----------------------------------------------------------------------------
vtkInformationExecutivePortVectorKey::~vtkInformationExecutivePortVectorKey()
{
}

//----------------------------------------------------------------------------
void vtkInformationExecutivePortVectorKey::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}

//----------------------------------------------------------------------------
class vtkInformationExecutivePortVectorValue: public vtkObjectBase
{
public:
  vtkTypeMacro(vtkInformationExecutivePortVectorValue, vtkObjectBase);
  vtkstd::vector<vtkExecutive*> Executives;
  vtkstd::vector<int> Ports;

  virtual ~vtkInformationExecutivePortVectorValue();
  void UnRegisterAllExecutives();
};

//----------------------------------------------------------------------------
vtkInformationExecutivePortVectorValue
::~vtkInformationExecutivePortVectorValue()
{
  // Remove all our references to executives before erasing the vector.
  this->UnRegisterAllExecutives();
}

//----------------------------------------------------------------------------
void vtkInformationExecutivePortVectorValue::UnRegisterAllExecutives()
{
#ifndef VTK_USE_SINGLE_REF
  for(vtkstd::vector<vtkExecutive*>::iterator i = this->Executives.begin();
      i != this->Executives.end(); ++i)
    {
    if(vtkExecutive* e = *i)
      {
      e->UnRegister(0);
      }
    }
#endif
}

//----------------------------------------------------------------------------
void vtkInformationExecutivePortVectorKey::Append(vtkInformation* info,
                                                  vtkExecutive* executive,
                                                  int port)
{
  if(vtkInformationExecutivePortVectorValue* v =
     static_cast<vtkInformationExecutivePortVectorValue *>
     (this->GetAsObjectBase(info)))
    {
    // The entry already exists.  Append to its vector.
#ifndef VTK_USE_SINGLE_REF
    executive->Register(0);
#endif
    v->Executives.push_back(executive);
    v->Ports.push_back(port);
    }
  else
    {
    // The entry does not yet exist.  Just create it.
    this->Set(info, &executive, &port, 1);
    }
}

//----------------------------------------------------------------------------
void vtkInformationExecutivePortVectorKey::Remove(vtkInformation* info,
                                                  vtkExecutive* executive,
                                                  int port)
{
  if(vtkInformationExecutivePortVectorValue* v =
     static_cast<vtkInformationExecutivePortVectorValue *>
     (this->GetAsObjectBase(info)))
    {
    // The entry exists.  Find this executive/port pair and remove it.
    for(unsigned int i=0; i < v->Executives.size(); ++i)
      {
      if(v->Executives[i] == executive && v->Ports[i] == port)
        {
        v->Executives.erase(v->Executives.begin()+i);
        v->Ports.erase(v->Ports.begin()+i);
#ifndef VTK_USE_SINGLE_REF
        executive->UnRegister(0);
#endif
        break;
        }
      }

    // If the last entry was removed, remove the entire value.
    if(v->Executives.empty())
      {
      this->SetAsObjectBase(info, 0);
      }
    }
}

//----------------------------------------------------------------------------
void vtkInformationExecutivePortVectorKey::Set(vtkInformation* info,
                                               vtkExecutive** executives,
                                               int* ports, int length)
{
  if(executives && ports && length > 0)
    {
#ifndef VTK_USE_SINGLE_REF
    // Register our references to all the given executives.
    for(int i=0; i < length; ++i)
      {
      if(executives[i])
        {
        executives[i]->Register(0);
        }
      }
#endif
    // Store the vector of pointers.
    vtkInformationExecutivePortVectorValue* oldv =
      static_cast<vtkInformationExecutivePortVectorValue *>
      (this->GetAsObjectBase(info));
    if(oldv && static_cast<int>(oldv->Executives.size()) == length)
      {
      // Replace the existing value.
      oldv->UnRegisterAllExecutives();
      vtkstd::copy(executives, executives+length, oldv->Executives.begin());
      vtkstd::copy(ports, ports+length, oldv->Ports.begin());
      // Since this sets a value without call SetAsObjectBase(),
      // the info has to be modified here (instead of 
      // vtkInformation::SetAsObjectBase()
      info->Modified();
      }
    else
      {
      // Allocate a new value.
      vtkInformationExecutivePortVectorValue* v =
        new vtkInformationExecutivePortVectorValue;
      this->ConstructClass("vtkInformationExecutivePortVectorValue");
      v->Executives.insert(v->Executives.begin(), executives, executives+length);
      v->Ports.insert(v->Ports.begin(), ports, ports+length);
      this->SetAsObjectBase(info, v);
      v->Delete();
      }
    }
  else
    {
    this->SetAsObjectBase(info, 0);
    }
}

//----------------------------------------------------------------------------
vtkExecutive**
vtkInformationExecutivePortVectorKey::GetExecutives(vtkInformation* info)
{
  vtkInformationExecutivePortVectorValue* v =
    static_cast<vtkInformationExecutivePortVectorValue *>
    (this->GetAsObjectBase(info));
  return (v && !v->Executives.empty())?(&v->Executives[0]):0;
}

//----------------------------------------------------------------------------
int* vtkInformationExecutivePortVectorKey::GetPorts(vtkInformation* info)
{
  vtkInformationExecutivePortVectorValue* v =
    static_cast<vtkInformationExecutivePortVectorValue *>
    (this->GetAsObjectBase(info));
  return (v && !v->Ports.empty())?(&v->Ports[0]):0;
}

//----------------------------------------------------------------------------
void vtkInformationExecutivePortVectorKey::Get(vtkInformation* info,
                                               vtkExecutive** executives,
                                               int* ports)
{
  if(vtkInformationExecutivePortVectorValue* v =
     static_cast<vtkInformationExecutivePortVectorValue *>
     (this->GetAsObjectBase(info)))
    {
    vtkstd::copy(v->Executives.begin(), v->Executives.end(), executives);
    vtkstd::copy(v->Ports.begin(), v->Ports.end(), ports);
    }
}

//----------------------------------------------------------------------------
int vtkInformationExecutivePortVectorKey::Length(vtkInformation* info)
{
  vtkInformationExecutivePortVectorValue* v =
    static_cast<vtkInformationExecutivePortVectorValue *>
    (this->GetAsObjectBase(info));
  return v?static_cast<int>(v->Executives.size()):0;
}

//----------------------------------------------------------------------------
int vtkInformationExecutivePortVectorKey::Has(vtkInformation* info)
{
  return this->GetAsObjectBase(info)?1:0;
}

//----------------------------------------------------------------------------
void vtkInformationExecutivePortVectorKey::ShallowCopy(vtkInformation* from,
                                                vtkInformation* to)
{
  this->Set(to, this->GetExecutives(from), this->GetPorts(from),
            this->Length(from));
}

//----------------------------------------------------------------------------
void vtkInformationExecutivePortVectorKey::Remove(vtkInformation* info)
{
  this->Superclass::Remove(info);
}

//----------------------------------------------------------------------------
void vtkInformationExecutivePortVectorKey::Print(ostream& os,
                                                 vtkInformation* info)
{
  // Print the value.
  if(this->Has(info))
    {
    vtkExecutive** executives = this->GetExecutives(info);
    int* ports = this->GetPorts(info);
    int length = this->Length(info);
    const char* sep = "";
    for(int i=0; i < length; ++i)
      {
      if(executives[i])
        {
        os << sep << executives[i]->GetClassName()
           << "(" << executives[i] << ") port " << ports[i];
        }
      else
        {
        os << sep << "(NULL) port " << ports[i];
        }
      sep = ", ";
      }
    }
}

//----------------------------------------------------------------------------
void
#ifdef VTK_USE_SINGLE_REF
vtkInformationExecutivePortVectorKey::Report(vtkInformation*,
                                             vtkGarbageCollector*)
{
#else
vtkInformationExecutivePortVectorKey::Report(vtkInformation* info,
                                             vtkGarbageCollector* collector)
{
  if(vtkInformationExecutivePortVectorValue* v =
     static_cast<vtkInformationExecutivePortVectorValue *>
     (this->GetAsObjectBase(info)))
    {
    for(vtkstd::vector<vtkExecutive*>::iterator i = v->Executives.begin();
        i != v->Executives.end(); ++i)
      {
      vtkGarbageCollectorReport(collector, *i, this->GetName());
      }
    }
#endif
}

//----------------------------------------------------------------------------
vtkExecutive**
vtkInformationExecutivePortVectorKey
::GetExecutivesWatchAddress(vtkInformation* info)
{
  vtkInformationExecutivePortVectorValue* v =
    static_cast<vtkInformationExecutivePortVectorValue*>
    (this->GetAsObjectBase(info));
  return (v && !v->Executives.empty())?(&v->Executives[0]):0;
}

//----------------------------------------------------------------------------
int*
vtkInformationExecutivePortVectorKey
::GetPortsWatchAddress(vtkInformation* info)
{
  vtkInformationExecutivePortVectorValue* v =
    static_cast<vtkInformationExecutivePortVectorValue*>
    (this->GetAsObjectBase(info));
  return (v && !v->Ports.empty())?(&v->Ports[0]):0;
}