Topic: Add operator&() to std::unique_ptr to get internal pointer
Author: thestorm.nik@gmail.com
Date: Wed, 11 Apr 2018 15:22:39 -0700 (PDT)
Raw View
------=_Part_1511_64925966.1523485359311
Content-Type: multipart/alternative;
boundary="----=_Part_1512_1486863115.1523485359311"
------=_Part_1512_1486863115.1523485359311
Content-Type: text/plain; charset="UTF-8"
Hello,
There are many old APIs that are using double pointer as argument to return
an object. Wide usage examples are WinAPI, SDL, etc.
Currently you have to use one more temporary variable to pass it and then
assign it to std::unique_ptr.
*// Some utility header file...*
*struct ComCloser*
*{*
* constexpr ComCloser() noexcept = default;*
* void operator()( IUnknown* comObj ) noexcept*
* {*
* comObj->Release();*
* }*
*};*
*template<typename T>*
*using ComResource = std::unique_ptr<T, ComCloser>;*
*// Some cpp file*
*IWbemLocator* pLoc = nullptr;*
*HRESULT hres = CoCreateInstance( CLSID_WbemLocator, 0,
CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLoc );*
*_com_util::CheckError( hres ); // will throw exception on error*
*ComResource<IWbemLocator> locator( pLoc );*
By adding
*pointer* operator&()*
*{*
* reset();*
* return &_ptr;*
*}*
We can omit most of the cpp file code to become like
*// Some cpp file*
*ComResource<IWbemLocator> locator;*
*HRESULT hres = ::CoCreateInstance( CLSID_WbemLocator, 0,
CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&locator );*
*_com_util::CheckError( hres );*
Of course when using operator&() if there is any previous pointer it will
be destroyed by reset(); as it can be seen from my example.
Please provide feedback and suggestions. :)
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5198c390-26d7-4750-8065-eb6fe653b86c%40isocpp.org.
------=_Part_1512_1486863115.1523485359311
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Hello,<div><br></div><div>There are many old APIs that are=
using double pointer as argument to return an object. Wide usage examples =
are WinAPI, SDL, etc.</div><div>Currently you have to use one more temporar=
y variable to pass it and then assign it to std::unique_ptr.</div><div><br>=
</div><div><div><i>// Some utility header file...</i></div><div><i>struct C=
omCloser</i></div><div><i>{</i></div><div><i><span style=3D"white-space:pre=
"> </span>constexpr ComCloser() noexcept =3D default;</i></div><div><i><spa=
n style=3D"white-space:pre"> </span>void operator()( IUnknown* comObj ) noe=
xcept</i></div><div><i><span style=3D"white-space:pre"> </span>{</i></div><=
div><i><span style=3D"white-space:pre"> </span>comObj->Release();</i></=
div><div><i><span style=3D"white-space:pre"> </span>}</i></div><div><i>};</=
i></div><div><i><br></i></div><div><i>template<typename T></i></div><=
div><i>using ComResource =3D std::unique_ptr<T, ComCloser>;</i></div>=
<div><i><br></i></div><div><i>// Some cpp file</i></div><div><i>IWbemLocato=
r* pLoc =3D nullptr;</i></div><div><i>HRESULT hres =3D CoCreateInstance( CL=
SID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&p=
Loc );</i></div><div><i>_com_util::CheckError( hres ); // will throw except=
ion on error</i></div><div><i><br></i></div><div><i>ComResource<IWbemLoc=
ator> locator( pLoc );</i></div></div><div><br></div><div>By adding</div=
><div><div><i>pointer* operator&()</i></div><div><i>{</i></div><div><i>=
<span style=3D"white-space: pre;"> </span>reset();<br></i></div><div><i><sp=
an style=3D"white-space:pre"> </span>return &_ptr;</i></div><div><i>}</=
i></div></div><div><br></div><div>We can omit most of the cpp file code to =
become like</div><div><div><i>// Some cpp file</i></div><div><i>ComResource=
<IWbemLocator> locator;</i></div><div><i>HRESULT hres =3D ::CoCreateI=
nstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPV=
OID*)&locator );</i></div><div><i>_com_util::CheckError( hres );</i></d=
iv></div><div><br></div><div>Of course when using operator&() if there =
is any previous pointer it will be destroyed by reset(); as it can be seen =
from my example.</div><div>Please provide feedback and suggestions. :)</div=
></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/5198c390-26d7-4750-8065-eb6fe653b86c%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/5198c390-26d7-4750-8065-eb6fe653b86c=
%40isocpp.org</a>.<br />
------=_Part_1512_1486863115.1523485359311--
------=_Part_1511_64925966.1523485359311--
.