54 lines
2.3 KiB
C
54 lines
2.3 KiB
C
#pragma once
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
|
|
typedef uint8_t u8;
|
|
typedef uint16_t u16;
|
|
typedef int16_t i16;
|
|
typedef int32_t i32;
|
|
typedef uint32_t u32;
|
|
typedef uint64_t u64;
|
|
typedef float f32;
|
|
typedef double f64;
|
|
typedef uintptr_t uptr;
|
|
typedef char sbyte;
|
|
typedef ptrdiff_t size;
|
|
typedef size_t usize;
|
|
|
|
enum OptionTag {NONE, SOME};
|
|
|
|
#define OPTION(type) \
|
|
typedef struct \
|
|
{ \
|
|
enum OptionTag tag; \
|
|
union { \
|
|
char none; \
|
|
type value; \
|
|
}; \
|
|
} type##_opt; \
|
|
\
|
|
static inline type##_opt none_##type(void) \
|
|
{ \
|
|
return (type##_opt){ .tag = NONE, .none = 0 }; \
|
|
} \
|
|
\
|
|
static inline type##_opt some_##type(type value) \
|
|
{ \
|
|
return (type##_opt){ .tag = SOME, .value = value }; \
|
|
} \
|
|
\
|
|
static inline int get_some_##type(type##_opt opt, type* out_value) \
|
|
{ \
|
|
if (opt.tag != SOME) return 0; \
|
|
*out_value = opt.value; \
|
|
return 1; \
|
|
}
|
|
|
|
#define IF_LET_SOME(type, var, opt) \
|
|
type var; \
|
|
if (get_some_##type(opt, &var))
|
|
|
|
|
|
OPTION(u8)
|
|
OPTION(u16)
|