|
Boost : |
Subject: Re: [boost] boost::directx?
From: Zachary Turner (divisortheory_at_[hidden])
Date: 2009-06-08 01:40:35
On Sun, Jun 7, 2009 at 10:16 PM, Christian Schladetsch <
christian.schladetsch_at_[hidden]> wrote:
> Hello,
>
> I have decided to put my money where my mouth is, and have uploaded a
> proposal to the vault. It is at
>
> http://www.boostpro.com/vault/index.php?action=downloadfile&filename=make_vertex.zip&directory=&
> .
>
> The proposal is for a means to create DirectX vertex types, with the
> accompanying vertex declaration structure. I also added a simple
> com_ptr<T>,
> based on boost::shared_ptr<T>.
>
> If there is any interest in this, please let me know.
>
I do think it would be interesting to see what things could evolve out of
such a library, so hopefully there's other people on the list more
experienced with DirectX than I that could help it mature and perhaps add
some backend support for OpenGL.
That being said, below is my own com_ptr class.
Before posting the code though, here's how it can be used:
d3d_ptr<IDirectInputDevice8> keyboard_;
HR(direct_input_->CreateDevice(GUID_SysKeyboard, recv_d3d_ptr(keyboard_),
NULL));
Basically solving the problem of having to first declare a raw pointer, then
pass the address to a COM api, then initialize it in the constructor, this
does it all in one shot.
#define d3d_ptr boost::intrusive_ptr
inline void intrusive_ptr_add_ref(IUnknown* punk) { punk->AddRef(); }
inline void intrusive_ptr_release(IUnknown* punk) { punk->Release(); }
template<typename T>
d3d_ptr<T> take_ptr(T* t)
{
return d3d_ptr<T>(t, false);
}
template<typename T>
class recv_d3d_ptr_t
{
public:
recv_d3d_ptr_t(d3d_ptr<T>& r) : m_auto(r), m_raw(NULL) {}
~recv_d3d_ptr_t() {m_auto = take_ptr(m_raw);}
operator T**() { return &m_raw; }
operator void**() { return reinterpret_cast<void**>(&m_raw); }
private:
T* m_raw;
d3d_ptr<T>& m_auto;
};
template<typename T>
recv_d3d_ptr_t<T> recv_d3d_ptr(d3d_ptr<T>& rptr)
{
return recv_d3d_ptr_t<T>(rptr);
}
template<typename Dest, typename Src>
d3d_ptr<Dest> query_interface(Src p, const IID& iid = __uuidof(Dest))
{
void* pv = NULL;
p->QueryInterface(iid, &pv);
Dest* p2 = static_cast<Dest*>(pv);
return d3d_ptr<Dest>(p2, false);
}
Out of curiosity what was the motivation for deriving from shared_ptr
instead of using intrusive_ptr?
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk