RAII pointers
Russel Winder via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jun 3 10:40:32 PDT 2017
Thanks to Moritz and Stanislav for their examples, most useful. There
are similarities (which I have just taken :-) but also some
differences. Would one be considered more idiomatic D, or is it a
question of different circumstances different approaches. The
differences are mainly in construction I believe.
On Tue, 2017-05-30 at 00:31 +0000, Moritz Maxeiner via Digitalmars-d-
learn wrote:
> […]
>
> struct FrontendParameters_Ptr {
> private:
> dvb_v5_fe_parms* ptr;
> public:
> @disable this(this);
> this(ref const FrontendId fei, const uint verbose = 0, const
> uint legacy = 0) { ... }
> ~this() { dvb_fe_close(ptr); }
> auto c_ptr() const { return ptr; }
> alias c_ptr this;
> }
> ---
On Tue, 2017-05-30 at 00:32 +0000, Stanislav Blinov via Digitalmars-d-
learn wrote:
[…]
>
> struct FrontendParametersPtr
> {
> // No constructors, initialization with parameters
> // is done via the frontendParametersPtr function
> @disable this(this);
>
> ~this()
> {
> // null check is often useful to detect e.g.
> // if this object has been `move`d
> if (_ptr) dvb_fe_close(_ptr);
> }
>
> // with DIP1000, could also return `scope`
> inout(dvb_v5_fe_parms)* ptr() inout { return _ptr; }
> alias ptr this;
> package:
>
> void construct(/*your args here*/) { /*...*/ }
>
> private:
> dvb_v5_fe_parms* _ptr;
> }
>
> /// Replaces constructor, i.e. can be called with no arguments for
> /// replacing "default" construction of C++
> auto frontendParametersPtr(Args...)(auto ref Args args)
> {
> import std.functional : forward;
> FrontendParametersPtr result = void;
> result.construct(forward!args);
> return result; // moves result, no copy is made
> }
>
[…]
--
Russel.
=============================================================================
Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder at ekiga.net
41 Buckmaster Road m: +44 7770 465 077 xmpp: russel at winder.org.uk
London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20170603/6d9d6742/attachment.sig>
More information about the Digitalmars-d-learn
mailing list