work in progress: use factory object for creating records
This commit is contained in:
parent
d0c990fe7b
commit
19054f600f
2 changed files with 54 additions and 18 deletions
|
@ -10,12 +10,19 @@
|
||||||
|
|
||||||
;;;; message
|
;;;; message
|
||||||
|
|
||||||
|
(defun message-factory ()
|
||||||
|
(make-instance 'shape:record-factory :item-class 'message
|
||||||
|
:head-fields '(:domain :action :class :item)))
|
||||||
|
|
||||||
(defclass message (shape:record)
|
(defclass message (shape:record)
|
||||||
((shape:head-fields :initform '(:domain :action :class :item))
|
((factory :initform (message-factory))
|
||||||
|
(shape:head-fields :initform '(:domain :action :class :item))
|
||||||
(sender :reader sender :initarg :sender :initform nil)))
|
(sender :reader sender :initarg :sender :initform nil)))
|
||||||
|
|
||||||
(defun create (head &key data sender)
|
(defun create (head &key data sender)
|
||||||
(make-instance 'message :head head :data data :sender sender))
|
(shape:create 'message :head head :data data :sender sender))
|
||||||
|
;(shape:create-item (message-factory) :head head :data data :sender sender))
|
||||||
|
;(make-instance 'message :head head :data data :sender sender))
|
||||||
|
|
||||||
(defmethod print-object ((msg message) stream)
|
(defmethod print-object ((msg message) stream)
|
||||||
(shape:print-slots msg stream 'shape:head 'sender 'shape:data))
|
(shape:print-slots msg stream 'shape:head 'sender 'shape:data))
|
||||||
|
|
|
@ -2,15 +2,42 @@
|
||||||
|
|
||||||
(defpackage :scopes/shape
|
(defpackage :scopes/shape
|
||||||
(:use :common-lisp)
|
(:use :common-lisp)
|
||||||
(:local-nicknames (:util :scopes/util))
|
(:local-nicknames (:util :scopes/util)
|
||||||
(:export #:record #:print-fields #:print-slots
|
(:mop :closer-mop))
|
||||||
|
(:export #:factory #:create #:create-item #:record-factory
|
||||||
|
#:record #:print-fields #:print-slots
|
||||||
#:head-fields #:head #:head-value #:head-plist
|
#:head-fields #:head #:head-value #:head-plist
|
||||||
#:data-fields #:data #:data-value))
|
#:data-fields #:data #:data-value))
|
||||||
|
|
||||||
(in-package :scopes/shape)
|
(in-package :scopes/shape)
|
||||||
|
|
||||||
(defclass record ()
|
;;;; abstract base item and factory
|
||||||
((head-fields :reader head-fields :initarg :head-fields
|
|
||||||
|
(defclass factory ()
|
||||||
|
((item-class :reader item-class :initarg :item-class)))
|
||||||
|
|
||||||
|
(defclass item ()
|
||||||
|
((factory :reader factory :initarg :factory :allocation :class)))
|
||||||
|
|
||||||
|
(defun create (cls &rest args)
|
||||||
|
(let* ((ic (find-class cls)))
|
||||||
|
(mop:ensure-finalized ic)
|
||||||
|
(apply #'make-instance cls :factory (factory (mop:class-prototype ic)) args)))
|
||||||
|
|
||||||
|
(defun create-item (fac &rest args)
|
||||||
|
(apply #'make-instance (item-class fac) args))
|
||||||
|
|
||||||
|
;;;; record class, factory, utilities
|
||||||
|
|
||||||
|
(defclass record-factory (factory)
|
||||||
|
((item-class :initform 'record)
|
||||||
|
(head-fields :reader head-fields :initarg :head-fields
|
||||||
|
:initform '(:taskid :username))
|
||||||
|
(data-fields :reader data-fields :initarg :data-fields :initform nil)))
|
||||||
|
|
||||||
|
(defclass record (item)
|
||||||
|
((factory :initform (make-instance 'record-factory))
|
||||||
|
(head-fields :reader head-fields :initarg :head-fields
|
||||||
:initform '(:taskid :username) :allocation :class)
|
:initform '(:taskid :username) :allocation :class)
|
||||||
(data-fields :reader data-fields :initarg :data-fields
|
(data-fields :reader data-fields :initarg :data-fields
|
||||||
:initform nil :allocation :class)
|
:initform nil :allocation :class)
|
||||||
|
@ -23,18 +50,6 @@
|
||||||
(defmethod print-object ((rec record) stream)
|
(defmethod print-object ((rec record) stream)
|
||||||
(print-fields rec stream 'head 'data))
|
(print-fields rec stream 'head 'data))
|
||||||
|
|
||||||
(defun print-fields (rec stream &rest fields)
|
|
||||||
(let ((fm (util:make-vars-format fields)))
|
|
||||||
(print-unreadable-object (rec stream :type t)
|
|
||||||
(apply #'format stream fm (mapcar #'(lambda (x) (funcall x rec)) fields)))))
|
|
||||||
|
|
||||||
(defun print-slots (rec stream &rest fields)
|
|
||||||
(let ((fm (util:make-vars-format fields)))
|
|
||||||
(print-unreadable-object (rec stream :type t)
|
|
||||||
(apply #'format stream fm
|
|
||||||
(mapcar #'(lambda (x) (when (slot-boundp rec x) (slot-value rec x)))
|
|
||||||
fields)))))
|
|
||||||
|
|
||||||
(defun head-value (rec key)
|
(defun head-value (rec key)
|
||||||
(elt (head rec) (position key (head-fields rec))))
|
(elt (head rec) (position key (head-fields rec))))
|
||||||
|
|
||||||
|
@ -52,3 +67,17 @@
|
||||||
|
|
||||||
(defun (setf data-value) (val rec key)
|
(defun (setf data-value) (val rec key)
|
||||||
(setf (getf (data rec) key) val))
|
(setf (getf (data rec) key) val))
|
||||||
|
|
||||||
|
;;;; printing utilities
|
||||||
|
|
||||||
|
(defun print-fields (rec stream &rest fields)
|
||||||
|
(let ((fm (util:make-vars-format fields)))
|
||||||
|
(print-unreadable-object (rec stream :type t)
|
||||||
|
(apply #'format stream fm (mapcar #'(lambda (x) (funcall x rec)) fields)))))
|
||||||
|
|
||||||
|
(defun print-slots (rec stream &rest fields)
|
||||||
|
(let ((fm (util:make-vars-format fields)))
|
||||||
|
(print-unreadable-object (rec stream :type t)
|
||||||
|
(apply #'format stream fm
|
||||||
|
(mapcar #'(lambda (x) (when (slot-boundp rec x) (slot-value rec x)))
|
||||||
|
fields)))))
|
||||||
|
|
Loading…
Add table
Reference in a new issue