CGo unexported types

Showing 1-5 of 5 messages
CGo unexported types dpx.in...@gmail.com 11/26/12 10:37 AM
Hi,

I asked this question on stackoverflow and still didn't get definite answer, so I decided to try here. Text below mostly is copied from the stackoverflow question.


Suppose there is CGo package with a struct defined like this:

package test
...
type Test struct {
    Field *C.C_Test
}
...

Now suppose that from somewhere else I get unsafe.Pointer which I know points to C_Test C structure.

Do I understand correctly that there is completely no way to create new test.Test instance from the unsafe.Pointer value while being in a package other than test?

Attempting to use something like &test.Test{ptr}, where ptr is unsafe.Pointer value, fails for obvious reasons with message cannot use ptr (type unsafe.Pointer) as type *test._Ctype_C_Test in field value, and type conversion to *test._Ctype_C_Test does not work as well because this type is not exported.
And I think that making my other module to use CGo and redefining the same C structure in it will not work either since that package will have something like client._Ctype_C_Test but test.Test requires test._Ctype_C_Test, and they are different from the point of view of the typechecker.

Some background: I need a way to create such structure when I use GtkBuilder with go-gtk library.
Its GtkBuilder.GetObject(name) method returns *GObject pointer which in turn contains unsafe.Pointer field which I need to turn somehow into, say, gtk.GtkEntry struct. gtk.GtkEntry itself contains implicit field of type gtk.GtkWidget which has explicit field of type *C.GtkWidget, so I need to convert unsafe.Pointer to *C.GtkWidget, but I cannot, as I have described in the simple example above.

A piece of code I'm trying to fix: https://gist.github.com/4141343

Re: [go-nuts] CGo unexported types Ian Lance Taylor 11/26/12 12:11 PM
On Mon, Nov 26, 2012 at 10:37 AM,  <dpx.in...@gmail.com> wrote:
>
> I asked this question on stackoverflow and still didn't get definite answer,
> so I decided to try here. Text below mostly is copied from the stackoverflow
> question.
>
>
> Suppose there is CGo package with a struct defined like this:
>
> package test
> ...
> type Test struct {
>     Field *C.C_Test
> }
> ...
>
> Now suppose that from somewhere else I get unsafe.Pointer which I know
> points to C_Test C structure.
>
> Do I understand correctly that there is completely no way to create new
> test.Test instance from the unsafe.Pointer value while being in a package
> other than test?

Other than using the unsafe package to push things through pointers, I
believe that is correct.

> Some background: I need a way to create such structure when I use GtkBuilder
> with go-gtk library.
> Its GtkBuilder.GetObject(name) method returns *GObject pointer which in turn
> contains unsafe.Pointer field which I need to turn somehow into, say,
> gtk.GtkEntry struct. gtk.GtkEntry itself contains implicit field of type
> gtk.GtkWidget which has explicit field of type *C.GtkWidget, so I need to
> convert unsafe.Pointer to *C.GtkWidget, but I cannot, as I have described in
> the simple example above.

If that is a reasonable thing to do, then one would hope that the
go-gtk package would provide a way to do it.  I don't know enough
about gtk to know how reasonable it is.

Ian
Re: [go-nuts] CGo unexported types dpx.in...@gmail.com 11/26/12 2:07 PM
Ian,

Thank you for your answer. At least now I certainly know this is hard)


> Other than using the unsafe package to push things through pointers, I
> believe that is correct.

Could you please explain how to do this? I have tried (see stackoverflow question and answer) playing with unsafe pointers but couldn't manage to do it. I don't know how to beat type system to somehow accept unsafe pointer as standard pointer to C struct provided C struct is defined in another package.

вторник, 27 ноября 2012 г., 0:11:22 UTC+4 пользователь Ian Lance Taylor написал:
Re: [go-nuts] CGo unexported types Ian Lance Taylor 11/26/12 3:46 PM
On Mon, Nov 26, 2012 at 2:07 PM,  <dpx.in...@gmail.com> wrote:
>
>> Other than using the unsafe package to push things through pointers, I
>> believe that is correct.
>
> Could you please explain how to do this? I have tried (see stackoverflow
> question and answer) playing with unsafe pointers but couldn't manage to do
> it. I don't know how to beat type system to somehow accept unsafe pointer as
> standard pointer to C struct provided C struct is defined in another
> package.

Untested:

// Assume u is the pointer I want to store.  Presumably it was
obtained from C code in some way.

var t test.Test
p := (*unsafe.Pointer)(unsafe.Pointer(&t.Field))
*p = unsafe.Pointer(u)

Ian
> --
>
>
Re: [go-nuts] CGo unexported types dpx.in...@gmail.com 11/27/12 1:01 AM
Yeah, this seem to work! Thank you very much!

вторник, 27 ноября 2012 г., 3:46:44 UTC+4 пользователь Ian Lance Taylor написал: