diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2017-04-10 13:51:29 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2017-04-10 13:51:29 -0400 |
commit | ff9398052df511e7789b697e1be3bd940d45d74a (patch) | |
tree | 55f878605dc8bc45cd175b2a8a9dc29f9e7fefb7 | |
parent | 649cd9085efb2cb4e21eec3b3861f46e89ba3a34 (diff) |
Improve castNode notation by introducing list-extraction-specific variants.
This extends the castNode() notation introduced by commit 5bcab1114 to
provide, in one step, extraction of a list cell's pointer and coercion to
a concrete node type. For example, "lfirst_node(Foo, lc)" is the same
as "castNode(Foo, lfirst(lc))". Almost half of the uses of castNode
that have appeared so far include a list extraction call, so this is
pretty widely useful, and it saves a few more keystrokes compared to the
old way.
As with the previous patch, back-patch the addition of these macros to
pg_list.h, so that the notation will be available when back-patching.
Patch by me, after an idea of Andrew Gierth's.
Discussion: https://postgr.es/m/14197.1491841216@sss.pgh.pa.us
-rw-r--r-- | src/include/nodes/pg_list.h | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/src/include/nodes/pg_list.h b/src/include/nodes/pg_list.h index 16040f48dc1..67204b4c2e0 100644 --- a/src/include/nodes/pg_list.h +++ b/src/include/nodes/pg_list.h @@ -115,26 +115,32 @@ list_length(const List *l) #define lfirst(lc) ((lc)->data.ptr_value) #define lfirst_int(lc) ((lc)->data.int_value) #define lfirst_oid(lc) ((lc)->data.oid_value) +#define lfirst_node(type,lc) castNode(type, lfirst(lc)) #define linitial(l) lfirst(list_head(l)) #define linitial_int(l) lfirst_int(list_head(l)) #define linitial_oid(l) lfirst_oid(list_head(l)) +#define linitial_node(type,l) castNode(type, linitial(l)) #define lsecond(l) lfirst(lnext(list_head(l))) #define lsecond_int(l) lfirst_int(lnext(list_head(l))) #define lsecond_oid(l) lfirst_oid(lnext(list_head(l))) +#define lsecond_node(type,l) castNode(type, lsecond(l)) #define lthird(l) lfirst(lnext(lnext(list_head(l)))) #define lthird_int(l) lfirst_int(lnext(lnext(list_head(l)))) #define lthird_oid(l) lfirst_oid(lnext(lnext(list_head(l)))) +#define lthird_node(type,l) castNode(type, lthird(l)) #define lfourth(l) lfirst(lnext(lnext(lnext(list_head(l))))) #define lfourth_int(l) lfirst_int(lnext(lnext(lnext(list_head(l))))) #define lfourth_oid(l) lfirst_oid(lnext(lnext(lnext(list_head(l))))) +#define lfourth_node(type,l) castNode(type, lfourth(l)) #define llast(l) lfirst(list_tail(l)) #define llast_int(l) lfirst_int(list_tail(l)) #define llast_oid(l) lfirst_oid(list_tail(l)) +#define llast_node(type,l) castNode(type, llast(l)) /* * Convenience macros for building fixed-length lists @@ -209,6 +215,7 @@ extern List *list_truncate(List *list, int new_size); extern void *list_nth(const List *list, int n); extern int list_nth_int(const List *list, int n); extern Oid list_nth_oid(const List *list, int n); +#define list_nth_node(type,list,n) castNode(type, list_nth(list, n)) extern bool list_member(const List *list, const void *datum); extern bool list_member_ptr(const List *list, const void *datum); |