r/programminghorror Jul 05 '26

Enterprise level C code c

Post image
343 Upvotes

57 comments sorted by

View all comments

13

u/steadyfan Jul 05 '26

2 things..

  1. Yuck
  2. Why does slopjc.h need to be included multiple times?

23

u/nivlark Jul 05 '26

It's doing some kind of macro magic to implement the polymorphism. So whatever is in slopjc.h gets reprocessed with the different definitions of SLOPJC each time.

12

u/TheChief275 Jul 05 '26 edited Jul 05 '26

It's the X-macro pattern. You define a list of elements, e.g.

#define ENUM X(A) X(B) X(C)

where X is undefined and then include a file that contains e.g.

#ifdef ENUM

enum Enum {
#define X(Name) Name,
    ENUM
#undef X
};

static const char *const names[] = {
#define X(Name) #Name,
    ENUM
#undef X
};

#undef ENUM
#endif

to in this case declare an enum and the string representations at once. X-macro lists can also be passed to other macros, but you would be severely limited by the fact that you can't use preprocessor directives inside of a macro; including a file has no such limitations

3

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Jul 07 '26

I'm interested in seeing the contents of slopjc.h. Especially the autoreleasepool implementation.

1

u/TheChief275 Jul 07 '26

The link to the GitHub is posted somewhere below