osh.h

My personal stdc++ replacement
git clone git://git.oshgnacknak.de/osh.h.git
Log | Files | Refs

commit 9caa86436efcc9c8e82d6e08320f93a153d737e7
parent d661faf0dc8827cb0550e11aaf353fd2fc013e73
Author: Oshgnacknak <osh@oshgnacknak.de>
Date:   Sun, 23 Jan 2022 17:57:03 +0100

Refactor PrintableTo concept

Makes print.cpp compile with OSH_H_IMPLEMENTATION at the bottom

Diffstat:
Mexamples/print.cpp | 16+++++++++++-----
Mosh.h | 35+++++++++++++----------------------
2 files changed, 24 insertions(+), 27 deletions(-)

diff --git a/examples/print.cpp b/examples/print.cpp @@ -1,4 +1,3 @@ -#define OSH_H_IMPLEMENTATION #include "osh.h" using namespace osh; @@ -7,12 +6,13 @@ struct Vector2 { double x, y; }; -template<typename P> -void print1(P p, Vector2& v) { - printp(p, "Vector2(", v.x, ", ", v.y, ')'); +void print1(Formatter auto& fmt, Vector2& v) { + printp(fmt, "Vector2(", v.x, ", ", v.y, ')'); } int main() { + println("Hallo, Welt"); + println("Concatinating different data types:", '-', 5, 6.0d, 9, '+', 'L'); @@ -78,7 +78,13 @@ int main() { println("Printing costume types (see print1 above): ", v); println("^ Comment it out, error message is ACTUALLY readable"); - printp(stderr, "Printing to any FILE* other than stdout: ", buf, " - ", v, '\n'); + printp(ferr, "Printing to stderr: ", "you error is soos ", d, f, '\n'); + + FileFormatter fmt(stderr); + printp(fmt, "Printing to any FILE* other than stdout: ", buf, " - ", v, '\n'); return 0; } + +#define OSH_H_IMPLEMENTATION +#include "osh.h" diff --git a/osh.h b/osh.h @@ -11,8 +11,8 @@ namespace osh { template<typename T> T max(T a, T b); - template<typename P, typename T> - concept PrintableTo = requires(P p, T t) { + template<typename T, typename P> + concept PrintableTo = requires(T t, P p) { print1(p, t); }; @@ -36,12 +36,12 @@ namespace osh { extern FileFormatter ferr; template<typename P, PrintableTo<P>... Args> - void printp(P& p, Args...); + void printp(P&, Args...); - template<PrintableTo<Formatter>... Args> - void print(Args...); - template<PrintableTo<Formatter>... Args> - void println(Args&&... args); + template<PrintableTo<FileFormatter>... Args> + void print(Args&&...); + template<PrintableTo<FileFormatter>... Args> + void println(Args&&...); void print1(Formatter auto&, const char*); void print1(Formatter auto&, char*); @@ -60,10 +60,7 @@ namespace osh { void print1(Formatter auto&, const double&); void print1(Formatter auto&, const long double&); - template<typename T> - void print1(FILE* stream, const T& t); - - template<PrintableTo<Formatter>... Args> + template<PrintableTo<FileFormatter>... Args> [[noreturn]] void panic(Args&&... args); template<typename... Args> @@ -196,17 +193,17 @@ namespace osh { fmt.format("%Lf", d); } - template<typename P, typename... Args> - void printp(P& p, Args&&... args) { + template<typename P, PrintableTo<P>... Args> + void printp(P& p, Args... args) { (print1(p, args), ...); } - template<typename... Args> + template<PrintableTo<FileFormatter>... Args> void print(Args&&... args) { printp(fout, args...); } - template<typename... Args> + template<PrintableTo<FileFormatter>... Args> void println(Args&&... args) { printp(fout, args..., '\n'); } @@ -222,17 +219,11 @@ namespace osh { fprintf(stream, fmt, args...); } - template<typename T> - void print1(FILE* stream, const T& t) { - FileFormatter fmt(stream); - print1(fmt, t); - } - void FileFormatter::flush() { fflush(stream); } - template<typename... Args> + template<PrintableTo<FileFormatter>... Args> void panic(Args&&... args) { printp(ferr, args...); exit(1);