Browse thread
Weak hash table for attaching extra data to an object
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Richard Jones <rich@a...> |
| Subject: | Weak hash table for attaching extra data to an object |
Problem: I have a module which I can't edit[1]. This module, let's call it Document, stores a representation of an XML tree in internal node objects: type intnode What I want to do is -- completely outside Document -- attach my own extra information to these nodes. The key point is that if the garbage collector collects an intnode, then I want my extra information to be garbage collected too. My proposed solution would use a weak hash table similar to / derived from this one: http://remi.vanicat.free.fr/ocaml/hweak/. I would store in this hash table mappings from (weak) intnode pointers to my extra data structure. Remi's Hweak module isn't quite what I need because the data is also weak. Since nothing except the hash table itself is tracking my extra data, the GC can potentially drop all of the bindings, and that's not much help. I tried to modify Hweak so that the data is not weak, but that proved to be quite hard. I wonder if anyone has done this, or can suggest a better way to solve this problem? The issue of attaching extra data to some existing object would seem to be a fairly common one ... Rich. [1] By "can't edit" I really mean "don't want to modify". I want Document to be general purpose. Also, unlike PXP, I want multiple callers to be able to individually attach data to nodes. -- Richard Jones Red Hat