forked from Shirakumo/trial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource.lisp
55 lines (42 loc) · 1.62 KB
/
resource.lisp
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
#|
This file is a part of trial
(c) 2018 Shirakumo http://tymoon.eu ([email protected])
Author: Nicolas Hafner <[email protected]>
|#
(in-package #:org.shirakumo.fraf.trial)
;; FIXME: configurable defaults
(define-condition resource-not-allocated (error)
((resource :initarg :resource))
(:report (lambda (c s) (format s "The resource~% ~s~%is required to be allocated, but was not yet."
(slot-value c 'resource)))))
(defclass resource ()
((generator :initarg :generator :initform NIL :reader generator)
(name :initarg :name :initform NIL :reader name)))
(defgeneric allocate (resource))
(defgeneric deallocate (resource))
(defgeneric allocated-p (resource))
(defmethod load ((resource resource))
(unless (allocated-p resource)
(v:trace :trial.resource "Loading ~a" resource)
(allocate resource)))
(defmethod allocate :around ((resource resource))
(call-next-method)
resource)
(defmethod deallocate :around ((resource resource))
(call-next-method)
resource)
(defun check-allocated (resource)
(unless (allocated-p resource)
(restart-case
(error 'resource-not-allocated :resource resource)
(continue ()
:report "Allocate the resource now and continue."
(allocate resource)))))
(defclass foreign-resource (resource)
((data-pointer :initform NIL :initarg :data-pointer :accessor data-pointer)))
(defmethod allocated-p ((resource foreign-resource))
(data-pointer resource))
(defmethod deallocate :after ((resource foreign-resource))
(setf (data-pointer resource) NIL))
(defclass gl-resource (foreign-resource)
((data-pointer :accessor gl-name)))