From 3976c84ee45ee1500216b48f18a89e0f125ba202 Mon Sep 17 00:00:00 2001
From: liam <liam@a3d8a0fb-c1db-0310-ace7-a616afeb9e30>
Date: Fri, 7 Apr 2006 22:33:17 +0000
Subject: [PATCH] Generic functions and some methods data-import, data-export,
 data-validate.  Option to import and validate using theses in with-data.

git-svn-id: svn+ssh://pop/opt/space/mathematics/gsl/trunk@3026 a3d8a0fb-c1db-0310-ace7-a616afeb9e30
---
 data/combination.lisp |  7 ++++---
 data/data.lisp        | 38 +++++++++++++++++++++++++++-----------
 data/permutation.lisp |  5 +++--
 3 files changed, 34 insertions(+), 16 deletions(-)

diff --git a/data/combination.lisp b/data/combination.lisp
index b259d9fc..26e6c644 100644
--- a/data/combination.lisp
+++ b/data/combination.lisp
@@ -3,7 +3,7 @@
 ; description: Combinations
 ; date:        Sun Mar 26 2006 - 11:51                   
 ; author:      Liam M. Healy                             
-; modified:    Thu Apr  6 2006 - 23:17
+; modified:    Fri Apr  7 2006 - 18:04
 ;********************************************************
 ;;; $Id: $
 
@@ -61,7 +61,7 @@
   :c-return-value :return
   :documentation "The ith element of the combination.")
 
-(defunx combination-list (combination)
+(defmethod data-export ((combination gsl-combination))
   "The combination as a list."
   (loop for j below (combination-size combination)
 	collect	(gsl-aref combination j)))
@@ -91,8 +91,9 @@
   :documentation
   "A pointer to the array of elements in the combination @var{p}.")
 
-(defun-gsl combination-valid ((c gsl-combination-c))
+(defun-gsl data-valid (((pointer combination) :pointer))
   "gsl_combination_valid"
+  :method ((combination gsl-combination))
   :c-return-value :return
   :return (:boolean) 
   :documentation
diff --git a/data/data.lisp b/data/data.lisp
index 65516ef1..8ca442ab 100644
--- a/data/data.lisp
+++ b/data/data.lisp
@@ -3,7 +3,7 @@
 ; description: Using GSL storage.                        
 ; date:        Sun Mar 26 2006 - 16:32                   
 ; author:      Liam M. Healy                             
-; modified:    Thu Apr  6 2006 - 22:45
+; modified:    Fri Apr  7 2006 - 18:00
 ;********************************************************
 ;;; $Id: $
 
@@ -87,17 +87,28 @@
 (defgeneric set-identity (object)
   (:documentation "Set elements to represent the identity."))
 
+(export 'data-import)
+(defgeneric data-import (object from)
+  (:documentation "Import the values from the CL object."))
+
+(export 'data-export)
+(defgeneric data-export (object)
+  (:documentation "Create a CL object with the values."))
+
+(export 'data-validate)
+(defgeneric data-valid (object)
+  (:documentation "Validate the values in the object."))
 
 (export 'with-data)
-(defmacro with-data ((symbol type size &optional zero) &body body)
+(defmacro with-data ((symbol type size &optional init validate) &body body)
   "Allocate GSL data, bind to pointer,
-   and then deallocated it when done.  If zero is T, zero the
+   and then deallocated it when done.  If init is T, init the
    contents when allocating."
   (flet ((cl-name (action)
 	   (intern (format nil "GSL-~a-~a" type action))))
     (let ((ptr (gensym "PTR")))
       `(let* ((,ptr
-	       (,(if zero (cl-name 'calloc) (cl-name 'alloc))
+	       (,(if (eq init t) (cl-name 'calloc) (cl-name 'alloc))
 		 ,@(if (listp size) size (list size))))
 	      (,symbol
 	       (make-instance ',(intern (format nil "GSL-~a" type))
@@ -105,13 +116,18 @@
 	(check-null-pointer ,ptr
 	 :ENOMEM
 	 (format nil "For '~a of GSL type ~a." ',symbol ',type))
-	#+old
-	(when (cffi:null-pointer-p ,ptr)
-	  (error 'gsl-error
-		 :gsl-errno (cffi:foreign-enum-value 'gsl-errorno :ENOMEM)
-		 :gsl-reason
-		 (format nil "For '~a of GSL type ~a." ',symbol ',type)))
 	(unwind-protect 
-	     (progn ,@body)
+	     (progn
+	       ,@(case init
+		       (:identity `((set-identity ,symbol)))
+		       ((t) nil)
+		       (t `((data-import ,symbol ,init))))
+	       ,@(when
+		  validate
+		  `((unless (data-valid ,symbol)
+		      (error "Invalid ~a, ~a" type init))))
+	       ,@body)
 	  (,(cl-name 'free) ,ptr))))))
 
+;;;(with-data (p permutation 5 #(2 3 4 0) t)  foo)
+
diff --git a/data/permutation.lisp b/data/permutation.lisp
index d2f536a5..b2b26a47 100644
--- a/data/permutation.lisp
+++ b/data/permutation.lisp
@@ -3,7 +3,7 @@
 ; description: Permutations
 ; date:        Sun Mar 26 2006 - 11:51                   
 ; author:      Liam M. Healy                             
-; modified:    Wed Apr  5 2006 - 23:39
+; modified:    Fri Apr  7 2006 - 18:00
 ;********************************************************
 ;;; $Id: $
 
@@ -78,8 +78,9 @@ permutation @var{dest}.  The two permutations must have the same size.")
   "A pointer to the array of elements in the
    permutation @var{p}.")
 
-(defun-gsl permutation-valid ((p gsl-permutation-c))
+(defun-gsl data-valid (((pointer permutation) :pointer))
   "gsl_permutation_valid"
+  :method ((permutation gsl-permutation))
   :c-return-value :return
   :return (:boolean) 
   :documentation
-- 
GitLab