Contexts are error handled + tests for that.
[cl-zmq.git] / src / meta.lisp
blob576da6ae6fbeaa5436c98a009e1d1f49b9ae6b73
1 ;; This file is part of CL-ZMQ.
3 (in-package :zeromq)
5 (define-condition zmq-error (error)
6 ((code :reader error-code :initarg :code)
7 (description :reader error-description :initarg :description))
8 (:report (lambda (condition stream)
9 (format stream "ZMQ Error ~A - ~A."
10 (error-code condition)
11 (error-description condition)))))
13 (defun call-with-error-check (function args &key (type :int) error-p)
14 (let ((error-p (or error-p
15 (if (eq type :int)
16 #'minusp
17 #'null-pointer-p)))
18 (ret (apply function args)))
19 (if (funcall error-p ret)
20 (let* ((error-code (%errno))
21 (error-description
22 (convert-from-foreign (%strerror error-code) :string)))
23 (error 'zmq-error
24 :code error-code
25 :description error-description))
26 ret)))
28 ;; Stolen from CFFI. Uses custom allocator (alloc-fn) instead of foreign-alloc
29 (defun copy-lisp-string-octets (string alloc-fn
30 &key
31 (encoding cffi::*default-foreign-encoding*)
32 (start 0) end)
33 "Allocate a foreign string containing Lisp string STRING.
34 The string must be freed with FOREIGN-STRING-FREE."
35 (check-type string string)
36 (cffi::with-checked-simple-vector ((string
37 (coerce string 'babel:unicode-string))
38 (start start) (end end))
39 (declare (type simple-string string))
40 (let* ((mapping (cffi::lookup-mapping
41 cffi::*foreign-string-mappings*
42 encoding))
43 (count (funcall (cffi::octet-counter mapping) string start end 0))
44 (ptr (funcall alloc-fn count)))
45 (funcall (cffi::encoder mapping) string start end ptr 0)
46 (values ptr count))))
48 (defmacro with-zmq-string (args &body body)
49 `(with-foreign-string (,@args :null-terminated-p '())
50 ,@body))