0

I'm looking for a way to save and load several kinds of objects on file, and being able to load them in any particular order based on some kind of unique identifier. I would like all instances of each type of object to be stored in a particular, different file ( e.g. class1instances.dat, class2instances.dat and so on).

I've looked at Boost Serialization but it appears as if it can only load objects in the same (reverse) sequence in which it saved them. Should I try to extend their Archive class or is there something already done and I missed it?

Svalorzen
  • 5,353
  • 3
  • 30
  • 54
  • This sounds more like what a database would do. – Bo Persson Oct 27 '12 at 09:49
  • I think that sometimes you have to write your own code. I would suggest some sort of index files to go along side your .dat files. The index file would have the unique identifier and the position in the data file for each saved object. – john Oct 27 '12 at 09:50
  • @BoPersson Well, I could save things in the same file, then load them all into a vector and choose the one that I want, but it doesn't feel right. Is such a small modification that big of a deal? I don't have to perform complex operations, just fetch one item at a time with a specific id. – Svalorzen Oct 27 '12 at 10:07
  • Serialization gets complicated as soon as you have cyclic references and thelike. That could be the reason the boost folks didn't implement random access to objects. – Jonas Schäfer Oct 27 '12 at 10:30

2 Answers2

1

Just have two files for each class, rather than one: "class1instances.dat" and "class1indeces.dat`.

The first class is a sequentional file of your objects, one after other. The second file is used as index for your objects in the first file. It contains the positions of the objects in the first file. For example, class1indeces.dat would look like:

0
1235
12343
43455
899432

Means that, the first object is at position 0 of file "class1instances.dat". The second object is at position 1235 and so on. You then use stream seek function to begin reading.

Another index method is key-value. You can have a unique ID for each object and write each object ID and its corresponding index. For example:

0:0
2:1235
1:12343
320:43455
3:899432

To retrieve object with ID = 320, for example, just have linear search for ID 320 in the file. When you find it, use its corresponding position to begin reading the object from it until the next position.

You can also use stream bin mode to serialize your objects. Example.

  • Thanks, but I was looking for something already done. I know how to do it myself, I just hoped this was a common problem and there would have been a library for it. – Svalorzen Oct 27 '12 at 11:17
  • @Svalorzen Kindly check this http://www.ocoudert.com/blog/2011/07/09/a-practical-guide-to-c-serialization/ –  Oct 27 '12 at 16:05
  • This is a tutorial for boost::serialize.. so? As already said it doesn't support loading of particular elements. – Svalorzen Oct 27 '12 at 23:29
0

I solved this by simply using JsonCpp. In the end it was the easiest and most flexible solution.

Svalorzen
  • 5,353
  • 3
  • 30
  • 54