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 #ifndef itkObject_h
29 #define itkObject_h
30 
31 #include "itkLightObject.h"
32 #include "itkEventObject.h"
33 #include "itkMetaDataDictionary.h"
34 #include "itkSingletonMacro.h"
35 
36 namespace itk
37 {
38 // Forward reference because of private implementation
39 class SubjectImplementation;
40 // Forward reference because of circular dependencies
41 class ITK_FORWARD_EXPORT Command;
42 
43 /** \class Object
44  * \brief Base class for most ITK classes.
45  *
46  * Object is the second-highest level base class for most itk objects.
47  * It extends the base object functionality of LightObject by
48  * implementing callbacks (via object/observer), debug flags/methods,
49  * and modification time tracking. Most ITK classes should be a subclass
50  * of Object due to the need to keep track of modified time.
51  *
52  * \ingroup ITKSystemObjects
53  * \ingroup DataRepresentation
54  * \ingroup ITKCommon
55  *
56  * \wiki
57  * \wikiexample{Utilities/CreateAnother,Copy a filter}
58  * \endwiki
59  */
60 class ITKCommon_EXPORT Object:public LightObject
61 {
62 public:
63   ITK_DISALLOW_COPY_AND_ASSIGN(Object);
64 
65   /** Smart pointer type alias support */
66   using Self = Object;
67   using Superclass = LightObject;
68   using Pointer = SmartPointer< Self >;
69   using ConstPointer = SmartPointer< const Self >;
70 
71   /** Method for creation through the object factory. */
72   static Pointer New();
73 
74   /** Create an object from an instance, potentially deferring to a
75    * factory.  This method allows you to create an instance of an
76    * object that is exactly the same type as the referring object.
77    * This is useful in cases where an object has been cast back to a
78    * base class. */
79   LightObject::Pointer CreateAnother() const override;
80 
81   /** Standard part of all itk objects. */
82   itkTypeMacro(Object, LightObject);
83 
84   /** Turn debugging output on.  */
85   virtual void DebugOn() const;
86 
87   /** Turn debugging output off.  */
88   virtual void DebugOff() const;
89 
90   /** Get the value of the debug flag.  */
91   bool GetDebug() const;
92 
93   /** Set the value of the debug flag. A non-zero value turns debugging on. */
94   void SetDebug(bool debugFlag) const;
95 
96   /** Return this object's modified time.  */
97   virtual ModifiedTimeType GetMTime() const;
98 
99   /** Return this object's time stamp.  */
100   virtual const TimeStamp & GetTimeStamp() const;
101 
102   /** Update the modification time for this object. Many filters rely on the
103    * modification time to determine if they need to recompute their data.  */
104   virtual void Modified() const;
105 
106   /** Increase the reference count (mark as used by another object).  */
107   void Register() const override;
108 
109   /** Decrease the reference count (release by another object).  */
110   void UnRegister() const noexcept override;
111 
112   /** Sets the reference count (use with care)  */
113   void SetReferenceCount(int) override;
114 
115   /** This is a global flag that controls whether any debug, warning
116    *  or error messages are displayed.  */
117   static void SetGlobalWarningDisplay(bool flag);
118 
119   static bool GetGlobalWarningDisplay();
120 
GlobalWarningDisplayOn()121   static void GlobalWarningDisplayOn()
122   { SetGlobalWarningDisplay(true); }
GlobalWarningDisplayOff()123   static void GlobalWarningDisplayOff()
124   { SetGlobalWarningDisplay(false); }
125 
126   /** Allow people to add/remove/invoke observers (callbacks) to any ITK
127    * object. This is an implementation of the subject/observer design
128    * pattern. An observer is added by specifying an event to respond to
129    * and an itk::Command to execute. It returns an unsigned long tag
130    * which can be used later to remove the event or retrieve the
131    * command.  The memory for the Command becomes the responsibility of
132    * this object, so don't pass the same instance of a command to two
133    * different objects  */
134   unsigned long AddObserver(const EventObject & event, Command *);
135 
136   unsigned long AddObserver(const EventObject & event, Command *) const;
137 
138   /** Get the command associated with the given tag.  NOTE: This returns
139    * a pointer to a Command, but it is safe to assign this to a
140    * Command::Pointer.  Since Command inherits from LightObject, at this
141    * point in the code, only a pointer or a reference to the Command can
142    * be used.   */
143   Command * GetCommand(unsigned long tag);
144 
145   /** Call Execute on all the Commands observing this event id. */
146   void InvokeEvent(const EventObject &);
147 
148   /** Call Execute on all the Commands observing this event id.
149    * The actions triggered by this call doesn't modify this object. */
150   void InvokeEvent(const EventObject &) const;
151 
152   /** Remove the observer with this tag value. */
153   void RemoveObserver(unsigned long tag);
154 
155   /** Remove all observers . */
156   void RemoveAllObservers();
157 
158   /** Return true if an observer is registered for this event. */
159   bool HasObserver(const EventObject & event) const;
160 
161   /**
162    * \return A reference to this objects MetaDataDictionary.
163    * \warning This reference may be changed.
164    */
165   MetaDataDictionary & GetMetaDataDictionary();
166 
167   /**
168    * \return A constant reference to this objects MetaDataDictionary.
169    */
170   const MetaDataDictionary & GetMetaDataDictionary() const;
171 
172   /**
173    * Set the MetaDataDictionary
174    */
175   void SetMetaDataDictionary(const MetaDataDictionary & rhs);
176   void SetMetaDataDictionary( MetaDataDictionary && rrhs);
177 
178   /**
179    * A facility to help application programmers set a
180    * human identifiable name for a given object.
181    * This has no inherent use in ITK, but is a
182    * convenience to allow developers to provide a
183    * name for this object.
184    */
185   itkSetMacro(ObjectName, std::string);
186   itkGetConstReferenceMacro(ObjectName, std::string);
187 
188 protected:
189   Object();
190   ~Object() override;
191 
192   /** Methods invoked by Print() to print information about the object
193    * including superclasses. Typically not called by the user (use Print()
194    * instead) but used in the hierarchical print process to combine the
195    * output of several classes.  */
196   void PrintSelf(std::ostream & os, Indent indent) const override;
197 
198   bool PrintObservers(std::ostream & os, Indent indent) const;
199 
200   /** Set the time stamp of this object.
201    * This method must be used very carefully !!!.
202    * Most mortals will never need to call this method. */
203   virtual void SetTimeStamp( const TimeStamp & time );
204 
205 private:
206   /** Only used to synchronize the global variable across static libraries.*/
207   itkGetGlobalDeclarationMacro(bool, GlobalWarningDisplay);
208 
209   /** Enable/Disable debug messages. */
210   mutable bool m_Debug{false};
211 
212   /** Keep track of modification time. */
213   mutable TimeStamp m_MTime;
214 
215   /** Global object debug flag. */
216   static bool *m_GlobalWarningDisplay;
217 
218   /** Implementation class for Subject/Observer Pattern.
219    * This is only allocated if used. */
220   SubjectImplementation *m_SubjectImplementation{nullptr};
221   /**
222    * Implementation for holding Object MetaData
223    * @see itk::MetaDataDictionary
224    * @see itk::MetaDataObjectBase
225    * @see itk::MetaDataObject
226    * This is only allocated if used.
227    */
228   mutable MetaDataDictionary *m_MetaDataDictionary{nullptr};
229 
230   std::string m_ObjectName;
231 };
232 } // end namespace itk
233 
234 #endif
235