What about a castNode() macro?

Started by Andres Freundalmost 12 years ago2 messageshackers
Jump to latest
#1Andres Freund
andres@anarazel.de

Hi,

There's a repeated pattern of

Assert(IsA(ptr, nodetype));
foo = (nodetype *) ptr;

how about adding a castNode() that combines those? Something like:

#if !defined(USE_ASSERT_CHECKING)

#define castNode(nodeptr,_type_) \
((_type_ *) (nodeptr))

#elif defined(__GNUC__)

#define castNode(nodeptr,_type_) \
((_type_ *) ({ \
Node *_result; \
_result = nodeptr; \
Assert(IsA(_result, _type_)); \
_result; \
}))

#else

extern PGDLLIMPORT Node *newNodeMacroHolder;
#define castNode(nodePtr,_type_) \
( \
newNodeMacroHolder = nodePtr, \
AssertMacro(IsA(newNodeMacroHolder, _type_)), \
(_type_ *) newNodeMacroHolder \
)

#endif

Greetings,

Andres Freund

--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, 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

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Andres Freund (#1)
Re: What about a castNode() macro?

Andres Freund <andres@2ndquadrant.com> writes:

There's a repeated pattern of

Assert(IsA(ptr, nodetype));
foo = (nodetype *) ptr;

how about adding a castNode() that combines those?

Doesn't really seem worth it. The notational advantage is not great,
and to the extent that it were to touch a lot of places, the main outcome
would be pain for back-patching.

regards, tom lane

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers