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 itkEventObject_h 19 #define itkEventObject_h 20 21 #include "itkIndent.h" 22 23 namespace itk 24 { 25 /** \class EventObject 26 * \brief Abstraction of the Events used to communicating among filters 27 and with GUIs. 28 * 29 * EventObject provides a standard coding for sending and receiving messages 30 * indicating things like the initiation of processes, end of processes, 31 * modification of filters. 32 * 33 * EventObjects form a hierarchy similar to the itk::ExceptionObject allowing 34 * to factorize common events in a tree-like structure. Higher detail can 35 * be assigned by users by subclassing existing itk::EventObjects. 36 * 37 * EventObjects are used by itk::Command and itk::Object for implementing the 38 * Observer/Subject design pattern. Observers register their interest in 39 * particular kinds of events produced by a specific itk::Object. This 40 * mechanism decouples classes among them. 41 * 42 * As opposed to itk::Exception, itk::EventObject does not represent error 43 * states, but simply flow of information allowing to trigger actions 44 * as a consequence of changes occurring in state on some itk::Objects. 45 * 46 * itk::EventObject carries information in its own type, it relies on the 47 * appropriate use of the RTTI (Run Time Type Information). 48 * 49 * A set of standard EventObjects is defined near the end of itkEventObject.h. 50 * 51 * \sa itk::Command 52 * \sa itk::ExceptionObject 53 * 54 * \ingroup ITKSystemObjects 55 * \ingroup ITKCommon 56 */ 57 class ITKCommon_EXPORT EventObject 58 { 59 public: 60 /** Constructor and copy constructor. Note that these functions will be 61 * called when children are instantiated. */ 62 EventObject() = default; 63 64 EventObject(const EventObject &)= default; 65 66 /** Virtual destructor needed */ 67 virtual ~EventObject() = default; 68 69 /** Create an Event of this type This method work as a Factory for 70 * creating events of each particular type. */ 71 virtual EventObject * MakeObject() const = 0; 72 73 /** Print Event information. This method can be overridden by 74 * specific Event subtypes. The default is to print out the type of 75 * the event. */ 76 virtual void Print(std::ostream & os) const; 77 78 /** Return the StringName associated with the event. */ 79 virtual const char * GetEventName() const = 0; 80 81 /** Check if given event matches or derives from this event. */ 82 virtual bool CheckEvent(const EventObject *) const = 0; 83 84 protected: 85 /** Methods invoked by Print() to print information about the object 86 * including superclasses. Typically not called by the user (use Print() 87 * instead) but used in the hierarchical print process to combine the 88 * output of several classes. */ 89 virtual void PrintSelf(std::ostream & os, Indent indent) const; 90 91 virtual void PrintHeader(std::ostream & os, Indent indent) const; 92 93 virtual void PrintTrailer(std::ostream & os, Indent indent) const; 94 95 private: 96 void operator=(const EventObject &) = delete; 97 }; 98 99 /** Generic inserter operator for EventObject and its subclasses. */ 100 inline std::ostream & operator<<(std::ostream & os, EventObject & e) 101 { 102 ( &e )->Print(os); 103 return os; 104 } 105 106 107 #define ITKEvent_EXPORT ITKCommon_EXPORT 108 109 /** 110 * Macros for creating new Events 111 */ 112 113 #define itkEventMacroDeclaration(classname, super) \ 114 /** \class classname */ \ 115 class ITKEvent_EXPORT classname:public super \ 116 { \ 117 public: \ 118 using Self = classname; \ 119 using Superclass = super; \ 120 classname(); \ 121 classname(const Self &s); \ 122 virtual ~classname(); \ 123 virtual const char *GetEventName() const; \ 124 virtual bool CheckEvent(const::itk::EventObject * e) const; \ 125 virtual ::itk::EventObject *MakeObject() const; \ 126 private: \ 127 void operator=(const Self &); \ 128 }; 129 130 #define itkEventMacroDefinition(classname, super) \ 131 classname::classname() {} \ 132 classname::classname(const classname &s):super(s){}; \ 133 classname::~classname() {} \ 134 const char * classname::GetEventName() const { return #classname; } \ 135 bool classname::CheckEvent(const::itk::EventObject * e) const \ 136 { return ( dynamic_cast< const classname * >( e ) != nullptr ); } \ 137 ::itk::EventObject *classname::MakeObject() const { return new classname; } \ 138 139 // 140 // This macro duplicates some of the declaration and definition 141 // macro code. The purpose is to provide a backward compatibility API 142 // for ITK applications. 143 // NOTE: New applications should use itkEventMacroDeclaration (in a 144 // .h file) and itkEventMacroDefinition (in a compiled .cxx 145 // file). This new approach guarantees that only one copy of the 146 // implementation will be present. 147 // 148 #define itkEventMacro(classname, super) \ 149 /** \class classname */ \ 150 class ITKEvent_EXPORT classname:public super \ 151 { \ 152 public: \ 153 using Self = classname; \ 154 using Superclass = super; \ 155 classname() {} \ 156 virtual ~classname() {} \ 157 virtual const char *GetEventName() const { return #classname; } \ 158 virtual bool CheckEvent(const::itk::EventObject * e) const \ 159 { return ( dynamic_cast< const Self * >( e ) != nullptr ); } \ 160 virtual::itk::EventObject *MakeObject() const \ 161 { return new Self; } \ 162 classname(const Self &s):super(s){}; \ 163 private: \ 164 void operator=(const Self &); \ 165 }; 166 167 /** 168 * Delclare some common ITK events 169 */ 170 itkEventMacroDeclaration(NoEvent, EventObject) 171 itkEventMacroDeclaration(AnyEvent, EventObject) 172 itkEventMacroDeclaration(DeleteEvent, AnyEvent) 173 itkEventMacroDeclaration(StartEvent, AnyEvent) 174 itkEventMacroDeclaration(EndEvent, AnyEvent) 175 itkEventMacroDeclaration(ProgressEvent, AnyEvent) 176 itkEventMacroDeclaration(ExitEvent, AnyEvent) 177 itkEventMacroDeclaration(AbortEvent, AnyEvent) 178 itkEventMacroDeclaration(ModifiedEvent, AnyEvent) 179 itkEventMacroDeclaration(InitializeEvent, AnyEvent) 180 itkEventMacroDeclaration(IterationEvent, AnyEvent) 181 itkEventMacroDeclaration(MultiResolutionIterationEvent,IterationEvent) 182 itkEventMacroDeclaration(PickEvent, AnyEvent) 183 itkEventMacroDeclaration(StartPickEvent, PickEvent) 184 itkEventMacroDeclaration(EndPickEvent, PickEvent) 185 itkEventMacroDeclaration(AbortCheckEvent, PickEvent) 186 itkEventMacroDeclaration(FunctionEvaluationIterationEvent, IterationEvent) 187 itkEventMacroDeclaration(GradientEvaluationIterationEvent, IterationEvent) 188 itkEventMacroDeclaration(FunctionAndGradientEvaluationIterationEvent, IterationEvent) 189 itkEventMacroDeclaration(UserEvent, AnyEvent) 190 191 #undef ITKEvent_EXPORT 192 #define ITKEvent_EXPORT ITK_ABI_EXPORT 193 194 } // end namespace itk 195 196 #endif 197