18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

C++ APPLICATION PROGRAMMING INTERFACE<br />

Replicating the functionality of instanceof (Java) or TypeOf (Visual<br />

Basic)<br />

It is common to have an interface pointer that could point to one of several<br />

coclasses. You can find out more information about the coclass by attempting to<br />

QI to other interfaces using if/else logic. For example, both the RasterDataset and<br />

FeatureDataset coclasses implement IDataset. If you are passed IDataset as a function<br />

parameter, you can determine which coclass the IDataset references as follows:<br />

void Foo (IDataset *pDataset)<br />

{<br />

IFeatureDatasetPtr ipFeatureDataset(pDataset);<br />

if (ipFeatureDataset != 0)<br />

{<br />

// Use IFeatureDataset methods.<br />

}<br />

else<br />

{<br />

IRasterDataset2Ptr ipRasterDataset(pDataset);<br />

if (ipRasterDataset!= 0)<br />

{<br />

// Use IRasterDataset2 methods.<br />

}<br />

}<br />

}<br />

Raw pointers in function signatures<br />

Rather than having a smart pointer in the function signature, consider using a raw<br />

pointer to save the overhead of a call to the smart pointer constructor upon<br />

invocation of the function. You can still pass smart pointer objects to functions<br />

since each smart pointer has an overloaded pointer operator that returns the<br />

underlying raw pointer. The following example illustrates this:<br />

HRESULT DoRasterOp(IRaster* pRaster); // Function dec: raw pointer<br />

IRasterPtr ipRaster;<br />

HRESULT hr = DoRasterOp(ipRaster);<br />

// Pass in smart pointer<br />

Return ArcObjects from functions<br />

This tip builds on the previous one. In this case, raw pointers are used in the<br />

function declaration, and a double indirection is used for the object that will be<br />

returned. This allows you to alter what the pointer you are passed points to.<br />

Next, initialize a smart pointer object with the value you want to return and<br />

assign it to the pointer you were passed.<br />

HRESULT GetTinWorkspace(char* path, ITinWorkspace** ppTinWorkspace)<br />

{<br />

if (!ppTinWorkspace)<br />

return E_POINTER;<br />

HRESULT hr = S_OK;<br />

IWorkspaceFactoryPtr ipWorkspaceFactory(CLSID_TinWorkspaceFactory);<br />

228 • <strong>ArcGIS</strong> <strong>Engine</strong> <strong>Developer</strong> <strong>Guide</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!