VCOM:VCOMPtr

From Vectorworks Developer
Revision as of 15:09, 12 August 2013 by Root (talk | contribs) (1 revision)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

.SDK|SDK ..SDK:Types|SDK Types ..VCOM:VCOM (Vectorworks Component Object Model)|VCOM Basics ..VCOM:Class Reference|VCOM Class Reference

What is this?

Using VCOM:VCOM (VectorWorks Component Object Model) requires quering interfaces. The basic function for that is SDK:GS_VWQueryInterface.

However SDK:GS_VWQueryInterface function is tricky to use since it requires the developer to call VCOM:IUnknown::Release for the interface instances that are queried.

To simplify this there is the VCOMPtr<> template class that manages the queried interface instances.

template<class T> class VCOMPtr
{
public:
			VCOMPtr()			{ fPtr = NULL; }
			VCOMPtr(T* ptr)			{ fPtr = ptr; if ( fPtr ) { fPtr->AddRef(); } }
			VCOMPtr(const VWIID& iid)	{ if ( VCOM_FAILED( ::GS_VWQueryInterface( iid, (IVWUnknown**) & fPtr ) ) ) { fPtr = NULL; } }
			VCOMPtr(const VCOMPtr& p)	{ fPtr = p.fPtr; if ( fPtr ) { fPtr->AddRef(); } }
			~VCOMPtr()			{ this->Release(); }

	VCOMError	Query(const VWIID& iid)		{ this->Release(); VCOMError err = ::GS_VWQueryInterface( iid, (IVWUnknown**) & fPtr ); if ( VCOM_FAILED( err ) ) { fPtr = NULL; } return err; }
	long		Release()			{ long res = 0; if ( fPtr ) { res = fPtr->Release(); } fPtr = NULL; return res; }

	VCOMPtr&	operator=(T* ptr)		{ this->Release(); fPtr = ptr; if ( fPtr ) { fPtr->AddRef(); } return *this; }
	VCOMPtr&	operator=(const VCOMPtr& p)	{ this->Release(); fPtr = p.fPtr; if ( fPtr ) { fPtr->AddRef(); } return *this; }

	T*		operator->() const		{ return fPtr; }
	T**		operator&()			{ return & fPtr; }
			operator T*() const 		{ return fPtr; }

private:
	T*		fPtr;
};

Sample

{
  VCOMPtr    pSampleInit( IID_VCOMSample ); // this queries the interface
                                            // the interface instance
                                            // will be autmatically released
  if ( pSampleInit)  {                      // determine if it contains
                                            // valid interface pointer
      VCOMError   vcomErr    = pSampleInit->Function();
  }
}      // <- the destructor of 'pSampleInit' will release the interface here