Betterer smostream
This commit is contained in:
parent
f797aea454
commit
c66c4d5574
@ -28,7 +28,7 @@ public:
|
|||||||
virtual void process(const SmartText&) = 0;
|
virtual void process(const SmartText&) = 0;
|
||||||
|
|
||||||
static const SmartFormatMap Formatters;
|
static const SmartFormatMap Formatters;
|
||||||
enum FormatterCodes : unsigned int;
|
|
||||||
protected:
|
protected:
|
||||||
SmartOStreamBase(ST& stream) : _ostream{&stream} {}
|
SmartOStreamBase(ST& stream) : _ostream{&stream} {}
|
||||||
ST* _ostream;
|
ST* _ostream;
|
||||||
@ -38,14 +38,14 @@ protected:
|
|||||||
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 STD, typename Derived>
|
template<typename ST, typename STD, typename Derived>
|
||||||
class SmartOStreamBaseImpl<SmartOStream<ST, STD>, Derived> : public SmartOStreamBase<SmartOStream<ST, STD>>{
|
class SmartOStreamBaseImpl<SmartOStream<ST, STD>, Derived> : public SmartOStreamBase<SmartOStream<ST, STD>>{
|
||||||
using Base = SmartOStreamBase<SmartOStream<ST, STD>>;
|
|
||||||
public:
|
public:
|
||||||
using StreamType = SmartOStream<ST, STD>;
|
using StreamType = SmartOStream<ST, STD>;
|
||||||
|
using Base = SmartOStreamBase<StreamType>;
|
||||||
using Base::process;
|
using Base::process;
|
||||||
using Base::Formatters;
|
using Base::Formatters;
|
||||||
|
|
||||||
@ -55,9 +55,9 @@ protected:
|
|||||||
|
|
||||||
template<typename C, typename T, typename Derived>
|
template<typename C, typename T, typename Derived>
|
||||||
class SmartOStreamBaseImpl<std::basic_ostream<C, T>, Derived> : public SmartOStreamBase<std::basic_ostream<C, T>> {
|
class SmartOStreamBaseImpl<std::basic_ostream<C, T>, Derived> : public SmartOStreamBase<std::basic_ostream<C, T>> {
|
||||||
using Base = SmartOStreamBase<std::basic_ostream<C, T>>;
|
|
||||||
public:
|
public:
|
||||||
using StreamType = std::basic_ostream<C, T>;
|
using StreamType = std::basic_ostream<C, T>;
|
||||||
|
using Base = SmartOStreamBase<StreamType>;
|
||||||
using Base::process;
|
using Base::process;
|
||||||
using Base::Formatters;
|
using Base::Formatters;
|
||||||
|
|
||||||
@ -72,7 +72,8 @@ class SmartOStream : public SmartOStreamBaseImpl<StreamType, Derived> {
|
|||||||
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>;
|
using Base = SmartOStreamBaseImpl<StreamType, Derived>;
|
||||||
|
SmartOStream(StreamType& stream) : Base(stream) {}
|
||||||
|
|
||||||
StreamType* getStream() const { return _ostream; }
|
StreamType* getStream() const { return _ostream; }
|
||||||
|
|
||||||
template <typename X>
|
template <typename X>
|
||||||
@ -80,12 +81,11 @@ public:
|
|||||||
*_ostream << val;
|
*_ostream << val;
|
||||||
}
|
}
|
||||||
|
|
||||||
void process(const SmartText&);
|
virtual void process(const SmartText&);
|
||||||
|
|
||||||
static const SmartFormatMap Formatters;
|
static const SmartFormatMap Formatters;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
using Base::Base;
|
|
||||||
using Base::_ostream;
|
using Base::_ostream;
|
||||||
|
|
||||||
static std::string defaultFormatter(std::string msg, std::string fmt) {
|
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>
|
template<typename ST, typename D>
|
||||||
const SmartFormatMap SmartOStream<ST, D>::Formatters = {
|
const SmartFormatMap SmartOStream<ST, D>::Formatters = {
|
||||||
{SmartOStream<ST, D>::FormatterCodes::DEFAULT, defaultFormatter}
|
{0, 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) {
|
||||||
static_cast<D*>(&stream)->process(msg);
|
using Derived = typename std::conditional<std::is_void<D>::value, SmartOStream<ST, void>, D>::type;
|
||||||
return stream;
|
static_cast<Derived*>(&stream)->process(msg);
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,30 +122,24 @@ public:
|
|||||||
using Base::Base;
|
using Base::Base;
|
||||||
|
|
||||||
static const SmartFormatMap Formatters;
|
static const SmartFormatMap Formatters;
|
||||||
enum FormatterCodes : unsigned int;
|
|
||||||
|
|
||||||
|
enum FormatterCodes : unsigned int {
|
||||||
|
DEFAULT = 0,
|
||||||
|
COLOR,
|
||||||
|
CLEAR,
|
||||||
|
BOLD,
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename ST, typename D>
|
|
||||||
enum SmartTerminal<ST, D>::FormatterCodes : unsigned int {
|
|
||||||
DEFAULT,
|
|
||||||
COLOR,
|
|
||||||
BACKGROUND,
|
|
||||||
BOLD,
|
|
||||||
CLEAR
|
|
||||||
};
|
|
||||||
|
|
||||||
{
|
|
||||||
|
|
||||||
template<typename ST, typename D>
|
template<typename ST, typename D>
|
||||||
const SmartTerminal<ST, D>::Formatters = {
|
const SmartFormatMap SmartTerminal<ST, D>::Formatters = {
|
||||||
{SmartTerminal<ST>}
|
{SmartTerminal<ST, D>::DEFAULT, SmartOStream<ST, D>::defaultFormatter}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // _NB_SMARTSTREAM
|
#endif // _NB_SMARTSTREAM
|
||||||
@ -30,7 +30,7 @@ int main() {
|
|||||||
|
|
||||||
auto tout = TestStream(std::cout);
|
auto tout = TestStream(std::cout);
|
||||||
tout << nb::SmartText{"Gloop!\n", {2, "32"}};
|
tout << nb::SmartText{"Gloop!\n", {2, "32"}};
|
||||||
tout << 8989;
|
tout << 8989 << "\n";
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user