Skip to content

Instantly share code, notes, and snippets.

@fogus
Forked from dasshiva/extensions.c
Created December 13, 2023 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fogus/9fd771e38c561d868216725dc67e0f57 to your computer and use it in GitHub Desktop.
Save fogus/9fd771e38c561d868216725dc67e0f57 to your computer and use it in GitHub Desktop.
// Copyright (C) 2023 dasshiva
#include <stdlib.h>
// Compiles on GCC 11.4.0 ubuntu idk about other systems
// Abusing macros to make C look a tiny bit better (maybe worse for some)
#define class(x, contents) typedef struct x x; struct x contents; // declare a class x
#define var(ty, name) ty name; // declare a variable
#define func(ty, x, ...) ty (*##x) (__VA_ARGS__); // declare a member function maybe static or non-static
#define static_func_def(ty, x, ...) ty x (__VA_ARGS__) // declare a static function
#define func_defnp(class, ty, x) ty x (class* self) // define a non-static function taking no parameters
#define func_def(class, ty, x, ...) ty x (class* self, __VA_ARGS__) // define a non-static function taking parameters
#define new(class, ...) class* new_##class(__VA_ARGS__) // define the class's constructor always called new_<class-name>
class(system, {
var(int, hi)
var(int, hey)
func(void, function)
})
func_defnp(system, int, function) {
return 0;
}
new(system, user_input) {
system* p = malloc(sizeof(system));
p->function = function;
p->hey = 0;
p->hi = user_input;
return p;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment