220 21015 <CAHJ4w5rGWn-2PsG16TpQ=8ZRFA4qfFXQC97JgOJF6pGeAd7D4g@mail.gmail.com> article
Path: news.gmane.org!not-for-mail
From: Christopher Horvath <blackencino@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Re: optional_ptr - A smart pointer which
 optionally owns the resource
Date: Tue, 29 Sep 2015 15:57:06 -0700
Lines: 184
Approved: news@gmane.org
Message-ID: <CAHJ4w5rGWn-2PsG16TpQ=8ZRFA4qfFXQC97JgOJF6pGeAd7D4g@mail.gmail.com>
References: <7502c195-e5b9-4f04-8997-666933cef239@isocpp.org>
	<CAOHCbiv5dTMZreCAu=ihJOJVAXNk85HxW2ex33NJVTSF30psUw@mail.gmail.com>
	<fd011b4d-0217-43e1-85d6-7e6f7ff25a92@isocpp.org>
	<muf47a$23u$1@ger.gmane.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=001a113f8e26ac38a30520eabb92
X-Trace: ger.gmane.org 1443567430 13697 80.91.229.3 (29 Sep 2015 22:57:10 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Tue, 29 Sep 2015 22:57:10 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBC2YFPWU5AOBBQ5OVSYAKGQEBZPOP6Y@isocpp.org Wed Sep 30 00:57:10 2015
Return-path: <std-proposals+bncBC2YFPWU5AOBBQ5OVSYAKGQEBZPOP6Y@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-vk0-f72.google.com ([209.85.213.72])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBC2YFPWU5AOBBQ5OVSYAKGQEBZPOP6Y@isocpp.org>)
	id 1Zh3pd-0006Y9-5j
	for gclcip-std-proposals@m.gmane.org; Wed, 30 Sep 2015 00:57:09 +0200
Original-Received: by vkgd64 with SMTP id d64sf28123790vkg.2
        for <gclcip-std-proposals@m.gmane.org>; Tue, 29 Sep 2015 15:57:08 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:mime-version:in-reply-to:references:date
         :message-id:subject:from:to:content-type:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=jlzxn7BH8PUHo8RQgTi/yIUpSD/dqtEyt9MAY/tVKSI=;
        b=Rx59J2H+m85l8WIfbP6VvZ7kDUPPFAP+dUyXzbhpL1T862Od5x+vDbP7sT08NOeHT8
         TwsRsNLWv9Ur7FD30IQLq1Ev2gnzAA8lS2Ua6Vw5nU/91WNz2oUrg96C1gQgZGqaMuOn
         ysfLwzg60RNJw5w9mp8Qmr4uv8u/LMrwIYNRsbBG2v290WXzNUG2fwmNZ/xtNGnKLKeU
         glu3+2cw1KSS4IE3yqquNna3hg8V6hgrTzkcQgirdR5S9oI0GJeknZ0nXTckwi8hg+6B
         oaDZR4spqs635zlxF2MbgFrQpo82513vE0tO2mBe15d0DodTLFByQ+XuZWukVOZR948Q
         53Qw==
X-Gm-Message-State: ALoCoQkmTCPwNqXKwb4L75wmfqhuI0LRUlMgTnCgZ6nsSS4T7ng58NGz20zbICZHA3tKn7AmLbOA
X-Received: by 10.13.233.197 with SMTP id s188mr453931ywe.23.1443567428323;
        Tue, 29 Sep 2015 15:57:08 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.50.29.36 with SMTP id g4ls1256437igh.31.canary; Tue, 29 Sep
 2015 15:57:07 -0700 (PDT)
X-Received: by 10.107.128.145 with SMTP id k17mr1632831ioi.24.1443567427312;
        Tue, 29 Sep 2015 15:57:07 -0700 (PDT)
Original-Received: from mail-io0-x22e.google.com (mail-io0-x22e.google.com. [2607:f8b0:4001:c06::22e])
        by mx.google.com with ESMTPS id i19si18616662igt.102.2015.09.29.15.57.07
        for <std-proposals@isocpp.org>
        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Tue, 29 Sep 2015 15:57:07 -0700 (PDT)
