[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

managing a resource of instances

How would one set up make-instance to do the following for some class,
say FONT.  When creating an instance of a font check to see if a font
with the same initialization arguments has been created already.  If
so then return the old font instance otherwise return a new one.

The CLOS definition for make-instance looks like this:

(defmethod make-instance ((class standard-class) &rest initargs)
    (setq init-args (default-initargs class initargs))
    (let ((instance (apply #'allocate-instance class initargs)))
      (apply #'initialize-instance instance initargs)

I suppose what I'd like to do is gain control after initargs has been
bound and then just return an old instance if one existed that had
been created with the same initargs.  Unfortunately this can't be done
directly.  What one could do is specialize allocate-instance - make it
try and find an appropriate old instance - and specialize
initialize-instance - make it a noop when given an old instance.  The
principal flaw with this is that allocate-instance isn't part of the
exported CLOS interface.  Seems a little underhanded to depend on it.

Is there a "right" way to do this?

Hans Muller