109 lines
2.7 KiB
C++
109 lines
2.7 KiB
C++
#pragma once
|
|
#ifndef _NB_SMARTSTREAM
|
|
#define _NB_SMARTSTREAM
|
|
|
|
#include <ostream>
|
|
#include <type_traits>
|
|
#include <unordered_map>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace nb {
|
|
|
|
typedef std::pair<unsigned int, std::string> SmartTextKV;
|
|
|
|
//template<typename T=std::string>
|
|
struct SmartText {
|
|
std::string msg;
|
|
SmartTextKV kv = {0, ""};
|
|
};
|
|
|
|
typedef std::string (*SmartTextFormatter)(std::string, std::string);
|
|
|
|
typedef std::unordered_map<unsigned int, SmartTextFormatter> SmartFormatMap;
|
|
|
|
template<typename ST, typename Derived>
|
|
class SmartOStreamBaseImpl;
|
|
|
|
template<typename ST, typename Derived=void>
|
|
class SmartOStream;
|
|
|
|
template<typename ST, typename Derived>
|
|
class SmartOStreamBaseImpl {
|
|
public:
|
|
virtual void process(const SmartText&) = 0;
|
|
|
|
static const SmartFormatMap Formatters;
|
|
using StreamType = SmartOStream<ST>;
|
|
protected:
|
|
SmartOStreamBaseImpl(StreamType& stream) : _ostream(&stream) {}
|
|
StreamType* _ostream;
|
|
};
|
|
|
|
template<typename C, typename T, typename Derived>
|
|
class SmartOStreamBaseImpl<std::basic_ostream<C, T>, Derived> {
|
|
public:
|
|
virtual void process(const SmartText&) = 0;
|
|
|
|
static const SmartFormatMap Formatters;
|
|
using StreamType = std::basic_ostream<C, T>;
|
|
protected:
|
|
SmartOStreamBaseImpl(StreamType& stream) : _ostream(&stream) {}
|
|
StreamType* _ostream;
|
|
};
|
|
|
|
|
|
template<typename StreamType, typename Derived>
|
|
class SmartOStream : public SmartOStreamBaseImpl<StreamType, Derived> {
|
|
template <typename ST, typename D, typename U>
|
|
friend SmartOStream<ST, D>& operator<<(SmartOStream<ST, D>&, const U&);
|
|
|
|
public:
|
|
StreamType* getStream() const { return _ostream; }
|
|
|
|
template <typename X>
|
|
void process(const X&);
|
|
|
|
void process(const SmartText&);
|
|
|
|
static const SmartFormatMap Formatters;
|
|
using Base = SmartOStreamBaseImpl<StreamType, Derived>;
|
|
|
|
protected:
|
|
using Base::Base;
|
|
using Base::_ostream;
|
|
|
|
static std::string defaultFormatter(std::string msg, std::string fmt) {
|
|
return msg;
|
|
}
|
|
};
|
|
|
|
template<typename ST, typename D>
|
|
const SmartFormatMap SmartOStream<ST, D>::Formatters = {
|
|
{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 D>
|
|
void SmartOStream<ST, D>::process(const SmartText& msg) {
|
|
try {
|
|
*_ostream << D::Formatters.at(msg.kv.first)(msg.msg, msg.kv.second);
|
|
} catch (const std::out_of_range& e) {
|
|
*_ostream << D::Formatters.at(0)(msg.msg, msg.kv.second);
|
|
}
|
|
}
|
|
|
|
template<typename ST, typename D>
|
|
template<typename X>
|
|
void SmartOStream<ST, D>::process(const X& msg) {
|
|
*_ostream << msg;
|
|
}
|
|
|
|
}
|
|
|
|
#endif // _NB_SMARTSTREAM
|