Topic: [std-proposals] Adding the Keyword proxy in C+
Author: Mingxin Wang <wmx16835vv@163.com>
Date: Wed, 10 May 2017 03:46:22 -0700 (PDT)
Raw View
------=_Part_4049_1454517741.1494413182510
Content-Type: multipart/alternative;
boundary="----=_Part_4050_911092216.1494413182511"
------=_Part_4050_911092216.1494413182511
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
This is an update version for another topic =E2=80=9CAdding the Keyword =E2=
=80=9Cinterface=E2=80=9D=20
in C++=20
<https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/uyfpeyE=
yW4o>=E2=80=9D=20
I started days before, which was an idea about automatically generating new=
=20
types which are implicitly convertible from any type that meets some=20
specific requirements. In this post, according to the feedbacks, two=20
adjustments are made to make the solution more reasonable comparing to the=
=20
initial version:
- Changing the keyword =E2=80=9Cinterface=E2=80=9D into =E2=80=9Cproxy=
=E2=80=9D (*thanks to Thiago=20
Macierira, Myriachan and Jakob Riedle*), and
- Changing the way of passing the concrete implementations by value into=
=20
by reference, so that polymorphism and lifetime management are decoupled=
(*thanks=20
to Bengt Gustafsson*).
The rules are not changed in defining =E2=80=9Cproxies=E2=80=9D (=E2=80=9Ci=
nterface=E2=80=9D in the initial=20
post):
proxy Runnable {
void operator()();
};
It is only allowed to initialize a proxy object with reference (lvalue):
auto lambda =3D [] { puts("Lambda"); };
Runnable r(lambda);
r();
The code below is ill-formed now (but leagal in the initial version):
Runnable r([] { puts("Lambda"); });
Here is a possible implementation to generate for the proxy defined above=
=20
(decouples polymorphism and lifetime management comparing to the initial=20
version, and no dynamic memory allocation is required any more):
class Runnable {
public:
template <class Data>
Runnable(Data& data) requires requires(Data& data) { { data() }; } {
Implementation<Data> a(data);
memcpy(&data_, &a, sizeof(Abstraction));
}
Runnable() =3D default;
Runnable(Runnable&&) =3D default;
Runnable(const Runnable&) =3D default;
Runnable& operator=3D(Runnable&&) =3D default;
Runnable& operator=3D(const Runnable&) =3D default;
void operator()() { reinterpret_cast<Abstraction*>(&data_)->op_0(); }
private:
class Abstraction {
public:
Abstraction(void* data) : data_(data) {}
virtual void op_0() =3D 0;
template <class T>
T* get() { return static_cast<T*>(data_); }
private:
void* data_;
};
template <class Data>
class Implementation final : public Abstraction {
public:
Implementation(Data& data) : Abstraction(&data) {}
void op_0() override { (*get<Data>())(); }
};
char data_[sizeof(Abstraction)];
};
I am looking forward to your comments and suggestions!
Thank you!
Mingxin Wang
--=20
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 e=
mail 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/974213b5-2afd-4c05-8ee2-d06de3e3cdd6%40isocpp.or=
g.
------=_Part_4050_911092216.1494413182511
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><font face=3D"georgia, serif">This is an update version fo=
r another topic =E2=80=9C<a href=3D"https://groups.google.com/a/isocpp.org/=
forum/#!topic/std-proposals/uyfpeyEyW4o">Adding the Keyword =E2=80=9Cinterf=
ace=E2=80=9D in C++</a>=E2=80=9D I started days before, which was an idea a=
bout automatically generating new types which are implicitly convertible fr=
om any type that meets some specific requirements. In this post, according =
to the feedbacks, two adjustments are made to make the solution more reason=
able comparing to the initial version:</font><div><div><ul><li><font face=
=3D"georgia, serif">Changing the keyword =E2=80=9Cinterface=E2=80=9D into =
=E2=80=9Cproxy=E2=80=9D (<b>thanks to Thiago Macierira, Myriachan and Jakob=
Riedle</b>), and<br></font></li><li><font face=3D"georgia, serif">Changing=
the way of passing the concrete implementations by value into by reference=
, so that polymorphism and lifetime management are decoupled (<b>thanks to =
Bengt Gustafsson</b>).</font></li></ul></div></div><div><div><font face=3D"=
georgia, serif">The rules are not changed in defining =E2=80=9Cproxies=E2=
=80=9D (=E2=80=9Cinterface=E2=80=9D in the initial post):</font></div></div=
><div><br></div><div><div class=3D"prettyprint" style=3D"border: 1px solid =
rgb(187, 187, 187); word-wrap: break-word; background-color: rgb(250, 250, =
250);"><code class=3D"prettyprint"><div class=3D"subprettyprint"><p class=
=3D"MsoNormal"><font color=3D"#000000"><span style=3D"color: #000;" class=
=3D"styled-by-prettify">proxy </span><span style=3D"color: #606;" class=3D"=
styled-by-prettify">Runnable</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">{</span></font></p><p class=3D"MsoNormal"><font color=3D"#000000">=
<span style=3D"color: #000;" class=3D"styled-by-prettify"> =C2=A0</span><sp=
an style=3D"color: #008;" class=3D"styled-by-prettify">void</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"col=
or: #008;" class=3D"styled-by-prettify">operator</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">()();</span></font></p><p class=3D"Ms=
oNormal"><font color=3D"#000000"><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">};</span></font></p></div></code></div><br></div><div><font=
face=3D"georgia, serif">It is only allowed to initialize a proxy object wi=
th reference (lvalue):</font><br></div><div><br></div><div><div class=3D"pr=
ettyprint" style=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-=
word; background-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><d=
iv class=3D"subprettyprint"><div class=3D"subprettyprint">auto lambda =3D [=
] { puts("Lambda"); };</div><div class=3D"subprettyprint">Runnabl=
e r(lambda);</div><div class=3D"subprettyprint">r();</div></div></code></di=
v><br></div><div><font face=3D"georgia, serif">The code below is ill-formed=
now (but leagal in the initial version):</font><br></div><div><br></div><d=
iv><div class=3D"prettyprint" style=3D"border: 1px solid rgb(187, 187, 187)=
; word-wrap: break-word; background-color: rgb(250, 250, 250);"><code class=
=3D"prettyprint"><div class=3D"subprettyprint"><font color=3D"#660066">Runn=
able r([] { puts("Lambda"); });</font><br></div></code></div><br>=
</div><div><font face=3D"georgia, serif">Here is a possible implementation =
to generate for the proxy defined above (decouples polymorphism and lifetim=
e management comparing to the initial version, and no=C2=A0dynamic memory=
=C2=A0allocation=C2=A0is=C2=A0required any more</font><span style=3D"font-f=
amily: georgia, serif;">):</span></div><div><br></div><div><div class=3D"pr=
ettyprint" style=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-=
word; background-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><d=
iv class=3D"subprettyprint"><font color=3D"#660066"><div class=3D"subpretty=
print">class Runnable {</div><div class=3D"subprettyprint">=C2=A0public:</d=
iv><div class=3D"subprettyprint">=C2=A0 template <class Data></div><d=
iv class=3D"subprettyprint">=C2=A0 Runnable(Data& data) requires requir=
es(Data& data) { { data() }; } {</div><div class=3D"subprettyprint">=C2=
=A0 =C2=A0 Implementation<Data> a(data);</div><div class=3D"subpretty=
print">=C2=A0 =C2=A0 memcpy(&data_, &a, sizeof(Abstraction));</div>=
<div class=3D"subprettyprint">=C2=A0 }</div><div class=3D"subprettyprint">=
=C2=A0 Runnable() =3D default;</div><div class=3D"subprettyprint">=C2=A0 Ru=
nnable(Runnable&&) =3D default;</div><div class=3D"subprettyprint">=
=C2=A0 Runnable(const Runnable&) =3D default;</div><div class=3D"subpre=
ttyprint">=C2=A0 Runnable& operator=3D(Runnable&&) =3D default;=
</div><div class=3D"subprettyprint">=C2=A0 Runnable& operator=3D(const =
Runnable&) =3D default;</div><div class=3D"subprettyprint"><br></div><d=
iv class=3D"subprettyprint">=C2=A0 void operator()() { reinterpret_cast<=
Abstraction*>(&data_)->op_0(); }</div><div class=3D"subprettyprin=
t"><br></div><div class=3D"subprettyprint">=C2=A0private:</div><div class=
=3D"subprettyprint">=C2=A0 class Abstraction {</div><div class=3D"subpretty=
print">=C2=A0 =C2=A0public:</div><div class=3D"subprettyprint">=C2=A0 =C2=
=A0 Abstraction(void* data) : data_(data) {}</div><div class=3D"subprettypr=
int">=C2=A0 =C2=A0 virtual void op_0() =3D 0;</div><div class=3D"subprettyp=
rint">=C2=A0 =C2=A0 template <class T></div><div class=3D"subprettypr=
int">=C2=A0 =C2=A0 T* get() { return static_cast<T*>(data_); }</div><=
div class=3D"subprettyprint"><br></div><div class=3D"subprettyprint">=C2=A0=
=C2=A0private:</div><div class=3D"subprettyprint">=C2=A0 =C2=A0 void* data=
_;</div><div class=3D"subprettyprint">=C2=A0 };</div><div class=3D"subprett=
yprint"><br></div><div class=3D"subprettyprint">=C2=A0 template <class D=
ata></div><div class=3D"subprettyprint">=C2=A0 class Implementation fina=
l : public Abstraction {</div><div class=3D"subprettyprint">=C2=A0 =C2=A0pu=
blic:</div><div class=3D"subprettyprint">=C2=A0 =C2=A0 Implementation(Data&=
amp; data) : Abstraction(&data) {}</div><div class=3D"subprettyprint">=
=C2=A0 =C2=A0 void op_0() override { (*get<Data>())(); }</div><div cl=
ass=3D"subprettyprint">=C2=A0 };</div><div class=3D"subprettyprint"><br></d=
iv><div class=3D"subprettyprint">=C2=A0 char data_[sizeof(Abstraction)];</d=
iv><div class=3D"subprettyprint">};</div></font></div></code></div></div><d=
iv><br></div><div><div><font face=3D"georgia, serif">I am looking forward t=
o your comments and suggestions!</font></div><div><font face=3D"georgia, se=
rif"><br></font></div><div><font face=3D"georgia, serif">Thank you!</font><=
/div><div><font face=3D"georgia, serif"><br></font></div><div><font face=3D=
"georgia, serif">Mingxin Wang</font></div></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/974213b5-2afd-4c05-8ee2-d06de3e3cdd6%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/974213b5-2afd-4c05-8ee2-d06de3e3cdd6=
%40isocpp.org</a>.<br />
------=_Part_4050_911092216.1494413182511--
------=_Part_4049_1454517741.1494413182510--
.