aboutsummaryrefslogtreecommitdiff
path: root/include/nullable.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/nullable.h')
-rw-r--r--include/nullable.h29
1 files changed, 29 insertions, 0 deletions
diff --git a/include/nullable.h b/include/nullable.h
new file mode 100644
index 0000000..6c4d3a7
--- /dev/null
+++ b/include/nullable.h
@@ -0,0 +1,29 @@
+#ifndef AMAL_NULLABLE_H
+#define AMAL_NULLABLE_H
+
+struct __nullable_type_dummy{ int _; };
+
+int assert_not_null(void *val);
+
+#ifdef DEBUG
+#define DefineNullablePtrType(type) \
+ typedef struct type##_nullable type##_nullable; \
+ struct type##_nullable { \
+ type *value; \
+ }
+#define NullablePtr(type) type##_nullable
+#define nullable_unwrap(nullable_type) \
+ (assert_not_null((nullable_type).value) ? ((nullable_type).value) : NULL)
+#define nullable_assign(nullable_type, new_value) ((nullable_type).value = (new_value))
+#define is_not_null(nullable_type) ((nullable_type).value != NULL)
+#define nullable_raw(nullable_type) ((nullable_type).value)
+#else
+#define DefineNullablePtrType(type)
+#define NullablePtr(type) type*
+#define nullable_unwrap(value) value
+#define nullable_assign(nullable_type, new_value) ((nullable_type) = (new_value))
+#define is_not_null(nullable_type) ((nullable_type) != NULL)
+#define nullable_raw(nullable_type) (nullable_type)
+#endif
+
+#endif