[HACKERS] safer node casting
От
Peter Eisentraut
Тема
[HACKERS] safer node casting
Дата
Msg-id
c5d387d9-3440-f5e0-f9d4-71d53b9fbe52@2ndquadrant.com
Список
Дерево обсуждения
[HACKERS] safer node casting Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
Re: [HACKERS] safer node casting Andres Freund <andres@anarazel.de>
Re: [HACKERS] safer node casting Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>
Re: [HACKERS] safer node casting Andres Freund <andres@anarazel.de>
Re: [HACKERS] safer node casting Andres Freund <andres@anarazel.de>
Re: [HACKERS] safer node casting Tom Lane <tgl@sss.pgh.pa.us>
Re: [HACKERS] safer node casting Andres Freund <andres@anarazel.de>
Re: [HACKERS] safer node casting Tom Lane <tgl@sss.pgh.pa.us>
Re: [HACKERS] safer node casting Andres Freund <andres@anarazel.de>
Re: [HACKERS] safer node casting Tom Lane <tgl@sss.pgh.pa.us>
Re: [HACKERS] safer node casting Andres Freund <andres@anarazel.de>
Re: [HACKERS] safer node casting Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
Re: [HACKERS] safer node casting Jeff Janes <jeff.janes@gmail.com>
Re: [HACKERS] safer node casting Tom Lane <tgl@sss.pgh.pa.us>
Re: [HACKERS] safer node casting Andres Freund <andres@anarazel.de>
Re: [HACKERS] safer node casting Tom Lane <tgl@sss.pgh.pa.us>
Re: [HACKERS] safer node casting Andres Freund <andres@anarazel.de>
Re: [HACKERS] safer node casting Andres Freund <andres@anarazel.de>
Re: [HACKERS] safer node casting Tom Lane <tgl@sss.pgh.pa.us>
Re: [HACKERS] safer node casting Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
Re: [HACKERS] safer node casting Tom Lane <tgl@sss.pgh.pa.us>
Re: [HACKERS] safer node casting Tom Lane <tgl@sss.pgh.pa.us>
Re: [HACKERS] safer node casting Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>
There is a common coding pattern that goes like this:
RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
Assert(IsA(rinfo, RestrictInfo));
(Arguably, the Assert should come before the cast, but I guess it's done
this way out of convenience.)
(Not to mention the other common coding pattern of just doing the cast
and hoping for the best.)
I propose a macro castNode() that combines the assertion and the cast,
so this would become
RestrictInfo *rinfo = castNode(RestrictInfo, lfirst(lc));
This is inspired by the dynamic_cast operator in C++, but follows the
syntax of the well-known makeNode() macro.
Besides saving a bunch of code and making things safer, the function
syntax also makes some code easier to read by saving levels of
parentheses, for example:
- Assert(IsA(sstate->testexpr, BoolExprState));
- oplist = ((BoolExprState *) sstate->testexpr)->args;
+ oplist = castNode(BoolExprState, sstate->testexpr)->args;
Attached is a patch that shows how this would work. There is a lot more
that can be done, but I just stopped after a while for now.
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
В списке pgsql-hackers по дате отправления