Comments (8)
Hey Maxim,
thanks for catching up! Well, before I got to know c-for-go I already had written most of the binding by hand. There is still some stuff missing, but I had at least something to work with. It's still on my task list to complete the binding.
I don't recall the exact reason why I didn't pursue using c-to-go further. I think I was in a hurry at that time to get at least parts of the binding up and working, so I stuck with what I had already created.
I still prefer much more a binding which is at least in parts auto generated and before completing the hamlib binding by hand I will take again the time to thoroughly try to do it with c-for-go.
from c-for-go.
@dh1tw
Thanks, I wish more people were considering using c-for-go rather than manual labor!
The interface hamlib/rig.h
looks like easy peasy, since I have a trained hand already, I quickly did a starter repo for you, feel free to clone and make all the required adjustments:
https://github.com/xlab/hamlib-go
It compiles and links fine under OS X (brew install hamlib
beforehand), the header is not ideal, but feasible! You need to ensure that args are pointers where expected pointers and arrays where arrays are expected. Also you may add some getters/setters for raw structs. I left most structs raw because of too much inner state. A raw struct looks like this:
// RigCaps as declared in hamlib/rig.h:1135
type RigCaps C.struct_rig_caps
Later I can automate getter/setter in my generator, but for now it's one manual thing to do. I hope I didn't kill your fun :D
The library seems very usable though.
Good luck.
from c-for-go.
@dh1tw
Also, you may prefer ALL_CAPS constants for easy migration of C code. Just sayin
from c-for-go.
Cool! Thanks for your work! Awesome! I just checked it and wrote a tiny test program. With the execption of a few strings which are not exported correctly (hamlib_version / hamlib_copyright), the test of the binding actually works! Pretty cool!
I started writing now the PtrTips.
However I have one question - I would like to make the binding more go-stylish. Almost all functions take as the first argument a pointer to an instance of a rig (radio). For example:
extern HAMLIB_EXPORT(int) rig_get_freq HAMLIB_PARAMS((RIG *rig, vfo_t vfo, freq_t *freq));
which c-for-go translates into:
// RigGetFreq function as declared in hamlib/rig.h:1545
func RigGetFreq(rig *Rig, vfo int32, freq *float64) int32 {
crig, _ := (*C.RIG)(unsafe.Pointer(rig)), cgoAllocsUnknown
cvfo, _ := (C.vfo_t)(vfo), cgoAllocsUnknown
cfreq, _ := (*C.freq_t)(unsafe.Pointer(freq)), cgoAllocsUnknown
__ret := C.rig_get_freq(crig, cvfo, cfreq)
__v := (int32)(__ret)
return __v
}
However I would prefer a more go-style function signature:
func (rig *RIG) GetFreq(vfo int32) (float64, error) {
....
}
Would that be possible?
from c-for-go.
This would be dream if we can do this automatically, but actually we can't.
There is a lot of pitfalls, for example, this transform introduces a state, and there is no way to guess that state automatically. And describing the state in YAML tips is harder than just write Go code.
I usually do high-level wrappers for small packages, for example:
generated: https://github.com/xlab/pocketsphinx-go/tree/master/pocketsphinx
handmade wrapper: https://github.com/xlab/pocketsphinx-go/tree/master/sphinx
generated: https://github.com/zenhotels/lmdb-go/tree/master/mdb
handmade wrapper: https://github.com/zenhotels/lmdb-go/tree/master/lmdb
As you may notice, some wrappers are there just to have OOP semantics, other wrappers manage the state, internal resources, mutexes and other stuff, also introducing a handy API.
The point of C bindings generation is to avoid handmade CGO calls, but making the API itself more idiomatic and handy will require some hands and soul of a human :)
from c-for-go.
ok, got it. I guess I asked for to much :-) In my particular case this means that I have to write actually again quite a log of code in order to wrap the entire hamlib-go API (as most of the functions are *rig related. If I understand it right I also have to wrap again all the CONSTs in order provide a unique interface to work with, right?
from c-for-go.
I also have to wrap again all the CONSTs in order provide a unique interface to work with, right?
That was my preference, you don't have to. And you just can copy that file over..
that I have to write actually again quite a log of code in order to wrap the entire hamlib-go API
Not much actually, but again, it's your preference to do so.
from c-for-go.
In my particular case this means that I have to write actually again quite a lot of code
@dh1tw kinda ironic how you decided to write the whole binding by hand :)
Were there any problems with automated process?
from c-for-go.
Related Issues (20)
- type C.struct_ in strut and func HOT 1
- How to handle a function returning a struct as value
- Type translation does not respect systems data model HOT 1
- Verbose argument to print more logs
- Avoid lock / unlock for some function call
- Wiki need to be updated
- panic: unknown type _Float128 HOT 2
- Non int return function pointer not recognized
- Generator uses underlying type of typedef when used as a parameter
- Website down HOT 3
- It would be nice to at least have an example of the YML required in the readme since the website does not work HOT 1
- build HOT 5
- [OS X / Ubuntu ARM64] include file not found: <string> HOT 3
- Extracting SAL annotations from declarations HOT 2
- Example where C const get translated into Go typed const
- Callback generation HOT 2
- THIS IS NOT AN ISSUE!!!
- Avoid symbol lookup error
- Passing in an array to a C function and reading the data - incorrect autogenerated Go code
- Write rules against the location of the type/define rather than the name
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from c-for-go.