[3743] | 1 | ;;; -*- Mode: Lisp -*-
|
---|
[3444] | 2 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 3 | ;;;
|
---|
[3742] | 4 | ;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>
|
---|
[3444] | 5 | ;;;
|
---|
[3742] | 6 | ;;; This program is free software; you can redistribute it and/or modify
|
---|
| 7 | ;;; it under the terms of the GNU General Public License as published by
|
---|
| 8 | ;;; the Free Software Foundation; either version 2 of the License, or
|
---|
[3444] | 9 | ;;; (at your option) any later version.
|
---|
| 10 | ;;;
|
---|
[3742] | 11 | ;;; This program is distributed in the hope that it will be useful,
|
---|
| 12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | ;;; GNU General Public License for more details.
|
---|
[3444] | 15 | ;;;
|
---|
[3742] | 16 | ;;; You should have received a copy of the GNU General Public License
|
---|
| 17 | ;;; along with this program; if not, write to the Free Software
|
---|
| 18 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
[3444] | 19 | ;;;
|
---|
| 20 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 21 |
|
---|
| 22 | (defpackage "COPY"
|
---|
| 23 | (:use :cl)
|
---|
| 24 | (:export "COPY-INSTANCE")
|
---|
| 25 | (:shadowing-import-from
|
---|
| 26 | #+openmcl-native-threads #:ccl
|
---|
| 27 | #+cmu #:pcl
|
---|
| 28 | #+sbcl #:sb-pcl
|
---|
| 29 | #+lispworks #:hcl
|
---|
| 30 | #+allegro #:mop
|
---|
| 31 | #+clisp #:clos
|
---|
| 32 | #:class-slots #:slot-definition-name)
|
---|
[3457] | 33 | (:documentation "Implements generic clone operation."))
|
---|
[3444] | 34 |
|
---|
| 35 | (in-package :copy)
|
---|
| 36 |
|
---|
| 37 | ;; Source: http://stackoverflow.com/questions/11067899/is-there-a-generic-method-for-cloning-clos-objects
|
---|
| 38 | ;; NOTE: This is a shallow copy. Add an around method for classes which need deep copy of the slots.
|
---|
| 39 | (defgeneric copy-instance (object &rest initargs &key &allow-other-keys)
|
---|
[4218] | 40 | (:method ((object number) &rest initargs &key &allow-other-keys)
|
---|
[3671] | 41 | (declare (ignore initargs))
|
---|
[3660] | 42 | object)
|
---|
[3444] | 43 | (:method ((object cons) &rest initargs &key &allow-other-keys)
|
---|
[3445] | 44 | (declare (ignore initargs))
|
---|
[3444] | 45 | (copy-seq object))
|
---|
| 46 | (:documentation "Makes and returns a shallow copy of OBJECT.
|
---|
| 47 |
|
---|
| 48 | An uninitialized object of the same class as OBJECT is allocated by
|
---|
| 49 | calling ALLOCATE-INSTANCE. For all slots returned by
|
---|
| 50 | CLASS-SLOTS, the returned object has the
|
---|
| 51 | same slot values and slot-unbound status as OBJECT.
|
---|
| 52 |
|
---|
| 53 | REINITIALIZE-INSTANCE is called to update the copy with INITARGS.")
|
---|
| 54 | (:method ((object standard-object) &rest initargs &key &allow-other-keys)
|
---|
| 55 | (let* ((class (class-of object))
|
---|
| 56 | (copy (allocate-instance class)))
|
---|
| 57 | (dolist (slot-name (mapcar #'slot-definition-name (class-slots class)))
|
---|
| 58 | (when (slot-boundp object slot-name)
|
---|
| 59 | (setf (slot-value copy slot-name)
|
---|
| 60 | (slot-value object slot-name))))
|
---|
| 61 | (apply #'reinitialize-instance copy initargs))))
|
---|
[3460] | 62 |
|
---|
| 63 | #|
|
---|
| 64 | ;; A stripped-down version of shallow copy
|
---|
| 65 | ;; Source: http://stackoverflow.com/questions/11067899/is-there-a-generic-method-for-cloning-clos-objects
|
---|
| 66 | (defun shallow-copy-object (original)
|
---|
| 67 | (let* ((class (class-of original))
|
---|
| 68 | (copy (allocate-instance class)))
|
---|
| 69 | (dolist (slot (mapcar #'slot-definition-name (class-slots class)))
|
---|
| 70 | (when (slot-boundp original slot)
|
---|
| 71 | (setf (slot-value copy slot)
|
---|
| 72 | (slot-value original slot))))
|
---|
| 73 | copy))
|
---|
| 74 | |#
|
---|