How can I call all of these CFFI defcfuns by the same name?

Discussion of Common Lisp
Post Reply
joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

How can I call all of these CFFI defcfuns by the same name?

Post by joeish80829 » Sat May 03, 2014 9:00 pm

i have the 8 defcfuns below and I can call them all by the same name as in my example at the bottom of the page...but I was hoping someone could edit my version to make it safer and faster, but am new to multimethods and could use help implementing them...I would love to use multimethods to do this providing they are faster than what I have. This is just a very imortant part of the library I'm building and I thought I should run it by more experienced programmers to make sure I do it right. It should seem evident in my MAT defun at the bottom of the page that all my garbage collected functions need to be called by adding a ":t" to the end of the function name as in :

(mat :t ....)
(mat :t 4 4 0) ...etc


Any guidance on this is very important and much, much appreciated:)

Code: Select all

;; Mat::Mat()
;; Mat* cv_create_Mat()
(defcfun ("cv_create_Mat" %mat) mat
  "MAT constructor")

;; Mat::Mat()
;; Mat* cv_create_Mat()
(defcfun ("cv_create_Mat" %%mat) (mat :garbage-collect t)
  "MAT constructor")


;; Mat::Mat(int rows, int cols, int type)
;; Mat* cv_create_Mat_typed(int rows, int cols, int type)
(defcfun ("cv_create_Mat_typed" %mat-typed) mat
  "MAT constructor with a row, column and type parameter."
  (rows :int)
  (cols :int)
  (type :int))


;; Mat::Mat(int rows, int cols, int type)
;; Mat* cv_create_Mat_typed(int rows, int cols, int type)
(defcfun ("cv_create_Mat_typed" %%mat-typed) (mat :garbage-collect t)
  "MAT constructor with a row, column and type parameter."
  (rows :int)
  (cols :int)
  (type :int))


(defun mat-typed (&rest args)
  (cond ((eq (fourth args) nil) (%mat-typed (first args) (second args) (third args)))
	((eq :t (car args)) (%%mat-typed (second args) (third args) (fourth args)))
	(t nil)))


;; Mat::Mat(int rows, int cols, int type, const Scalar& s)
;; Mat* cv_create_Mat_with_value(int rows, int cols, int type, Scalar* s)
(defcfun ("cv_create_Mat_with_value" %mat-value) mat
  (rows :int)
  (cols :int)
  (type :int)
  (s scalar))


;; Mat::Mat(int rows, int cols, int type, const Scalar& s)
;; Mat* cv_create_Mat_with_value(int rows, int cols, int type, Scalar* s)
(defcfun ("cv_create_Mat_with_value" %%mat-value) (mat :garbage-collect t)
  (rows :int)
  (cols :int)
  (type :int)
  (s scalar))


;; Mat::Mat(int rows, int cols, int type, void* data) 
;; Mat* cv_create_Mat_with_data(int rows, int cols, int type, void* data)
(defcfun ("cv_create_Mat_with_data" %mat-data) mat
  (rows :int)
  (cols :int)
  (type :int)
  (data :pointer))


;; Mat::Mat(int rows, int cols, int type, void* data) 
;; Mat* cv_create_Mat_with_data(int rows, int cols, int type, void* data)
(defcfun ("cv_create_Mat_with_data" %%mat-data) (mat :garbage-collect t)
  (rows :int)
  (cols :int)
  (type :int)
  (data :pointer))

Code: Select all

(defun mat (&rest args)
  (cond ((eq (first args) nil) (%mat))

	((and (eq :t (first args)) (not (second args))) (%%mat))

        ((and (eq (fourth args) nil) (first args)) 
	 (%mat-typed (first args) (second args) (third args)))

	((and (eq (first args) :t) (eq (fifth args) nil)) 
	 (%%mat-typed (second args) (third args) (fourth args)))

	((eq (type-of (fourth args)) 'cv-scalar)

	 (%mat-value (first args) (second args) (third args) (fourth args)))

	((and (eq (first args) :t) (eq (type-of (fifth args)) 'cv-scalar))
	 (%%mat-value (second args) (third args) (fourth args) (fifth args)))

	((pointerp (fourth args))
	 (%mat-data (first args) (second args) (third args) (fourth args)))

	((and (eq (first args) :t) (pointerp (fifth args)))
	 (%%mat-data  (second args) (third args) (fourth args) (fifth args)))

	(t nil)))

Post Reply