Jump to content
  • Advertisement
Sign in to follow this  
CodaKiller

boost::shared_ptr == CComPtr?

This topic is 3290 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well I am looking for a smart pointer class to replace CComPtr, I got the_COM_SMARTPTR_TYPEDEF macro to work perfectly but since I have to include boost into my project I may as well use it for something but does boost::shared_ptr have a similar function to CoCreateInstance or CreateInstance?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Sneftel
clicky. If you like, it shouldn't be too hard to wrap a macro around that.


I think since it requires extra code to work properly I may as well stick with _COM_SMARTPTR_TYPEDEF I was hoping it would be a slightly more standard approach which can be applied to other things but it does not seem like it is. Thanks for the information though!

Share this post


Link to post
Share on other sites
I think intrusive_ptr would be a better fit if you wanted to use a boost-provided smart pointer. COM already has reference counting, no need to have shared_ptr manage its own refcount.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sc4Freak
I think intrusive_ptr would be a better fit if you wanted to use a boost-provided smart pointer. COM already has reference counting, no need to have shared_ptr manage its own refcount.


Again how would I create an instance of a COM object with intrusive_ptr?

Share this post


Link to post
Share on other sites
Quote:
Original post by Sc4Freak
I think intrusive_ptr would be a better fit if you wanted to use a boost-provided smart pointer. COM already has reference counting, no need to have shared_ptr manage its own refcount.


again how would I create an instance of a com object with intrusive_ptr?

Share this post


Link to post
Share on other sites
// Global namespace:
template < typename ComType > typename boost::enable_if<boost::is_base_of<IUnknown,ComType>,void>::type intrusive_ptr_add_ref( ComType* ptr ) {
ptr->AddRef();
}
template < typename ComType > boost::enable_if<boost::is_base_of<IUnknown,ComType>,void>::type intrusive_ptr_release( ComType* ptr ) {
ptr->Release();
}

namespace yourproj {
boost::intrusive_ptr<IDirect3D9> Direct3DCreate9( UINT sdkver ) {
return boost::intrusive_ptr<IDirect3D9>( ::Direct3DCreate9(sdkver), false ); // interfaces start at 1 refcount, passing false here prevents us from adding another
}
...
void assuming_we_dont_wrap_createdevice() {
boost::intrusive_ptr<IDirect3D9> d3d = Direct3DCreate(...);

IDirect3DDevice9* rawdevice;
d3d->CreateDevice( ..., &rawdevice );
boost::intrusive_ptr<IDirect3DDevice9> device( rawdevice, false );
boost::intrusive_ptr<IDirect3DDevice9> anotherpointerforshitsandgiggles = device;
}

boost::intrusive_ptr<IDirect3DDevice9> WrapCreateDevice(
intrusive_ptr<IDirect3D9> d3d,
UINT Adapter,
D3DDEVTYPE DeviceType,
HWND hFocusWindow,
DWORD BehaviorFlags,
D3DPRESENT_PARAMETERS *pPresentationParameters
) {
IDirect3DDevice9* rawptr;
HRESULT hr = d3d->CreateDevice( Adapter, DeviceType, hFocusWindow, BehaviorFlags, &rawptr );
if (S_OK(hr)) return boost::intrusive_ptr<IDirect3DDevice9>(rawptr,false);
else throw exception...
}
}


Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
*** Source Snippet Removed ***


I'm using SAPI, I don't understand how to translate this code to create an instance for SAPI objects?

Share this post


Link to post
Share on other sites
You can wrap the calls to CoCreateInstance just like I've wrapped the call to CreateDevice (the only difference being CreateDevice is a member function, and so requires the extra parameter).

e.g. call it as normal, then create an intrusive_ptr<T>( pointer, false ); from the raw pointer.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!