Skip to content

Commit

Permalink
Add comment to explain CLAY macro
Browse files Browse the repository at this point in the history
  • Loading branch information
nicbarker committed Jan 2, 2025
1 parent cd01083 commit a44423a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions clay.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,30 @@
static uint8_t CLAY__ELEMENT_DEFINITION_LATCH;

// Publicly visible layout element macros -----------------------------------------------------

/* This macro looks scary on the surface, but is actually quite simple.
It turns a macro call like this:
CLAY(
CLAY_RECTANGLE(),
CLAY_ID()
) {
...children declared here
}
Into calls like this:
Clay_OpenElement();
CLAY_RECTANGLE();
CLAY_ID();
Clay_ElementPostConfiguration();
...children declared here
Clay_CloseElement();
The for loop will only ever run a single iteration, putting Clay__CloseElement() in the increment of the loop
means that it will run after the body - where the children are declared. It just exists to make sure you don't forget
to call Clay_CloseElement().
*/
#define CLAY(...) \
for (\
CLAY__ELEMENT_DEFINITION_LATCH = (Clay__OpenElement(), ##__VA_ARGS__, Clay__ElementPostConfiguration(), 0); \
Expand Down

3 comments on commit a44423a

@VisenDev
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so If I understand correctly, if we don't want to use the macro for whatever reason (cross language bindings, etc...), just manually using Clay_OpenElement(); Clay_ElementPostConfiguration(); and Clay_CloseElement(); ought to do the same thing. This makes much more sense now

@FintasticMan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, exactly.

@nicbarker
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep exactly - you can take a look at the odin bindings implementation of the clay macro here for an example:
https://github.com/nicbarker/clay/blob/main/bindings/odin/clay-odin/clay.odin#L333
Odin supports something called "deferred attributes" which allows us to tag the function and say "whenever the scope this function was called in exits, call the CloseElement function", which means we don't need a macro in Odin, can get away with just functions 🙂

Please sign in to comment.