diff options
| author | Amlal El Mahrouss <113760121+Amllx@users.noreply.github.com> | 2023-04-25 13:20:10 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-25 13:20:10 +0200 |
| commit | 7cfa485a3a75470d4a15fed189f3485fd8f57450 (patch) | |
| tree | 541c718a316276ebc0e32261c98b8e9360f05183 | |
| parent | 44c46af5c682f2e2cb19df8c5d87493facd0430c (diff) | |
meta: add stdx (C++ std extension)
| -rw-r--r-- | stdx | 102 |
1 files changed, 102 insertions, 0 deletions
@@ -0,0 +1,102 @@ +/* + * Written by Amlal El Mahrouss, + * Copyright 2023, all rights reserved. + */ + +#ifndef _STDX +#define _STDX + +#include <stdexcept> + +namespace stdx +{ + enum class ret + { + Ok, + 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... Args> + stdx::ret eval(Teller tell, Args... arg) + { + return tell(arg...) ? stdx::ret::Ok : 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::Ok : ret::Err; + } + + template <typename... Lst> + ret eval_eq(Lst... arg) + { + static traits::int_eq_teller less_than; + return less_than(arg...) ? ret::Ok : ret::Err; + } + + template <typename... Lst> + ret eval_greater(Lst... arg) + { + static traits::int_greater_than_teller greater_than; + return greater_than(arg...) ? ret::Ok : ret::Err; + } + +} /* namespace stdx */ + +#endif /* ifndef _STDX */ |
