52 lines
1.1 KiB
C++
52 lines
1.1 KiB
C++
#pragma once
|
|
#ifndef _NB_PRINTER
|
|
#define _NB_PRINTER
|
|
|
|
#include <condition_variable>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <mutex>
|
|
|
|
namespace nb {
|
|
|
|
template<typename STREAM, typename PrinterType>
|
|
class Printer {
|
|
private:
|
|
PrinterType* const type_ptr = static_cast<PrinterType*>(this);
|
|
|
|
protected:
|
|
STREAM& _stream;
|
|
Printer(STREAM* const stream_) : _stream(stream_) {}
|
|
|
|
public:
|
|
template<typename T>
|
|
PrinterType& operator<<(const T& val_) {
|
|
(*type_ptr) << val_;
|
|
return type_ptr;
|
|
}
|
|
|
|
};
|
|
|
|
class Terminal : public Printer<decltype(std::cout), Terminal> {
|
|
private:
|
|
Terminal* const type_ptr = static_cast<Terminal*>(this);
|
|
using MutexLock = std::lock_guard<std::mutex>;
|
|
|
|
protected:
|
|
using Base = Printer<decltype(std::cout), Terminal>;
|
|
using Base::_stream;
|
|
std::mutex _mutex;
|
|
|
|
public:
|
|
using Base::Base;
|
|
|
|
template<typename T>
|
|
Terminal& operator<<(const T& val_) {
|
|
MutexLock lock(_mutex);
|
|
_stream << val_;
|
|
return *type_ptr;
|
|
}
|
|
};
|
|
|
|
} // namespace nb
|
|
#endif // _NB_PRINTER
|