Betterer smostream

This commit is contained in:
NaifBanana 2025-12-11 01:31:11 -06:00
parent f797aea454
commit c66c4d5574
2 changed files with 21 additions and 37 deletions

View File

@ -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<typename ST, typename Derived>
class SmartOStreamBaseImpl;
template<typename ST, typename Derived=void>
template<typename ST, typename Derived = void>
class SmartOStream;
template<typename ST, typename STD, typename Derived>
class SmartOStreamBaseImpl<SmartOStream<ST, STD>, Derived> : public SmartOStreamBase<SmartOStream<ST, STD>>{
using Base = SmartOStreamBase<SmartOStream<ST, STD>>;
public:
using StreamType = SmartOStream<ST, STD>;
using Base = SmartOStreamBase<StreamType>;
using Base::process;
using Base::Formatters;
@ -55,9 +55,9 @@ protected:
template<typename C, typename T, typename Derived>
class SmartOStreamBaseImpl<std::basic_ostream<C, T>, Derived> : public SmartOStreamBase<std::basic_ostream<C, T>> {
using Base = SmartOStreamBase<std::basic_ostream<C, T>>;
public:
using StreamType = std::basic_ostream<C, T>;
using Base = SmartOStreamBase<StreamType>;
using Base::process;
using Base::Formatters;
@ -72,7 +72,8 @@ class SmartOStream : public SmartOStreamBaseImpl<StreamType, Derived> {
friend SmartOStream<ST, D>& operator<<(SmartOStream<ST, D>&, const U&);
public:
using Base = SmartOStreamBaseImpl<StreamType, Derived>;
SmartOStream(StreamType& stream) : Base(stream) {}
StreamType* getStream() const { return _ostream; }
template <typename X>
@ -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<typename ST, typename D>
enum SmartOStream<ST, D>::FormatterCodes : unsigned int {
DEFAULT
};
template<typename ST, typename D>
const SmartFormatMap SmartOStream<ST, D>::Formatters = {
{SmartOStream<ST, D>::FormatterCodes::DEFAULT, defaultFormatter}
{0, defaultFormatter}
};
template <typename ST, typename D, typename U>
SmartOStream<ST, D>& operator<<(SmartOStream<ST, D>& stream, const U& msg) {
static_cast<D*>(&stream)->process(msg);
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);
using Derived = typename std::conditional<std::is_void<D>::value, SmartOStream<ST, void>, D>::type;
static_cast<Derived*>(&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 <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>}
const SmartFormatMap SmartTerminal<ST, D>::Formatters = {
{SmartTerminal<ST, D>::DEFAULT, SmartOStream<ST, D>::defaultFormatter}
};
}
}
#endif // _NB_SMARTSTREAM

View File

@ -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;
}