diff --git a/libretroshare/src/serialiser/rsserial.h b/libretroshare/src/serialiser/rsserial.h index 2b4aa304d..0abc63af6 100644 --- a/libretroshare/src/serialiser/rsserial.h +++ b/libretroshare/src/serialiser/rsserial.h @@ -120,7 +120,7 @@ class RsItem: public RsMemoryManagement::SmallObject * after where this RsSerializable has been serialized. * @return true if serialization successed, false otherwise */ - typedef enum { SIZE_ESTIMATE = 0x01, SERIALIZE = 0x02, DESERIALIZE = 0x03} SerializeJob ; + typedef enum { SIZE_ESTIMATE = 0x01, SERIALIZE = 0x02, DESERIALIZE = 0x03, PRINT=0x04 } SerializeJob ; virtual void serial_process(SerializeJob j,SerializeContext& ctx) { diff --git a/libretroshare/src/serialization/rsserializer.cc b/libretroshare/src/serialization/rsserializer.cc index e12878b1d..f7926f8e2 100644 --- a/libretroshare/src/serialization/rsserializer.cc +++ b/libretroshare/src/serialization/rsserializer.cc @@ -65,5 +65,13 @@ uint32_t RsSerializer::size(RsItem *item) return ctx.mSize ; } +void RsSerializer::print(RsItem *item) +{ + SerializeContext ctx(NULL,0); + + std::cerr << "***** RsItem class: \"" << typeid(*item).name() << "\" *****" << std::endl; + item->serial_process(RsItem::PRINT, ctx) ; + std::cerr << "******************************" << std::endl; +} diff --git a/libretroshare/src/serialization/rstypeserializer.h b/libretroshare/src/serialization/rstypeserializer.h index 25c4d45b2..37c1f9dc7 100644 --- a/libretroshare/src/serialization/rstypeserializer.h +++ b/libretroshare/src/serialization/rstypeserializer.h @@ -9,7 +9,7 @@ class RsTypeSerializer typedef std::pair TlvString; template - static void serial_process(RsItem::SerializeJob j,SerializeContext& ctx,T& member) + static void serial_process(RsItem::SerializeJob j,SerializeContext& ctx,T& member,const std::string& member_name) { switch(j) { @@ -22,6 +22,9 @@ class RsTypeSerializer case RsItem::SERIALIZE: ctx.mOk = ctx.mOk && serialize(ctx.mData,ctx.mSize,ctx.mOffset,member) ; break ; + case RsItem::PRINT: + print_data(member_name,member); + break; default: ctx.mOk = false ; throw std::runtime_error("Unknown serial job") ; @@ -32,6 +35,7 @@ class RsTypeSerializer template static bool serialize (uint8_t data[], uint32_t size, uint32_t &offset, const T& member); template static bool deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, T& member); template static uint32_t serial_size(const T& /* member */); + template static void print_data(const std::string& name,const T& /* member */); }; diff --git a/libretroshare/src/serialization/serial_test.cc b/libretroshare/src/serialization/serial_test.cc index 24822284e..786fd1d88 100644 --- a/libretroshare/src/serialization/serial_test.cc +++ b/libretroshare/src/serialization/serial_test.cc @@ -11,6 +11,8 @@ #include "rsserializer.h" #include "rstypeserializer.h" +#define GET_VARIABLE_NAME(str) #str + static const uint16_t RS_SERVICE_TYPE_TEST = 0xffff; static const uint8_t RS_ITEM_SUBTYPE_TEST1 = 0x01 ; @@ -71,6 +73,11 @@ template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t siz return ok; } +template<> void RsTypeSerializer::print_data(const std::string& s,const std::set& set) +{ + std::cerr << " [set] " << s << " : set of size " << set.size() << std::endl; +} + // New item class. This class needs to define: // - a serial_process method that tells which members to serialize // - overload the clear() and print() methods of RsItem @@ -94,15 +101,15 @@ class RsTestItem: public RsItem { RsTypeSerializer::TlvString tt(str,TLV_TYPE_STR_DESCR) ; - RsTypeSerializer::serial_process(j,ctx,ts ) ; - RsTypeSerializer::serial_process(j,ctx,tt ) ; - RsTypeSerializer::serial_process(j,ctx,int_set ) ; + RsTypeSerializer::serial_process(j,ctx,ts ,GET_VARIABLE_NAME(ts) ) ; + RsTypeSerializer::serial_process(j,ctx,tt ,GET_VARIABLE_NAME(str) ) ; + RsTypeSerializer::serial_process(j,ctx,int_set,GET_VARIABLE_NAME(int_set) ) ; } - // Derived from RsItem + // Derived from RsItem, because they are pure virtuals. Normally print() should disappear soon. // virtual void clear() {} - virtual std::ostream& print(std::ostream&,uint16_t indent) {} + virtual std::ostream& print(std::ostream&,uint16_t) {} private: std::string str ; @@ -156,6 +163,9 @@ int main(int argc,char *argv[]) // RsTemporaryMemory mem1(size); + std::cerr << "Item to be serialized:" << std::endl; + + RsTestSerializer().print(&t1) ; RsTestSerializer().serialise(&t1,mem1,mem1.size()) ; std::cerr << "Serialized t1: " << RsUtil::BinToHex(mem1,mem1.size()) << std::endl;