13 #ifndef _XDRPP_PRINT_H_HEADER_INCLUDED_ 14 #define _XDRPP_PRINT_H_HEADER_INCLUDED_ 1 23 #define ENABLE_IF(expr) typename std::enable_if<expr>::type 29 std::string
hexdump(
const void *data,
size_t len);
34 std::ostringstream buf_;
40 Printer(
int indent) : indent_(indent) {}
42 std::ostream &bol(
const char *name =
nullptr);
43 void p(
const char *field,
const char *s) { bol(field) << s; }
44 void p(
const char *field,
const std::string &s) { bol(field) << s; }
46 void operator()(
const char *field,
xdr_void) { bol(field) <<
"void"; }
48 template<std::u
int32_t N>
void 49 operator()(
const char *field,
const xstring<N> &s) {
52 template<std::u
int32_t N>
void 53 operator()(
const char *field,
const opaque_array<N> &v) {
54 p(field,
hexdump(v.data(), v.size()));
56 template<std::u
int32_t N>
57 void operator()(
const char *field,
const opaque_vec<N> &v) {
58 p(field,
hexdump(v.data(), v.size()));
62 operator()(
const char *field, T t) {
63 if (
const char *n = xdr_traits<T>::enum_name(t))
66 p(field, std::to_string(t));
70 operator()(
const char *field, T t) { p(field, std::to_string(t)); }
73 template<
typename T>
void 74 operator()(
const char *field,
const std::tuple<T> &t) {
75 archive(*
this, std::get<0>(t), field);
78 template<
typename T>
void operator()(
const char *field,
const pointer<T> &t) {
80 archive(*
this, *t, field);
86 operator()(
const char *field,
const T &t) {
94 xdr_traits<T>::save(*
this, t);
107 operator()(
const char *field,
const T &t) {
108 bool skipnl = !field;
115 for (
const auto &o : t)
129 template<
typename T>
class has_xdr_printer {
130 template<
typename U>
static std::true_type
131 test(decltype(xdr_printer(*(U*)0)) *);
133 template<
typename U>
static std::false_type test(...);
135 static constexpr
bool value = decltype(test<T>(0))::value;
140 template<>
struct archive_adapter<detail::Printer> {
141 using Printer = detail::Printer;
143 template<
typename T>
static 144 typename std::enable_if<!detail::has_xdr_printer<T>::value>::type
145 apply(Printer &p,
const T &obj,
const char *field) {
149 template<
typename T>
static 150 typename std::enable_if<detail::has_xdr_printer<T>::value>::type
151 apply(Printer &p,
const T &obj,
const char *field) {
152 p.p(field, xdr_printer(obj));
161 template<
typename T> std::string
164 detail::Printer p(indent);
176 inline typename std::enable_if<xdr_traits<T>::valid, std::ostream &>::type
184 #endif // !_XDRPP_PRINT_H_HEADER_INCLUDED_ std::string escape_string(const std::string &s)
Use hex escapes for non-printable characters, and prefix backslashes and quotes with backslash...
Type definitions for xdrc compiler output.
std::tuple<> xdr_void
Placehoder type representing void values marshaled as 0 bytes.
static Constexpr const bool is_class
T is an XDR struct or union.
#define ENABLE_IF(expr)
Poor man's version of C++14 enable_if_t.
Most of the xdrpp library is encapsulated in the xdr namespace.
std::enable_if< xdr_traits< T >::valid, std::ostream & >::type operator<<(std::ostream &os, const T &t)
Print an arbitrary XDR structure to a std::ostream.
static Constexpr const bool is_enum
T is an XDR enum or bool (traits have enum_name).
static Constexpr const bool is_container
T is an xdr::pointer, xdr::xarray, or xdr::xvector (with load/save).
static Constexpr const bool is_numeric
T is one of [u]int{32,64}_t, float, or double.
std::string hexdump(const void *_data, size_t len)
Turn a string into a double-length sequence of hex nibbles.
std::string xdr_to_string(const T &t, const char *name=nullptr, int indent=0)
Return a std::string containing a pretty-printed version an XDR data type.