accessors.scm 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. ;;;; Copyright (C) 1999, 2000, 2005, 2006 Free Software Foundation, Inc.
  2. ;;;;
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;;;
  17. (define-module (oop goops accessors)
  18. :use-module (oop goops)
  19. :re-export (standard-define-class)
  20. :export (define-class-with-accessors
  21. define-class-with-accessors-keywords))
  22. (define-macro (define-class-with-accessors name supers . slots)
  23. (let ((eat? #f))
  24. `(standard-define-class
  25. ,name ,supers
  26. ,@(map-in-order
  27. (lambda (slot)
  28. (cond (eat?
  29. (set! eat? #f)
  30. slot)
  31. ((keyword? slot)
  32. (set! eat? #t)
  33. slot)
  34. ((pair? slot)
  35. (if (get-keyword #:accessor (cdr slot) #f)
  36. slot
  37. (let ((name (car slot)))
  38. `(,name #:accessor ,name ,@(cdr slot)))))
  39. (else
  40. `(,slot #:accessor ,slot))))
  41. slots))))
  42. (define-macro (define-class-with-accessors-keywords name supers . slots)
  43. (let ((eat? #f))
  44. `(standard-define-class
  45. ,name ,supers
  46. ,@(map-in-order
  47. (lambda (slot)
  48. (cond (eat?
  49. (set! eat? #f)
  50. slot)
  51. ((keyword? slot)
  52. (set! eat? #t)
  53. slot)
  54. ((pair? slot)
  55. (let ((slot
  56. (if (get-keyword #:accessor (cdr slot) #f)
  57. slot
  58. (let ((name (car slot)))
  59. `(,name #:accessor ,name ,@(cdr slot))))))
  60. (if (get-keyword #:init-keyword (cdr slot) #f)
  61. slot
  62. (let* ((name (car slot))
  63. (keyword (symbol->keyword name)))
  64. `(,name #:init-keyword ,keyword ,@(cdr slot))))))
  65. (else
  66. `(,slot #:accessor ,slot
  67. #:init-keyword ,(symbol->keyword slot)))))
  68. slots))))