Received-SPF: pass (google.com: domain of blackencino@gmail.com designates 2607:f8b0:4001:c06::22e as permitted sender) client-ip=2607:f8b0:4001:c06::22e;
Original-Received: by iofb144 with SMTP id b144so28489986iof.1
        for <std-proposals@isocpp.org>; Tue, 29 Sep 2015 15:57:07 -0700 (PDT)
X-Received: by 10.107.9.151 with SMTP id 23mr1606089ioj.170.1443567426613;
 Tue, 29 Sep 2015 15:57:06 -0700 (PDT)
Original-Received: by 10.36.78.20 with HTTP; Tue, 29 Sep 2015 15:57:06 -0700 (PDT)
In-Reply-To: <muf47a$23u$1@ger.gmane.org>
X-Original-Sender: blackencino@gmail.com
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of blackencino@gmail.com designates 2607:f8b0:4001:c06::22e as
 permitted sender) smtp.mailfrom=blackencino@gmail.com;       dkim=pass
 header.i=@gmail.com;       dmarc=pass (p=NONE dis=NONE) header.from=gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Spam-Checked-In-Group: std-proposals@isocpp.org
X-Google-Group-Id: 399137483710
List-Post: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:21015
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/21015>

--001a113f8e26ac38a30520eabb92
Content-Type: text/plain; charset=UTF-8

I also run into this pattern fairly frequently. An example is in an OpenGL
renderer, where a user may want to share vertex buffers between multiple
meshes, but the default case is for the meshes to have unique vertex
buffers.  Right now, in different implementations I have one solution where
everything is managed in one central place, which avoids having to use
shared_ptr by making everything's reference essentially a weak_ptr.
However, this is a brittle design and requires frequent changes to multiple
code locations.  A much better solution would be to have this optional_ptr
(or whatever it ends up being called, I agree that optional_ptr is a bad
name), instead of what I have now:

// This is a sketch, please don't over-criticize!
class Mesh {
private:
    std::unique_ptr<VertexBuffer> m_unique_positions;
    std::shared_ptr<VertexBuffer> m_shared_positions;

public:
    // This assumes the invariant that one of the two pointers is non-null,
enforced elsewhere
    VertexBuffer& getVertexBuffer() { return m_unique_positions ?
*m_unique_positions : *m_shared_positions; }
};

C


On Tue, Sep 29, 2015 at 3:43 PM, Ross Smith <ross.smith@otoy.com> wrote:

> On 2015-09-30 07:38, Bengt Gustafsson wrote:
>
>> I see this pattern quite often. Usually you want to keep the option open
>> to send a pointer to a by value member to conserve allocations. Often in
>> framework code when you want to keep options open for framework users.
>>
>>
> One place I frequently need an optionally-owning pointer is in simple
> command line utilities, where the user can optionally supply a file name to
> read from (or write to), with a filename of "-" traditionally meaning "read
> from standard input" (or "write to standard output"). So a lot of programs
> intended to be run from the command line start with code like this (error
> checking skipped for simplicity):
>
>         string filename = argv[1];
>         shared_ptr<istream> in;
>         if (filename == "-")
>             in.reset(&cin, [] (void*) {});
>         else
>             in = make_shared<ifstream>(filename);
>         string line;
>         getline(*in, line);
>
> Another situation where I used an optionally-owning pointer recently is in
> an image manipulation class, where the image class object might own the
> image buffer itself, or might be operating on an image owned by some other
> entity.
>
> Ross Smith
>
>
>
> --
>
> --- 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.
> Visit this group at
> http://groups.google.com/a/isocpp.org/group/std-proposals/.
>

-- 

--- 
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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

--001a113f8e26ac38a30520eabb92
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">I also run into this pattern fairly frequently. An example=
 is in an OpenGL renderer, where a user may want to share vertex buffers be=
