From 4bf7ff0fac043bbe1516e00fdd3567ed38fe6ff4 Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Sat, 4 Aug 2007 17:27:49 +0200 Subject: Define an external interface, properly document FIND-OBJC-CLASS. darcs-hash:2e4424a65b72a13e3cfa26d07dce945bc35ba8e1 --- Lisp/libobjcl.lisp | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 3 deletions(-) (limited to 'Lisp/libobjcl.lisp') diff --git a/Lisp/libobjcl.lisp b/Lisp/libobjcl.lisp index d735773..a7171b4 100644 --- a/Lisp/libobjcl.lisp +++ b/Lisp/libobjcl.lisp @@ -7,8 +7,10 @@ (use-foreign-library libobjcl) -(defcfun "objcl_initialise_runtime" :void) -(defcfun "objcl_shutdown_runtime" :void) +(defcfun ("objcl_initialise_runtime" initialise-runtime) :void) + +(defcfun ("objcl_shutdown_runtime" shutdown-runtime) :void) + (defcfun ("objcl_invoke_instance_method" %objcl-invoke-instance-method) obj-data (receiver obj-data) @@ -30,7 +32,67 @@ (class obj-data)) -(defun objcl-find-class (class-name) +(defun symbol->objc-class-name (symbol) + (let ((components (split-sequence #\- (symbol-name symbol) + :remove-empty-subseqs t))) + (reduce #'(lambda (x y) (concatenate 'string x y)) + (mapcar #'(lambda (x) + (concatenate 'string + (string (char x 0)) + (string-downcase (subseq x 1)))) + (subseq components 1)) + :initial-value (concatenate 'string + (string (char (first components) 0)) + (string-upcase + (subseq (first components) 1)))))) + + +(defun find-objc-class (class-name) + "Retrieve an Objective C class by name. + +CLASS-NAME: a symbol or a string. + +Returns: an OBJC-CLASS object representing the class whose name is +CLASS-NAME. + + +If CLASS-NAME is a symbol which does not contain a hyphen, its symbol +name is converted to lower case except for the first letter, which is +left intact, and the resulting string used as if directly given as an +argument to FIND-OBJC-CLASS. + +If CLASS-NAME is a symbol which containts a hyphen, its symbol name is +split into components seperated by hyphens and each component is +converted into a string according to the following rules: + + 1. The first component is fully converted to upper case except for its + first letter, which is left intact. + + 2. Any additional components have all of their letters converted to + lower case, except for their first letters, which are left intact. + +After this, the components are concatenated in order and the resulting +string used as if directly given as an argument to FIND-OBJC-CLASS. + +Examples: + + (find-objc-class \"NSObject\") ;=> # + (find-objc-class 'ns-object) ;=> # + (find-objc-class 'nsobject) ;=> NIL + +Rationale: + +The first component of an Objective C class name is conventionally +thought of as a namespace identifier. It is therefore sensible to +expect it to be converted to upper case by default, which is the +conventional case for namespace identifiers in Objective C." + + (typecase class-name + (string (find-objc-class-by-name class-name)) + (symbol (find-objc-class-by-name (symbol->objc-class-name class-name))))) + + +(defun find-objc-class-by-name (class-name) (let ((obj-data (%objcl-find-class class-name))) (prog1 (if (null-pointer-p (foreign-slot-value -- cgit v1.2.3