Still kinda working on smartstreams

This commit is contained in:
NaifBanana 2025-12-11 00:41:52 -06:00
parent 95120f411e
commit f797aea454
2 changed files with 77 additions and 27 deletions

View File

@ -22,34 +22,47 @@ typedef std::string (*SmartTextFormatter)(std::string, std::string);
typedef std::unordered_map<unsigned int, SmartTextFormatter> SmartFormatMap; typedef std::unordered_map<unsigned int, SmartTextFormatter> SmartFormatMap;
template <typename ST>
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<typename ST, typename Derived> template<typename ST, typename Derived>
class SmartOStreamBaseImpl; class SmartOStreamBaseImpl;
template<typename ST, typename Derived=void> template<typename ST, typename Derived=void>
class SmartOStream; class SmartOStream;
template<typename ST, typename Derived> template<typename ST, typename STD, typename Derived>
class SmartOStreamBaseImpl { class SmartOStreamBaseImpl<SmartOStream<ST, STD>, Derived> : public SmartOStreamBase<SmartOStream<ST, STD>>{
using Base = SmartOStreamBase<SmartOStream<ST, STD>>;
public: public:
virtual void process(const SmartText&) = 0; using StreamType = SmartOStream<ST, STD>;
using Base::process;
static const SmartFormatMap Formatters; using Base::Formatters;
using StreamType = SmartOStream<ST>;
protected: protected:
SmartOStreamBaseImpl(StreamType& stream) : _ostream(&stream) {} using Base::Base;
StreamType* _ostream;
}; };
template<typename C, typename T, typename Derived> template<typename C, typename T, typename Derived>
class SmartOStreamBaseImpl<std::basic_ostream<C, T>, Derived> { class SmartOStreamBaseImpl<std::basic_ostream<C, T>, Derived> : public SmartOStreamBase<std::basic_ostream<C, T>> {
public: using Base = SmartOStreamBase<std::basic_ostream<C, T>>;
virtual void process(const SmartText&) = 0; public:
static const SmartFormatMap Formatters;
using StreamType = std::basic_ostream<C, T>; using StreamType = std::basic_ostream<C, T>;
using Base::process;
using Base::Formatters;
protected: protected:
SmartOStreamBaseImpl(StreamType& stream) : _ostream(&stream) {} using Base::Base;
StreamType* _ostream;
}; };
@ -57,17 +70,19 @@ template<typename StreamType, typename Derived>
class SmartOStream : public SmartOStreamBaseImpl<StreamType, Derived> { class SmartOStream : public SmartOStreamBaseImpl<StreamType, Derived> {
template <typename ST, typename D, typename U> template <typename ST, typename D, typename U>
friend SmartOStream<ST, D>& operator<<(SmartOStream<ST, D>&, const U&); friend SmartOStream<ST, D>& operator<<(SmartOStream<ST, D>&, const U&);
public: public:
using Base = SmartOStreamBaseImpl<StreamType, Derived>;
StreamType* getStream() const { return _ostream; } StreamType* getStream() const { return _ostream; }
template <typename X> template <typename X>
void process(const X&); void process(const X& val) {
*_ostream << val;
}
void process(const SmartText&); void process(const SmartText&);
static const SmartFormatMap Formatters; static const SmartFormatMap Formatters;
using Base = SmartOStreamBaseImpl<StreamType, Derived>;
protected: protected:
using Base::Base; using Base::Base;
@ -78,10 +93,15 @@ protected:
} }
}; };
template<typename ST, typename D>
enum SmartOStream<ST, D>::FormatterCodes : unsigned int {
DEFAULT
};
template<typename ST, typename D> template<typename ST, typename D>
const SmartFormatMap SmartOStream<ST, D>::Formatters = { const SmartFormatMap SmartOStream<ST, D>::Formatters = {
{0, defaultFormatter} {SmartOStream<ST, D>::FormatterCodes::DEFAULT, defaultFormatter}
}; };
template <typename ST, typename D, typename U> template <typename ST, typename D, typename U>
SmartOStream<ST, D>& operator<<(SmartOStream<ST, D>& stream, const U& msg) { SmartOStream<ST, D>& operator<<(SmartOStream<ST, D>& stream, const U& msg) {
@ -89,19 +109,51 @@ SmartOStream<ST, D>& operator<<(SmartOStream<ST, D>& stream, const U& msg) {
return stream; return stream;
} }
template <typename ST, typename U>
SmartOStream<ST, void>& operator<<(SmartOStream<ST, void>& stream, const U& msg) {
static_cast<SmartOStream<ST, void>*>(&stream)->process(msg);
return stream;
}
template<typename ST, typename D> template<typename ST, typename D>
void SmartOStream<ST, D>::process(const SmartText& msg) { void SmartOStream<ST, D>::process(const SmartText& msg) {
using Derived = typename std::conditional<std::is_void<D>::value, SmartOStream<ST, void>, D>::type;
try { 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) { } 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<typename ST, typename D> template<typename ST, typename D>
template<typename X> class SmartTerminal : public SmartOStream<ST, SmartTerminal<ST, D>> {
void SmartOStream<ST, D>::process(const X& msg) { using Base = SmartOStream<ST, SmartTerminal<ST, D>>;
*_ostream << msg; public:
using Base::Base;
static const SmartFormatMap Formatters;
enum FormatterCodes : unsigned int;
protected:
};
template <typename ST, typename D>
enum SmartTerminal<ST, D>::FormatterCodes : unsigned int {
DEFAULT,
COLOR,
BACKGROUND,
BOLD,
CLEAR
};
{
template<typename ST, typename D>
const SmartTerminal<ST, D>::Formatters = {
{SmartTerminal<ST>}
};
} }
} }

View File

@ -9,8 +9,6 @@ public:
TestStream(ST& stream) : Base(stream) {} TestStream(ST& stream) : Base(stream) {}
static const nb::SmartFormatMap Formatters; static const nb::SmartFormatMap Formatters;
// using Base::Formatters;
protected: protected:
}; };