summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorAmlal El Mahrouss <113760121+Amllx@users.noreply.github.com>2023-04-25 17:09:32 +0200
committerGitHub <noreply@github.com>2023-04-25 17:09:32 +0200
commitb192014ebed8d71fe675fc81d047eccc14e59a64 (patch)
tree09266907f2cb9164ab4f66143721796922564f16 /examples
parent219c6a2d0cb93534eb4cee96c86aeab01ac6f58b (diff)
Create str_checksum.cc
new example to illustrate usage of stdx::opt.
Diffstat (limited to 'examples')
-rw-r--r--examples/str_checksum.cc43
1 files changed, 43 insertions, 0 deletions
diff --git a/examples/str_checksum.cc b/examples/str_checksum.cc
new file mode 100644
index 0000000..05cdfd8
--- /dev/null
+++ b/examples/str_checksum.cc
@@ -0,0 +1,43 @@
+/*
+ string checksum example
+ written by Amlal El Mahrouss.
+ licensed under GPL-2 license
+ */
+
+#include <stdx>
+
+extern "C" {
+# include <string.h>
+}
+
+static const char do_hash(const char* in)
+{
+ int hash = 0;
+
+ for (long index = 0; index < strlen(in); ++index)
+ {
+ hash += in[index];
+ }
+
+ return hash;
+}
+
+static auto do_some(const char* recv_data, const char* check_data)
+{
+ const int hash_to_check = do_hash(check_data); /* here we assume this should match opt_hash */
+ const int opt_hash = do_hash(recv_data); /* we assume that the hash is correct */
+
+ auto opt = stdx::opt(stdx::eval_eq(hash_to_check, opt_hash)); /* do the compute */
+ return opt;
+}
+
+/* finally test it */
+int main(int argc, char** argv)
+{
+ // ... let's assume we fetch data from network...
+
+ auto opt = do_some("Michigan", "Ohio");
+ opt.expect("Checksum failed, Michigan isn't Ohio!");
+
+ return 0;
+}