0

I have a data class to be saved and loaded using boost serializer.

The class includes two members En *sender, En *receiver to two objects that are already created in my system. I don't need them to be created again. I just need to send(serialize) their address, to be used as reference at the other end.

If I use normal pointers like En *sender, En *receiver , boost will serialize the entire objects(which I don't want).

So I figured I should use En **sender, En **receiver which generates error. May I know how I should modify the class to serve my purpose?

thanks very much.

class dataMessage
{

    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {            
        ar & sender;
        ar & receiver;
    }
    //I figured I should user pointer to pointer coz in case of the normal pointer,
    //the serilizer would serializes the object that the pointer is pointing to;
    //whereas I just need to save the 'pointer' to object so that we
    //can use it as a reference at the other end.
    En **sender;
    En **receiver;
public:
    dataMessage(){
        sender = receiver = 0;
    }
    void setDataClassType();
    virtual void registerType(boost::archive::text_oarchive &oa)
    {
        oa.register_type(static_cast<dataMessage *>(NULL));
    }

    virtual void registerType(boost::archive::text_iarchive &ia)
    {
        ia.register_type(static_cast<dataMessage *>(NULL));
    }
};

part of the error :

error: request for member ‘serialize’ in ‘t’, which is of pointer type ‘En*’ (maybe you meant to use ‘->’ ?)
rahman
  • 4,820
  • 16
  • 52
  • 86
  • What is the "other end"? A pointer is only valid in your program. – n. m. could be an AI Mar 13 '13 at 06:31
  • Hi,yes,the other end is my program.(story: I use serialization for sending/receiving data between agents in our simulator. this send/receive has to follow models of wireless network uncertainty like delay, loss etc. So I serialize the packets putting sender receiver information in the packet, then send the packets to another simulator by writing to a file[send file]. the second simulator reads this file and doesn't care about the content of the packet. It just writes the contents to another file[receive file] after a delay(or not send it at all to resemble 'loss). The first simulator reads.... – rahman Mar 13 '13 at 06:53
  • reads the receive file, deserialize and according to receiver's information, delivers the information to the incoming buffer of the receiver). Here, i just need the receiver information to be just the address of the receiving object... the rest is clear(i think) – rahman Mar 13 '13 at 06:56
  • 2
    If all this happens within the same program/process, why serialize anything? You could just pass a pointer to the top-level data structure. Anyway, you can cast the pointer to some integer type of the right size and serialize that, and cast in the other direction when deserializing. I think you will need separate serialization and deserialization methods, the tutorial has an example of how to do that. – n. m. could be an AI Mar 13 '13 at 07:05
  • @n.m. yeah, I think casting to integer type is the only way. Thanks. and, based on tutorial, I also separated the send and receive. and finally about "pass a pointer to the top-level data structure"... I am not using heap.If you ask why, I would have no answer. Just didn't want to dynamically create data objects and clean them after that. So the data is generated locally and restored in the same way as explained. – rahman Mar 13 '13 at 07:27
  • Another point that I didnt tell you is that very soon the simulator will be distributed and agents reside on different machines(we need to use mpi). so creating the data on one machine and referencing on the other will not make sense at the first glance.thanks – rahman Mar 13 '13 at 07:31

1 Answers1

0

It seems there is no way than to store the addresses manually otherwise boostserializer will serialize the target object. so I modified the sender receiver type to:

class dataMessage
{

 //......
public:
//.....
    //a proper size of integer is used coz in case of the normal pointer,
    //the serilizer serializes the object that the pointer is pointing to
    //whereas we just need to save the 'pointer' to object so that we
    //can use it as a reference at the other end. so instead of storing 'sim_mob::Entity *'
    //we store:
#if __x86_64__  //64 bit
    unsigned long sender;
    unsigned long receiver;
#else          //32 bit
    unsigned int sender;
    unsigned int receiver;

#endif


    dataMessage(){
        sender = receiver = 0;
    }
//...
};

later when using the pointer, i will make a simple cast to the appropriate pointer type.

rahman
  • 4,820
  • 16
  • 52
  • 86