Skip to content
This repository

Can't use the structure which declared with lowercase in C header files. #121

Open
Watson1978 opened this Issue July 03, 2012 · 7 comments

5 participants

Watson weizhong Joshua Ballanco Mark Villacampa Mark Rada
Watson
Owner

Test case

// foo.h
struct foo {
    int xxx;
};

struct Bar {
    int xxx;
};
$ gen_bridge_metadata -c '-I .' foo.h > foo.bridgesupport
$ macirb --simple-prompt
>> load_bridge_support_file 'foo.bridgesupport'
=> main
>> p Foo.type
NameError: uninitialized constant Foo

>> p Bar.type
"{Bar=i}"
=> "{Bar=i}"
>> 
weizhong

Hi,
I am new man on macruby.
but I think all class name are Upcase in ruby world

Joshua Ballanco
Owner

Indeed, but then supporting C structs in MacRuby has always been a bit...uncomfortable. We could do similarly to what we do with constants, and automatically convert the first letter to uppercase. We just have to hope that no one attempts to use a C library with both foo and Foo structs defined. ;)

On another note: we should probably merge gen_bridge_metadata into MacRuby at some point, since I think we're the only project still actively using it...

Mark Villacampa

Is there any solution for this yet besides an obj-c wrapper? :)

Mark Rada
Owner

@MarkVillacampa try this:

NSClassFromString("foo").type
Mark Rada
Owner

To let you know, there is also an __NSCF class in MacRuby (or perhaps Cocoa?), which I have to access in a roundabout way like this. The class exists, but the language doesn't provide a non-awkward way to communicate with it.

Mark Rada
Owner

Maybe a semi-decent solution would be to automatically create methods on Object for structs whose type has a lower class letter:

class Object
  def foo
    NSClassFromString("foo")
  end
end

Another solution, if you have access/control to the headers, would be to just create a typedef:

// foo.h
struct foo {
    int xxx;
};

typedef struct foo Foo;

struct Bar {
    int xxx;
};

Which will then add the Foo class properly into MacRuby through bridge support.

Mark Villacampa

At the end my problem was quite different, what I was trying to do is access a c++ class (which I've learnt can't be done with MacRuby), which BridgeSupport was mistakenly interpreting as a struct.

The solution was to create a simple objective-c++ wrapper around those c++ classes.

Taking into account that MacRuby automatically handles lowercase constants, and that we can access lowercase structs through NSClassFromString, I dont think this is a very big issue right now :)

Thanks for the tip @ferrous26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.