Betterer smostream
This commit is contained in:
parent
f797aea454
commit
c66c4d5574
@ -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;
|
||||
@ -43,9 +43,9 @@ 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,6 +72,7 @@ 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; }
|
||||
|
||||
@ -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
|
||||
const SmartFormatMap SmartTerminal<ST, D>::Formatters = {
|
||||
{SmartTerminal<ST, D>::DEFAULT, SmartOStream<ST, D>::defaultFormatter}
|
||||
};
|
||||
|
||||
{
|
||||
|
||||
template<typename ST, typename D>
|
||||
const SmartTerminal<ST, D>::Formatters = {
|
||||
{SmartTerminal<ST>}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // _NB_SMARTSTREAM
|
||||
@ -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;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user