osh.h

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

commit d5ef5fa0b3ce838406a241f7f9c84cd0a46fcf79
parent 62acc7fa6823eccfb6458cbcf04824d7a3d2fea3
Author: Oshgnacknak <osh@oshgnacknak.de>
Date:   Mon,  3 Jan 2022 14:26:10 +0100

Pass all arguments to print as reference

Diffstat:
Mosh.h | 30++++++++++++++++++++++--------
1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/osh.h b/osh.h @@ -28,7 +28,7 @@ namespace osh { FileFormatter(FILE* stream); template<typename... Args> - void format(const char* fmt, Args...); + void format(const char* fmt, Args&&...); void flush(); }; @@ -41,7 +41,7 @@ namespace osh { template<PrintableTo<Formatter>... Args> void print(Args...); template<PrintableTo<Formatter>... Args> - void println(Args... args); + void println(Args&&... args); void print1(Formatter auto&, const char*); void print1(Formatter auto&, char*); @@ -64,7 +64,10 @@ namespace osh { void print1(FILE* stream, const T& t); template<PrintableTo<Formatter>... Args> - [[noreturn]] void panic(Args... args); + [[noreturn]] void panic(Args&&... args); + + template<typename... Args> + void assert(bool condition, Args&&... args); } #endif /* OSH_H */ @@ -151,17 +154,17 @@ namespace osh { } template<typename P, typename... Args> - void printp(P& p, Args... args) { + void printp(P& p, Args&&... args) { (print1(p, args), ...); } template<typename... Args> - void print(Args... args) { + void print(Args&&... args) { printp(fout, args...); } template<typename... Args> - void println(Args... args) { + void println(Args&&... args) { printp(fout, args..., '\n'); } @@ -172,7 +175,7 @@ namespace osh { : stream(stream) {} template<typename... Args> - void FileFormatter::format(const char* fmt, Args... args) { + void FileFormatter::format(const char* fmt, Args&&... args) { fprintf(stream, fmt, args...); } @@ -187,10 +190,21 @@ namespace osh { } template<typename... Args> - void panic(Args... args) { + void panic(Args&&... args) { printp(ferr, args...); exit(1); } + + template<typename... Args> + void assert(bool condition, Args&&... args) { + if (!condition) { + if (sizeof...(Args) > 0) { + panic(args...); + } else { + panic("Assertion failed"); + } + } + } } #endif // OSH_H_IMPLEMENTATION