KWStyle - itkObject.cxx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkObject.cxx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:43 $
7   Version:   $Revision: 1.4 $
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 IND *****PURPOSE.  See the above copyright notices for more information.
18
19 DEF =========================================================================*/
20 #include "itkObject.h"
21 #include "itkObjectFactory.h"
22 #include "itkCommand.h"
23
24 namespace itk
25 {
26 /**
27  * Initialize static member that controls warning display.
28  */
29 bool Object::m_GlobalWarningDisplay = true;
30
31 class Observer
32 {
33 public:
34   Observer(Command* c, 
35            const EventObject * event,
36            unsigned long tag) :m_Command(c),
37 IND *******************************m_Event(event),
38 IND *******************************m_Tag(tag)
39 IND **{ }
40   virtual ~Observer() 
41 IVP,IND **{ delete m_Event; }
42 IVP   Command::Pointer m_Command;
43 IVP   const EventObject * m_Event;
44 IVP   unsigned long m_Tag;
45 };
46     
47   
48 class SubjectImplementation
49 {
50 public:
51   SubjectImplementation() {m_Count = 0;}
52   ~SubjectImplementation();
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
67 SubjectImplementation::
68 ~SubjectImplementation()
69 {
70   for(std::list<Observer* >::iterator i = m_Observers.begin();
71 IND ******i != m_Observers.end(); ++i)
72     {
73     delete (*i);
74     }
75 }
76
77
78 unsigned long 
79 SubjectImplementation::
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 
91 SubjectImplementation::
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
104 SubjectImplementation::
105 RemoveObserver(unsigned long tag)
106 {
107   for(std::list<Observer* >::iterator i = m_Observers.begin();
108 IND ******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
121 SubjectImplementation::
122 RemoveAllObservers()
123 {
124   for(std::list<Observer* >::iterator i = m_Observers.begin();
125 IND ******i != m_Observers.end(); ++i)
126     {
127     delete (*i);
128     }
129   m_Observers.clear();
130 }
131
132
133 void 
134 SubjectImplementation::
135 InvokeEvent( const EventObject & event,
136              Object* self)
137 {
138   for(std::list<Observer* >::iterator i = m_Observers.begin();
139 IND ******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 
150 SubjectImplementation::
151 InvokeEvent( const EventObject & event,
152              const Object* self)
153 {
154   for(std::list<Observer* >::iterator i = m_Observers.begin();
155 IND ******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*
167 SubjectImplementation::
168 GetCommand(unsigned long tag)
169 {
170   for(std::list<Observer* >::iterator i = m_Observers.begin();
171 IND ******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
182 SubjectImplementation::
183 HasObserver(const EventObject & event) const
184 {
185   for(std::list<Observer* >::const_iterator i = m_Observers.begin();
186 IND ******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
198 SubjectImplementation::
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 IND ******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
216 Object::Pointer
217 Object::New()
218 {
219   Pointer smartPtr;
220   Object *rawPtr = ::itk::ObjectFactory<Object>::Create();
221   if(rawPtr == NULL)
222     {
223     rawPtr = new Object;
224     }
225   smartPtr = rawPtr;
226   rawPtr->UnRegister();
227   return smartPtr;
228 }
229
230 LightObject::Pointer
231 Object::CreateAnother() const
232 {
233   return Object::New().GetPointer();
234 }
235
236
237 /**
238  * Turn debugging output on.
239  */
240 void 
241 Object
242 ::DebugOn() const
243 {
244   m_Debug = true;
245 }
246
247
248 /**
249  * Turn debugging output off.
250  */
251 void 
252 Object
253 ::DebugOff() const
254 {
255   m_Debug = false;
256 }
257
258
259 /**
260  * Get the value of the debug flag.
261  */
262 bool 
263 Object
264 ::GetDebug() const
265 {
266   return m_Debug;
267 }
268
269
270 /**
271  * Set the value of the debug flag. A non-zero value turns debugging on.
272  */
273 void 
274 Object
275 ::SetDebug(bool debugFlag) const
276 {
277   m_Debug = debugFlag;
278 }
279
280
281 /**
282  * Return the modification for this object.
283  */
284 unsigned long 
285 Object
286 ::GetMTime() const
287 {
288   return m_MTime.GetMTime();
289 }
290
291
292 /**
293  * Make sure this object's modified time is greater than all others.
294  */
295 void 
296 Object
297 ::Modified() const
298 {
299   m_MTime.Modified();
300   InvokeEvent( ModifiedEvent() );
301 }
302
303
304 /**
305  * Increase the reference count (mark as used by another object).
306  */
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
319 /**
320  * Decrease the reference count (release by another object).
321  */
322 void 
323 Object
324 ::UnRegister() const
325 {
326   // call the parent 
327   itkDebugMacro(<< "UnRegistered, "
328                 << "ReferenceCount = " << (m_ReferenceCount-1));
329
330   if ( (m_ReferenceCount-1) <= 0)
331     {
332     /**
333      * If there is a delete method, invoke it.
334      */
335     this->InvokeEvent(DeleteEvent());
336     }
337
338   Superclass::UnRegister();
339 }
340
341
342 /**
343  * Sets the reference count (use with care)
344  */
345 void 
346 Object
347 ::SetReferenceCount(int ref)
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     {
355     /**
356      * If there is a delete method, invoke it.
357      */
358     this->InvokeEvent(DeleteEvent());
359     }
360
361   Superclass::SetReferenceCount(ref);
362 }
363
364
365 /**
366  * Set the value of the global debug output control flag.
367  */
368 void 
369 Object
370 ::SetGlobalWarningDisplay(bool val)
371 {
372   m_GlobalWarningDisplay = val;
373 }
374
375
376 /**
377  * Get the value of the global debug output control flag.
378  */
379 bool 
380 Object
381 ::GetGlobalWarningDisplay()
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
435 ::RemoveAllObservers()
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
466 EML
467 EML
468 bool
469 Object
470 ::HasObserver( const EventObject & event ) const
471 {
472   if (this->m_SubjectImplementation)
473     {
474     return this->m_SubjectImplementation->HasObserver(event);
475     }
476   return false;
477 }
478
479
480 EML
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
492
493 EML
494 /**
495  * Create an object with Debug turned off and modified time initialized 
496  * to the most recently modified object.
497  */
498 Object
499 ::Object():
500 IND **LightObject(),
501 IND **m_Debug(false),
502 IND **m_SubjectImplementation(NULL),
503 IND **m_MetaDataDictionary(NULL)
504 {
505   this->Modified();
506 }
507
508
509 Object
510 ::~Object() 
511 {
512   itkDebugMacro(<< "Destructing!");
513   delete m_SubjectImplementation;
514   delete m_MetaDataDictionary;//Deleteing a NULL pointer does nothing.
515 }
516
517
518 /**
519  * Chaining method to print an object's instance variables, as well as
520  * its superclasses.
521  */
522 void 
523 Object
524 ::PrintSelf(std::ostream& os, Indent indent) const
525 {
526   Superclass::PrintSelf(os, indent);
527
528   os << indent << "Modified Time: " << this->GetMTime() << std::endl;
529   os << indent << "Debug: " << (m_Debug ? "On\n" : "Off\n");
530   os << indent << "Observers: \n";
531   if(!this->PrintObservers(os, indent.GetNextIndent()))
532     {
533     os << indent.GetNextIndent() << "none\n";
534     }
535 }
536
537 MetaDataDictionary &
538 Object
539 ::GetMetaDataDictionary(void)
540 {
541   if(m_MetaDataDictionary==NULL)
542     {
543     m_MetaDataDictionary=new MetaDataDictionary;
544     }
545   return *m_MetaDataDictionary;
546 }
547
548 const MetaDataDictionary &
549 Object
550 ::GetMetaDataDictionary(void) const
551 {
552   if(m_MetaDataDictionary==NULL)
553     {
554     m_MetaDataDictionary=new MetaDataDictionary;
555     }
556   return *m_MetaDataDictionary;
557 }
558
559 void
560 Object
561 ::SetMetaDataDictionary(const MetaDataDictionary & rhs)
562 {
563   if(m_MetaDataDictionary==NULL)
564     {
565     m_MetaDataDictionary=new MetaDataDictionary;
566     }
567   *m_MetaDataDictionary=rhs;
568 }
569
570 // end namespace itk
571

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:37PM
© Kitware Inc.