Skip to content

Commit bcb1a27

Browse files
committed
Improve castNode notation by introducing list-extraction-specific variants.
This extends the castNode() notation introduced by commit 5bcab11 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
1 parent 5fcf1f4 commit bcb1a27

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/include/nodes/pg_list.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,32 @@ list_length(const List *l)
115115
#define lfirst(lc) ((lc)->data.ptr_value)
116116
#define lfirst_int(lc) ((lc)->data.int_value)
117117
#define lfirst_oid(lc) ((lc)->data.oid_value)
118+
#define lfirst_node(type,lc) castNode(type, lfirst(lc))
118119

119120
#define linitial(l) lfirst(list_head(l))
120121
#define linitial_int(l) lfirst_int(list_head(l))
121122
#define linitial_oid(l) lfirst_oid(list_head(l))
123+
#define linitial_node(type,l) castNode(type, linitial(l))
122124

123125
#define lsecond(l) lfirst(lnext(list_head(l)))
124126
#define lsecond_int(l) lfirst_int(lnext(list_head(l)))
125127
#define lsecond_oid(l) lfirst_oid(lnext(list_head(l)))
128+
#define lsecond_node(type,l) castNode(type, lsecond(l))
126129

127130
#define lthird(l) lfirst(lnext(lnext(list_head(l))))
128131
#define lthird_int(l) lfirst_int(lnext(lnext(list_head(l))))
129132
#define lthird_oid(l) lfirst_oid(lnext(lnext(list_head(l))))
133+
#define lthird_node(type,l) castNode(type, lthird(l))
130134

131135
#define lfourth(l) lfirst(lnext(lnext(lnext(list_head(l)))))
132136
#define lfourth_int(l) lfirst_int(lnext(lnext(lnext(list_head(l)))))
133137
#define lfourth_oid(l) lfirst_oid(lnext(lnext(lnext(list_head(l)))))
138+
#define lfourth_node(type,l) castNode(type, lfourth(l))
134139

135140
#define llast(l) lfirst(list_tail(l))
136141
#define llast_int(l) lfirst_int(list_tail(l))
137142
#define llast_oid(l) lfirst_oid(list_tail(l))
143+
#define llast_node(type,l) castNode(type, llast(l))
138144

139145
/*
140146
* Convenience macros for building fixed-length lists
@@ -210,6 +216,7 @@ extern ListCell *list_nth_cell(const List *list, int n);
210216
extern void *list_nth(const List *list, int n);
211217
extern int list_nth_int(const List *list, int n);
212218
extern Oid list_nth_oid(const List *list, int n);
219+
#define list_nth_node(type,list,n) castNode(type, list_nth(list, n))
213220

214221
extern bool list_member(const List *list, const void *datum);
215222
extern bool list_member_ptr(const List *list, const void *datum);

0 commit comments

Comments
 (0)