Skip to content

Typed Lists#150

Merged
martinfouilleul merged 1 commit into
mainfrom
martin/typedlists
May 26, 2026
Merged

Typed Lists#150
martinfouilleul merged 1 commit into
mainfrom
martin/typedlists

Conversation

@martinfouilleul

Copy link
Copy Markdown
Collaborator

This adds a typed instrusive doubly-linked list type to util/lists.h.

It works the same way as the existing untyped oc_list type, but ensures that a given list can only contain items of a single type, and that these items are all linked through the same pair of next/prev pointers. This also simplifies APIs for manipulating the list and idioms like loops, since we don't have to pass the type of elements and the name of the link-pair field.

Usage is as follows:

typedef struct my_type 
{
    oc_typed_list_links links;
    int i;
} my_type;

 // declare a type for a linked list that can hold items of type my_type, linked through the links field.
typedef oc_typed_list(my_type, links) my_type_list;

// Create a list and push some items
my_type_list list = {0};
my_type t1 = {.i = 1}, t2 = {.i = 2}, t3 = {.i = 3};

oc_typed_list_push_back(&list, &t1);
oc_typed_list_push_back(&list, &t2);
oc_typed_list_push_back(&list, &t3);

// Loop through the list and print items
oc_typed_list_for(list, elt)
{
    printf("%i ", elt->i);
}

@martinfouilleul martinfouilleul force-pushed the martin/typedlists branch 2 times, most recently from a389ea7 to a3cd6b6 Compare May 26, 2026 12:47
@martinfouilleul martinfouilleul merged commit a54fe9c into main May 26, 2026
6 checks passed
@martinfouilleul martinfouilleul deleted the martin/typedlists branch May 26, 2026 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant