summaryrefslogtreecommitdiff
path: root/protocols.lisp
blob: a5f21026131704a3a40577f2fb6d17ebf1e54a15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
(in-package #:mulk.protocols)


(declaim (optimize debug safety (speed 0) (space 0)))


(defvar *protocols* (make-hash-table))


(defun find-protocol (name &optional (errorp t))
  (let ((protocol (gethash name *protocols* nil)))
    (when (and errorp (null protocol))
      (error "There is no protocol named ~A." name))
    protocol))


(defun (setf find-protocol) (protocol name &optional errorp)
  (declare (ignore errorp))
  (setf (gethash name *protocols*) protocol))


(defun conforms-to-p (class protocol)
  (let ((real-class (typecase class
                      (symbol (find-class class))
                      (t class)))
        (real-protocol (typecase protocol
                         (symbol (find-protocol protocol))
                         (t protocol))))
    (%conforms-to-p real-class real-protocol)))


(defun really-conforms-to-p (class protocol)
  (let ((real-class (typecase class
                      (symbol (find-class class))
                      (t class)))
        (real-protocol (typecase protocol
                         (symbol (find-protocol protocol))
                         (t protocol))))
    (%really-conforms-to-p real-class real-protocol)))


(defgeneric %conforms-to-p (class protocol))

(defmethod %conforms-to-p ((class t) (protocol t))
  (declare (ignore class protocol))
  nil)


(defgeneric %really-conforms-to-p (class protocol))

(defmethod %really-conforms-to-p ((class t) (protocol t))
  (declare (ignore class protocol))
  nil)


(defclass protocol ()
  ((name :initarg :name
         :reader protocol-name)
   (superprotocols :initarg :superprotocols
                   :reader protocol-superprotocols)
   (methods :initarg :methods
            :reader protocol-methods)
   (options :initarg :options
            :reader protocol-options)))


(defmacro define-protocol (name superprotocols methods &rest options)
  "Define a new protocol.

superprotocols ::= (*name*\\*)

methods ::= ((method-name [\\* | class-name]\\*)\\*)"
  `(progn
     (when (find-protocol ',name nil)
       (warn (make-condition 'simple-style-warning
                             :format-control "Redefining protocol ~A."
                             :format-arguments (list ',name))))
     (setf (find-protocol ',name)
           (make-instance 'protocol
              :name ',name
              :superprotocols (mapcar #'find-protocol ',superprotocols)
              :methods ',methods
              :options ',options))))


(defmethod print-object ((protocol protocol) stream)
  (print-unreadable-object (protocol stream)
    (format stream "PROTOCOL ~A" (protocol-name protocol))))


(defun ensure-conformance (class-name protocol-name)
  (let ((protocol (find-protocol protocol-name)))
    (with-slots (name superprotocols methods options)
        protocol
      (loop for method in methods
            for (name . raw-argument-class-list) = method
            for argument-class-list = (substitute class-name
                                                  '*
                                                  raw-argument-class-list)
            for real-argument-class-list = (mapcar #'find-class
                                                   argument-class-list)
            when (null (funcall #'compute-applicable-methods-using-classes
                                (fdefinition name)
                                real-argument-class-list))
              collect method into missing-methods
              and do (warn (make-condition 'simple-style-warning
                                :format-control "Class ~A does not implement ~
                                                 method ~A with argument types ~
                                                 ~A as required by ~
                                                 protocol ~A."
                                :format-arguments (list class-name
                                                        (first method)
                                                        real-argument-class-list
                                                        (protocol-name protocol))))
            finally (return (null missing-methods))))))


(defmacro implement-protocols (class protocols &body definitions)
  `(progn
     ,@definitions
     ,@(mapcan #'(lambda (protocol)
                   `((let ((conformance (ensure-conformance ',class ',protocol)))
                       (defmethod %really-conforms-to-p
                           ((class (eql (find-class ',class)))
                            (protocol (eql (find-protocol ',protocol))))
                         conformance))
                     (defmethod %conforms-to-p
                         ((class (eql (find-class ',class)))
                          (protocol (eql (find-protocol ',protocol))))
                       t)))
               protocols)))