otbWriterWatcherBase.cxx 12.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 1999-2011 Insight Software Consortium
 * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
21 22 23 24 25 26 27 28 29 30

#include "otbWriterWatcherBase.h"

namespace otb
{

WriterWatcherBase
::WriterWatcherBase()
{
  // Initialize state
31
  m_Comment = "Not watching any object";
32
  m_Process = ITK_NULLPTR;
33 34 35 36 37 38
  m_StartFilterTag = 0;
  m_EndFilterTag = 0;
  m_ProgressFilterTag = 0;
  m_StartWriterTag = 0;
  m_EndWriterTag = 0;
  m_ProgressWriterTag = 0;
39 40 41 42
}

WriterWatcherBase
::WriterWatcherBase(itk::ProcessObject* process,
43
                    const char *comment)
44 45 46 47
{
  // Initialize state
  m_Process = process;
  m_Comment = comment;
48

49 50 51 52 53 54 55
  // Create a series of commands
  m_StartWriterCommand = CommandType::New();
  m_EndWriterCommand = CommandType::New();
  m_ProgressWriterCommand = CommandType::New();
  m_StartFilterCommand = CommandType::New();
  m_EndFilterCommand = CommandType::New();
  m_ProgressFilterCommand = CommandType::New();
56

57 58
  // Assign the callbacks
  m_StartFilterCommand->SetCallbackFunction(this,
OTB Bot's avatar
OTB Bot committed
59
                                            &WriterWatcherBase::StartFilter);
60
  m_EndFilterCommand->SetCallbackFunction(this,
61
                                          &WriterWatcherBase::EndFilter);
62
  m_ProgressFilterCommand->SetCallbackFunction(this,
OTB Bot's avatar
OTB Bot committed
63
                                               &WriterWatcherBase::ShowFilterProgress);
64
  m_StartWriterCommand->SetCallbackFunction(this,
OTB Bot's avatar
OTB Bot committed
65
                                            &WriterWatcherBase::StartWriter);
66
  m_EndWriterCommand->SetCallbackFunction(this,
67
                                          &WriterWatcherBase::EndWriter);
68
  m_ProgressWriterCommand->SetCallbackFunction(this,
OTB Bot's avatar
OTB Bot committed
69
                                               &WriterWatcherBase::ShowWriterProgress);
70

71 72
  // Add the commands as observers
  m_StartWriterTag = m_Process->AddObserver(itk::StartEvent(),
OTB Bot's avatar
OTB Bot committed
73
                                            m_StartWriterCommand);
74

75
  m_EndWriterTag = m_Process->AddObserver(itk::EndEvent(),
76
                                          m_EndWriterCommand);
77

78
  m_ProgressWriterTag = m_Process->AddObserver(itk::ProgressEvent(),
OTB Bot's avatar
OTB Bot committed
79
                                               m_ProgressWriterCommand);
80

81 82 83 84 85
  m_StartFilterTag = 0;
  m_EndFilterTag = 0;
  m_ProgressFilterTag = 0;

 // Try to get the filter that is wired to m_Process.
86
  if (m_Process->GetInputs()[0]->GetSource())
OTB Bot's avatar
OTB Bot committed
87
    {
88
    m_SourceProcess = m_Process->GetInputs()[0]->GetSource();
89

90 91
    // Add the commands as observers
    m_StartFilterTag = m_SourceProcess->AddObserver(itk::StartEvent(),
OTB Bot's avatar
OTB Bot committed
92
                                                    m_StartFilterCommand);
93

94
    m_EndFilterTag = m_SourceProcess->AddObserver(itk::EndEvent(),
OTB Bot's avatar
OTB Bot committed
95
                                                  m_EndFilterCommand);
96

97
    m_ProgressFilterTag = m_SourceProcess->AddObserver(itk::ProgressEvent(),
OTB Bot's avatar
OTB Bot committed
98 99
                                                       m_ProgressFilterCommand);
    }
100 101
}

102 103
WriterWatcherBase
::WriterWatcherBase(itk::ProcessObject* process, itk::ProcessObject * source,
104
                    const char *comment)
105 106 107 108
{
  // Initialize state
  m_Process = process;
  m_Comment = comment;
109

110 111 112 113 114 115 116
  // Create a series of commands
  m_StartWriterCommand = CommandType::New();
  m_EndWriterCommand = CommandType::New();
  m_ProgressWriterCommand = CommandType::New();
  m_StartFilterCommand = CommandType::New();
  m_EndFilterCommand = CommandType::New();
  m_ProgressFilterCommand = CommandType::New();
117

118 119
  // Assign the callbacks
  m_StartFilterCommand->SetCallbackFunction(this,
OTB Bot's avatar
OTB Bot committed
120
                                            &WriterWatcherBase::StartFilter);
121
  m_EndFilterCommand->SetCallbackFunction(this,
122
                                          &WriterWatcherBase::EndFilter);
123
  m_ProgressFilterCommand->SetCallbackFunction(this,
OTB Bot's avatar
OTB Bot committed
124
                                               &WriterWatcherBase::ShowFilterProgress);
125
  m_StartWriterCommand->SetCallbackFunction(this,
OTB Bot's avatar
OTB Bot committed
126
                                            &WriterWatcherBase::StartWriter);
127
  m_EndWriterCommand->SetCallbackFunction(this,
128
                                          &WriterWatcherBase::EndWriter);
129
  m_ProgressWriterCommand->SetCallbackFunction(this,
OTB Bot's avatar
OTB Bot committed
130
                                               &WriterWatcherBase::ShowWriterProgress);
131

132 133
  // Add the commands as observers
  m_StartWriterTag = m_Process->AddObserver(itk::StartEvent(),
OTB Bot's avatar
OTB Bot committed
134
                                            m_StartWriterCommand);
135

136
  m_EndWriterTag = m_Process->AddObserver(itk::EndEvent(),
137
                                          m_EndWriterCommand);
138

139
  m_ProgressWriterTag = m_Process->AddObserver(itk::ProgressEvent(),
OTB Bot's avatar
OTB Bot committed
140
                                               m_ProgressWriterCommand);
141 142

  m_SourceProcess = source;
143

144 145
  // Add the commands as observers
  m_StartFilterTag = m_SourceProcess->AddObserver(itk::StartEvent(),
OTB Bot's avatar
OTB Bot committed
146
                                                  m_StartFilterCommand);
147

148
  m_EndFilterTag = m_SourceProcess->AddObserver(itk::EndEvent(),
OTB Bot's avatar
OTB Bot committed
149
                                                m_EndFilterCommand);
150

151
  m_ProgressFilterTag = m_SourceProcess->AddObserver(itk::ProgressEvent(),
OTB Bot's avatar
OTB Bot committed
152
                                                     m_ProgressFilterCommand);
153 154
}

155
WriterWatcherBase
OTB Bot's avatar
OTB Bot committed
156
::WriterWatcherBase(const WriterWatcherBase& watch)
157 158 159
{
  // Remove any observers we have on the old process object
  if (m_Process)
160
    {
OTB Bot's avatar
OTB Bot committed
161 162
    if (m_StartWriterCommand)
      {
163
      m_Process->RemoveObserver(m_StartWriterTag);
OTB Bot's avatar
OTB Bot committed
164
      }
165
    if (m_EndWriterCommand)
OTB Bot's avatar
OTB Bot committed
166
      {
167
      m_Process->RemoveObserver(m_EndWriterTag);
OTB Bot's avatar
OTB Bot committed
168
      }
169
    if (m_ProgressWriterCommand)
OTB Bot's avatar
OTB Bot committed
170
      {
171
      m_Process->RemoveObserver(m_ProgressWriterTag);
OTB Bot's avatar
OTB Bot committed
172
      }
173 174 175
    }
  if (m_SourceProcess)
    {
OTB Bot's avatar
OTB Bot committed
176 177
    if (m_StartFilterCommand)
      {
178
      m_SourceProcess->RemoveObserver(m_StartFilterTag);
OTB Bot's avatar
OTB Bot committed
179
      }
180
    if (m_EndFilterCommand)
OTB Bot's avatar
OTB Bot committed
181
      {
182
      m_SourceProcess->RemoveObserver(m_EndFilterTag);
OTB Bot's avatar
OTB Bot committed
183
      }
184
    if (m_ProgressFilterCommand)
OTB Bot's avatar
OTB Bot committed
185
      {
186
      m_SourceProcess->RemoveObserver(m_ProgressFilterTag);
OTB Bot's avatar
OTB Bot committed
187
      }
188
    }
189

190
  // Initialize state
191
  m_Stopwatch = watch.m_Stopwatch;
192 193 194
  m_Process = watch.m_Process;
  m_SourceProcess = watch.m_SourceProcess;
  m_Comment = watch.m_Comment;
195

196 197 198 199 200 201
  m_StartFilterTag = 0;
  m_EndFilterTag = 0;
  m_ProgressFilterTag = 0;
  m_StartWriterTag = 0;
  m_EndWriterTag = 0;
  m_ProgressWriterTag = 0;
202

203 204
  // Create a series of commands
  if (m_Process)
OTB Bot's avatar
OTB Bot committed
205
    {
206 207 208 209 210 211
    m_StartWriterCommand = CommandType::New();
    m_EndWriterCommand = CommandType::New();
    m_ProgressWriterCommand = CommandType::New();

    // Assign the callbacks
    m_StartWriterCommand->SetCallbackFunction(this,
OTB Bot's avatar
OTB Bot committed
212
                                              &WriterWatcherBase::StartWriter);
213 214 215
    m_EndWriterCommand->SetCallbackFunction(this,
                                            &WriterWatcherBase::EndWriter);
    m_ProgressWriterCommand->SetCallbackFunction(this,
OTB Bot's avatar
OTB Bot committed
216
                                                 &WriterWatcherBase::ShowWriterProgress);
217 218 219 220 221

    // Add the commands as observers
    m_StartWriterTag = m_Process->AddObserver(itk::StartEvent(), m_StartWriterCommand);
    m_EndWriterTag = m_Process->AddObserver(itk::EndEvent(), m_EndWriterCommand);
    m_ProgressWriterTag
OTB Bot's avatar
OTB Bot committed
222 223
      = m_Process->AddObserver(itk::ProgressEvent(), m_ProgressWriterCommand);
    }
224 225

  if (m_SourceProcess)
OTB Bot's avatar
OTB Bot committed
226
    {
227 228 229 230 231 232
    m_StartFilterCommand = CommandType::New();
    m_EndFilterCommand = CommandType::New();
    m_ProgressFilterCommand = CommandType::New();

    // Assign the callbacks
    m_StartFilterCommand->SetCallbackFunction(this,
OTB Bot's avatar
OTB Bot committed
233
                                              &WriterWatcherBase::StartFilter);
234 235 236
    m_EndFilterCommand->SetCallbackFunction(this,
                                            &WriterWatcherBase::EndFilter);
    m_ProgressFilterCommand->SetCallbackFunction(this,
OTB Bot's avatar
OTB Bot committed
237
                                                 &WriterWatcherBase::ShowFilterProgress);
238 239 240 241 242

    // Add the commands as observers
    m_StartFilterTag = m_SourceProcess->AddObserver(itk::StartEvent(), m_StartFilterCommand);
    m_EndFilterTag = m_SourceProcess->AddObserver(itk::EndEvent(), m_EndFilterCommand);
    m_ProgressFilterTag
OTB Bot's avatar
OTB Bot committed
243 244
      = m_SourceProcess->AddObserver(itk::ProgressEvent(), m_ProgressFilterCommand);
    }
245 246 247 248
}

void
WriterWatcherBase
OTB Bot's avatar
OTB Bot committed
249
::operator =(const WriterWatcherBase& watch)
250 251 252
{
// Remove any observers we have on the old process object
  if (m_Process)
253
    {
OTB Bot's avatar
OTB Bot committed
254 255
    if (m_StartWriterCommand)
      {
256
      m_Process->RemoveObserver(m_StartWriterTag);
OTB Bot's avatar
OTB Bot committed
257
      }
258
    if (m_EndWriterCommand)
OTB Bot's avatar
OTB Bot committed
259
      {
260
      m_Process->RemoveObserver(m_EndWriterTag);
OTB Bot's avatar
OTB Bot committed
261
      }
262
    if (m_ProgressWriterCommand)
OTB Bot's avatar
OTB Bot committed
263
      {
264
      m_Process->RemoveObserver(m_ProgressWriterTag);
OTB Bot's avatar
OTB Bot committed
265
      }
266 267 268
    }
  if (m_SourceProcess)
    {
OTB Bot's avatar
OTB Bot committed
269 270
    if (m_StartFilterCommand)
      {
271
      m_SourceProcess->RemoveObserver(m_StartFilterTag);
OTB Bot's avatar
OTB Bot committed
272
      }
273
    if (m_EndFilterCommand)
OTB Bot's avatar
OTB Bot committed
274
      {
275
      m_SourceProcess->RemoveObserver(m_EndFilterTag);
OTB Bot's avatar
OTB Bot committed
276
      }
277
    if (m_ProgressFilterCommand)
OTB Bot's avatar
OTB Bot committed
278
      {
279
      m_SourceProcess->RemoveObserver(m_ProgressFilterTag);
OTB Bot's avatar
OTB Bot committed
280
      }
281
    }
282

283
  // Initialize state
284
  m_Stopwatch = watch.m_Stopwatch;
285 286 287
  m_Process = watch.m_Process;
  m_SourceProcess = watch.m_SourceProcess;
  m_Comment = watch.m_Comment;
288

289 290 291 292 293 294
  m_StartFilterTag = 0;
  m_EndFilterTag = 0;
  m_ProgressFilterTag = 0;
  m_StartWriterTag = 0;
  m_EndWriterTag = 0;
  m_ProgressWriterTag = 0;
295

296 297
  // Create a series of commands
  if (m_Process)
OTB Bot's avatar
OTB Bot committed
298
    {
299 300 301 302 303 304
    m_StartWriterCommand = CommandType::New();
    m_EndWriterCommand = CommandType::New();
    m_ProgressWriterCommand = CommandType::New();

    // Assign the callbacks
    m_StartWriterCommand->SetCallbackFunction(this,
OTB Bot's avatar
OTB Bot committed
305
                                              &WriterWatcherBase::StartWriter);
306 307 308
    m_EndWriterCommand->SetCallbackFunction(this,
                                            &WriterWatcherBase::EndWriter);
    m_ProgressWriterCommand->SetCallbackFunction(this,
OTB Bot's avatar
OTB Bot committed
309
                                                 &WriterWatcherBase::ShowWriterProgress);
310 311 312 313 314

    // Add the commands as observers
    m_StartWriterTag = m_Process->AddObserver(itk::StartEvent(), m_StartWriterCommand);
    m_EndWriterTag = m_Process->AddObserver(itk::EndEvent(), m_EndWriterCommand);
    m_ProgressWriterTag
OTB Bot's avatar
OTB Bot committed
315 316
      = m_Process->AddObserver(itk::ProgressEvent(), m_ProgressWriterCommand);
    }
317
  if (m_SourceProcess)
OTB Bot's avatar
OTB Bot committed
318
    {
319 320 321 322 323 324
    m_StartFilterCommand = CommandType::New();
    m_EndFilterCommand = CommandType::New();
    m_ProgressFilterCommand = CommandType::New();

    // Assign the callbacks
    m_StartFilterCommand->SetCallbackFunction(this,
OTB Bot's avatar
OTB Bot committed
325
                                              &WriterWatcherBase::StartFilter);
326 327 328
    m_EndFilterCommand->SetCallbackFunction(this,
                                            &WriterWatcherBase::EndFilter);
    m_ProgressFilterCommand->SetCallbackFunction(this,
OTB Bot's avatar
OTB Bot committed
329
                                                 &WriterWatcherBase::ShowFilterProgress);
330 331 332 333 334

    // Add the commands as observers
    m_StartFilterTag = m_SourceProcess->AddObserver(itk::StartEvent(), m_StartFilterCommand);
    m_EndFilterTag = m_SourceProcess->AddObserver(itk::EndEvent(), m_EndFilterCommand);
    m_ProgressFilterTag
OTB Bot's avatar
OTB Bot committed
335 336
      = m_SourceProcess->AddObserver(itk::ProgressEvent(), m_ProgressFilterCommand);
    }
337 338 339 340 341 342 343
}

WriterWatcherBase
::~WriterWatcherBase()
{
  // Remove any observers we have on the old process object
  if (m_Process)
344
    {
OTB Bot's avatar
OTB Bot committed
345 346
    if (m_StartWriterCommand)
      {
347
      m_Process->RemoveObserver(m_StartWriterTag);
OTB Bot's avatar
OTB Bot committed
348
      }
349
    if (m_EndWriterCommand)
OTB Bot's avatar
OTB Bot committed
350
      {
351
      m_Process->RemoveObserver(m_EndWriterTag);
OTB Bot's avatar
OTB Bot committed
352
      }
353
    if (m_ProgressWriterCommand)
OTB Bot's avatar
OTB Bot committed
354
      {
355
      m_Process->RemoveObserver(m_ProgressWriterTag);
OTB Bot's avatar
OTB Bot committed
356
      }
357 358 359
    }
  if (m_SourceProcess)
    {
OTB Bot's avatar
OTB Bot committed
360 361
    if (m_StartFilterCommand)
      {
362
      m_SourceProcess->RemoveObserver(m_StartFilterTag);
OTB Bot's avatar
OTB Bot committed
363
      }
364
    if (m_EndFilterCommand)
OTB Bot's avatar
OTB Bot committed
365
      {
366
      m_SourceProcess->RemoveObserver(m_EndFilterTag);
OTB Bot's avatar
OTB Bot committed
367
      }
368
    if (m_ProgressFilterCommand)
OTB Bot's avatar
OTB Bot committed
369
      {
370
      m_SourceProcess->RemoveObserver(m_ProgressFilterTag);
OTB Bot's avatar
OTB Bot committed
371
      }
372
    }
373 374 375
}

} // end namespace otb