To be able to store handles you'll need to implement the techniques used with serialization. I can't go into details here, but basically it is about keeping track of all objects that have already been stored in an map, and when a handle is found it converts the pointer to an index into the map. When loading the objects again the same map is also filled with the pointers of the new objects, and when the handle is loaded it uses the index to get the real pointer from the map.

- For each object
- - If pointer to object not in array, add it
- - store the object
- for each handle
- - find index to pointer in array
- - store the index

Restore

- For each object
- - load the object
- - store the new pointer in the array
- For each handle
- - load the index
- - set the handle to the pointer from the array

The array can be a simple std::vector<void*>, which will let you map pointer to index and back. Just make sure the array is populated in the same order for both storing and restoring so the indexes are not mixed up.

The tricky part is when a handle points to a non-handle member. In this case when loading the handle you will create an object with the data, but when loading the member later on you need to use that object to set the value of the member, and then substitute the pointer in the handle for the actual member.

I suggest you look for tutorials on serialization. The theory is exactly the same whether you serialize scripts or C++ objects, or any other language for that matter.