osh.h

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

commit 24a90f39dbb2e8d7aa89dafd782a6c514774725e
Author: Oshgnacknak <osh@oshgnacknak.de>
Date:   Fri, 17 Dec 2021 23:07:49 +0100

osh::print + examples

Diffstat:
Aexamples/print.cpp | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aosh.h | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 147 insertions(+), 0 deletions(-)

diff --git a/examples/print.cpp b/examples/print.cpp @@ -0,0 +1,64 @@ +#define OSH_H_IMPLEMENTATION +#include "osh.h" + +struct Vector2 { + double x, y; +}; + +void print1(Vector2& v) { + osh::print("Vector2(", v.x, ", ", v.y, ')'); +} + +int main() { + + osh::println("Concatinating different data types:", + '-', 5, 6.0d, 9, '+', 'L'); + + char c = 'A'; + osh::println("Printing char: ", c); + + int i = 69; + osh::println("Printing int: ", i); + + short int si = 420; + osh::println("Printing short int: ", si); + + long int li = 666; + osh::println("Printing long int: ", li); + + long long int lli = 0xdeadbeef; + osh::println("Printing long long int: ", lli); + + float f = 3.141592653589793; + osh::println("Printing float: ", f); + + double d = 2.71828; + osh::println("Printing double: ", d); + + long double ld = 1.141592653589793; + osh::println("Printing long double: ", ld); + + osh::println("Printing a pointer to anythink: ", + &i, ", ", + &si, ", ", + &li, ", ", + &lli, ", ", + &f, ", ", + &d, ", ", + &ld); + + char buf[32]; + buf[0] = 'b'; + buf[1] = 'u'; + buf[2] = 'f'; + buf[3] = 'f'; + buf[4] = 'e'; + buf[4] = 'r'; + buf[5] = '\0'; + osh::println("Printing char buffers: ", buf); + + Vector2 v = { 69, 420 }; + osh::println("Printing costume types (see print1 above): ", v); + + return 0; +} diff --git a/osh.h b/osh.h @@ -0,0 +1,83 @@ +#ifndef OSH_H +#define OSH_H + +namespace osh { + + template<typename T> + void print1(T& t); + + template<typename... Args> + void print(Args... args); + + template<typename... Args> + void println(Args... args); +} + +#endif /* OSH_H */ + +#ifdef OSH_H_IMPLEMENTATION + +#include <stdio.h> + +namespace osh { + + void print1(const char* s) { + fputs(s, stdout); + } + + void print1(char* s) { + fputs(s, stdout); + } + + template<typename T> + void print1(T* p) { + printf("%p", p); + } + + void print1(char& c) { + putchar(c); + } + + void print1(int& n) { + printf("%d", n); + } + + void print1(short int& n) { + printf("%d", n); + } + + void print1(long int& n) { + printf("%ld", n); + } + + void print1(long long int& n) { + printf("%lld", n); + } + + void print1(float& f) { + printf("%f", f); + } + + void print1(double& d) { + printf("%f", d); + } + + void print1(long double& d) { + printf("%Lf", d); + } + + void print() {} + + template<typename T, typename... Args> + void print(T& t, Args... args) { + print1(t); + print(args...); + } + + template<typename... Args> + void println(Args... args) { + print(args..., '\n'); + } +} + +#endif // OSH_H_IMPLEMENTATION