compiler: protect the stacktrace-aware C code with the preprocessor
[nit.git] / lib / serialization / caching.nit
index d32a0c2..186c96c 100644 (file)
@@ -24,6 +24,21 @@ abstract class CachingSerializer
 
        # Cache of known objects
        var cache = new SerializerCache is lazy, writable
+
+       # Link the cache of `self` with `deserializer`
+       #
+       # This allows reference objects by id when they are known by the other side
+       # of the stream.
+       #
+       # Use `cache` if it is a `DuplexCache`, otherwise create a new one.
+       fun link(deserializer: CachingDeserializer)
+       do
+               var mem = self.cache
+               if not mem isa DuplexCache then mem = new DuplexCache
+
+               self.cache = mem
+               deserializer.cache = mem
+       end
 end
 
 # A `Deserializer` with a `cache`
@@ -81,3 +96,23 @@ class DeserializerCache
        # Associate `object` to `id`
        fun []=(id: Int, object: Object) do received[id] = object
 end
+
+# A shared cache for serialization and deserialization
+class DuplexCache
+       super SerializerCache
+       super DeserializerCache
+
+       redef fun new_id_for(object)
+       do
+               var id = super
+               received[id] = object
+               return id
+       end
+
+       redef fun []=(id, object)
+       do
+               super
+               assert object isa Serializable
+               sent[object] = id
+       end
+end