Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preset/fixed item ID numbers #16

Open
zzo38 opened this issue Nov 21, 2022 · 2 comments
Open

Preset/fixed item ID numbers #16

zzo38 opened this issue Nov 21, 2022 · 2 comments

Comments

@zzo38
Copy link

zzo38 commented Nov 21, 2022

I would think what might sometimes be good to have is the mode for preset (fixed) item ID numbers, you will specify how many they are and then you will call the function to allocate that many, instead of calling lay_item for each one.

This could be done to check first if ctx->count is zero; if not, then it is an error, but if it is zero then it will set its capacity and count to the specified number, allocate them, and then initialize each item in a loop, similarly than lay_item already does.

(My own "smallxrm" library, which is an implementation of the X resource manager, has a xrm_init_quarks function with a similar use. You will allocate many items with fixed preset ID numbers from the beginning, which can be compile-time constants, rather than having to pass strings always or allocating them and assigning them to variables. (You can still add additional quarks afterward which do not have fixed numbers.))

@zzo38
Copy link
Author

zzo38 commented Nov 21, 2022

I wrote a code below. I have not tested this and it may be wrong.

void lay_preset_items(lay_context *ctx, lay_id total)
{
    lay_id idx;
    ASSERT(ctx && !ctx->count);
    ctx->count = total;

    if (total > ctx->capacity) {
        ctx->capacity = total;
        const size_t item_size = sizeof(lay_item_t) + sizeof(lay_vec4);
        ctx->items = (lay_item_t*)LAY_REALLOC(ctx->items, ctx->capacity * item_size);
        const lay_item_t *past_last = ctx->items + ctx->capacity;
        ctx->rects = (lay_vec4*)past_last;
    }

    for(idx=0;idx<total;idx++) {
        lay_item_t *item = ctx->items + idx;
        LAY_MEMSET(item, 0, sizeof(lay_item_t));
        item->first_child = LAY_INVALID_ID;
        item->next_sibling = LAY_INVALID_ID;
        LAY_MEMSET(&ctx->rects[idx], 0, sizeof(lay_vec4));
    }
}

(I agree that this code is public domain, and you are permitted to include it in the code which is licensed by the license of this project.)

@randrew
Copy link
Owner

randrew commented Aug 14, 2023

I think a good way might be to change the default way to allocate items to allocate them as an array instead of a single, and make the lay_item function call lay_items or something with a value of 1. Maybe I'll do that in a future version.

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

No branches or pull requests

2 participants