[3444] | 1 | ;;; -*- mode: lisp; package: maxima; syntax: common-lisp; base: 10 -*-
|
---|
| 2 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 3 | ;;;
|
---|
| 4 | ;;; copyright (c) 1999, 2002, 2009, 2015 marek rychlik <rychlik@u.arizona.edu>
|
---|
| 5 | ;;;
|
---|
| 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
|
---|
| 9 | ;;; (at your option) any later version.
|
---|
| 10 | ;;;
|
---|
| 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.
|
---|
| 15 | ;;;
|
---|
| 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.
|
---|
| 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 |
|
---|
| 38 | ;; Source: http://stackoverflow.com/questions/11067899/is-there-a-generic-method-for-cloning-clos-objects
|
---|
| 39 | ;; NOTE: This is a shallow copy. Add an around method for classes which need deep copy of the slots.
|
---|
| 40 | (defgeneric copy-instance (object &rest initargs &key &allow-other-keys)
|
---|
| 41 | (:method ((object cons) &rest initargs &key &allow-other-keys)
|
---|
[3445] | 42 | (declare (ignore initargs))
|
---|
[3444] | 43 | (copy-seq object))
|
---|
| 44 | (:documentation "Makes and returns a shallow copy of OBJECT.
|
---|
| 45 |
|
---|
| 46 | An uninitialized object of the same class as OBJECT is allocated by
|
---|
| 47 | calling ALLOCATE-INSTANCE. For all slots returned by
|
---|
| 48 | CLASS-SLOTS, the returned object has the
|
---|
| 49 | same slot values and slot-unbound status as OBJECT.
|
---|
| 50 |
|
---|
| 51 | REINITIALIZE-INSTANCE is called to update the copy with INITARGS.")
|
---|
| 52 | (:method ((object standard-object) &rest initargs &key &allow-other-keys)
|
---|
| 53 | (let* ((class (class-of object))
|
---|
| 54 | (copy (allocate-instance class)))
|
---|
| 55 | (dolist (slot-name (mapcar #'slot-definition-name (class-slots class)))
|
---|
| 56 | (when (slot-boundp object slot-name)
|
---|
| 57 | (setf (slot-value copy slot-name)
|
---|
| 58 | (slot-value object slot-name))))
|
---|
| 59 | (apply #'reinitialize-instance copy initargs))))
|
---|
[3460] | 60 |
|
---|
| 61 | #|
|
---|
| 62 | ;; A stripped-down version of shallow copy
|
---|
| 63 | ;; Source: http://stackoverflow.com/questions/11067899/is-there-a-generic-method-for-cloning-clos-objects
|
---|
| 64 | (defun shallow-copy-object (original)
|
---|
| 65 | (let* ((class (class-of original))
|
---|
| 66 | (copy (allocate-instance class)))
|
---|
| 67 | (dolist (slot (mapcar #'slot-definition-name (class-slots class)))
|
---|
| 68 | (when (slot-boundp original slot)
|
---|
| 69 | (setf (slot-value copy slot)
|
---|
| 70 | (slot-value original slot))))
|
---|
| 71 | copy))
|
---|
| 72 | |#
|
---|