3 * Copyright (c) 2007-2010 SlimDX Group
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
6 * of this software and associated documentation files (the "Software"), to deal
\r
7 * in the Software without restriction, including without limitation the rights
\r
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
9 * copies of the Software, and to permit persons to whom the Software is
\r
10 * furnished to do so, subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in
\r
13 * all copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
24 #include "ComObject.h"
\r
25 #include "Configuration.h"
\r
27 using namespace System;
\r
31 ComObject::ComObject()
\r
35 ComObject::~ComObject()
\r
37 if( m_Owner != nullptr )
\r
43 bool ComObject::Disposed::get()
\r
45 if( m_Unknown == 0 )
\r
48 if( !ObjectTable::Contains( this ) )
\r
54 IntPtr ComObject::ComPointer::get()
\r
56 return IntPtr( m_Unknown );
\r
59 ComObject^ ComObject::Owner::get()
\r
64 void ComObject::Owner::set( ComObject^ value )
\r
69 IUnknown* ComObject::UnknownPointer::get()
\r
74 IUnknown* ComObject::InternalPointer::get()
\r
79 void ComObject::SetFlags( ComObjectFlags flags )
\r
84 void ComObject::SetSource( System::Diagnostics::StackTrace^ stack )
\r
89 System::Diagnostics::StackTrace^ ComObject::CreationSource::get()
\r
94 void ComObject::SetCreationTime( int time )
\r
96 m_CreationTime = time;
\r
99 int ComObject::CreationTime::get()
\r
101 return m_CreationTime;
\r
104 void ComObject::Construct( IUnknown* pointer )
\r
106 Construct( pointer, nullptr );
\r
109 void ComObject::Construct( IUnknown* pointer, ComObject^ owner )
\r
112 throw gcnew ArgumentNullException( "pointer" );
\r
114 m_Unknown = pointer;
\r
115 ObjectTable::Add( this, owner );
\r
118 void ComObject::Construct( IntPtr pointer, Guid guid )
\r
120 if( pointer == IntPtr::Zero )
\r
121 throw gcnew ArgumentNullException( "pointer" );
\r
124 IUnknown* unknown = static_cast<IUnknown*>( pointer.ToPointer() );
\r
125 HRESULT hr = unknown->QueryInterface( Utilities::ConvertManagedGuid( guid ), &result );
\r
127 throw gcnew InvalidCastException( "Failed to QueryInterface on user-supplied pointer." );
\r
129 Construct( unknown );
\r
132 void ComObject::Destruct()
\r
134 if( m_Unknown == 0 || !ObjectTable::Remove( this ) )
\r
136 // test for CreationTime is to avoid throwing ObjectDisposedException when
\r
137 // a constructor for an object fails
\r
138 if( Configuration::DetectDoubleDispose && CreationTime != 0 )
\r
140 Type^ myType = GetType();
\r
141 throw gcnew ObjectDisposedException( myType->FullName );
\r
146 if( static_cast<int>( m_Flags & ComObjectFlags::IsAncillary ) == 0 )
\r
147 m_Unknown->Release();
\r