mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-01-14 08:59:50 -05:00
Safer and elgant serial helper macros
This commit is contained in:
parent
7409de5170
commit
13d4a2c916
@ -212,4 +212,4 @@ RS_REGISTER_SERIALIZABLE_TYPE_DEF(RsChatMsgItem)
|
||||
void PrivateOugoingMapItem::serial_process(
|
||||
RsGenericSerializer::SerializeJob j,
|
||||
RsGenericSerializer::SerializeContext& ctx )
|
||||
{ RS_PROCESS_SERIAL_MEMBER(store); }
|
||||
{ RS_SERIAL_PROCESS(store); }
|
||||
|
@ -48,24 +48,24 @@ void OutgoingRecord_deprecated::serial_process(
|
||||
RsGenericSerializer::SerializeJob j,
|
||||
RsGenericSerializer::SerializeContext& ctx )
|
||||
{
|
||||
RS_PROCESS_SERIAL_MEMBER_TYPED(status, uint8_t);
|
||||
RS_PROCESS_SERIAL_MEMBER(recipient);
|
||||
RS_PROCESS_SERIAL_MEMBER(mailItem);
|
||||
RS_PROCESS_SERIAL_MEMBER(mailData);
|
||||
RS_PROCESS_SERIAL_MEMBER_TYPED(clientService, uint16_t);
|
||||
RS_PROCESS_SERIAL_MEMBER(presignedReceipt);
|
||||
RS_SERIAL_PROCESS(status);
|
||||
RS_SERIAL_PROCESS(recipient);
|
||||
RS_SERIAL_PROCESS(mailItem);
|
||||
RS_SERIAL_PROCESS(mailData);
|
||||
RS_SERIAL_PROCESS(clientService);
|
||||
RS_SERIAL_PROCESS(presignedReceipt);
|
||||
}
|
||||
|
||||
void OutgoingRecord::serial_process(RsGenericSerializer::SerializeJob j,
|
||||
RsGenericSerializer::SerializeContext& ctx)
|
||||
{
|
||||
RS_PROCESS_SERIAL_MEMBER_TYPED(status, uint8_t);
|
||||
RS_PROCESS_SERIAL_MEMBER(recipient);
|
||||
RS_PROCESS_SERIAL_MEMBER(author);
|
||||
RS_PROCESS_SERIAL_MEMBER(group_id);
|
||||
RS_PROCESS_SERIAL_MEMBER(sent_ts);
|
||||
RS_PROCESS_SERIAL_MEMBER(mailItem);
|
||||
RS_PROCESS_SERIAL_MEMBER(mailData);
|
||||
RS_PROCESS_SERIAL_MEMBER_TYPED(clientService, uint16_t);
|
||||
RS_PROCESS_SERIAL_MEMBER(presignedReceipt);
|
||||
RS_SERIAL_PROCESS(status);
|
||||
RS_SERIAL_PROCESS(recipient);
|
||||
RS_SERIAL_PROCESS(author);
|
||||
RS_SERIAL_PROCESS(group_id);
|
||||
RS_SERIAL_PROCESS(sent_ts);
|
||||
RS_SERIAL_PROCESS(mailItem);
|
||||
RS_SERIAL_PROCESS(mailData);
|
||||
RS_SERIAL_PROCESS(clientService);
|
||||
RS_SERIAL_PROCESS(presignedReceipt);
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ public:
|
||||
|
||||
void serial_process( RsGenericSerializer::SerializeJob j,
|
||||
RsGenericSerializer::SerializeContext& ctx )
|
||||
{ RS_PROCESS_SERIAL_MEMBER_TYPED(mailId, uint64_t); }
|
||||
{ RS_SERIAL_PROCESS(mailId); }
|
||||
};
|
||||
|
||||
class RsGxsTransPresignedReceipt : public RsGxsTransBaseMsgItem
|
||||
@ -140,9 +140,9 @@ public:
|
||||
RsGenericSerializer::SerializeContext& ctx )
|
||||
{
|
||||
RsGxsTransBaseMsgItem::serial_process(j, ctx);
|
||||
RS_PROCESS_SERIAL_MEMBER_TYPED(cryptoType, uint8_t);
|
||||
RS_PROCESS_SERIAL_MEMBER(recipientHint);
|
||||
RS_PROCESS_SERIAL_MEMBER(payload);
|
||||
RS_SERIAL_PROCESS(cryptoType);
|
||||
RS_SERIAL_PROCESS(recipientHint);
|
||||
RS_SERIAL_PROCESS(payload);
|
||||
}
|
||||
|
||||
void clear()
|
||||
|
@ -100,24 +100,24 @@ struct RsGroupMetaData : RsSerializable
|
||||
void serial_process( RsGenericSerializer::SerializeJob j,
|
||||
RsGenericSerializer::SerializeContext& ctx )
|
||||
{
|
||||
RS_PROCESS_SERIAL_MEMBER(mGroupId);
|
||||
RS_PROCESS_SERIAL_MEMBER(mGroupName);
|
||||
RS_PROCESS_SERIAL_MEMBER(mGroupFlags);
|
||||
RS_PROCESS_SERIAL_MEMBER(mSignFlags);
|
||||
RS_PROCESS_SERIAL_MEMBER(mPublishTs);
|
||||
RS_PROCESS_SERIAL_MEMBER(mAuthorId);
|
||||
RS_PROCESS_SERIAL_MEMBER(mCircleId);
|
||||
RS_PROCESS_SERIAL_MEMBER(mCircleType);
|
||||
RS_PROCESS_SERIAL_MEMBER(mAuthenFlags);
|
||||
RS_PROCESS_SERIAL_MEMBER(mParentGrpId);
|
||||
RS_PROCESS_SERIAL_MEMBER(mSubscribeFlags);
|
||||
RS_PROCESS_SERIAL_MEMBER(mPop);
|
||||
RS_PROCESS_SERIAL_MEMBER(mVisibleMsgCount);
|
||||
RS_PROCESS_SERIAL_MEMBER(mLastPost);
|
||||
RS_PROCESS_SERIAL_MEMBER(mGroupStatus);
|
||||
RS_PROCESS_SERIAL_MEMBER(mServiceString);
|
||||
RS_PROCESS_SERIAL_MEMBER(mOriginator);
|
||||
RS_PROCESS_SERIAL_MEMBER(mInternalCircle);
|
||||
RS_SERIAL_PROCESS(mGroupId);
|
||||
RS_SERIAL_PROCESS(mGroupName);
|
||||
RS_SERIAL_PROCESS(mGroupFlags);
|
||||
RS_SERIAL_PROCESS(mSignFlags);
|
||||
RS_SERIAL_PROCESS(mPublishTs);
|
||||
RS_SERIAL_PROCESS(mAuthorId);
|
||||
RS_SERIAL_PROCESS(mCircleId);
|
||||
RS_SERIAL_PROCESS(mCircleType);
|
||||
RS_SERIAL_PROCESS(mAuthenFlags);
|
||||
RS_SERIAL_PROCESS(mParentGrpId);
|
||||
RS_SERIAL_PROCESS(mSubscribeFlags);
|
||||
RS_SERIAL_PROCESS(mPop);
|
||||
RS_SERIAL_PROCESS(mVisibleMsgCount);
|
||||
RS_SERIAL_PROCESS(mLastPost);
|
||||
RS_SERIAL_PROCESS(mGroupStatus);
|
||||
RS_SERIAL_PROCESS(mServiceString);
|
||||
RS_SERIAL_PROCESS(mOriginator);
|
||||
RS_SERIAL_PROCESS(mInternalCircle);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -98,10 +98,10 @@ struct GxsReputation : RsSerializable
|
||||
void serial_process( RsGenericSerializer::SerializeJob j,
|
||||
RsGenericSerializer::SerializeContext& ctx )
|
||||
{
|
||||
RS_PROCESS_SERIAL_MEMBER(mOverallScore);
|
||||
RS_PROCESS_SERIAL_MEMBER(mIdScore);
|
||||
RS_PROCESS_SERIAL_MEMBER(mOwnOpinion);
|
||||
RS_PROCESS_SERIAL_MEMBER(mPeerOpinion);
|
||||
RS_SERIAL_PROCESS(mOverallScore);
|
||||
RS_SERIAL_PROCESS(mIdScore);
|
||||
RS_SERIAL_PROCESS(mOwnOpinion);
|
||||
RS_SERIAL_PROCESS(mPeerOpinion);
|
||||
}
|
||||
};
|
||||
|
||||
@ -278,13 +278,13 @@ struct RsIdentityUsage : RsSerializable
|
||||
void serial_process( RsGenericSerializer::SerializeJob j,
|
||||
RsGenericSerializer::SerializeContext& ctx )
|
||||
{
|
||||
RS_PROCESS_SERIAL_MEMBER(mServiceId);
|
||||
RS_PROCESS_SERIAL_MEMBER_TYPED(mUsageCode, uint8_t);
|
||||
RS_PROCESS_SERIAL_MEMBER(mGrpId);
|
||||
RS_PROCESS_SERIAL_MEMBER(mMsgId);
|
||||
RS_PROCESS_SERIAL_MEMBER(mAdditionalId);
|
||||
RS_PROCESS_SERIAL_MEMBER(mComment);
|
||||
RS_PROCESS_SERIAL_MEMBER(mHash);
|
||||
RS_SERIAL_PROCESS(mServiceId);
|
||||
RS_SERIAL_PROCESS(mUsageCode);
|
||||
RS_SERIAL_PROCESS(mGrpId);
|
||||
RS_SERIAL_PROCESS(mMsgId);
|
||||
RS_SERIAL_PROCESS(mAdditionalId);
|
||||
RS_SERIAL_PROCESS(mComment);
|
||||
RS_SERIAL_PROCESS(mHash);
|
||||
}
|
||||
|
||||
friend struct RsTypeSerializer;
|
||||
@ -329,14 +329,14 @@ struct RsIdentityDetails : RsSerializable
|
||||
virtual void serial_process(RsGenericSerializer::SerializeJob j,
|
||||
RsGenericSerializer::SerializeContext& ctx)
|
||||
{
|
||||
RS_PROCESS_SERIAL_MEMBER(mId);
|
||||
RS_PROCESS_SERIAL_MEMBER(mNickname);
|
||||
RS_PROCESS_SERIAL_MEMBER(mFlags);
|
||||
RS_PROCESS_SERIAL_MEMBER(mPgpId);
|
||||
//RS_PROCESS_SERIAL_MEMBER_TYPED(mReputation, RsSerializable);
|
||||
//RS_PROCESS_SERIAL_MEMBER_TYPED(mAvatar, RsSerializable);
|
||||
RS_PROCESS_SERIAL_MEMBER(mLastUsageTS);
|
||||
RS_PROCESS_SERIAL_MEMBER(mUseCases);
|
||||
RS_SERIAL_PROCESS(mId);
|
||||
RS_SERIAL_PROCESS(mNickname);
|
||||
RS_SERIAL_PROCESS(mFlags);
|
||||
RS_SERIAL_PROCESS(mPgpId);
|
||||
//RS_SERIAL_PROCESS(mReputation);
|
||||
//RS_SERIAL_PROCESS(mAvatar);
|
||||
RS_SERIAL_PROCESS(mLastUsageTS);
|
||||
RS_SERIAL_PROCESS(mUseCases);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -73,12 +73,12 @@ void RsNxsSyncMsgItem::serial_process(RsGenericSerializer::SerializeJob j,RsGene
|
||||
void RsNxsMsg::serial_process( RsGenericSerializer::SerializeJob j,
|
||||
RsGenericSerializer::SerializeContext& ctx )
|
||||
{
|
||||
RS_PROCESS_SERIAL_MEMBER_TYPED(transactionNumber, uint32_t);
|
||||
RS_PROCESS_SERIAL_MEMBER_TYPED(pos, uint8_t);
|
||||
RS_PROCESS_SERIAL_MEMBER(msgId);
|
||||
RS_PROCESS_SERIAL_MEMBER(grpId);
|
||||
RS_PROCESS_SERIAL_MEMBER_TYPED(msg, RsTlvItem);
|
||||
RS_PROCESS_SERIAL_MEMBER_TYPED(meta, RsTlvItem);
|
||||
RS_SERIAL_PROCESS(transactionNumber);
|
||||
RS_SERIAL_PROCESS(pos);
|
||||
RS_SERIAL_PROCESS(msgId);
|
||||
RS_SERIAL_PROCESS(grpId);
|
||||
RS_SERIAL_PROCESS(msg);
|
||||
RS_SERIAL_PROCESS(meta);
|
||||
}
|
||||
|
||||
void RsNxsGrp::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx)
|
||||
|
@ -17,60 +17,48 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "serialiser/rsserializer.h"
|
||||
|
||||
/** @brief Minimal ancestor for all serializable structs in RetroShare
|
||||
|
||||
/** @brief Minimal ancestor for all serializable structs in RetroShare.
|
||||
* If you want your struct to be easly serializable you should inherit from this
|
||||
* struct.
|
||||
* If you want your struct to be serializable as part of a container like an
|
||||
* `std::vector<T>` @see RS_REGISTER_SERIALIZABLE_TYPE(T)
|
||||
* `std::vector<T>` @see RS_REGISTER_SERIALIZABLE_TYPE_DEF(T) and
|
||||
* @see RS_REGISTER_SERIALIZABLE_TYPE_DECL(T)
|
||||
*/
|
||||
struct RsSerializable
|
||||
{
|
||||
/** Register struct members to serialize in this method taking advantage of
|
||||
* the helper macros
|
||||
* @see RS_PROCESS_SERIAL_MEMBER(I)
|
||||
* @see RS_PROCESS_SERIAL_MEMBER_TYPED(I, T)
|
||||
* the helper macro @see RS_SERIAL_PROCESS(I)
|
||||
*/
|
||||
virtual void serial_process(RsGenericSerializer::SerializeJob j,
|
||||
RsGenericSerializer::SerializeContext& ctx) = 0;
|
||||
};
|
||||
|
||||
|
||||
/** @def RS_PROCESS_SERIAL_MEMBER(I)
|
||||
/** @def RS_SERIAL_PROCESS(I)
|
||||
* Use this macro to register the members of `YourSerializable` for serial
|
||||
* processing inside `YourSerializable::serial_process(j, ctx)`
|
||||
*
|
||||
* Pay special attention for member of enum type which must be declared
|
||||
* specifying the underlying type otherwise the serialization format may differ
|
||||
* in an uncompatible way depending on the compiler/platform.
|
||||
*
|
||||
* If your member is a derivative of RsSerializable in some cases it may be
|
||||
* convenient also to register your item type with
|
||||
* @see RS_REGISTER_SERIALIZABLE_TYPE_DEF(T).
|
||||
*
|
||||
* Inspired by http://stackoverflow.com/a/39345864
|
||||
*/
|
||||
#define RS_PROCESS_SERIAL_MEMBER(I) \
|
||||
do { RsTypeSerializer::serial_process(j, ctx, I, #I); } while(0)
|
||||
|
||||
|
||||
/** @def RS_PROCESS_SERIAL_MEMBER_TYPED(I, T)
|
||||
* This macro usage is similar to @see RS_PROCESS_SERIAL_MEMBER(I) but it
|
||||
* permit to force serialization/deserialization type, it is expecially useful
|
||||
* with enum class members or RsTlvItem derivative members, be very careful with
|
||||
* the type you pass, as reinterpret_cast on a reference is used that is
|
||||
* expecially permissive so you can shot your feet if not carefull enough.
|
||||
*
|
||||
* If you are using this with an RsSerializable derivative (so passing
|
||||
* RsSerializable as T) consider to register your item type with
|
||||
* @see RS_REGISTER_SERIALIZABLE_TYPE(T) in
|
||||
* association with @see RS_PROCESS_SERIAL_MEMBER(I) that rely on template
|
||||
* function generation, as in this particular case
|
||||
* RS_PROCESS_SERIAL_MEMBER_TYPED(I, T) would cause the serial code rely on
|
||||
* C++ dynamic dispatching that may have a noticeable impact on runtime
|
||||
* performances.
|
||||
*/
|
||||
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
|
||||
#define RS_PROCESS_SERIAL_MEMBER_TYPED(I, T) do {\
|
||||
RsTypeSerializer::serial_process<T>(j, ctx, reinterpret_cast<T&>(I), #I);\
|
||||
#define RS_SERIAL_PROCESS(I) do { \
|
||||
RsTypeSerializer::serial_process(j, ctx, __priv_to_RS_S_TYPE(I), #I ); \
|
||||
} while(0)
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
|
||||
/** @def RS_REGISTER_SERIALIZABLE_TYPE(T)
|
||||
/** @def RS_REGISTER_SERIALIZABLE_TYPE_DEF(T)
|
||||
* Use this macro into `youritem.cc` only if you need to process members of
|
||||
* subtypes of RsSerializable.
|
||||
*
|
||||
@ -90,21 +78,15 @@ struct PrivateOugoingMapItem : RsChatItem
|
||||
std::map<uint64_t, RsChatMsgItem> store;
|
||||
};
|
||||
|
||||
RS_REGISTER_SERIALIZABLE_TYPE(RsChatMsgItem)
|
||||
RS_REGISTER_SERIALIZABLE_TYPE_DEF(RsChatMsgItem)
|
||||
|
||||
void PrivateOugoingMapItem::serial_process(
|
||||
RsGenericSerializer::SerializeJob j,
|
||||
RsGenericSerializer::SerializeContext& ctx )
|
||||
{
|
||||
// store is of type
|
||||
RS_PROCESS_SERIAL_MEMBER(store);
|
||||
RS_SERIAL_PROCESS(store);
|
||||
}
|
||||
* @endcode
|
||||
*
|
||||
* If you use this macro with a lot of different item types this can cause the
|
||||
* generated binary grow in size, consider the usage of
|
||||
* @see RS_PROCESS_SERIAL_MEMBER_TYPED(I, T) passing RsSerializable as type in
|
||||
* that case.
|
||||
*/
|
||||
#define RS_REGISTER_SERIALIZABLE_TYPE_DEF(T) template<> /*static*/\
|
||||
void RsTypeSerializer::serial_process<T>( \
|
||||
@ -118,11 +100,11 @@ void PrivateOugoingMapItem::serial_process(
|
||||
|
||||
|
||||
/** @def RS_REGISTER_SERIALIZABLE_TYPE_DECL(T)
|
||||
* The usage of this macro into your header file is needed only in case you
|
||||
* The usage of this macro into your header file may be needed only in case you
|
||||
* needed @see RS_REGISTER_SERIALIZABLE_TYPE_DEF(T) in your definitions file,
|
||||
* but it was not enough and the compiler kept complanining about undefined
|
||||
* references to serialize, deserialize, serial_size, print_data, to_JSON,
|
||||
* from_JSON for your RsSerializable derrived type.
|
||||
* from_JSON for your RsSerializable derived type.
|
||||
*
|
||||
* One example of such case is RsIdentityUsage that is declared in
|
||||
* retroshare/rsidentity.h but defined in services/p3idservice.cc, also if
|
||||
@ -135,4 +117,40 @@ void PrivateOugoingMapItem::serial_process(
|
||||
void RsTypeSerializer::serial_process<T>( \
|
||||
RsGenericSerializer::SerializeJob j,\
|
||||
RsGenericSerializer::SerializeContext& ctx, T& item,\
|
||||
const std::string& /*objName*/ );
|
||||
const std::string& objName );
|
||||
|
||||
|
||||
//============================================================================//
|
||||
// Private type conversion helpers //
|
||||
//============================================================================//
|
||||
|
||||
/**
|
||||
* @brief Privates type conversion helpers for RS_SERIAL_PROCESS.
|
||||
* @private DO NOT use explicitely outside this file.
|
||||
* Used to cast enums and derived type to matching types supported by
|
||||
* RsTypeSerializer::serial_process(...) templated methods.
|
||||
*/
|
||||
template<typename T, typename = typename std::enable_if<std::is_enum<T>::value>::type>
|
||||
inline typename std::underlying_type<T>::type& __priv_to_RS_S_TYPE(T& i)
|
||||
{
|
||||
return reinterpret_cast<typename std::underlying_type<T>::type&>(i);
|
||||
}
|
||||
|
||||
template<typename T, typename = typename std::enable_if<std::is_base_of<RsSerializable, T>::value>::type>
|
||||
inline RsSerializable& __priv_to_RS_S_TYPE(T& i)
|
||||
{
|
||||
return static_cast<RsSerializable&>(i);
|
||||
}
|
||||
|
||||
struct RsTlvItem;
|
||||
template<typename T, typename = typename std::enable_if<std::is_base_of<RsTlvItem, T>::value>::type>
|
||||
inline RsTlvItem& __priv_to_RS_S_TYPE(T& i)
|
||||
{
|
||||
return static_cast<RsTlvItem&>(i);
|
||||
}
|
||||
|
||||
template<typename T, typename = typename std::enable_if<!(std::is_enum<T>::value||std::is_base_of<RsTlvItem, T>::value||std::is_base_of<RsSerializable, T>::value)>::type>
|
||||
inline T& __priv_to_RS_S_TYPE(T& i)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
|
@ -4510,16 +4510,16 @@ void RsGxsIdGroup::serial_process(
|
||||
RsGenericSerializer::SerializeJob j,
|
||||
RsGenericSerializer::SerializeContext& ctx )
|
||||
{
|
||||
RS_PROCESS_SERIAL_MEMBER_TYPED(mMeta, RsSerializable);
|
||||
RS_PROCESS_SERIAL_MEMBER(mPgpIdHash);
|
||||
//RS_PROCESS_SERIAL_MEMBER(mPgpIdSign);
|
||||
RS_PROCESS_SERIAL_MEMBER(mRecognTags);
|
||||
//RS_PROCESS_SERIAL_MEMBER(mImage);
|
||||
RS_PROCESS_SERIAL_MEMBER(mLastUsageTS);
|
||||
RS_PROCESS_SERIAL_MEMBER(mPgpKnown);
|
||||
RS_PROCESS_SERIAL_MEMBER(mIsAContact);
|
||||
RS_PROCESS_SERIAL_MEMBER(mPgpId);
|
||||
RS_PROCESS_SERIAL_MEMBER_TYPED(mReputation, RsSerializable);
|
||||
RS_SERIAL_PROCESS(mMeta);
|
||||
RS_SERIAL_PROCESS(mPgpIdHash);
|
||||
//RS_SERIAL_PROCESS(mPgpIdSign);
|
||||
RS_SERIAL_PROCESS(mRecognTags);
|
||||
//RS_SERIAL_PROCESS(mImage);
|
||||
RS_SERIAL_PROCESS(mLastUsageTS);
|
||||
RS_SERIAL_PROCESS(mPgpKnown);
|
||||
RS_SERIAL_PROCESS(mIsAContact);
|
||||
RS_SERIAL_PROCESS(mPgpId);
|
||||
RS_SERIAL_PROCESS(mReputation);
|
||||
}
|
||||
|
||||
RsIdentityUsage::RsIdentityUsage(
|
||||
|
Loading…
Reference in New Issue
Block a user