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 #ifndef itkLightObject_h
19 #define itkLightObject_h
20 
21 #include "itkMacro.h"
22 #include "itkSmartPointer.h"
23 #include "itkTimeStamp.h"
24 #include "itkIndent.h"
25 #include <atomic>
26 
27 #include <iostream>
28 #include <typeinfo>
29 
30 #if defined( _WIN32 )
31 // To get LONG defined
32   #include "itkWindows.h"
33 #elif defined( __APPLE__ )
34 // To get MAC_OS_X_VERSION_MIN_REQUIRED defined
35   #include <AvailabilityMacros.h>
36 #endif
37 
38 namespace itk
39 {
40 /** \class LightObject
41  * \brief Light weight base class for most itk classes.
42  *
43  * LightObject is the highest level base class for most itk objects. It
44  * implements reference counting and the API for object printing.
45  * It can be used as a lightweight base class in preference to Object.
46  * (LightObject does not support callbacks or modified time as Object
47  * does.) All ITK objects should be a subclass of LightObject or Object
48  * with few exceptions (due to performance concerns).
49  *
50  * \sa Object
51  * \ingroup ITKSystemObjects
52  * \ingroup DataRepresentation
53  * \ingroup ITKCommon
54  */
55 class ITKCommon_EXPORT LightObject
56 {
57 public:
58   ITK_DISALLOW_COPY_AND_ASSIGN(LightObject);
59 
60   /** Standard class type aliases. */
61   using Self = LightObject;
62   using Pointer = SmartPointer< Self >;
63   using ConstPointer = SmartPointer< const Self >;
64 
65   /** Method for creation through the object factory. */
66   static Pointer New();
67 
68   /** Create an object from an instance, potentially deferring to a
69    * factory.  This method allows you to create an instance of an
70    * object that is exactly the same type as the referring object.
71    * This is useful in cases where an object has been cast back to a
72    * base class. */
73   virtual Pointer CreateAnother() const;
74 
75   itkCloneMacro(Self);
76 
77   /** Delete an itk object.  This method should always be used to delete an
78    * object when the new operator was used to create it. Using the C
79    *  delete method will not work with reference counting.  */
80   virtual void Delete();
81 
82   /** Return the name of this class as a string. Used by the object factory
83    * (implemented in New()) to instantiate objects of a named type. Also
84    * used for debugging and other output information.  */
85   virtual const char * GetNameOfClass() const;
86 
87 #ifdef _WIN32
88   /** Used to avoid dll boundary problems.  */
89   void * operator new(size_t);
90 
91   void * operator new[](size_t);
92 
93   void operator delete(void *);
94 
95   void operator delete[](void *, size_t);
96 
97 #endif
98 
99   /** Cause the object to print itself out. */
100   void Print(std::ostream & os, Indent indent = 0) const;
101 
102   /** This method is called when itkExceptionMacro executes. It allows
103    * the debugger to break on error.  */
104   static void BreakOnError();
105 
106   /** Increase the reference count (mark as used by another object).  */
107   virtual void Register() const;
108 
109   /** Decrease the reference count (release by another object).  */
110   virtual void UnRegister() const noexcept;
111 
112   /** Gets the reference count on this object. */
GetReferenceCount()113   virtual int GetReferenceCount() const
114   { return m_ReferenceCount; }
115 
116   /** Sets the reference count on this object. This is a dangerous
117    * method, use it with care. */
118   virtual void SetReferenceCount(int);
119 
120 protected:
121   LightObject();
122   virtual ~LightObject();
123 
124   /** Methods invoked by Print() to print information about the object
125    * including superclasses. Typically not called by the user (use Print()
126    * instead) but used in the hierarchical print process to combine the
127    * output of several classes.  */
128   virtual void PrintSelf(std::ostream & os, Indent indent) const;
129 
130   virtual void PrintHeader(std::ostream & os, Indent indent) const;
131 
132   virtual void PrintTrailer(std::ostream & os, Indent indent) const;
133 
134   /**
135    * Actual implementation of the clone method. This method should be reimplemeted
136    * in subclasses to clone the extra required parameters.
137    */
138   virtual LightObject::Pointer InternalClone() const;
139 
140   /** Number of uses of this object by other objects. */
141   mutable std::atomic<int> m_ReferenceCount;
142 
143 };
144 
145 /**
146  * This operator allows all subclasses of LightObject to be printed via <<.
147  * It in turn invokes the Print method, which in turn will invoke the
148  * PrintSelf method that all objects should define, if they have anything
149  * interesting to print out.
150  */
151 ITKCommon_EXPORT std::ostream &
152 operator<<(std::ostream & os, const LightObject & o);
153 
154 } // end namespace itk
155 
156 #endif
157