From 7cfa485a3a75470d4a15fed189f3485fd8f57450 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss <113760121+Amllx@users.noreply.github.com> Date: Tue, 25 Apr 2023 13:20:10 +0200 Subject: meta: add stdx (C++ std extension) --- stdx | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 stdx (limited to 'stdx') diff --git a/stdx b/stdx new file mode 100644 index 0000000..ec79f72 --- /dev/null +++ b/stdx @@ -0,0 +1,102 @@ +/* + * Written by Amlal El Mahrouss, + * Copyright 2023, all rights reserved. + */ + +#ifndef _STDX +#define _STDX + +#include + +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 + 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 + ret eval_less_than(Lst... arg) + { + static traits::int_less_than_teller eq; + return eq(arg...) ? ret::Ok : ret::Err; + } + + template + ret eval_eq(Lst... arg) + { + static traits::int_eq_teller less_than; + return less_than(arg...) ? ret::Ok : ret::Err; + } + + template + 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 */ -- cgit v1.2.3