commit 13f14ae87bfb6cf4940558fd47584a535e2decb4
parent 8783f7c97b8365a5405f7373f155f854c7093f19
Author: Oshgnacknak <osh@oshgnacknak.de>
Date: Sat, 25 Dec 2021 00:30:48 +0100
Replace IsPrintable with PrintableTo
This allows us to print to other thinks, like string buffers, later on
Diffstat:
M | examples/print.cpp | | | 8 | ++++---- |
M | osh.h | | | 90 | +++++++++++++++++++++++++++++++++++++++++++++++-------------------------------- |
2 files changed, 58 insertions(+), 40 deletions(-)
diff --git a/examples/print.cpp b/examples/print.cpp
@@ -5,12 +5,12 @@ struct Vector2 {
double x, y;
};
-void print1(Vector2& v) {
- osh::print("Vector2(", v.x, ", ", v.y, ')');
+template<typename P>
+void print1(P p, Vector2& v) {
+ osh::printp(p, "Vector2(", v.x, ", ", v.y, ')');
}
int main() {
-
osh::println("Concatinating different data types:",
'-', 5, 6.0d, 9, '+', 'L');
@@ -61,5 +61,5 @@ int main() {
osh::println("Printing costume types (see print1 above): ", v);
osh::println("^ Comment it out, error message is ACTUALLY readable");
- return 0;
+ return 0;
}
diff --git a/osh.h b/osh.h
@@ -1,85 +1,103 @@
#ifndef OSH_H
#define OSH_H
+#include <stdio.h>
+
namespace osh {
- template<typename T>
- concept IsPrintable = requires(T t) {
- print1(t);
+ template<typename P, typename T>
+ concept PrintableTo = requires(P p, T t) {
+ print1(p, t);
};
- template<IsPrintable... Args>
- void print(Args... args);
+ template<typename P, PrintableTo<P>... Args>
+ void printp(P& p, Args...);
- template<typename... Args>
+ template<PrintableTo<FILE*>... Args>
+ void print(Args...);
+ template<PrintableTo<FILE*>... Args>
void println(Args... args);
+
+ void print1(FILE*, const char*);
+ void print1(FILE*, char*);
+ template<typename T> void print1(FILE*, const T*);
+ void print1(FILE*, const char&);
+ void print1(FILE*, const int&);
+ void print1(FILE*, const short int&);
+ void print1(FILE*, const long int&);
+ void print1(FILE*, const long long int&);
+ void print1(FILE*, const float&);
+ void print1(FILE*, const double&);
+ void print1(FILE*, const long double&);
+
}
#endif /* OSH_H */
#ifdef OSH_H_IMPLEMENTATION
-#include <stdio.h>
-
namespace osh {
- void print1(const char* s) {
- fputs(s, stdout);
+ void print1(FILE* stream, const char* s) {
+ fputs(s, stream);
}
- void print1(char* s) {
- fputs(s, stdout);
+ void print1(FILE* stream, char* s) {
+ fputs(s, stream);
}
template<typename T>
- void print1(const T* p) {
- printf("%p", p);
+ void print1(FILE* stream, const T* p) {
+ fprintf(stream, "%p", p);
}
- void print1(const char& c) {
- putchar(c);
+ void print1(FILE* stream, const char& c) {
+ fputc(c, stream);
}
- void print1(const int& n) {
- printf("%d", n);
+ void print1(FILE* stream, const int& n) {
+ fprintf(stream, "%d", n);
}
- void print1(const short int& n) {
- printf("%d", n);
+ void print1(FILE* stream, const short int& n) {
+ fprintf(stream, "%d", n);
}
- void print1(const long int& n) {
- printf("%ld", n);
+ void print1(FILE* stream, const long int& n) {
+ fprintf(stream, "%ld", n);
}
- void print1(const long long int& n) {
- printf("%lld", n);
+ void print1(FILE* stream, const long long int& n) {
+ fprintf(stream, "%lld", n);
}
- void print1(const float& f) {
- printf("%f", f);
+ void print1(FILE* stream, const float& f) {
+ fprintf(stream, "%f", f);
}
- void print1(const double& d) {
- printf("%f", d);
+ void print1(FILE* stream, const double& d) {
+ fprintf(stream, "%f", d);
}
- void print1(const long double& d) {
- printf("%Lf", d);
+ void print1(FILE* stream, const long double& d) {
+ fprintf(stream, "%Lf", d);
}
- void print() {}
+ template<typename P, typename... Args>
+ void printp(P& p, Args... args) {
+ (print1(p, args), ...);
+ }
- template<typename T, typename... Args>
- void print(T& t, Args... args) {
- print1(t);
- print(args...);
+ template<typename... Args>
+ void print(Args... args) {
+ printp(stdout, args...);
}
template<typename... Args>
void println(Args... args) {
- print(args..., '\n');
+ printp(stdout, args..., '\n');
}
+
}
#endif // OSH_H_IMPLEMENTATION