Orfeo Toolbox  3.16
itkObject.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkObject.cxx,v $
5  Language: C++
6  Date: $Date: 2009-02-06 20:53:14 $
7  Version: $Revision: 1.46 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  Portions of this code are covered under the VTK copyright.
13  See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
14 
15  This software is distributed WITHOUT ANY WARRANTY; without even
16  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
17  PURPOSE. See the above copyright notices for more information.
18 
19 =========================================================================*/
20 #include "itkObject.h"
21 #include "itkObjectFactory.h"
22 #include "itkCommand.h"
23 
24 namespace itk
25 {
30 
31 class Observer
32 {
33 public:
35  const EventObject * event,
36  unsigned long tag) :m_Command(c),
37  m_Event(event),
38  m_Tag(tag)
39  {}
40  virtual ~Observer()
41  { delete m_Event; }
44  unsigned long m_Tag;
45 };
46 
47 
49 {
50 public:
53  unsigned long AddObserver(const EventObject & event, Command* cmd);
54  unsigned long AddObserver(const EventObject & event, Command* cmd) const;
55  void RemoveObserver(unsigned long tag);
56  void RemoveAllObservers();
57  void InvokeEvent( const EventObject & event, Object* self);
58  void InvokeEvent( const EventObject & event, const Object* self);
59  Command *GetCommand(unsigned long tag);
60  bool HasObserver(const EventObject & event) const;
61  bool PrintObservers(std::ostream& os, Indent indent) const;
62 private:
63  std::list<Observer* > m_Observers;
64  unsigned long m_Count;
65 };
66 
69 {
70  for(std::list<Observer* >::iterator i = m_Observers.begin();
71  i != m_Observers.end(); ++i)
72  {
73  delete (*i);
74  }
75 }
76 
77 
78 unsigned long
80 AddObserver(const EventObject & event,
81  Command* cmd)
82 {
83  Observer* ptr = new Observer(cmd, event.MakeObject(), m_Count);
84  m_Observers.push_back(ptr);
85  m_Count++;
86  return ptr->m_Tag;
87 }
88 
89 
90 unsigned long
92 AddObserver(const EventObject & event,
93  Command* cmd) const
94 {
95  Observer* ptr = new Observer(cmd, event.MakeObject(), m_Count);
96  SubjectImplementation * me = const_cast<SubjectImplementation *>( this );
97  me->m_Observers.push_back(ptr);
98  me->m_Count++;
99  return ptr->m_Tag;
100 }
101 
102 
103 void
105 RemoveObserver(unsigned long tag)
106 {
107  for(std::list<Observer* >::iterator i = m_Observers.begin();
108  i != m_Observers.end(); ++i)
109  {
110  if((*i)->m_Tag == tag)
111  {
112  delete (*i);
113  m_Observers.erase(i);
114  return;
115  }
116  }
117 }
118 
119 
120 void
123 {
124  for(std::list<Observer* >::iterator i = m_Observers.begin();
125  i != m_Observers.end(); ++i)
126  {
127  delete (*i);
128  }
129  m_Observers.clear();
130 }
131 
132 
133 void
135 InvokeEvent( const EventObject & event,
136  Object* self)
137 {
138  for(std::list<Observer* >::iterator i = m_Observers.begin();
139  i != m_Observers.end(); ++i)
140  {
141  const EventObject * e = (*i)->m_Event;
142  if(e->CheckEvent(&event))
143  {
144  (*i)->m_Command->Execute(self, event);
145  }
146  }
147 }
148 
149 void
151 InvokeEvent( const EventObject & event,
152  const Object* self)
153 {
154  for(std::list<Observer* >::iterator i = m_Observers.begin();
155  i != m_Observers.end(); ++i)
156  {
157  const EventObject * e = (*i)->m_Event;
158  if(e->CheckEvent(&event))
159  {
160  (*i)->m_Command->Execute(self, event);
161  }
162  }
163 }
164 
165 
166 Command*
168 GetCommand(unsigned long tag)
169 {
170  for(std::list<Observer* >::iterator i = m_Observers.begin();
171  i != m_Observers.end(); ++i)
172  {
173  if ( (*i)->m_Tag == tag)
174  {
175  return (*i)->m_Command;
176  }
177  }
178  return 0;
179 }
180 
181 bool
183 HasObserver(const EventObject & event) const
184 {
185  for(std::list<Observer* >::const_iterator i = m_Observers.begin();
186  i != m_Observers.end(); ++i)
187  {
188  const EventObject * e = (*i)->m_Event;
189  if(e->CheckEvent(&event))
190  {
191  return true;
192  }
193  }
194  return false;
195 }
196 
197 bool
199 PrintObservers(std::ostream& os, Indent indent) const
200 {
201  if(m_Observers.empty())
202  {
203  return false;
204  }
205 
206  for(std::list<Observer* >::const_iterator i = m_Observers.begin();
207  i != m_Observers.end(); ++i)
208  {
209  const EventObject * e = (*i)->m_Event;
210  const Command* c = (*i)->m_Command;
211  os << indent << e->GetEventName() << "(" << c->GetNameOfClass() << ")\n";
212  }
213  return true;
214 }
215 
218 {
219  Pointer smartPtr;
221  if(rawPtr == NULL)
222  {
223  rawPtr = new Object;
224  }
225  smartPtr = rawPtr;
226  rawPtr->UnRegister();
227  return smartPtr;
228 }
229 
232 {
233  return Object::New().GetPointer();
234 }
235 
236 
240 void
241 Object
242 ::DebugOn() const
243 {
244  m_Debug = true;
245 }
246 
247 
251 void
252 Object
253 ::DebugOff() const
254 {
255  m_Debug = false;
256 }
257 
258 
262 bool
263 Object
264 ::GetDebug() const
265 {
266  return m_Debug;
267 }
268 
269 
273 void
274 Object
275 ::SetDebug(bool debugFlag) const
276 {
277  m_Debug = debugFlag;
278 }
279 
280 
284 unsigned long
285 Object
286 ::GetMTime() const
287 {
288  return m_MTime.GetMTime();
289 }
290 
291 
295 void
296 Object
297 ::Modified() const
298 {
299  m_MTime.Modified();
300  InvokeEvent( ModifiedEvent() );
301 }
302 
303 
307 void
308 Object
309 ::Register() const
310 {
311  itkDebugMacro(<< "Registered, "
312  << "ReferenceCount = " << (m_ReferenceCount+1));
313 
314  // call the parent
315  Superclass::Register();
316 }
317 
318 
322 void
323 Object
325 {
326  // call the parent
327  itkDebugMacro(<< "UnRegistered, "
328  << "ReferenceCount = " << (m_ReferenceCount-1));
329 
330  if ( (m_ReferenceCount-1) <= 0)
331  {
335  this->InvokeEvent(DeleteEvent());
336  }
337 
338  Superclass::UnRegister();
339 }
340 
341 
345 void
346 Object
348 {
349  itkDebugMacro(<< "Reference Count set to " << ref);
350 
351  // ReferenceCount in now unlocked. We may have a race condition to
352  // to delete the object.
353  if( ref <= 0 )
354  {
358  this->InvokeEvent(DeleteEvent());
359  }
360 
361  Superclass::SetReferenceCount(ref);
362 }
363 
364 
368 void
369 Object
371 {
372  m_GlobalWarningDisplay = val;
373 }
374 
375 
379 bool
380 Object
382 {
383  return m_GlobalWarningDisplay;
384 }
385 
386 
387 unsigned long
388 Object
389 ::AddObserver(const EventObject & event, Command *cmd)
390 {
391  if (!this->m_SubjectImplementation)
392  {
393  this->m_SubjectImplementation = new SubjectImplementation;
394  }
395  return this->m_SubjectImplementation->AddObserver(event,cmd);
396 }
397 
398 
399 unsigned long
400 Object
401 ::AddObserver(const EventObject & event, Command *cmd) const
402 {
403  if (!this->m_SubjectImplementation)
404  {
405  Self * me = const_cast<Self *>( this );
406  me->m_SubjectImplementation = new SubjectImplementation;
407  }
408  return this->m_SubjectImplementation->AddObserver(event,cmd);
409 }
410 
411 
412 Command*
413 Object
414 ::GetCommand(unsigned long tag)
415 {
416  if (this->m_SubjectImplementation)
417  {
418  return this->m_SubjectImplementation->GetCommand(tag);
419  }
420  return NULL;
421 }
422 
423 void
424 Object
425 ::RemoveObserver(unsigned long tag)
426 {
427  if (this->m_SubjectImplementation)
428  {
429  this->m_SubjectImplementation->RemoveObserver(tag);
430  }
431 }
432 
433 void
434 Object
436 {
437  if (this->m_SubjectImplementation)
438  {
439  this->m_SubjectImplementation->RemoveAllObservers();
440  }
441 }
442 
443 
444 void
445 Object
446 ::InvokeEvent( const EventObject & event )
447 {
448  if (this->m_SubjectImplementation)
449  {
450  this->m_SubjectImplementation->InvokeEvent(event,this);
451  }
452 }
453 
454 
455 void
456 Object
457 ::InvokeEvent( const EventObject & event ) const
458 {
459  if (this->m_SubjectImplementation)
460  {
461  this->m_SubjectImplementation->InvokeEvent(event,this);
462  }
463 }
464 
465 bool
466 Object
467 ::HasObserver( const EventObject & event ) const
468 {
469  if (this->m_SubjectImplementation)
470  {
471  return this->m_SubjectImplementation->HasObserver(event);
472  }
473  return false;
474 }
475 
476 bool
477 Object
478 ::PrintObservers(std::ostream& os, Indent indent) const
479 {
480  if (this->m_SubjectImplementation)
481  {
482  return this->m_SubjectImplementation->PrintObservers(os, indent);
483  }
484  return false;
485 }
486 
491 Object
493  LightObject(),
494  m_Debug(false),
495  m_SubjectImplementation(NULL),
496  m_MetaDataDictionary(NULL)
497 {
498  this->Modified();
499 }
500 
501 
502 Object
504 {
505  itkDebugMacro(<< "Destructing!");
506  delete m_SubjectImplementation;
507  delete m_MetaDataDictionary;//Deleting a NULL pointer does nothing.
508 }
509 
510 
515 void
516 Object
517 ::PrintSelf(std::ostream& os, Indent indent) const
518 {
519  Superclass::PrintSelf(os, indent);
520 
521  os << indent << "Modified Time: " << this->GetMTime() << std::endl;
522  os << indent << "Debug: " << (m_Debug ? "On\n" : "Off\n");
523  os << indent << "Observers: \n";
524  if(!this->PrintObservers(os, indent.GetNextIndent()))
525  {
526  os << indent.GetNextIndent() << "none\n";
527  }
528 }
529 
531 Object
533 {
534  if(m_MetaDataDictionary==NULL)
535  {
536  m_MetaDataDictionary=new MetaDataDictionary;
537  }
538  return *m_MetaDataDictionary;
539 }
540 
541 const MetaDataDictionary &
542 Object
544 {
545  if(m_MetaDataDictionary==NULL)
546  {
547  m_MetaDataDictionary=new MetaDataDictionary;
548  }
549  return *m_MetaDataDictionary;
550 }
551 
552 void
553 Object
555 {
556  if(m_MetaDataDictionary==NULL)
557  {
558  m_MetaDataDictionary=new MetaDataDictionary;
559  }
560  *m_MetaDataDictionary=rhs;
561 }
562 
563 } // end namespace itk

Generated at Sat Feb 2 2013 23:56:47 for Orfeo Toolbox with doxygen 1.8.1.1