1

constexpr stream manipulators to allow "macroing"

This commit is contained in:
2022-07-11 18:18:18 +02:00
parent 7f7c528949
commit 24c418eb0a
2 changed files with 34 additions and 25 deletions

View File

@ -15,25 +15,39 @@
#ifndef __CGA_Stream_include__
#define __CGA_Stream_include__
// #include "devices/BufferedCGA.h"
#include "devices/BufferedCGA.h"
#include "lib/OutStream.h"
// NOTE: I added this
// NOTE: I added these classes to allow for easier stream-like color changing
class fgc {
public:
fgc() : fg(CGA::LIGHT_GREY) {}
fgc(CGA::color fg) : fg(fg) {}
CGA::color fg;
};
class bgc {
public:
bgc() : bg(CGA::BLACK) {}
bgc(CGA::color bg) : bg(bg) {}
CGA::color bg;
constexpr fgc(const CGA::color fg) : fg(fg) {}
const CGA::color fg;
};
// NOTE: I added this (changed this) to use BufferedCGA
// NOTE: I could have used a struct to remove the public, but I like to use
// structs exclusively for C like memory "descriptions", although they're
// techinally the same to classes
class bgc {
public:
constexpr bgc(const CGA::color bg) : bg(bg) {} // Make this into a literal type to allow macro-like usage
const CGA::color bg;
};
// NOTE: I didn't want to use macro definitions since I don't like them,
// makes it easier to change colors
constexpr bgc white_b = bgc(CGA::WHITE);
constexpr bgc black_b = bgc(CGA::BLACK);
constexpr bgc green_b = bgc(CGA::GREEN);
constexpr bgc red_b = bgc(CGA::RED);
constexpr bgc lgrey_b = bgc(CGA::LIGHT_GREY);
constexpr fgc white_f = fgc(CGA::WHITE);
constexpr fgc black_f = fgc(CGA::BLACK);
constexpr fgc green_f = fgc(CGA::GREEN);
constexpr fgc red_f = fgc(CGA::RED);
constexpr fgc lgrey_f = fgc(CGA::LIGHT_GREY);
// NOTE: I changed this to use BufferedCGA so I can view offscreen text
class CGA_Stream : public OutStream, public BufferedCGA {
private:
CGA_Stream(CGA_Stream& copy) = delete; // Verhindere Kopieren
@ -50,7 +64,7 @@ public:
// Methode zur Ausgabe des Pufferinhalts der Basisklasse StringBuffer.
void flush() override;
// NOTE: I added this
// Change stream color
template<typename T>
// requires std::derived_from<T, CGA_Stream>
friend T& operator<<(T& os, const fgc& fg) {
@ -58,7 +72,6 @@ public:
os.color_fg = fg.fg;
return os;
}
template<typename T>
// requires std::derived_from<T, CGA_Stream>
friend T& operator<<(T& os, const bgc& bg) {

View File

@ -28,22 +28,20 @@
// NOTE: I added this
class fillw {
public:
fillw() : w(0) {}
fillw(unsigned char w) : w(w) {}
unsigned char w;
constexpr fillw(const unsigned char w) : w(w) {}
const unsigned char w;
};
class fillc {
public:
fillc() : c(' ') {}
fillc(char c) : c(c) {}
char c;
constexpr fillc(const char c) : c(c) {}
const char c;
};
class OutStream : public StringBuffer {
private:
OutStream(const OutStream& copy) = delete; // Verhindere Kopieren
// NOTE: I added this
// Some stream formatting
unsigned char fill_used; // indicates how many characters are already used by the text internally
void fill_use_char(); // recognizes that one char from the print width has been used up
void fill_finalize(); // does the filling after text has been written to buffer
@ -51,7 +49,7 @@ private:
public:
int base; // Basis des Zahlensystems: z.B. 2, 8, 10 oder 16
// NOTE: I added this
// // Some stream formatting
unsigned char fill_width;
char fill_char; // fill character for fixed width
@ -60,7 +58,6 @@ public:
// Methode zur Ausgabe des Pufferinhalts der Basisklasse StringBuffer.
void flush() override;
// NOTE: I added this, override put for fixed width
void put(char c) override;
// OPERATOR << : Umwandlung des angegebenen Datentypes in eine
@ -194,7 +191,7 @@ public:
return f(os);
}
// NOTE: I added this
// For stream formatting
template<typename T>
// requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, const fillw& w) {
@ -202,7 +199,6 @@ public:
os.fill_width = w.w;
return os;
}
template<typename T>
// requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, const fillc& c) {