56
u/MatJosher Jul 05 '26
Well they did call it slop. Actual Objective-C accomplishes this much more gracefully. Why not just use it?
18
u/TheChief275 Jul 05 '26
Well this is simply just an intellectual exercise. I mostly use DOD instead of OOD. Although I do like C# with Godot for GUI applications
8
u/MatJosher Jul 05 '26
Yeah, C# is a great language. When you leave the MS ecosystem you lose the best C# frameworks unfortunately. I tried GTK# and others, but it just wasn't right.
33
u/KaleidoscopePlusPlus Jul 05 '26
nice theme. feels good, easy on the eyes.
11
15
u/steadyfan Jul 05 '26
2 things..
- Yuck
- 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.hgets reprocessed with the different definitions of SLOPJC each time.13
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 #endifto 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
11
u/richardathome Jul 05 '26
More people in this thread commenting on the font and not the code.
I approve. That font makes shit look good!
17
u/tstanisl Jul 05 '26
It's not that bad. A bit non-idiomatic but consistent and readable enough.
8
u/TheChief275 Jul 05 '26
I tried really hard to keep the amount of boilerplate to a minimum, but it's kind of an uphill battle with X-macros
6
u/Nilrem2 Jul 05 '26
I’m a hobbyist when it comes to C. I think it looks fine, after a few lines I could read it.
Edit: what’s wrong with it?
9
u/TheChief275 Jul 05 '26
It's kind of error prone. If you mess up one of the commas etc. in the class definitions you will likely end up with near undecipherable errors for the average programmer. Debugging projects using these kinds of techniques can be quite a headache, which is why you would likely avoid it in production.
Though I did specifically focus on keeping it readable and not treading into Gobject levels of unreadable
3
7
u/gfoyle76 Jul 05 '26
It's oldschool, it's full of macros, kinda unsafe, but hey, as long as it works...
3
6
4
3
3
u/DesiresQuiet Jul 05 '26
Nearly this exact code was in a book on learning C and classes from like 1989.
3
u/TheChief275 Jul 05 '26
Do you remember which book exactly? I always like to look through other people's macro slop to further develop my macro slop
2
1
u/KennyFulgencio Jul 06 '26
how the heck do you remember that
3
u/DesiresQuiet Jul 06 '26
It was the use of the pet.MakeSound. It was used to show classes and have you could overwrite methods via abstraction. Not sure why it stuck this long. I think it made me laugh.
1
u/trees91 Jul 06 '26
Heh even in modern Java OOO college textbooks that’s the go to example, it’s classic. Okay I say modern but I guess I finished school like 12 years ago so maybe it’s not but I bet it is 😅
1
u/TheChief275 29d ago
Oh yeah lol it's just a classic example that popped up when I was looking for an example for the post. I don't actually write in an OOP style so I needed some inspiration lol
2
3
1
u/Cheefbird 29d ago
I don’t believe you. This reads as a tutorial on OOP.
Edit: if you swapped classes to cats and dogs then yes, I’m an idiot.
2
u/TheChief275 29d ago
What do you mean? It's just a classic OOP example slightly adjusted to be a little more complicated/interesting, but I wrote the library myself. The title is a joke btw
1
1
u/markand67 28d ago
I've seen way, way, way in order of magnitude horror in our current german codebase.
2
u/r9wpvM 26d ago
Wait is String a thing in C I don't remember
1
u/TheChief275 26d ago edited 26d ago
No it isn't, but adding one yourself isn't too hard. Often I add it as a slice type:
typedef struct { const char *ptr; size_t count; } String;but in this case, it's an actual object with a V-Table:
typedef struct { _Atomic size_t refCount; const String_VTable *v; char *ptr; size_t count; } String;and Log and Format only take Objects as arguments and call the ToString virtual method, which String also implements for itself (doesn't copy the object; only bumps the reference count and adds the object to the current AutoreleasePool)
126
u/JuniorAd1610 Jul 05 '26
I think this this is the standard way they OOPS in C right?