[MacRuby-devel] Understanding Pointer objects

Mark Rada mrada at marketcircle.com
Tue Oct 26 16:18:22 PDT 2010


On 2010-10-25, at 11:39 PM, Laurent Sansonetti wrote:

> Hi Mark,
> 
>> Ah, that does work, and seems to have worked for a number of other things I am trying to do. 
>> 
>> The only problem now is when I have something like
>> 
>> 	value = Pointer.new '^v' # pointer to pointer to void
>> 	AXUIElementCopyAttributeValue mail_object, 'AXHidden', value
>> 
>> In this case I am using it right now, it will be returning a boolean, but when I try to dereference it like
>> 
>> 	puts value[0][0] # => 120
>> 
>> it gives me a Fixnum, and then I can keep trying things like 
>> 
>> 	puts value[0][1] # => 104
>> 	puts value[0][10000] # => 0
>> 
>> And I seem to get nowhere.
>> 
>> Is there a way to cast the data back into the type I want it to be? Or am I doing something dumb?
> 
> Looks like this function has the following declaration:
> 
> extern AXError  AXUIElementCopyAttributeValue (AXUIElementRef element, CFStringRef attribute, CFTypeRef *value);
> 
> So, the 3rd argument is is a CF object returned by reference.
> 
> You should be able to do the following then:
> 
>   ptr = Pointer.new(:id)
>   AXUIElementCopyAttributeValue(mail_object, 'AXHidden', ptr)
>   value = ptr[0]
> 
> To reply to your other question, it is possible to cast the type of a Pointer object, using the #cast! method. Sometimes this is useful when you get a void pointer from a native API and want to cast it into something more useful. But you should be careful, MacRuby will not prevent you from doing bad casts.
> 

Hmm, I tried that but it did not work. The error I got was:

	TypeError: expected instance of Pointer of type `^v', got `@'

So I tried to see if I could cast a '^v' Pointer, but I do not know how #cast! wants the argument to be phrased, when I checked macri, I got this error:

	 # macri Pointer
	/Library/Frameworks/MacRuby.framework/Versions/0.7.1/usr/lib/ruby/1.9.2/rdoc/ri/driver.rb:383:in `create_class_cache': Permission denied - open() failed (Errno::EACCES)
		from /Library/Frameworks/MacRuby.framework/Versions/0.7.1/usr/lib/ruby/1.9.2/rdoc/ri/driver.rb:342:in `class_cache'
		from /Library/Frameworks/MacRuby.framework/Versions/0.7.1/usr/lib/ruby/1.9.2/rdoc/ri/driver.rb:596:in `block'
		from /Library/Frameworks/MacRuby.framework/Versions/0.7.1/usr/lib/ruby/1.9.2/rdoc/ri/driver.rb:591:in `run'
		from /usr/local/bin/macri:3:in `<main>'
	zsh: exit 1     macri Pointer

My feeble attempts to try and coerce a String from the pointer Pointer ended like this:

	irb(main):029:0> value = test[0].cast! 'String'
	=> #<Pointer:0x200c2ab80>
	irb(main):030:0> value = test[0][0].cast! 'String'
	NoMethodError: undefined method `cast!' for 120:Fixnum

But I don't know if it is because I am using #cast! incorrectly or not.

Also, in cases where I want to cast a boolean pointer into MacRuby would I have to do some additional logic to decide to cast to TrueClass and FalseClass or is there some more convenient way that will figure it out for me?

Thanks,
	Mark

> Laurent
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel



More information about the MacRuby-devel mailing list