diff --git a/include/SmartStreams.hpp b/include/SmartStreams.hpp index 070b531..d8beeaa 100644 --- a/include/SmartStreams.hpp +++ b/include/SmartStreams.hpp @@ -22,34 +22,47 @@ typedef std::string (*SmartTextFormatter)(std::string, std::string); typedef std::unordered_map SmartFormatMap; +template +class SmartOStreamBase { +public: + virtual void process(const SmartText&) = 0; + + static const SmartFormatMap Formatters; + enum FormatterCodes : unsigned int; +protected: + SmartOStreamBase(ST& stream) : _ostream{&stream} {} + ST* _ostream; + +}; + template class SmartOStreamBaseImpl; template class SmartOStream; -template -class SmartOStreamBaseImpl { +template +class SmartOStreamBaseImpl, Derived> : public SmartOStreamBase>{ + using Base = SmartOStreamBase>; public: - virtual void process(const SmartText&) = 0; - - static const SmartFormatMap Formatters; - using StreamType = SmartOStream; + using StreamType = SmartOStream; + using Base::process; + using Base::Formatters; + protected: - SmartOStreamBaseImpl(StreamType& stream) : _ostream(&stream) {} - StreamType* _ostream; + using Base::Base; }; template -class SmartOStreamBaseImpl, Derived> { - public: - virtual void process(const SmartText&) = 0; - - static const SmartFormatMap Formatters; +class SmartOStreamBaseImpl, Derived> : public SmartOStreamBase> { + using Base = SmartOStreamBase>; +public: using StreamType = std::basic_ostream; + using Base::process; + using Base::Formatters; + protected: - SmartOStreamBaseImpl(StreamType& stream) : _ostream(&stream) {} - StreamType* _ostream; + using Base::Base; }; @@ -57,17 +70,19 @@ template class SmartOStream : public SmartOStreamBaseImpl { template friend SmartOStream& operator<<(SmartOStream&, const U&); - public: + using Base = SmartOStreamBaseImpl; + StreamType* getStream() const { return _ostream; } template - void process(const X&); + void process(const X& val) { + *_ostream << val; + } void process(const SmartText&); static const SmartFormatMap Formatters; - using Base = SmartOStreamBaseImpl; protected: using Base::Base; @@ -78,10 +93,15 @@ protected: } }; +template +enum SmartOStream::FormatterCodes : unsigned int { + DEFAULT +}; + template const SmartFormatMap SmartOStream::Formatters = { - {0, defaultFormatter} - }; + {SmartOStream::FormatterCodes::DEFAULT, defaultFormatter} +}; template SmartOStream& operator<<(SmartOStream& stream, const U& msg) { @@ -89,19 +109,51 @@ SmartOStream& operator<<(SmartOStream& stream, const U& msg) { return stream; } +template +SmartOStream& operator<<(SmartOStream& stream, const U& msg) { + static_cast*>(&stream)->process(msg); + return stream; +} + template void SmartOStream::process(const SmartText& msg) { + using Derived = typename std::conditional::value, SmartOStream, D>::type; try { - *_ostream << D::Formatters.at(msg.kv.first)(msg.msg, msg.kv.second); + *_ostream << Derived::Formatters.at(msg.kv.first)(msg.msg, msg.kv.second); } catch (const std::out_of_range& e) { - *_ostream << D::Formatters.at(0)(msg.msg, msg.kv.second); + *_ostream << Derived::Formatters.at(0)(msg.msg, msg.kv.second); } } template -template -void SmartOStream::process(const X& msg) { - *_ostream << msg; +class SmartTerminal : public SmartOStream> { + using Base = SmartOStream>; +public: + using Base::Base; + + static const SmartFormatMap Formatters; + enum FormatterCodes : unsigned int; + +protected: + +}; + +template +enum SmartTerminal::FormatterCodes : unsigned int { + DEFAULT, + COLOR, + BACKGROUND, + BOLD, + CLEAR +}; + +{ + +template +const SmartTerminal::Formatters = { + {SmartTerminal} +}; + } } diff --git a/tests/SmartStreamTest/main.cpp b/tests/SmartStreamTest/main.cpp index 7c9815d..b1fb2b0 100644 --- a/tests/SmartStreamTest/main.cpp +++ b/tests/SmartStreamTest/main.cpp @@ -9,8 +9,6 @@ public: TestStream(ST& stream) : Base(stream) {} static const nb::SmartFormatMap Formatters; - // using Base::Formatters; - protected: };