You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to create a bridging header for Swift 5 and Xcode 12.5.1. Must my Swift code instantiate the BoardShim object, or can it just call prepare_session() and start_stream() directly?
The idea of bindings is to provide high-level API which is easier to use than plain C methods exposed from low-level library. So, we created BoardShim class for each binding. Users are expected to work with BoardShim methods instead calling low-level prepare_session and other methods directly.
I dont know swift but why do we need a bridging header at all?(and what is it?)
I think it can look like this(pseudocode inspired from python binding):
class BoardShimDynLibLoader {
handler = nil;
static func get_handler() {
if (handler == nil)
handler = dlopen("libBoardContoller.dylib", RTLD_NOW)
return handler;
}
class BoardShim {
func prepare_session() {
typealias prepare_session = @convention(c) (CInt, Cchar *) -> CInt
handler = BoardShimDynLibLoader.get_handler()
var prepare_session = dlsym(handle, "prepare_session")
if (prepare_session != nil) {
let f = unsafeBitCast(sym, prepare_session .self)
let result = f(board_id, params)
}
}
}
Idea is to create a singleton that will call dlopen only once and find addresses of required functions manually.
Is it worse or better?(I don't know swift best practices)
The bridging header tells Swift how to resolve the object names in your Swift code. Without it you get errors such as, "Cannot find 'prepare_stream' in scope". It's typically a .h file which simply includes other .h files. You have to register it in the Xcode build settings, so that Swift knows where to look.
I managed to call the low-level functions directly, such as prepare_session() and get_eeg_channels(), but so far I am unable to instantiate BoardShim(). I need some help. Everything I've found so far, I can't get working.
You should not call methods from C++ BoardShim class. This class even doesn't exist in low-level API(low-level API is for bindings, high-level API is for users).
Instead, you need to create a new class in Swift and call it BoardShim, inside this class you need to wrap methods from low-level API. Take a look at the cpp-package for reference. https://github.com/brainflow-dev/brainflow/blob/master/cpp-package/src/board_shim.cpp#L104 Here method is_prepared from BoardShim class wraps method is_prepared from low-level API, you need to do the same
OK I think I finally understand: you want me to write BoardShim.swift modeled after board_shim.cpp. That seems completely doable. I will also have to write swift bindings for all of BoardShim's dependencies, such as BrainFlowException, BrainFlowArray, BrainFlowInputParams, etc. And nowhere in all of those bindings will I need to instantiate any C++ classes, correct?
Correct, but probably you dont need BrainFlowArray(its specific for c++ for better perf, you can do the same as in java for example). Also, there are two more classes: DataFilter and MLModule, its the same there.(I can help and do it for some of them)
Thanks. Swift doesn't like binding its String type to "char *" parameters in its bridging headers, so I'm making them all "const char *" in board_controller.h instead.
Also I think Swift will automatically make BoardIds iterable if I add the CaseIterable protocol to its type definition. I'll try that shortly.
<<I'm not yet sure how to do the same in Swift, nor am I certain it's necessary.
This stuff with instance in java is kinda bridging header in swift. It describes the interface of BoardController.dll and allows you to call C\C++ methods. You don't need that.
<<I am ready to push BoardShim.swift and its dependencies. Please give me instructions for committing and pushing.
Create WIP PR from non-master branch of your fork and if you see a checkbox like "allow edit to maintainers" click on it. So, I will be able to make changes and help you a little. Make sure that swift binding matches the common structure in terms of folder and file names. Would be good to add a few tests to CI pipelines but it can be done later on
I created my fork and within it a branch named swift-bindings. I cloned that branch to my laptop, added my .swift files, committed and pushed them, and now am ready to create my pull request. How do I make it WIP?
Because of the Swift scope rules, I had to move the logger functions of DataFilter inside its type definition. Otherwise it would have caused name collisions with the analogous BoardShim functions. For consistency I should probably do the same for the BoardShim logger functions. What do you think?
I think all methods like get_exg_channels ,set_log_level, etc should be static class methods instead of global functions. This way it will be consistent with other bindings
Activity
ScottThomasMiller commentedon Aug 19, 2021
I am trying to create a bridging header for Swift 5 and Xcode 12.5.1. Must my Swift code instantiate the BoardShim object, or can it just call prepare_session() and start_stream() directly?
Andrey1994 commentedon Aug 19, 2021
The idea of bindings is to provide high-level API which is easier to use than plain C methods exposed from low-level library. So, we created BoardShim class for each binding. Users are expected to work with BoardShim methods instead calling low-level prepare_session and other methods directly.
Example from Java:(https://github.com/brainflow-dev/brainflow/blob/master/java-package/brainflow/src/main/java/brainflow/BoardShim.java)
Andrey1994 commentedon Aug 19, 2021
So I would say let's create BoardShim, DataFilter, and other classes like in existing bindings
Andrey1994 commentedon Aug 19, 2021
I dont know swift but why do we need a bridging header at all?(and what is it?)
I think it can look like this(pseudocode inspired from python binding):
Idea is to create a singleton that will call
dlopenonly once and find addresses of required functions manually.Is it worse or better?(I don't know swift best practices)
ScottThomasMiller commentedon Aug 19, 2021
The bridging header tells Swift how to resolve the object names in your Swift code. Without it you get errors such as, "Cannot find 'prepare_stream' in scope". It's typically a .h file which simply includes other .h files. You have to register it in the Xcode build settings, so that Swift knows where to look.
Your example is similar to this: https://stackoverflow.com/questions/35229149/interacting-with-c-classes-from-swift
I should be able to implement something like the that, because I already have all of BrainFlow's C++ headers.
I am still learning Swift, but I think that your idea is basically a wrapper, and wrappers are very "Swifty".
ScottThomasMiller commentedon Aug 23, 2021
I managed to call the low-level functions directly, such as prepare_session() and get_eeg_channels(), but so far I am unable to instantiate BoardShim(). I need some help. Everything I've found so far, I can't get working.
Andrey1994 commentedon Aug 23, 2021
You should not call methods from C++ BoardShim class. This class even doesn't exist in low-level API(low-level API is for bindings, high-level API is for users).
Instead, you need to create a new class in Swift and call it BoardShim, inside this class you need to wrap methods from low-level API. Take a look at the cpp-package for reference. https://github.com/brainflow-dev/brainflow/blob/master/cpp-package/src/board_shim.cpp#L104 Here method
is_preparedfrom BoardShim class wraps methodis_preparedfrom low-level API, you need to do the sameScottThomasMiller commentedon Aug 23, 2021
OK I think I finally understand: you want me to write BoardShim.swift modeled after board_shim.cpp. That seems completely doable. I will also have to write swift bindings for all of BoardShim's dependencies, such as BrainFlowException, BrainFlowArray, BrainFlowInputParams, etc. And nowhere in all of those bindings will I need to instantiate any C++ classes, correct?
Andrey1994 commentedon Aug 23, 2021
Correct, but probably you dont need BrainFlowArray(its specific for c++ for better perf, you can do the same as in java for example). Also, there are two more classes: DataFilter and MLModule, its the same there.(I can help and do it for some of them)
Andrey1994 commentedon Aug 23, 2021
Also, you need to duplicate enums like BoardIds, etc. All of that is pretty small and simple except main classes(BoardShim and DataFilter)
ScottThomasMiller commentedon Aug 24, 2021
Here's what I have so far: https://github.com/ScottThomasMiller/ScottsML/tree/main/Swift/BrainFlowBindings
Andrey1994 commentedon Aug 24, 2021
Looks good so far!
ScottThomasMiller commentedon Aug 24, 2021
Thanks. Swift doesn't like binding its String type to "char *" parameters in its bridging headers, so I'm making them all "const char *" in board_controller.h instead.
Also I think Swift will automatically make BoardIds iterable if I add the CaseIterable protocol to its type definition. I'll try that shortly.
31 remaining items
ScottThomasMiller commentedon Sep 9, 2021
I'm not yet sure how to do the same in Swift, nor am I certain it's necessary.
I am ready to push BoardShim.swift and its dependencies. Please give me instructions for committing and pushing.
Andrey1994 commentedon Sep 9, 2021
<<I'm not yet sure how to do the same in Swift, nor am I certain it's necessary.
This stuff with
instancein java is kinda bridging header in swift. It describes the interface of BoardController.dll and allows you to call C\C++ methods. You don't need that.<<I am ready to push BoardShim.swift and its dependencies. Please give me instructions for committing and pushing.
Create WIP PR from non-master branch of your fork and if you see a checkbox like "allow edit to maintainers" click on it. So, I will be able to make changes and help you a little. Make sure that swift binding matches the common structure in terms of folder and file names. Would be good to add a few tests to CI pipelines but it can be done later on
ScottThomasMiller commentedon Sep 9, 2021
In my fork should I duplicate ./brainflow/settings.xml and ./brainflow/pom.xml?
Andrey1994 commentedon Sep 9, 2021
no, these files are specific for maven packaging(like setup.py in python)
ScottThomasMiller commentedon Sep 9, 2021
I created my fork and within it a branch named swift-bindings. I cloned that branch to my laptop, added my .swift files, committed and pushed them, and now am ready to create my pull request. How do I make it WIP?
Here's a screenshot:
Andrey1994 commentedon Sep 9, 2021
Green Button "Create pull Request" click on triangle close to it
ScottThomasMiller commentedon Sep 9, 2021
"Create draft pull request"?
Andrey1994 commentedon Sep 9, 2021
yes
ScottThomasMiller commentedon Sep 11, 2021
Because of the Swift scope rules, I had to move the logger functions of DataFilter inside its type definition. Otherwise it would have caused name collisions with the analogous BoardShim functions. For consistency I should probably do the same for the BoardShim logger functions. What do you think?
Andrey1994 commentedon Sep 11, 2021
I think all methods like
get_exg_channels,set_log_level, etc should be static class methods instead of global functions. This way it will be consistent with other bindingsAndrey1994 commentedon Sep 11, 2021
In fact there should not be any global functions, only class methods and static class methods
Andrey1994 commentedon Sep 11, 2021
And lets move this discussion to PR comments
hab25 commentedon May 9, 2025
Looks like cf90732 may have solved this for macOS only, iOS still wanted.