tween multiple meshes, but the default case is for the meshes to have uniqu=
e vertex buffers.=C2=A0 Right now, in different implementations I have one =
solution where everything is managed in one central place, which avoids hav=
ing to use shared_ptr by making everything&#39;s reference essentially a we=
ak_ptr.=C2=A0 However, this is a brittle design and requires frequent chang=
es to multiple code locations.=C2=A0 A much better solution would be to hav=
e this optional_ptr (or whatever it ends up being called, I agree that opti=
onal_ptr is a bad name), instead of what I have now:<div><br></div><div><fo=
nt face=3D"monospace, monospace" size=3D"1">// This is a sketch, please don=
&#39;t over-criticize!</font></div><div><font face=3D"monospace, monospace"=
 size=3D"1">class Mesh {</font></div><div><font face=3D"monospace, monospac=
e" size=3D"1">private:</font></div><div><font face=3D"monospace, monospace"=
 size=3D"1">=C2=A0 =C2=A0 std::unique_ptr&lt;VertexBuffer&gt; m_unique_posi=
tions;</font></div><div><font face=3D"monospace, monospace" size=3D"1">=C2=
=A0 =C2=A0 std::shared_ptr&lt;VertexBuffer&gt; m_shared_positions;</font></=
div><div><font face=3D"monospace, monospace" size=3D"1"><br></font></div><d=
iv><font face=3D"monospace, monospace" size=3D"1">public:</font></div><div>=
<font face=3D"monospace, monospace" size=3D"1">=C2=A0 =C2=A0 // This assume=
s the invariant that one of the two pointers is non-null, enforced elsewher=
e</font></div><div><font face=3D"monospace, monospace" size=3D"1">=C2=A0 =
=C2=A0 VertexBuffer&amp; getVertexBuffer() { return m_unique_positions ? *m=
_unique_positions : *m_shared_positions; }</font></div><div><font face=3D"m=
onospace, monospace" size=3D"1">};</font></div><div><br></div><div>C</div><=
div><br></div></div><div class=3D"gmail_extra"><br><div class=3D"gmail_quot=
e">On Tue, Sep 29, 2015 at 3:43 PM, Ross Smith <span dir=3D"ltr">&lt;<a hre=
f=3D"mailto:ross.smith@otoy.com" target=3D"_blank">ross.smith@otoy.com</a>&=
gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 =
0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=3D"">On 201=
5-09-30 07:38, Bengt Gustafsson wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
I see this pattern quite often. Usually you want to keep the option open to=
 send a pointer to a by value member to conserve allocations. Often in fram=
ework code when you want to keep options open for framework users.<br>
<br>
</blockquote>
<br></span>
One place I frequently need an optionally-owning pointer is in simple comma=
nd line utilities, where the user can optionally supply a file name to read=
 from (or write to), with a filename of &quot;-&quot; traditionally meaning=
 &quot;read from standard input&quot; (or &quot;write to standard output&qu=
ot;). So a lot of programs intended to be run from the command line start w=
ith code like this (error checking skipped for simplicity):<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 string filename =3D argv[1];<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 shared_ptr&lt;istream&gt; in;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (filename =3D=3D &quot;-&quot;)<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 in.reset(&amp;cin, [] (void*) {})=
;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 else<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 in =3D make_shared&lt;ifstream&gt=
;(filename);<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 string line;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 getline(*in, line);<br>
<br>
Another situation where I used an optionally-owning pointer recently is in =
an image manipulation class, where the image class object might own the ima=
ge buffer itself, or might be operating on an image owned by some other ent=
ity.<span class=3D"HOEnZb"><font color=3D"#888888"><br>
<br>
Ross Smith</font></span><div class=3D"HOEnZb"><div class=3D"h5"><br>
<br>
<br>
-- <br>
<br>
--- You received this message because you are subscribed to the Google Grou=
ps &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals%2Bunsubscribe@isocpp.org" target=3D=
"_blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/" rel=3D"noreferrer" target=3D"_blank">http://groups.google.c=
om/a/isocpp.org/group/std-proposals/</a>.<br>
</div></div></blockquote></div><br></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

--001a113f8e26ac38a30520eabb92--

.
