Initial commit
This commit is contained in:
commit
267d349923
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
build/
|
||||||
|
pureoption-example
|
35
Makefile
Normal file
35
Makefile
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
CC = gcc
|
||||||
|
CCFLAGS = -Wall
|
||||||
|
BUILDDIR = build
|
||||||
|
SRCS = pureoption-example.c pureoption.c
|
||||||
|
OBJS = $(addprefix $(BUILDDIR)/, $(patsubst %.c,%.o,$(SRCS)))
|
||||||
|
OUT = pureoption-example
|
||||||
|
|
||||||
|
.PHONY: all format clean
|
||||||
|
.PRECIOUS: $(BUILDDIR)/. $(BUILDDIR)%/.
|
||||||
|
|
||||||
|
all: $(OUT)
|
||||||
|
|
||||||
|
debug: CCFLAGS += -g
|
||||||
|
debug: all
|
||||||
|
|
||||||
|
$(BUILDDIR)/.:
|
||||||
|
mkdir -p $@
|
||||||
|
|
||||||
|
$(BUILDDIR)%/.:
|
||||||
|
mkdir -p $@
|
||||||
|
|
||||||
|
$(OUT): $(OBJS)
|
||||||
|
$(CC) $^ -o $(OUT) $(LDFLAGS)
|
||||||
|
|
||||||
|
.SECONDEXPANSION:
|
||||||
|
|
||||||
|
$(BUILDDIR)/%.o: %.c | $$(@D)/.
|
||||||
|
$(CC) $(CCFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
format:
|
||||||
|
clang-format -i *.c *.h
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf $(BUILDDIR)
|
||||||
|
rm -f $(OUT)
|
15
pureoption-example.c
Normal file
15
pureoption-example.c
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#include "pureoption.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
pureoption opt = pureoption_some(3);
|
||||||
|
|
||||||
|
printf("pureoption_some(3)\n");
|
||||||
|
printf("\thas value: %s\n", pureoption_has(opt) == 1 ? "yes" : "no");
|
||||||
|
printf("\tvalue: %d\n", pureoption_get(int, opt));
|
||||||
|
|
||||||
|
opt = pureoption_none();
|
||||||
|
|
||||||
|
printf("pureoption_none\n");
|
||||||
|
printf("\thas value: %s\n", pureoption_has(opt) == 1 ? "yes" : "no");
|
||||||
|
}
|
23
pureoption.c
Normal file
23
pureoption.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include "pureoption.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
inline static pureoption __pureoption_new(bool has_value, void *value) {
|
||||||
|
pureoption result = {
|
||||||
|
.has_value = has_value,
|
||||||
|
.value = value,
|
||||||
|
};
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
pureoption _pureoption_some(void *value) {
|
||||||
|
return __pureoption_new(true, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
pureoption pureoption_none(void) { return __pureoption_new(false, NULL); }
|
||||||
|
|
||||||
|
bool pureoption_has(pureoption option) { return option.has_value; }
|
||||||
|
|
||||||
|
bool pureoption_lacks(pureoption option) { return !option.has_value; }
|
||||||
|
|
||||||
|
void *_pureoption_get(pureoption option) { return option.value; }
|
25
pureoption.h
Normal file
25
pureoption.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#ifndef PUREOPTION_H
|
||||||
|
#define PUREOPTION_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#define pureoption_some(x) \
|
||||||
|
({ \
|
||||||
|
static typeof(x) _temp = (x); \
|
||||||
|
_pureoption_some(&_temp); \
|
||||||
|
})
|
||||||
|
|
||||||
|
#define pureoption_get(x_type, x) *(x_type *)_pureoption_get(x)
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
bool has_value;
|
||||||
|
void *value;
|
||||||
|
} pureoption;
|
||||||
|
|
||||||
|
pureoption _pureoption_some(void *value);
|
||||||
|
pureoption pureoption_none(void);
|
||||||
|
bool pureoption_has(pureoption option);
|
||||||
|
bool pureoption_lacks(pureoption option);
|
||||||
|
void *_pureoption_get(pureoption option);
|
||||||
|
|
||||||
|
#endif // PUREOPTION_H
|
Loading…
x
Reference in New Issue
Block a user