Pointer Class

Watson1978 edited this page Feb 8, 2013 · 5 revisions

If you use the Cocoa APIs, you might have to pass a pointer variable into argument of API. Some cases, you might need a variable such as NSError* error;.

To create a pointer instance as NSError* error;, you can write a program as following.

error = Pointer.new('@')

You can create other Pointer instance if you pass a pointer type into Pointer.new. You can find the other pointer type in Type Encodings.

To create a Pointer instance such as char* name[5];, specify a size in the second argument.

name = Pointer.new('c', 5)
name[0] = 'a'
name[1] = 'b'
name[2] = 'c'
name[3] = 'd'
name[4] = 'e'

To create a Pointer instance of structure such as NSRect *rect[2];, you may write a program as following.

rect = Pointer.new("{CGRect={CGPoint=dd}{CGSize=dd}}", 2)

Or,

rect = Pointer.new(NSRect.type, 2)

Alias of Pointer Types

You may think difficult the pointer types such as '@'. MacRuby has the alias of pointer types.

error = Pointer.new(:object)  # alias of '@'
Meaning Pointer Types Alias
char Pointer.new('c') Pointer.new(:char)
unsigned char Pointer.new('C') Pointer.new(:uchar)
short Pointer.new('s') Pointer.new(:short)
unsigned short Pointer.new('S') Pointer.new(:ushort)
int Pointer.new('i') Pointer.new(:int)
unsigned int Pointer.new('I') Pointer.new(:uint)
long Pointer.new('l') Pointer.new(:long)
unsigned long Pointer.new('L') Pointer.new(:ulong)
long long Pointer.new('q') Pointer.new(:long_long)
unsigned long long Pointer.new('Q') Pointer.new(:ulong_long)
float Pointer.new('f') Pointer.new(:float)
double Pointer.new('d') Pointer.new(:double)
character string (char *) Pointer.new('*') Pointer.new(:string)
pointer Pointer.new('^type')
EX: Pointer.new('^f')
object Pointer.new('@') Pointer.new(:object)
Pointer.new(:id)
class object (Class) Pointer.new('#') Pointer.new(:class)
boolean Pointer.new('B') Pointer.new(:boolean)
method selector (SEL) Pointer.new(':') Pointer.new(:selector)
Pointer.new(:sel)

Methods in Pointer Class

Pointer.new

Returns a new Pointer instance.

  • new(type, size = 1) -> Pointer
    • [PARAM] type:
      • Specifies a pointer type.
    • [PARAM] size:
      • Specifies a size to allocate an array.
    • [RETURN]
      • Returns a new Pointer instance.

Pointer.new_with_type

This method is alias of Pointer.new.

Pointer.magic_cookie

Returns a new Pointer instance which cast an immediate value to (void *).

  • magic_cookie(val) -> Pointer
    • [PARAM] val:
      • Passes an immediate value to cast.
    • [RETURN]
      • Returns a new Pointer instance.

Pointer#type

Returns a pointer type.

  • type -> String
    • [RETURN]
      • Returns a string as pointer type.
>> framework 'Cocoa'
>> pointer = Pointer.new(NSRect.type)
>> pointer.type
=> "{CGRect={CGPoint=dd}{CGSize=dd}}"

Pointer#cast!

Changes a pointer type.

  • cast!(type) -> self
    • [PARAM] type:
      • Specifies a new point type.
    • [RETURN]
      • Returns a self which pointer type was changed.
>> pointer = Pointer.new('i')
>> pointer.type
=> "i"
>> pointer.cast!('I')
>> pointer.type
=> "I"

Pointer#[]

Get a value at nth position.

  • self[nth]
    • [PARAM] nth:
      • Specifies a position to get a value.
    • [RETURN]
      • Returns a value.

Pointer#[]=

Set a value into nth position.

  • self[nth] = val
    • [PARAM] nth:
      • Specifies a position to set a value.
    • [PARAM] val:
      • Passes a value to set.
    • [RETURN]
      • Returns a val.

Pointer#value

Get a value at 0 position.

  • value
    • [RETURN]
      • Returns a value at 0 position.
pointer = Pointer.new('c')
pointer[0] = 42

pointer[0]    # => 42
pointer.value # => 42

Pointer#assign

Set a value into 0 position.

  • assign(val)
    • [PARAM] val:
      • Specifies a position to set a value.
    • [RETURN]
      • Returns a val.

Pointer#+

Returns a new Pointer instance from the specified offset.

  • self + offset -> Pointer
    • [PARAM] offset:
      • Specifies an offset.
    • [RETURN]
      • Returns a new Pointer instance
name = Pointer.new('c', 5)
name[0] = 10
name[1] = 11
name[2] = 12
name[3] = 13
name[4] = 14

tmp = name + 3
2.times do |i|
  p tmp[i] # => 13, 14
end

Pointer#-

Returns a new Pointer instance from the specified offset.

  • self - offset -> Pointer
    • [PARAM] offset:
      • Specifies an offset.
    • [RETURN]
      • Returns a new Pointer instance

Pointer#to_object

Use with the object when an API returns void pointer which is a direct reference to an object.

  • to_object -> Object
    • [RETURN]
      • Returns an object
framework 'Cocoa'
keyboard = TISCopyCurrentKeyboardInputSource()
keyboard_name = TISGetInputSourceProperty(keyboard, KTISPropertyLocalizedName)
keyboard_name.to_object