summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--.clang-format16
-rw-r--r--examples/CMakeLists.txt8
-rw-r--r--examples/must_pass.cc40
-rw-r--r--examples/str_checksum.cc40
-rw-r--r--lib/stdx.cc1
-rw-r--r--lib/stdx.hpp185
6 files changed, 156 insertions, 134 deletions
diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..fb3cacb
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,16 @@
+---
+BasedOnStyle: Microsoft
+AccessModifierOffset: '-4'
+AlignAfterOpenBracket: Align
+AlignConsecutiveMacros: 'true'
+AlignConsecutiveAssignments: 'true'
+AlignConsecutiveDeclarations: 'true'
+BinPackParameters: 'false'
+ColumnLimit: '0'
+Language: Cpp
+NamespaceIndentation: All
+PointerAlignment: Left
+ReflowComments: 'true'
+SortIncludes: 'false'
+UseTab: Always
+...
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 0ba445b..0075ad5 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -2,11 +2,11 @@
cmake_minimum_required(VERSION 3.15...3.31)
project(
- StrCheckusm
+ MustPass
VERSION 1.0
LANGUAGES CXX)
-add_executable(StrCheckusm str_checksum.cc)
+add_executable(MustPass must_pass.cc)
-set_property(TARGET StrCheckusm PROPERTY CXX_STANDARD 20)
-target_include_directories(StrCheckusm PUBLIC ../lib)
+set_property(TARGET MustPass PROPERTY CXX_STANDARD 20)
+target_include_directories(MustPass PUBLIC ../lib)
diff --git a/examples/must_pass.cc b/examples/must_pass.cc
new file mode 100644
index 0000000..377f8bb
--- /dev/null
+++ b/examples/must_pass.cc
@@ -0,0 +1,40 @@
+/*
+ string checksum example
+ written by Amlal El Mahrouss.
+ licensed under GPL-2 license
+ */
+
+#include <stdx.hpp>
+#include <string>
+
+static const char do_hash(const std::string& in)
+{
+ int hash = 0;
+
+ for (long index = 0; index < in.size(); ++index)
+ {
+ hash += in[index];
+ }
+
+ return hash;
+}
+
+static auto do_some(const std::string recv_data, const std::string 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("Ohio", "Ohio");
+ opt.expect("Checksum failed, Ohio isn't Ohio!");
+
+ return 0;
+}
diff --git a/examples/str_checksum.cc b/examples/str_checksum.cc
deleted file mode 100644
index a4d5e87..0000000
--- a/examples/str_checksum.cc
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- string checksum example
- written by Amlal El Mahrouss.
- licensed under GPL-2 license
- */
-
-#include <stdx.hpp>
-#include <string>
-
-static const char do_hash(const std::string& in)
-{
- int hash = 0;
-
- for (long index = 0; index < in.size(); ++index)
- {
- hash += in[index];
- }
-
- return hash;
-}
-
-static auto do_some(const std::string recv_data, const std::string 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;
-}
diff --git a/lib/stdx.cc b/lib/stdx.cc
index dd6d4c6..a094f89 100644
--- a/lib/stdx.cc
+++ b/lib/stdx.cc
@@ -6,5 +6,4 @@
#include <stdx.hpp>
-
// EOF
diff --git a/lib/stdx.hpp b/lib/stdx.hpp
index 845bb67..25e4157 100644
--- a/lib/stdx.hpp
+++ b/lib/stdx.hpp
@@ -4,100 +4,107 @@
* Copyright 2023-2025, Amlal El Mahrouss all rights reserved.
*/
-#ifndef _STDX
-#define _STDX
+#ifndef _STDX_HPP
+#define _STDX_HPP
#include <stdexcept>
+#include <utility>
namespace stdx
{
- enum class ret
- {
- okay,
- err
- };
-
- struct opt final
- {
- explicit opt(const ret& ret)
- : m_ret(ret)
- {}
-
- opt& expect(const char* input)
- {
- if (m_ret == ret::err)
- {
- throw std::runtime_error(input);
- }
-
- return *this;
- }
-
- private:
- ret m_ret;
-
- };
-
- template <typename Teller, typename... Lst>
- stdx::ret eval(Teller tell, Lst&&... arg)
- {
- return tell(arg...) ? stdx::ret::okay : stdx::ret::err;
- }
-
- namespace traits
- {
- struct int_eq_teller
- {
- explicit int_eq_teller() {}
-
- bool operator()(int a, int b)
- {
- return (a == b);
- }
- };
-
- struct int_greater_than_teller
- {
- explicit int_greater_than_teller() {}
-
- bool operator()(int a, int b)
- {
- return (a > b);
- }
- };
-
- struct int_less_than_teller
- {
- explicit int_less_than_teller() {}
-
- bool operator()(int a, int b)
- {
- return (a < b);
- }
- };
- }
-
- template <typename... Lst>
- ret eval_less_than(Lst&&... arg)
- {
- static traits::int_less_than_teller eq;
- return eq(arg...) ? ret::okay : ret::err;
- }
-
- template <typename... Lst>
- ret eval_eq(Lst&&... arg)
- {
- static traits::int_eq_teller less_than;
- return less_than(arg...) ? ret::okay : ret::err;
- }
-
- template <typename... Lst>
- ret eval_greater(Lst&&... arg)
- {
- static traits::int_greater_than_teller greater_than;
- return greater_than(arg...) ? ret::okay : ret::err;
- }
+ enum class ret
+ {
+ okay,
+ err
+ };
+
+ struct opt final
+ {
+ explicit opt(const ret& ret)
+ : m_ret(ret)
+ {
+ }
+
+ opt& expect(const char* input)
+ {
+ if (m_ret == ret::err)
+ {
+ throw std::runtime_error(input);
+ }
+
+ return *this;
+ }
+
+ private:
+ ret m_ret;
+ };
+
+ template <typename Teller, typename... Lst>
+ stdx::ret eval(Teller tell, Lst&&... arg)
+ {
+ return tell(std::forward<Lst>(arg)...) ? stdx::ret::okay : stdx::ret::err;
+ }
+
+ namespace traits
+ {
+ struct int_eq_teller
+ {
+ explicit int_eq_teller()
+ {
+ }
+
+ bool operator()(int a, int b)
+ {
+ return (a == b);
+ }
+ };
+
+ struct int_greater_than_teller
+ {
+ explicit int_greater_than_teller()
+ {
+ }
+
+ bool operator()(int a, int b)
+ {
+ return (a > b);
+ }
+ };
+
+ struct int_less_than_teller
+ {
+ explicit int_less_than_teller()
+ {
+ }
+
+ bool operator()(int a, int b)
+ {
+ return (a < b);
+ }
+ };
+ } // namespace traits
+
+ template <typename... Lst>
+ ret eval_less_than(Lst&&... arg)
+ {
+ static traits::int_less_than_teller eq;
+ return eq(std::forward<Lst>(arg)...) ? ret::okay : ret::err;
+ }
+
+ template <typename... Lst>
+ ret eval_eq(Lst&&... arg)
+ {
+ static traits::int_eq_teller less_than;
+ return less_than(std::forward<Lst>(arg)...) ? ret::okay : ret::err;
+ }
+
+ template <typename... Lst>
+ ret eval_greater(Lst&&... arg)
+ {
+ static traits::int_greater_than_teller greater_than;
+ return greater_than(std::forward<Lst>(arg)...) ? ret::okay : ret::err;
+ }
} /* namespace stdx */
-#endif /* ifndef _STDX */
+#endif /* ifndef _STDX_HPP */