Implement ostream operator<< for RsSerializable

So it is not necessary to define it for each class we want to print in
  debug
This commit is contained in:
Gioacchino Mazzurco 2018-11-10 16:24:47 +01:00
parent e449ffba04
commit f84824271f
No known key found for this signature in database
GPG Key ID: A1FBCA3872E87051
4 changed files with 39 additions and 2 deletions

View File

@ -764,7 +764,8 @@ HEADERS += serialiser/rsserializable.h \
serialiser/rstypeserializer.h \
util/rsjson.h
SOURCES += serialiser/rsserializer.cc \
SOURCES += serialiser/rsserializable.cc \
serialiser/rsserializer.cc \
serialiser/rstypeserializer.cc \
util/rsjson.cc

View File

@ -0,0 +1,31 @@
/*******************************************************************************
* libretroshare: retroshare core library *
* *
* Copyright (C) 2018 Gioacchino Mazzurco <gio@eigenlab.org> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#include "serialiser/rsserializable.h"
#include <iostream>
std::ostream& operator<<(std::ostream& out, const RsSerializable& serializable)
{
RsGenericSerializer::SerializeContext ctx;
const_cast<RsSerializable&>(serializable) // safe with TO_JSON
.serial_process(RsGenericSerializer::TO_JSON, ctx);
return out << ctx.mJson;
}

View File

@ -20,8 +20,11 @@
* *
*******************************************************************************/
#pragma once
#include "serialiser/rsserializer.h"
#include <iosfwd>
/** @brief Minimal ancestor for all serializable structs in RetroShare.
* If you want your struct to be easly serializable you should inherit from this
@ -49,3 +52,5 @@ struct RsSerializable
#define RS_SERIAL_PROCESS(I) do { \
RsTypeSerializer::serial_process(j, ctx, I, #I ); \
} while(0)
std::ostream &operator<<(std::ostream& out, const RsSerializable& serializable);

View File

@ -39,7 +39,7 @@ typedef rapidjson::Document RsJson;
* @param[in] jDoc JSON document to print
* @return same output stream passed as out parameter
*/
std::ostream &operator<<(std::ostream &out, const RsJson &jDoc);
std::ostream& operator<<(std::ostream &out, const RsJson &jDoc);
/**
* Stream manipulator to print RsJson in compact format