Orfeo Toolbox  4.0
itkObject.cxx
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 /*=========================================================================
19  *
20  * Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  * Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  * For complete copyright, license and disclaimer of warranty information
25  * please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
28 #include "itkCommand.h"
29 
30 namespace itk
31 {
36 
37 class Observer
38 {
39 public:
41  const EventObject *event,
42  unsigned long tag):m_Command(c),
43  m_Event(event),
44  m_Tag(tag)
45  {}
46  virtual ~Observer()
47  { delete m_Event; }
50  unsigned long m_Tag;
51 };
52 
54 {
55 public:
58  unsigned long AddObserver(const EventObject & event, Command *cmd);
59 
60  unsigned long AddObserver(const EventObject & event, Command *cmd) const;
61 
62  void RemoveObserver(unsigned long tag);
63 
64  void RemoveAllObservers();
65 
66  void InvokeEvent(const EventObject & event, Object *self);
67 
68  void InvokeEvent(const EventObject & event, const Object *self);
69 
70  Command * GetCommand(unsigned long tag);
71 
72  bool HasObserver(const EventObject & event) const;
73 
74  bool PrintObservers(std::ostream & os, Indent indent) const;
75 
76 private:
77  std::list< Observer * > m_Observers;
78  unsigned long m_Count;
79 };
80 
83 {
84  for ( std::list< Observer * >::iterator i = m_Observers.begin();
85  i != m_Observers.end(); ++i )
86  {
87  delete ( *i );
88  }
89 }
90 
91 unsigned long
93  Command *cmd)
94 {
95  Observer *ptr = new Observer(cmd, event.MakeObject(), m_Count);
96 
97  m_Observers.push_back(ptr);
98  m_Count++;
99  return ptr->m_Tag;
100 }
101 
102 unsigned long
104  Command *cmd) const
105 {
106  Observer * ptr = new Observer(cmd, event.MakeObject(), m_Count);
107  SubjectImplementation *me = const_cast< SubjectImplementation * >( this );
108 
109  me->m_Observers.push_back(ptr);
110  me->m_Count++;
111  return ptr->m_Tag;
112 }
113 
114 void
116 {
117  for ( std::list< Observer * >::iterator i = m_Observers.begin();
118  i != m_Observers.end(); ++i )
119  {
120  if ( ( *i )->m_Tag == tag )
121  {
122  delete ( *i );
123  m_Observers.erase(i);
124  return;
125  }
126  }
127 }
128 
129 void
131 {
132  for ( std::list< Observer * >::iterator i = m_Observers.begin();
133  i != m_Observers.end(); ++i )
134  {
135  delete ( *i );
136  }
137  m_Observers.clear();
138 }
139 
140 void
142  Object *self)
143 {
144  for ( std::list< Observer * >::iterator i = m_Observers.begin();
145  i != m_Observers.end(); ++i )
146  {
147  const EventObject *e = ( *i )->m_Event;
148  if ( e->CheckEvent(&event) )
149  {
150  ( *i )->m_Command->Execute(self, event);
151  }
152  }
153 }
154 
155 void
157  const Object *self)
158 {
159  for ( std::list< Observer * >::iterator i = m_Observers.begin();
160  i != m_Observers.end(); ++i )
161  {
162  const EventObject *e = ( *i )->m_Event;
163  if ( e->CheckEvent(&event) )
164  {
165  ( *i )->m_Command->Execute(self, event);
166  }
167  }
168 }
169 
170 Command *
172 {
173  for ( std::list< Observer * >::iterator i = m_Observers.begin();
174  i != m_Observers.end(); ++i )
175  {
176  if ( ( *i )->m_Tag == tag )
177  {
178  return ( *i )->m_Command;
179  }
180  }
181  return 0;
182 }
183 
184 bool
186 {
187  for ( std::list< Observer * >::const_iterator i = m_Observers.begin();
188  i != m_Observers.end(); ++i )
189  {
190  const EventObject *e = ( *i )->m_Event;
191  if ( e->CheckEvent(&event) )
192  {
193  return true;
194  }
195  }
196  return false;
197 }
198 
199 bool
200 SubjectImplementation::PrintObservers(std::ostream & os, Indent indent) const
201 {
202  if ( m_Observers.empty() )
203  {
204  return false;
205  }
206 
207  for ( std::list< Observer * >::const_iterator i = m_Observers.begin();
208  i != m_Observers.end(); ++i )
209  {
210  const EventObject *e = ( *i )->m_Event;
211  const Command * c = ( *i )->m_Command;
212  os << indent << e->GetEventName() << "(" << c->GetNameOfClass() << ")\n";
213  }
214  return true;
215 }
216 
219 {
220  Pointer smartPtr;
222 
223  if ( rawPtr == NULL )
224  {
225  rawPtr = new Object;
226  }
227  smartPtr = rawPtr;
228  rawPtr->UnRegister();
229  return smartPtr;
230 }
231 
234 {
235  return Object::New().GetPointer();
236 }
237 
241 void
242 Object
243 ::DebugOn() const
244 {
245  m_Debug = true;
246 }
247 
251 void
252 Object
253 ::DebugOff() const
254 {
255  m_Debug = false;
256 }
257 
261 bool
262 Object
263 ::GetDebug() const
264 {
265  return m_Debug;
266 }
267 
271 void
272 Object
273 ::SetDebug(bool debugFlag) const
274 {
275  m_Debug = debugFlag;
276 }
277 
282 Object
283 ::GetMTime() const
284 {
285  return m_MTime.GetMTime();
286 }
287 
291 const TimeStamp &
292 Object
294 {
295  return m_MTime;
296 }
297 
300 void
301 Object
302 ::SetTimeStamp( const TimeStamp & timeStamp )
303 {
304  this->m_MTime = timeStamp;
305 }
306 
310 void
311 Object
312 ::Modified() const
313 {
314  m_MTime.Modified();
315  InvokeEvent( ModifiedEvent() );
316 }
317 
321 void
322 Object
323 ::Register() const
324 {
325  itkDebugMacro( << "Registered, "
326  << "ReferenceCount = " << ( m_ReferenceCount + 1 ) );
327 
328  // call the parent
329  Superclass::Register();
330 }
331 
335 void
336 Object
338 {
339  // call the parent
340  itkDebugMacro( << "UnRegistered, "
341  << "ReferenceCount = " << ( m_ReferenceCount - 1 ) );
342 
343  if ( ( m_ReferenceCount - 1 ) <= 0 )
344  {
348  this->InvokeEvent( DeleteEvent() );
349  }
350 
351  Superclass::UnRegister();
352 }
353 
357 void
358 Object
360 {
361  itkDebugMacro(<< "Reference Count set to " << ref);
362 
363  // ReferenceCount in now unlocked. We may have a race condition to
364  // to delete the object.
365  if ( ref <= 0 )
366  {
370  this->InvokeEvent( DeleteEvent() );
371  }
372 
373  Superclass::SetReferenceCount(ref);
374 }
375 
379 void
380 Object
382 {
383  m_GlobalWarningDisplay = val;
384 }
385 
389 bool
390 Object
392 {
393  return m_GlobalWarningDisplay;
394 }
395 
396 unsigned long
397 Object
398 ::AddObserver(const EventObject & event, Command *cmd)
399 {
400  if ( !this->m_SubjectImplementation )
401  {
402  this->m_SubjectImplementation = new SubjectImplementation;
403  }
404  return this->m_SubjectImplementation->AddObserver(event, cmd);
405 }
406 
407 unsigned long
408 Object
409 ::AddObserver(const EventObject & event, Command *cmd) const
410 {
411  if ( !this->m_SubjectImplementation )
412  {
413  Self *me = const_cast< Self * >( this );
414  me->m_SubjectImplementation = new SubjectImplementation;
415  }
416  return this->m_SubjectImplementation->AddObserver(event, cmd);
417 }
418 
419 Command *
420 Object
421 ::GetCommand(unsigned long tag)
422 {
423  if ( this->m_SubjectImplementation )
424  {
425  return this->m_SubjectImplementation->GetCommand(tag);
426  }
427  return NULL;
428 }
429 
430 void
431 Object
432 ::RemoveObserver(unsigned long tag)
433 {
434  if ( this->m_SubjectImplementation )
435  {
436  this->m_SubjectImplementation->RemoveObserver(tag);
437  }
438 }
439 
440 void
441 Object
443 {
444  if ( this->m_SubjectImplementation )
445  {
446  this->m_SubjectImplementation->RemoveAllObservers();
447  }
448 }
449 
450 void
451 Object
453 {
454  if ( this->m_SubjectImplementation )
455  {
456  this->m_SubjectImplementation->InvokeEvent(event, this);
457  }
458 }
459 
460 void
461 Object
462 ::InvokeEvent(const EventObject & event) const
463 {
464  if ( this->m_SubjectImplementation )
465  {
466  this->m_SubjectImplementation->InvokeEvent(event, this);
467  }
468 }
469 
470 bool
471 Object
472 ::HasObserver(const EventObject & event) const
473 {
474  if ( this->m_SubjectImplementation )
475  {
476  return this->m_SubjectImplementation->HasObserver(event);
477  }
478  return false;
479 }
480 
481 bool
482 Object
483 ::PrintObservers(std::ostream & os, Indent indent) const
484 {
485  if ( this->m_SubjectImplementation )
486  {
487  return this->m_SubjectImplementation->PrintObservers(os, indent);
488  }
489  return false;
490 }
491 
496 Object
498  LightObject(),
499  m_Debug(false),
500  m_SubjectImplementation(NULL),
501  m_MetaDataDictionary(NULL),
502  m_ObjectName()
503 {
504  this->Modified();
505 }
506 
507 Object
509 {
510  itkDebugMacro(<< "Destructing!");
511  delete m_SubjectImplementation;
512  delete m_MetaDataDictionary;
513 }
514 
519 void
520 Object
521 ::PrintSelf(std::ostream & os, Indent indent) const
522 {
523  Superclass::PrintSelf(os, indent);
524 
525  os << indent << "Modified Time: " << this->GetMTime() << std::endl;
526  os << indent << "Debug: " << ( m_Debug ? "On\n" : "Off\n" );
527  os << indent << "Object Name: " << this->GetObjectName() << std::endl;
528  os << indent << "Observers: \n";
529  if ( !this->PrintObservers( os, indent.GetNextIndent() ) )
530  {
531  os << indent.GetNextIndent() << "none\n";
532  }
533 }
534 
536 Object
538 {
539  if ( m_MetaDataDictionary == NULL )
540  {
541  m_MetaDataDictionary = new MetaDataDictionary;
542  }
543  return *m_MetaDataDictionary;
544 }
545 
546 const MetaDataDictionary &
547 Object
549 {
550  if ( m_MetaDataDictionary == NULL )
551  {
552  m_MetaDataDictionary = new MetaDataDictionary;
553  }
554  return *m_MetaDataDictionary;
555 }
556 
557 void
558 Object
560 {
561  if ( m_MetaDataDictionary == NULL )
562  {
563  m_MetaDataDictionary = new MetaDataDictionary;
564  }
565  *m_MetaDataDictionary = rhs;
566 }
567 } // end namespace itk

Generated at Sat Mar 8 2014 15:18:43 for Orfeo Toolbox with doxygen 1.8.3.1