1

clean cgastream

This commit is contained in:
2022-07-23 22:46:09 +02:00
parent ddad13defa
commit 3b69ffc82a
2 changed files with 18 additions and 30 deletions

View File

@ -24,23 +24,12 @@
* verwendet werden, um eine Ausgabe zu erzwingen. * * verwendet werden, um eine Ausgabe zu erzwingen. *
*****************************************************************************/ *****************************************************************************/
void CGA_Stream::flush() { void CGA_Stream::flush() {
print((char*)buffer, pos, attribute(this->color_bg, this->color_fg, this->blink)); print(buffer, pos, attribute(color_bg, color_fg, blink));
// TODO: Should not be reset like this
// Flushing resets attributes // Flushing resets attributes
this->blink = false; blink = false;
this->color_bg = CGA::BLACK; color_bg = CGA::BLACK;
this->color_fg = CGA::LIGHT_GREY; color_fg = CGA::LIGHT_GREY;
pos = 0; pos = 0;
} }
// Alternative way to write the templates which keeps definition/declaration separated
// Usable for our case but somehow defeats the purpose of templates
// template<typename T>
// T& operator<<(T& os, const fgc& fg) {
// os.color_fg = fg.fg;
// return os;
// }
// template CGA_Stream& operator<<<CGA_Stream>(CGA_Stream&, const fgc&);

View File

@ -19,61 +19,60 @@
#include "lib/OutStream.h" #include "lib/OutStream.h"
#include "lib/Semaphore.h" #include "lib/Semaphore.h"
// NOTE: I added these classes to allow for easier stream-like color changing // Allow for easier stream-like color changing
class fgc { class fgc {
public: public:
constexpr fgc(const CGA::color fg) : fg(fg) {} constexpr fgc(const CGA::color fg) : fg(fg) {}
const CGA::color fg; const CGA::color fg;
}; };
// 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 { class bgc {
public: public:
constexpr bgc(const CGA::color bg) : bg(bg) {} // Make this into a literal type to allow macro-like usage constexpr bgc(const CGA::color bg) : bg(bg) {}
const CGA::color bg; const CGA::color bg;
}; };
constexpr fgc white = fgc(CGA::WHITE); constexpr const fgc white = fgc(CGA::WHITE);
constexpr fgc black = fgc(CGA::BLACK); constexpr const fgc black = fgc(CGA::BLACK);
constexpr fgc green = fgc(CGA::GREEN); constexpr const fgc green = fgc(CGA::GREEN);
constexpr fgc red = fgc(CGA::RED); constexpr const fgc red = fgc(CGA::RED);
constexpr fgc lgrey = fgc(CGA::LIGHT_GREY); constexpr const fgc lgrey = fgc(CGA::LIGHT_GREY);
class CGA_Stream : public OutStream, public CGA { class CGA_Stream : public OutStream, public CGA {
private: private:
CGA_Stream(CGA_Stream& copy) = delete; // Verhindere Kopieren CGA_Stream(CGA_Stream& copy) = delete; // Verhindere Kopieren
// Allow for synchronization of output text, needed when running something in parallel to
// the PreemptiveThreadDemo for example
// NOTE: Should only be used by threads (like the demos) to not deadlock the system
Semaphore sem; Semaphore sem;
public:
CGA::color color_fg; CGA::color color_fg;
CGA::color color_bg; CGA::color color_bg;
bool blink; bool blink;
friend class Logger; // Give access to the color
public:
CGA_Stream() : sem(1), color_fg(CGA::LIGHT_GREY), color_bg(CGA::BLACK), blink(false) { CGA_Stream() : sem(1), color_fg(CGA::LIGHT_GREY), color_bg(CGA::BLACK), blink(false) {
flush(); flush();
} }
void lock() { sem.p(); } void lock() { sem.p(); }
void unlock() { sem.v(); } void unlock() { sem.v(); }
// void lock() {}
// void unlock() {}
// Methode zur Ausgabe des Pufferinhalts der Basisklasse StringBuffer. // Methode zur Ausgabe des Pufferinhalts der Basisklasse StringBuffer.
void flush() override; void flush() override;
// Change stream color // Change stream color
template<typename T> template<typename T>
// requires std::derived_from<T, CGA_Stream>
friend T& operator<<(T& os, const fgc& fg) { friend T& operator<<(T& os, const fgc& fg) {
os.flush(); os.flush();
os.color_fg = fg.fg; os.color_fg = fg.fg;
return os; return os;
} }
template<typename T> template<typename T>
// requires std::derived_from<T, CGA_Stream>
friend T& operator<<(T& os, const bgc& bg) { friend T& operator<<(T& os, const bgc& bg) {
os.flush(); os.flush();
os.color_fg = bg.bg; os.color_fg = bg.bg;