Skip to content

Commit be182d5

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 c0a493e commit be182d5

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
@@ -106,26 +106,32 @@ list_length(const List *l)
106106
#define lfirst(lc) ((lc)->data.ptr_value)
107107
#define lfirst_int(lc) ((lc)->data.int_value)
108108
#define lfirst_oid(lc) ((lc)->data.oid_value)
109+
#define lfirst_node(type,lc) castNode(type, lfirst(lc))
109110

110111
#define linitial(l) lfirst(list_head(l))
111112
#define linitial_int(l) lfirst_int(list_head(l))
112113
#define linitial_oid(l) lfirst_oid(list_head(l))
114+
#define linitial_node(type,l) castNode(type, linitial(l))
113115

114116
#define lsecond(l) lfirst(lnext(list_head(l)))
115117
#define lsecond_int(l) lfirst_int(lnext(list_head(l)))
116118
#define lsecond_oid(l) lfirst_oid(lnext(list_head(l)))
119+
#define lsecond_node(type,l) castNode(type, lsecond(l))
117120

118121
#define lthird(l) lfirst(lnext(lnext(list_head(l))))
119122
#define lthird_int(l) lfirst_int(lnext(lnext(list_head(l))))
120123
#define lthird_oid(l) lfirst_oid(lnext(lnext(list_head(l))))
124+
#define lthird_node(type,l) castNode(type, lthird(l))
121125

122126
#define lfourth(l) lfirst(lnext(lnext(lnext(list_head(l)))))
123127
#define lfourth_int(l) lfirst_int(lnext(lnext(lnext(list_head(l)))))
124128
#define lfourth_oid(l) lfirst_oid(lnext(lnext(lnext(list_head(l)))))
129+
#define lfourth_node(type,l) castNode(type, lfourth(l))
125130

126131
#define llast(l) lfirst(list_tail(l))
127132
#define llast_int(l) lfirst_int(list_tail(l))
128133
#define llast_oid(l) lfirst_oid(list_tail(l))
134+
#define llast_node(type,l) castNode(type, llast(l))
129135

130136
/*
131137
* Convenience macros for building fixed-length lists
@@ -204,6 +210,7 @@ extern ListCell *list_nth_cell(const List *list, int n);
204210
extern void *list_nth(const List *list, int n);
205211
extern int list_nth_int(const List *list, int n);
206212
extern Oid list_nth_oid(const List *list, int n);
213+
#define list_nth_node(type,list,n) castNode(type, list_nth(list, n))
207214

208215
extern bool list_member(const List *list, const void *datum);
209216
extern bool list_member_ptr(const List *list, const void *datum);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy