/*============================================================================= NOTE: THIS FILE IS A HANDMADE WRAPPER FOR THE ManagedITK PROJECT. Project: ManagedITK Program: Insight Segmentation & Registration Toolkit Module: itkManagedObject.cxx Language: C++/CLI Author: Dan Mueller $Date: 2007-04-11 16:26:16 +1000 (Wed, 11 Apr 2007) $ $Revision: 128 $ Copyright (c) Queensland University of Technology (QUT) 2007. All rights reserved. Portions of this code are covered under the ITK and VTK copyright. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =============================================================================*/ #pragma once #pragma warning( disable : 4635 ) // Disable warnings about XML doc comments #ifndef __itkManagedObject_cxx #define __itkManagedObject_cxx // Include some headers for string ops #include #include #include using std::string; // Include some useful ManagedITK files #include "itkManagedINativePointer.cxx" // Use some managed namespaces #using #using using namespace System; using namespace System::IO; using namespace System::Reflection; using namespace System::Diagnostics; using namespace System::Collections::Generic; namespace itk { /** Forward reference to itkObject */ ref class itkObject; ///A delegate for events sent from an itkObject. public delegate void itkObjectHandler(itkObject^ sender); ///A delegate for events sent from an itkObject, with a time. public delegate void itkTimedEventHandler(itkObject^ sender, DateTime time); /// ///This class is a managed replacement for itk::Object. /// /// ///Object is the second-highest level base class for most itk objects. ///It extends the base object functionality of LightObject by ///implementing callbacks (via object/observer), debug flags/methods, ///and modification time tracking. Most ITK classes should be a subclass ///of Object due to the need to keep track of modified time. /// public ref class itkObject abstract : INativePointer { private: String^ m_Name; Dictionary^ m_Metadata; itkTimedEventHandler^ m_EventStorage_Started; itkTimedEventHandler^ m_EventStorage_Ended; itkTimedEventHandler^ m_EventStorage_Aborted; itkObjectHandler^ m_EventStorage_Iteration; itkObjectHandler^ m_EventStorage_Modified; protected: bool m_IsDisposed; String^ m_MangledTypeString; ///Protected constructor. itkObject( ) { this->m_IsDisposed = false; this->m_MangledTypeString = String::Empty; } public: ///Gets if the object has been disposed. property bool IsDisposed { virtual bool get() { return this->m_IsDisposed; } } ///Get a [name, value] list of 'metadata'. /// ///This property was added to ManagedITK to easily create temporary ///(ie. non-persisent, not save to file) 'metadata'. /// property Dictionary^ Metadata { virtual Dictionary^ get() { // Create for first access if (this->m_Metadata == nullptr) this->m_Metadata = gcnew Dictionary(); // Return return this->m_Metadata; } } ///Get the last modified time. property unsigned long MTime { virtual unsigned long get()=0; } ///Get a string representing the type instance of this INativePointer. ///"IUC2", "IF3", "IUC2IUC2". property String^ MangledTypeString { virtual String^ get() { return this->m_MangledTypeString; } } /// ///Get/set the pointer to the native ITK object associated with ///this wrapper instance. /// ///The pointer to the native ITK object. property void* NativePointer{ virtual void* get()=0; virtual void set ( void* value )=0; } /// ///Remove all observers watching this object. /// /// ///By default, observers are created for all events. Calling this ///method removes all native observers, and therefore prevents ///the firing of managed events. Call AddAnyEventObserver() to ///reset the default observers which enable the managed events. /// virtual void RemoveAllObservers ( ) = 0; /// ///Adds a native observer watching for any event. /// /// ///By default, observers are created for all events. Calling this ///method adds a single native observer for any event. This ///observer invokes the managed events. Therefore, calling this ///method more than once, or calling it without first calling ///RemoveAllObservers() may cause the managed events to be ///invoked more than once. /// virtual void AddAnyEventObserver ( ) = 0; ///An event which is raised when the object is modified. event itkObjectHandler^ Modified { public: void add (itkObjectHandler^ handler) { m_EventStorage_Modified += handler; } void remove (itkObjectHandler^ handler) { m_EventStorage_Modified -= handler; } internal: void raise( itkObject^ obj ) { if (this->m_EventStorage_Modified != nullptr) this->m_EventStorage_Modified(obj); } } ///An event which is raised when the process is started. event itkTimedEventHandler^ Started { public: void add (itkTimedEventHandler^ handler) { m_EventStorage_Started += handler; } void remove (itkTimedEventHandler^ handler) { m_EventStorage_Started -= handler; } internal: void raise( itkObject^ obj, DateTime time ) { if (this->m_EventStorage_Started != nullptr) this->m_EventStorage_Started(obj, time); } } ///An event which is raised when the process has finished. event itkTimedEventHandler^ Ended { public: void add (itkTimedEventHandler^ handler) { m_EventStorage_Ended += handler; } void remove (itkTimedEventHandler^ handler) { m_EventStorage_Ended -= handler; } internal: void raise( itkObject^ obj, DateTime time ) { if (this->m_EventStorage_Ended != nullptr) this->m_EventStorage_Ended(obj, time); } } ///An event which is raised if the process is aborted. event itkTimedEventHandler^ Aborted { public: void add (itkTimedEventHandler^ handler) { m_EventStorage_Aborted += handler; } void remove (itkTimedEventHandler^ handler) { m_EventStorage_Aborted -= handler; } internal: void raise( itkObject^ obj, DateTime time ) { if (this->m_EventStorage_Aborted != nullptr) this->m_EventStorage_Aborted(obj, time); } } ///An event which is raised when the process iteration is updated. event itkObjectHandler^ Iteration { public: void add (itkObjectHandler^ handler) { m_EventStorage_Iteration += handler; } void remove (itkObjectHandler^ handler) { m_EventStorage_Iteration -= handler; } internal: void raise( itkObject^ obj ) { if (this->m_EventStorage_Iteration != nullptr) this->m_EventStorage_Iteration(obj); } } ///Invoke the Started event. virtual void InvokeStartedEvent ( DateTime time ) { this->Started(this, time); } ///Invoke the Ended event. virtual void InvokeEndedEvent ( DateTime time ) { this->Ended(this, time); } ///Invoke the Aborted event. virtual void InvokeAbortedEvent ( DateTime time ) { this->Aborted(this, time); } ///Invoke the Iteration event. virtual void InvokeIterationEvent ( ) { this->Iteration( this ); } ///Invoke the Modified event. virtual void InvokeModifiedEvent ( ) { this->Modified(this); } internal: ///Marshal a managed string to the unmanaged heap. static void MarshalString ( System::String^ s, std::string& os ) { const char* chars = (const char*)(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(s)).ToPointer(); os = chars; System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr((void*)chars)); } }; // end ref class } // end namespace itk #endif