aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYaroslav de la Peña Smirnov <yps@yaroslavps.com>2023-06-22 03:28:46 +0300
committerYaroslav de la Peña Smirnov <yps@yaroslavps.com>2023-06-22 03:28:46 +0300
commit13cf037953a79def598b7842ebdc41a50275bbd9 (patch)
tree67a3edb1ee63f11d2f48cc16100ecccef03f2724
parentdbf01c41e2a780318e440421d459a9cd86e18108 (diff)
downloadc-optional-master.tar.gz
c-optional-master.zip
README, LICENSE text, simple exampleHEADmaster
-rw-r--r--LICENSE22
-rw-r--r--README.md13
-rw-r--r--example.c72
3 files changed, 107 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..15f3d7d
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+MIT License
+
+Copyright (C) 2023 Yaroslav de la Peña Smirnov
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e6fa20e
--- /dev/null
+++ b/README.md
@@ -0,0 +1,13 @@
+# C Optional
+
+Poor man's optional "type templates" for C.
+
+In its current form it's intended to be used with "one word" types. Might
+or might not "make it compatible" with structs, enums, unions, etc. in the
+future, if the need arises.
+
+Just for fun.
+
+## Example usage
+
+If you want to see an example on how to use it, see `example.c`.
diff --git a/example.c b/example.c
new file mode 100644
index 0000000..fb71718
--- /dev/null
+++ b/example.c
@@ -0,0 +1,72 @@
+#include <stdio.h>
+
+#include "optional.h"
+
+OPTIONALDEC(int);
+OPTIONALDEC(float);
+
+struct mine {
+ OPTIONAL(int) a;
+ OPTIONAL(int) b;
+};
+
+OPTIONAL(int)
+ithas(void)
+{
+ OPTIONAL(int) r = OPTSOME(10);
+ return r;
+}
+
+OPTIONAL(int)
+ithasnt(void)
+{
+ OPTIONAL(int) r = OPTNONE;
+ return r;
+}
+
+void
+optput(OPTIONAL(float) *dst, float src)
+{
+ opt_set_some(*dst, 6.9);
+}
+
+int
+main(int argc, const char *argv[])
+{
+ int a, b;
+ struct mine mine = {
+ .a = OPTSOME(69),
+ .b = OPTNONE,
+ };
+ OPTIONAL(float) y = OPTNONE;
+ OPTIONAL(float) z = OPTSOME(1.1);
+ float uy, uz, xy = opt_default(y, 6.9), xz = opt_default(z, 6.9);
+ float e = y.has ? y.data : 6.9;
+ float f = opt_default(y, 6.9);
+
+ opt_set_some(mine.b, 10);
+ opt_set_none(mine.a);
+
+ if (opt_unwrap(mine.a, a)) {
+ printf("has a = %d\n", a);
+ } else {
+ printf("doesn't have a\n");
+ }
+ if (opt_unwrap(mine.b, b)) {
+ printf("has b = %d\n", b);
+ } else {
+ printf("doesn't have b\n");
+ }
+ if (opt_unwrap(y, uy)) {
+ printf("has y = %f\n", uy);
+ } else {
+ printf("doesn't have y\n");
+ }
+ if (opt_unwrap(z, uz)) {
+ printf("has z = %f\n", uz);
+ } else {
+ printf("doesn't have z\n");
+ }
+
+ printf("xy = %f, xz = %f\n", xy, xz);
+}