enums for addressing array contents!

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andrew Davie

I've just discovered the following.
I have an array of (stuff). I want to be able to add/delete from the array willy-nilly, but I also want to be able to reference the particular elemenst specifically. I want to know what the toystory entry # is. This is difficult if the array can change.  But I've just implemented the following, which is... elegant.

if I want toystory, then I can just go frame_set[TOYSTORY_FRAME].  And that's it. No need to change anything. I can add/remove from the frame_set array, and of course the enum, and everything is OK.  I can put the toystory entry first, or make them alphabetic... doesn't matter.  Also, at the end, it makes sure the array has all the entries the enum implies, and finally the enum itself gives the size of the array (MAX_FRAME).  I can detect an invalid frame e.g.,   if (frame >= MAX_FRAME) // -->error!

It just seems like beautiful "self-maintaining" code to me.

enum {
    SKULL_FRAME,
    ZPH_FRAME,
    SAM_FRAME,
    DH_FRAME,
    TOYSTORY_FRAME,

    MAX_FRAME,
};

const ScanFrame frame_set[MAX_FRAME] = {

    [SKULL_FRAME] = {skull_screen_frames, SCREEN_NUM_FRAMES},          //
    [ZPH_FRAME] = {zph_screen_frames, ZPH_NUM_FRAMES},                 //
    [SAM_FRAME] = {sam_screen_frames, SAM_NUM_FRAMES},                 //
    [DH_FRAME] = {dh_screen_frames, SCREEN_NUM_FRAMES},                //
    [TOYSTORY_FRAME] = {toystory_screen_frames, SCREEN_NUM_FRAMES},    //
};

static_assert(sizeof(frame_set) / sizeof(frame_set[0]) == MAX_FRAME, "frame_set/enum mismatch");


SpiceWare

Slick, haven't encountered that technique before.  I asked grok about it:

QuoteYes, this is a very common and powerful C idiom called designated initializers (specifically array designators).
...
This syntax was introduced in C99
...
Common Use Cases
...
Game engines (animation frames, sprite data, etc.)

Grok's full answer.



I've been working on a syntax highlighting editor for work recently and one of the newer features I've started to use is raw strings for the REGEX:

this->rules->SetRule("String", R"raw(\"((?:\\.|[^\"\\])*)\")raw", (int)SyntaxHighlighting::String);
this->rules->SetRule("Comment", R"raw((?<=^(?:(?:"(?:\\.|[^"\\])*")|[^"#])*)#.*$)raw", (int)SyntaxHighlighting::Comment);

In the above everything between R"raw( and )raw" is the string that is used exactly as it appears, so the special REGEX characters no longer need to be escaped like before:

this->rules->SetRule("String", "\\\"((?:\\\\.|[^\\\"\\\\])*)\\\"", (int)SyntaxHighlighting::String);
this->rules->SetRule("Comment", "(?<=^(?:(?:\"(?:\\\\.|[^\"\\\\])*\")|[^\"#])*)#.*$", (int)SyntaxHighlighting::Comment);

All that escaping can get quite confusing. I think our in-house AI keyed me in on using raw strings.

Andrew Davie

Quote from: SpiceWare on 08 Jul 2026, 01:01 AMI've been working on a syntax highlighting editor for work recently and one of the newer features I've started to use is raw strings for the REGEX:


Seems like a nice feature.
Kind of reminds me, in terms of making things easier, is Python's "f-strings" Prefix a string with f, and anything inside squiggly brackets {} inside the string is a python expression.

many = 1
print(f"I learned this {many} days ago\n")

-->  I learned this 1 days ago

It makes for extremely readable code. Beats putinng in this sort of guff...
 "stuff", var, " stuff ", var2, "\n"


Andrew Davie

Quote from: Andrew Davie on 07 Jul 2026, 09:44 PMconst ScanFrame frame_set[MAX_FRAME] = {

    [SKULL_FRAME] = {skull_screen_frames, SCREEN_NUM_FRAMES},          //
};

static_assert(sizeof(frame_set) / sizeof(frame_set[0]) == MAX_FRAME, "frame_set/enum mismatch");


I have since found that the static_assert will not fail, as I want it to, if you declare the array as being the correct size already. You need to allow the compiler to size the array...

const ScanFrame frame_set[] = {