1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Services for caching serialization engines
19 private import engine_tools
21 # A `Serializer` with a `cache`
22 abstract class CachingSerializer
25 # Cache of known objects
26 var cache
= new SerializerCache is lazy
, writable
29 # A `Deserializer` with a `cache`
30 abstract class CachingDeserializer
33 # Cache of known objects
34 var cache
= new DeserializerCache is lazy
, writable
37 # Cache of sent objects
39 # Used by `Serializer` to avoid duplicating objects, by serializing them once,
40 # then using a reference.
42 # Map of already serialized objects to the reference id
43 private var sent
: Map[Serializable, Int] = new StrictHashMap[Serializable, Int]
46 fun has_object
(object
: Serializable): Bool do return sent
.keys
.has
(object
)
48 # Get the id for `object`
50 # Require: `has_object(object)`
51 fun id_for
(object
: Serializable): Int
53 assert sent
.keys
.has
(object
)
57 # Get a new id for `object` and store it
59 # Require: `not has_object(object)`
60 fun new_id_for
(object
: Serializable): Int
68 # Cache of received objects sorted by there reference id
70 # Used by `Deserializer` to find already deserialized objects by their reference.
71 class DeserializerCache
72 # Map of references to already deserialized objects.
73 private var received
: Map[Int, Object] = new StrictHashMap[Int, Object]
75 # Is there an object associated to `id`?
76 fun has_id
(id
: Int): Bool do return received
.keys
.has
(id
)
78 # Get the object associated to `id`
79 fun object_for
(id
: Int): nullable Object do return received
[id
]
81 # Associate `object` to `id`
82 fun []=(id
: Int, object
: Object) do received
[id
] = object
85 # A shared cache for serialization and deserialization
88 super DeserializerCache
90 redef fun new_id_for
(object
)
97 redef fun []=(id
, object
)
100 assert object
isa Serializable