Skip to content

Instantly share code, notes, and snippets.

View AlexCeleste's full-sized avatar

Alex Eris Celeste née Gilding AlexCeleste

View GitHub Profile
// basic array mapper
// we can use it with mis-typed arguments
typedef void (* Mutate) (void *, void *);
typedef void * (* Step) (void *);
void addOneInt (void * in, void * out) { *(int *)out = *(int *)in + 1; }
void addOneFloat (void * in, void * out) { *(float *)out = *(float *)in + 1.0f; }
@AlexCeleste
AlexCeleste / counter.h
Last active March 1, 2023 18:56
stateful preprocessing
// counter.h
//
// increment the value of the counter
// every time this file is included
//
// no guard, repeatable inclusion
#ifndef MY_COUNTER // starting value
#include "typeid.h"
_Static_assert (TYPEID (void) == 0, "");
_Static_assert (TYPEID (int) == 6, "");
// _Static_assert (TYPEID (int) == 13, ""); // fails
_Static_assert (TYPEID (double) == 13, "");
// _Static_assert (TYPEID (double) == 7, ""); // fails
#include <stdio.h>
// typeclasses
#define ToLvalue(V) (&(struct { typeof (V) v; }){ V }.v)
typedef void (*BinOp) (void * out, void const * lhs, void const * rhs);
//
// Simple (i.e. very limited) metaprogramming macros
//
#ifndef C_MACROS_H
#define C_MACROS_H 1
#define M_MAX_DEPTH 32
@AlexCeleste
AlexCeleste / array.c
Created February 14, 2023 13:36
partial specialization in C
#define IsArray(V) _Generic((0, V) \
, typeof(V): false \
, default: true)
char ca[] = "hello";
int ia[10] = {};
static_assert (IsArray(ca));
static_assert (IsArray(ia));
@AlexCeleste
AlexCeleste / type_traits.h
Created September 18, 2023 12:38
type traits for array, function, array element, showing molding
// stdtypetraits.h
//
// start of a traits header
//
// a function identifier will always decay to a pointer to itself
#define SelfDecays(V) _Generic ((0, V) \
, typeof(V) * : true \
, default : false)