Smarter SmartText (not fully working)
This commit is contained in:
parent
c66c4d5574
commit
6a0583b44b
@ -10,12 +10,12 @@
|
||||
|
||||
namespace nb {
|
||||
|
||||
typedef std::pair<unsigned int, std::string> SmartTextKV;
|
||||
typedef std::pair<unsigned int, std::string> SmartFormat;
|
||||
|
||||
//template<typename T=std::string>
|
||||
template <typename T=const char*>
|
||||
struct SmartText {
|
||||
std::string msg;
|
||||
SmartTextKV kv = {0, ""};
|
||||
T msg;
|
||||
SmartFormat fmt;
|
||||
};
|
||||
|
||||
typedef std::string (*SmartTextFormatter)(std::string, std::string);
|
||||
@ -25,7 +25,7 @@ typedef std::unordered_map<unsigned int, SmartTextFormatter> SmartFormatMap;
|
||||
template <typename ST>
|
||||
class SmartOStreamBase {
|
||||
public:
|
||||
virtual void process(const SmartText&) = 0;
|
||||
virtual void process(const SmartText<const char*>&) = 0;
|
||||
|
||||
static const SmartFormatMap Formatters;
|
||||
|
||||
@ -81,7 +81,23 @@ public:
|
||||
*_ostream << val;
|
||||
}
|
||||
|
||||
virtual void process(const SmartText&);
|
||||
template <typename Msg>
|
||||
void process(const SmartText<Msg>& msg) {
|
||||
using D = typename std::conditional<std::is_void<Derived>::value, SmartOStream<StreamType, void>, Derived>::type;
|
||||
try {
|
||||
*_ostream << D::Formatters.at(msg.fmt.first)(
|
||||
static_cast<D*>(this)->process(static_cast<Msg>(msg.msg)),
|
||||
msg.fmt.second
|
||||
);
|
||||
} catch (const std::out_of_range& e) {
|
||||
*_ostream << D::Formatters.at(0)(
|
||||
static_cast<D*>(this)->process(static_cast<Msg>(msg.msg)),
|
||||
msg.fmt.second
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void process(const SmartText<const char*>&);
|
||||
|
||||
static const SmartFormatMap Formatters;
|
||||
|
||||
@ -106,12 +122,13 @@ SmartOStream<ST, D>& operator<<(SmartOStream<ST, D>& stream, const U& msg) {
|
||||
}
|
||||
|
||||
template<typename ST, typename D>
|
||||
void SmartOStream<ST, D>::process(const SmartText& msg) {
|
||||
void SmartOStream<ST, D>::process(const SmartText<const char*>& msg) {
|
||||
using Derived = typename std::conditional<std::is_void<D>::value, SmartOStream<ST, void>, D>::type;
|
||||
std::string msg_str(msg.msg);
|
||||
try {
|
||||
*_ostream << Derived::Formatters.at(msg.kv.first)(msg.msg, msg.kv.second);
|
||||
*_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.msg, msg.kv.second);
|
||||
*_ostream << Derived::Formatters.at(0)(msg_str, msg.fmt.second);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -23,13 +23,18 @@ const nb::SmartFormatMap TestStream<ST>::Formatters = {
|
||||
}},
|
||||
{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 << nb::SmartText{"Gloop!\n", {2, "32"}};
|
||||
tout << SmartText<SmartText<const char*>>{SmartText<const char*>{"Gloop\n", {3, ""}}, {2, "32"}};
|
||||
tout << 8989 << "\n";
|
||||
|
||||
return 0;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user