From c66c4d5574cdcb9ab9551432a4aca78dc4034e9d Mon Sep 17 00:00:00 2001 From: NaifBanana <30419422+NaifBanana@users.noreply.github.com> Date: Thu, 11 Dec 2025 01:31:11 -0600 Subject: [PATCH] Betterer smostream --- include/SmartStreams.hpp | 56 ++++++++++++---------------------- tests/SmartStreamTest/main.cpp | 2 +- 2 files changed, 21 insertions(+), 37 deletions(-) diff --git a/include/SmartStreams.hpp b/include/SmartStreams.hpp index d8beeaa..bb4ed6a 100644 --- a/include/SmartStreams.hpp +++ b/include/SmartStreams.hpp @@ -28,7 +28,7 @@ public: virtual void process(const SmartText&) = 0; static const SmartFormatMap Formatters; - enum FormatterCodes : unsigned int; + protected: SmartOStreamBase(ST& stream) : _ostream{&stream} {} ST* _ostream; @@ -38,14 +38,14 @@ protected: template class SmartOStreamBaseImpl; -template +template class SmartOStream; template class SmartOStreamBaseImpl, Derived> : public SmartOStreamBase>{ - using Base = SmartOStreamBase>; public: using StreamType = SmartOStream; + using Base = SmartOStreamBase; using Base::process; using Base::Formatters; @@ -55,9 +55,9 @@ protected: template class SmartOStreamBaseImpl, Derived> : public SmartOStreamBase> { - using Base = SmartOStreamBase>; public: using StreamType = std::basic_ostream; + using Base = SmartOStreamBase; using Base::process; using Base::Formatters; @@ -72,7 +72,8 @@ class SmartOStream : public SmartOStreamBaseImpl { friend SmartOStream& operator<<(SmartOStream&, const U&); public: using Base = SmartOStreamBaseImpl; - + SmartOStream(StreamType& stream) : Base(stream) {} + StreamType* getStream() const { return _ostream; } template @@ -80,12 +81,11 @@ public: *_ostream << val; } - void process(const SmartText&); + virtual void process(const SmartText&); static const SmartFormatMap Formatters; protected: - using Base::Base; using Base::_ostream; static std::string defaultFormatter(std::string msg, std::string fmt) { @@ -93,25 +93,15 @@ protected: } }; -template -enum SmartOStream::FormatterCodes : unsigned int { - DEFAULT -}; - template const SmartFormatMap SmartOStream::Formatters = { - {SmartOStream::FormatterCodes::DEFAULT, defaultFormatter} + {0, defaultFormatter} }; template SmartOStream& operator<<(SmartOStream& stream, const U& msg) { - static_cast(&stream)->process(msg); - return stream; -} - -template -SmartOStream& operator<<(SmartOStream& stream, const U& msg) { - static_cast*>(&stream)->process(msg); + using Derived = typename std::conditional::value, SmartOStream, D>::type; + static_cast(&stream)->process(msg); return stream; } @@ -132,30 +122,24 @@ public: using Base::Base; static const SmartFormatMap Formatters; - enum FormatterCodes : unsigned int; + enum FormatterCodes : unsigned int { + DEFAULT = 0, + COLOR, + CLEAR, + BOLD, + + }; + protected: }; -template -enum SmartTerminal::FormatterCodes : unsigned int { - DEFAULT, - COLOR, - BACKGROUND, - BOLD, - CLEAR -}; - -{ - template -const SmartTerminal::Formatters = { - {SmartTerminal} +const SmartFormatMap SmartTerminal::Formatters = { + {SmartTerminal::DEFAULT, SmartOStream::defaultFormatter} }; } -} - #endif // _NB_SMARTSTREAM \ No newline at end of file diff --git a/tests/SmartStreamTest/main.cpp b/tests/SmartStreamTest/main.cpp index b1fb2b0..ef0e757 100644 --- a/tests/SmartStreamTest/main.cpp +++ b/tests/SmartStreamTest/main.cpp @@ -30,7 +30,7 @@ int main() { auto tout = TestStream(std::cout); tout << nb::SmartText{"Gloop!\n", {2, "32"}}; - tout << 8989; + tout << 8989 << "\n"; return 0; }