/*============================================================================= 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: $Date: 2008-06-21 09:20:09 +0200 (Sat, 21 Jun 2008) $ Revision: $Revision: 18 $ Portions of this code are covered under the ITK and VTK copyright. See http://www.itk.org/HTML/Copyright.htm for details. See http://www.kitware.com/VTKCopyright.htm for details. Copyright (c) 2008 Queensland University of Technology (QUT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. =============================================================================*/ #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; /// ///EventArgs subclass for managed ITK objects. /// public ref class itkEventArgs : System::EventArgs { public: ///Default constructor. itkEventArgs( ) : System::EventArgs( ) { } }; ///A delegate for events sent from an itkObject. public delegate void itkEventHandler(itkObject^ sender, itkEventArgs^ e); /// ///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; itkEventHandler^ m_EventStorage_Started; itkEventHandler^ m_EventStorage_Ended; itkEventHandler^ m_EventStorage_Aborted; itkEventHandler^ m_EventStorage_Iteration; itkEventHandler^ m_EventStorage_Modified; protected: bool m_IsDisposed; bool m_DisposeNativeObjectOnFinalize; String^ m_MangledTypeString; ///Protected constructor. itkObject( ) { this->m_IsDisposed = false; this->m_DisposeNativeObjectOnFinalize = true; this->m_MangledTypeString = String::Empty; } public: ///Gets if the object has been disposed. property bool IsDisposed { virtual bool get() { return this->m_IsDisposed; } } /// ///Gets/sets if the underlying native object should be disposed when the managed object is finalized. ///The default is true. /// property bool DisposeNativeObjectOnFinalize { virtual bool get() { return this->m_DisposeNativeObjectOnFinalize; } virtual void set( bool value ) { this->m_DisposeNativeObjectOnFinalize = value; } } ///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 IntPtr NativePointer{ virtual IntPtr get()=0; virtual void set ( IntPtr 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 itkEventHandler^ Modified { public: void add (itkEventHandler^ handler) { m_EventStorage_Modified += handler; } void remove (itkEventHandler^ handler) { m_EventStorage_Modified -= handler; } internal: void raise( itkObject^ obj, itkEventArgs^ e ) { if (this->m_EventStorage_Modified != nullptr) this->m_EventStorage_Modified(obj, e); } } ///An event which is raised when the process is started. event itkEventHandler^ Started { public: void add (itkEventHandler^ handler) { m_EventStorage_Started += handler; } void remove (itkEventHandler^ handler) { m_EventStorage_Started -= handler; } internal: void raise( itkObject^ obj, itkEventArgs^ e ) { if (this->m_EventStorage_Started != nullptr) this->m_EventStorage_Started(obj, e); } } ///An event which is raised when the process has finished. event itkEventHandler^ Ended { public: void add (itkEventHandler^ handler) { m_EventStorage_Ended += handler; } void remove (itkEventHandler^ handler) { m_EventStorage_Ended -= handler; } internal: void raise( itkObject^ obj, itkEventArgs^ e ) { if (this->m_EventStorage_Ended != nullptr) this->m_EventStorage_Ended(obj, e); } } ///An event which is raised if the process is aborted. event itkEventHandler^ Aborted { public: void add (itkEventHandler^ handler) { m_EventStorage_Aborted += handler; } void remove (itkEventHandler^ handler) { m_EventStorage_Aborted -= handler; } internal: void raise( itkObject^ obj, itkEventArgs^ e ) { if (this->m_EventStorage_Aborted != nullptr) this->m_EventStorage_Aborted(obj, e); } } ///An event which is raised when the process iteration is updated. event itkEventHandler^ Iteration { public: void add (itkEventHandler^ handler) { m_EventStorage_Iteration += handler; } void remove (itkEventHandler^ handler) { m_EventStorage_Iteration -= handler; } internal: void raise( itkObject^ obj, itkEventArgs^ e ) { if (this->m_EventStorage_Iteration != nullptr) this->m_EventStorage_Iteration(obj, e); } } ///Invoke the Started event. virtual void InvokeStartedEvent ( itkEventArgs^ e ) { this->Started(this, e); } ///Invoke the Ended event. virtual void InvokeEndedEvent ( itkEventArgs^ e ) { this->Ended(this, e); } ///Invoke the Aborted event. virtual void InvokeAbortedEvent ( itkEventArgs^ e ) { this->Aborted(this, e); } ///Invoke the Iteration event. virtual void InvokeIterationEvent ( itkEventArgs^ e ) { this->Iteration(this, e); } ///Invoke the Modified event. virtual void InvokeModifiedEvent ( itkEventArgs^ e ) { this->Modified(this, e); } 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