Support "enum-like" pointers using cenum with :base-type :pointer

Bug #1660084 reported by Chris Hanson
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
CFFI
New
Undecided
Unassigned

Bug Description

It's common in some systems to have "enum-like" pointers. For example, in Apple's libdispatch (Grand Central Dispatch) there's a dispatch_source_type_t that is used like an enum, but whose members are actually well-known global variables of an underlying pointer type.

You can see all of the latest /usr/local/dispatch headers here, dispatch_source_type is defined in source.h: https://github.com/apple/swift-corelibs-libdispatch/tree/master/dispatch

What I'd like to do is use something like cenum to define these values in a way that results in loads of the global variables at runtime. For example:

    (in-package :dispatch)

    (import "dispatch/dispatch.h")

    (cenum (source-type :base-type :pointer "dispatch_source_type_t")
           ((data-add "DISPATCH_SOURCE_TYPE_DATA_ADD"))
           ((data-or "DISPATCH_SOURCE_TYPE_DATA_OR"))
           ((mach-send "DISPATCH_SOURCE_TYPE_MACH_SEND"))
           ((mach-recv "DISPATCH_SOURCE_TYPE_MACH_RECV"))
           ((memory-pressure "DISPATCH_SOURCE_TYPE_MEMORYPRESSURE"))
           ((proc "DISPATCH_SOURCE_TYPE_PROC"))
           ((read "DISPATCH_SOURCE_TYPE_READ"))
           ((signal "DISPATCH_SOURCE_TYPE_SIGNAL"))
           ((timer "DISPATCH_SOURCE_TYPE TIMER"))
           ((vnode "DISPATCH_SOURCE_TYPE_VNODE"))
           ((write "DISPATCH_SOURCE_TYPE_WRITE")))

The important aspect is that instances of source-type need to be global variables of a pointer type whose values comes from libdispatch at dynamic load time, rather than integer constants whose value is truly fixed.

I think I could achieve this right now with CFFI by using global variables, but that wouldn't express that these are all members of a particular type. (Of course, I could also annotate that, but it'd be nice to have it all be concise.)

I'm fine with using a new construct like "cenumlike" for this, since it's not technically a C enum.

Apple introduced a similar "enum-like string global" concept with macOS 10.12 and iOS 10 for better type system and IDE support in Objective-C and Swift interoperability, because of the pervasiveness of this sort of "global pointer used as enum" concept in its APIs.

(Disclosure: I work at Apple on developer tools and contributed to the design of ObjC/Swift string enums. I'm in no way representing my employer here though, I'm just an interested spare time hobbyist user of Lisp and CFFI, on Apple platforms and others as well.)

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.