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

Declare public types with tags to allow forward declaration #318

Open
Nelarius opened this issue Mar 8, 2025 · 4 comments
Open

Declare public types with tags to allow forward declaration #318

Nelarius opened this issue Mar 8, 2025 · 4 comments

Comments

@Nelarius
Copy link

Nelarius commented Mar 8, 2025

Problem

In Clay, the public API structs are declared without tags:

typedef struct {
    // fields...
} Clay_RenderCommandArray;

This means that, at least to my knowledge, the types can't be forward-declared when defining things in a header for e.g. a render backend:

#pragma once

// NOTE: I must include clay.h for Clay_RenderCommandArray
#include <clay.h>

struct clay_gpu_context;
struct gpu_render_pass;

void clay_gpu_render_commands(
    struct clay_gpu_context*, struct gpu_render_pass*, Clay_RenderCommandArray* commands,
    vec2s frame_size);

To be clear, this is a somewhat pedantic request, but it's nice to be able to keep headers as free of includes as possible 🙂

Proposal

Declare public types as

typedef struct Clay_RenderCommandArray {
    // fields...
} Clay_RenderCommandArray;

to allow forward-declaring the tag names in our headers.

Would be happy to make a pr if this is something deemed useful.

@emoon
Copy link
Contributor

emoon commented Mar 8, 2025

This is incorrect. You just do this

#pragma once

struct Clay_RenderCommandArray;
struct clay_gpu_context;
struct gpu_render_pass;

void clay_gpu_render_commands(
    struct clay_gpu_context*, struct gpu_render_pass*, struct Clay_RenderCommandArray* commands,
    vec2s frame_size);

And it will work fine.

@Nelarius
Copy link
Author

Nelarius commented Mar 8, 2025

But aren't struct Clay_RenderCommandArray and typedef struct {} Clay_RenderCommandArray distinct declarations? 🤔

@emoon
Copy link
Contributor

emoon commented Mar 9, 2025

I had a closer look at this and you are correct that you need do:

typedef struct Clay_RenderCommandArray {
    // fields...
} Clay_RenderCommandArray;

I confused the C rule with the C++ rule which will implicitly name the struct as well as do the typedef.

@nicbarker
Copy link
Owner

Hi @Nelarius, thanks for opening this issue! Looks like an oversight on my part for sure, I'm reasonably certain this wont impact any current usage so very happy for you to PR those changes if you have time! 🙂

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

3 participants