I'm dynamically loading symbols from a dynamic library. However there is one problem - I have to manually provide the types of symbols. I have a C header with the global variables and functions, however it's useful only when linking statically or without dynamically loading the symbols. I was thinking I could leverage the header somehow and view it as converted to Swift or even better - use the types there without much manual work.
To illustrate here is a sample C header:
uint32_t foo;
uint32_t bar(const void* x);
Here's what I would have in Swift:
class Lib {
let myFoo: UnsafeMutablePointer<UInt32>?
typealias cFuncBar = @convention(c) (UnsafeRawPointer) -> UInt32
let myBar: cFuncBar?
init(withHandle handle: UnsafeMutableRawPointer) {
myFoo = dlsym(handle, "foo")
myBar = dlsym(handle, "bar")
}
}
I tried using type(of:)
like so:
let myFoo: UnsafeMutablePointer<type(of: foo)>?
let myBar: type(of: bar)?
however there were syntax errors.
I also tried .dynamicType
:
let myFoo: UnsafeMutablePointer<foo.dynamicType>?
let myBar: bar.dynamicType?
but this time there is a compile error that it can't find foo and bar in scope (despite otherwise managing to find them just fine).
To clarify, I'm not looking for a way to invoke the imported symbols. I'm just looking for a way to have the types be matched to the header automatically, or to at least view the C header as converted to Swift.