diff --git a/include/SmartStreams.hpp b/include/SmartStreams.hpp deleted file mode 100644 index 8b974ff..0000000 --- a/include/SmartStreams.hpp +++ /dev/null @@ -1,162 +0,0 @@ -#pragma once -#ifndef _NB_SMARTSTREAM -#define _NB_SMARTSTREAM - -#include -#include -#include -#include -#include - -namespace nb { - -typedef std::pair SmartFormat; - -template -struct SmartText { - T msg; - SmartFormat fmt; -}; - -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; - -protected: - SmartOStreamBase(ST& stream) : _ostream{&stream} {} - ST* _ostream; - -}; - -template -class SmartOStreamBaseImpl; - -template -class SmartOStream; - -template -class SmartOStreamBaseImpl, Derived> : public SmartOStreamBase>{ -public: - using StreamType = SmartOStream; - using Base = SmartOStreamBase; - using Base::process; - using Base::Formatters; - -protected: - using Base::Base; -}; - -template -class SmartOStreamBaseImpl, Derived> : public SmartOStreamBase> { -public: - using StreamType = std::basic_ostream; - using Base = SmartOStreamBase; - using Base::process; - using Base::Formatters; - -protected: - using Base::Base; -}; - - -template -class SmartOStream : public SmartOStreamBaseImpl { - template - friend SmartOStream& operator<<(SmartOStream&, const U&); -public: - using Base = SmartOStreamBaseImpl; - SmartOStream(StreamType& stream) : Base(stream) {} - - StreamType* getStream() const { return _ostream; } - - template - void process(const X& val) { - *_ostream << val; - } - - template - void process(const SmartText& msg) { - using D = typename std::conditional::value, SmartOStream, Derived>::type; - try { - *_ostream << D::Formatters.at(msg.fmt.first)( - static_cast(this)->process(static_cast(msg.msg)), - msg.fmt.second - ); - } catch (const std::out_of_range& e) { - *_ostream << D::Formatters.at(0)( - static_cast(this)->process(static_cast(msg.msg)), - msg.fmt.second - ); - } - } - - virtual void process(const SmartText&); - - static const SmartFormatMap Formatters; - -protected: - using Base::_ostream; - - static std::string defaultFormatter(std::string msg, std::string fmt) { - return msg; - } -}; - -template -const SmartFormatMap SmartOStream::Formatters = { - {0, defaultFormatter} -}; - -template -SmartOStream& operator<<(SmartOStream& stream, const U& msg) { - using Derived = typename std::conditional::value, SmartOStream, D>::type; - static_cast(&stream)->process(msg); - return stream; -} - -template -void SmartOStream::process(const SmartText& msg) { - using Derived = typename std::conditional::value, SmartOStream, D>::type; - std::string msg_str(msg.msg); - try { - *_ostream << Derived::Formatters.at(msg.fmt.first)(msg_str, msg.fmt.second); - } catch (const std::out_of_range& e) { - *_ostream << Derived::Formatters.at(0)(msg_str, msg.fmt.second); - } -} - -template -class SmartTerminal : public SmartOStream> { - using Base = SmartOStream>; -public: - using Base::Base; - - static const SmartFormatMap Formatters; - - enum FormatterCodes : unsigned int { - DEFAULT = 0, - COLOR, - CLEAR, - BOLD, - - }; - -protected: - -}; - -template -const SmartFormatMap SmartTerminal::Formatters = { - {SmartTerminal::DEFAULT, SmartOStream::defaultFormatter} -}; - -} - -#endif // _NB_SMARTSTREAM \ No newline at end of file diff --git a/tests/SmartStreamTest/CMakeLists.txt b/tests/SmartStreamTest/CMakeLists.txt deleted file mode 100644 index 5aca423..0000000 --- a/tests/SmartStreamTest/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -cmake_minimum_required(VERSION 3.26.0) -project(gtest_Graphics VERSION 0.1.0 LANGUAGES C CXX) - -add_executable(SmartStreamTest main.cpp) -target_link_libraries(SmartStreamTest NBCore) \ No newline at end of file diff --git a/tests/SmartStreamTest/main.cpp b/tests/SmartStreamTest/main.cpp deleted file mode 100644 index 4d26296..0000000 --- a/tests/SmartStreamTest/main.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include - -#include "SmartStreams.hpp" - -template -class TestStream : public nb::SmartOStream> { - using Base = nb::SmartOStream>; -public: - TestStream(ST& stream) : Base(stream) {} - - static const nb::SmartFormatMap Formatters; - -protected: -}; - -template -const nb::SmartFormatMap TestStream::Formatters = { - {0, [](std::string msg, std::string fmt){ - return "[0] : " + msg; - }}, - {1, [](std::string msg, std::string fmt){ - return "\x1b[" + fmt + "m" + msg + "\x1b[0m"; - }}, - {2, [](std::string msg, std::string fmt){ - return "\x1b[1m" + msg + "\x1b[0m"; - }}, - {3, [](std::string msg, std::string fmt){ - return "\x1b[31m" + msg + "\x1b[0m"; - }} -}; - -using nb::SmartText; - -int main() { - - auto tout = TestStream(std::cout); - tout << SmartText>{SmartText{"Gloop\n", {3, ""}}, {2, "32"}}; - tout << 8989 << "\n"; - - return 0; -}