Last active
December 6, 2015 14:14
-
-
Save eyelash/5491d652c54a4de31964 to your computer and use it in GitHub Desktop.
a printf-like printer for arbitrary types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cstdio> | |
| class Printer; | |
| class Printable { | |
| public: | |
| virtual void print (Printer& printer) const = 0; | |
| }; | |
| class Printer { | |
| FILE* file; | |
| public: | |
| Printer (FILE* file = stdout): file(file) {} | |
| void print (const char* s) { | |
| fputs (s, file); | |
| } | |
| void print (int n) { | |
| fprintf (file, "%d", n); | |
| } | |
| void print (const Printable& p) { | |
| p.print (*this); | |
| } | |
| template <class T0, class... T> void print (const char* s, const T0& v0, const T&... v) { | |
| while (true) { | |
| if (*s == '\0') return; | |
| if (*s == '%') { | |
| ++s; | |
| if (*s != '%') break; | |
| } | |
| fputc (*s, file); | |
| ++s; | |
| } | |
| print (v0); | |
| print (s, v...); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment