added hability to print in the serialization process

This commit is contained in:
csoler 2017-04-04 14:00:17 +02:00
parent 75a3cf0f3b
commit 059dfcff47
4 changed files with 29 additions and 7 deletions

View File

@ -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)
{

View File

@ -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;
}

View File

@ -9,7 +9,7 @@ class RsTypeSerializer
typedef std::pair<std::string&,uint16_t> TlvString;
template<typename T>
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<typename T> static bool serialize (uint8_t data[], uint32_t size, uint32_t &offset, const T& member);
template<typename T> static bool deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, T& member);
template<typename T> static uint32_t serial_size(const T& /* member */);
template<typename T> static void print_data(const std::string& name,const T& /* member */);
};

View File

@ -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<uint32_t>& set)
{
std::cerr << " [set<uint32_t>] " << 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;