55 lines
1.3 KiB
C++
55 lines
1.3 KiB
C++
#pragma once
|
|
#include <google/protobuf/text_format.h>
|
|
#include <iostream>
|
|
|
|
namespace bts::core {
|
|
|
|
template <class T>
|
|
class ProtoPrinter {
|
|
public:
|
|
explicit ProtoPrinter(const T *message) noexcept;
|
|
static void print(const T &message, std::ostream &out = std::cout);
|
|
|
|
const T *message;
|
|
};
|
|
|
|
template <class T>
|
|
std::ostream &operator<<(
|
|
std::ostream &out, const ProtoPrinter<T> &printer) noexcept;
|
|
|
|
class ProtoPrinterFactory {
|
|
public:
|
|
template <class T>
|
|
static ProtoPrinter<T> create(const T *message);
|
|
};
|
|
|
|
//////////////////////////////////////////////////
|
|
// Definition
|
|
//////////////////////////////////////////////////
|
|
|
|
template <class T>
|
|
ProtoPrinter<T>::ProtoPrinter(const T *message) noexcept : message(message) {}
|
|
|
|
template <class T>
|
|
void ProtoPrinter<T>::print(const T &message, std::ostream &out) {
|
|
std::string str;
|
|
google::protobuf::TextFormat::PrintToString(message, &str);
|
|
out << str;
|
|
}
|
|
|
|
template <class T>
|
|
ProtoPrinter<T> ProtoPrinterFactory::create(const T *message) {
|
|
return ProtoPrinter<T>(message);
|
|
}
|
|
|
|
template <class T>
|
|
std::ostream &operator<<(
|
|
std::ostream &out, const ProtoPrinter<T> &printer) noexcept {
|
|
std::string str;
|
|
google::protobuf::TextFormat::PrintToString(*printer.message, &str);
|
|
out << str;
|
|
return out;
|
|
}
|
|
|
|
} // namespace bts::core
|