# Cache of sent objects
#
# Used by `Serializer` to avoid duplicating objects, by serializing them once,
# then using a reference.
class SerializerCache
# Map of already serialized objects to the reference id
protected var sent: Map[Serializable, Int] = new StrictHashMap[Serializable, Int]
# Is `object` known?
fun has_object(object: Serializable): Bool do return sent.keys.has(object)
# Get the id for `object`
#
# Require: `has_object(object)`
fun id_for(object: Serializable): Int
do
assert sent.keys.has(object)
return sent[object]
end
# Get a new id for `object` and store it
#
# Require: `not has_object(object)`
fun new_id_for(object: Serializable): Int
do
var id = next_available_id
sent[object] = id
return id
end
# Get a free id to associate to an object in the cache
protected fun next_available_id: Int do return sent.length
end
lib/serialization/caching.nit:52,1--84,3