MacRuby DoJo (道場)

Pointer Class

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.

1
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.

Here is a few detail sample about Pointer.

1
2
3
4
5
6
7
8
9
10
11
framework 'Cocoa'

error = Pointer.new('@')
url = NSURL.URLWithString("http://your_url")
string = NSString.stringWithContentsOfURL(url, encoding: NSUTF8StringEncoding, error: error)

if error[0]
  puts error[0].description
  raise
end
puts string

If an error occurs with NSString.stringWithContentsOfURL, an error is stored into error[0].

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

1
2
3
4
5
6
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.

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

Or,

1
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.

1
error = Pointer.new(:object)  # alias of '@'
MeaningPointer TypesAlias
charPointer.new(‘c’)Pointer.new(:char)
unsigned charPointer.new(‘C’)Pointer.new(:uchar)
shortPointer.new(’s’)Pointer.new(:short)
unsigned shortPointer.new(‘S’)Pointer.new(:ushort)
intPointer.new(‘i’)Pointer.new(:int)
unsigned intPointer.new(‘I’)Pointer.new(:uint)
longPointer.new(‘l’)Pointer.new(:long)
unsigned longPointer.new(‘L’)Pointer.new(:ulong)
long longPointer.new(‘q’)Pointer.new(:long_long)
unsigned long longPointer.new(‘Q’)Pointer.new(:ulong_long)
floatPointer.new(‘f’)Pointer.new(:float)
doublePointer.new(‘d’)Pointer.new(:double)
character string (char *)Pointer.new(‘*’)Pointer.new(:string)
pointerPointer.new(‘^’)Pointer.new(:pointer)
objectPointer.new(‘@’)Pointer.new(:object)
Pointer.new(:id)
class object (Class)Pointer.new(‘#’)Pointer.new(:class)
booleanPointer.new(‘B’)Pointer.new(:boolean)
Pointer.new(:bool)
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.
1
2
3
4
>> 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.
1
2
3
4
5
6
>> 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.
1
2
3
4
5
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
1
2
3
4
5
6
7
8
9
10
11
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

TBD

Comments