Skip to main content
gamedev.net
🔒 Locked

serialization with pointers

Started by staticVoid2 1 reply 950+ views
Original Post
staticVoid2
staticVoid2
hi there, I'm having trouble trying to work out how to serialize an object that contains a pointer to another object. I have an array of objects all of which point to another object, I need to serialize the object once and then restore these pointers, the trouble is trying to create a good interface for doing this. currently I have a class that takes all c++ primitive data types and saves them to file, it also takes an undefined class (using templates) and calls this class's serialize method:


class Archive
{
public:

   void operator <<(int i);
   void operator <<(float f);
   void operator <<(bool b);

   template <class T>
   void operator <<(const T& obj)
   {
      obj.serialize(this);
   }
};



this is similar to boost. I was thinking I could store the addresses of every object in the Archive class when they pass through the member functions as and array of and then when a (<<)member function is called just look up the address in the array and then save the index instead:



class Archive
{
public:

   void operator <<(int i);
   void operator <<(float f);
   void operator <<(bool b);

   template <class T>
   void operator <<(const T* obj)
   {
      for(int i = 0; i < adresses.size(); ++i)
      {
         // already been serialized
         if(obj == adresses)
         {
            // write i to file
            return;
         }
      }
      adresses.push_back(obj);
      obj->serialize(this);
   }


private:

   std::vector<const void*> addresses;

};



this means that the object is serialized once only but it seems very inefficient having to lookup every object you have serialized before just to serialize an object. Is there a better way to do this?
Zahlman
Zahlman
Quote:
Is there a better way to do this?


Of course there is. The very fact that there's a name ("serialization") for what you're trying to do is a sign that a lot of people have thought about the problem in the past. :)

General technique.

Or you could just use that Boost library you referred to? :)
Antheus
Antheus
Quote:
this means that the object is serialized once only but it seems very inefficient having to lookup every object you have serialized before just to serialize an object.


It's only part of the problem, you need to check for types and other issues in C++.

As far as efficiency goes, doing linear search will be inefficient for more than 10 elements, but this is why log n containers were invented, such as std::map or std::hash_map.

But here's another problem - who owns the pointers? Does serialization destroy them? Take ownership of them? Does the caller manage them? If you read through boost's serialization tutorials you'll see they don't bother solving that problem, and simply suggest using smart pointers.

It may seem trivial at first, but once you get into object graphs, things get really messy.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.