Class: Pointer
Overview
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. developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html
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 '@'. You could use the symbol as alias of pointer types.
error = Pointer.new(:object) # alias of '@'
Here is alias of Pointer Types.
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('^') Pointer.new(:pointer)
----------------------------------------------------------------------------
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)
Class Method Summary (collapse)
-
+ magic_cookie
Returns a new Pointer instance which cast an immediate value to (void *).
-
+ new
Returns a new Pointer instance.
-
+ new_with_type
Returns a new Pointer instance.
Instance Method Summary (collapse)
-
- +
Returns a new Pointer instance from the specified offset.
-
- -
Returns a new Pointer instance from the specified offset.
-
- []
Get a value at nth position.
-
- []=
Set a value into nth position.
-
- assign=
Set a value into 0 position.
-
- cast!
Changes a pointer type.
-
- to_object
Use with the object when an API returns void pointer which is a direct reference to an object.
-
- type
Returns a pointer type.
-
- value
Get a value at 0 position.
Methods inherited from NSObject
#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version
Constructor Details
This class inherits a constructor from NSObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class NSObject
Class Method Details
Instance Method Details
- (Pointer) +(offset)
Returns a new Pointer instance from the specified offset.
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
- (Object) [](fixnum)
Get a value at nth position.
- (Object) []=(fixnum)
Set a value into nth position.
- (Object) assign=(value)
Set a value into 0 position.
The following code is same meaning.
pointer[0] = 42
pointer.assign = 42
- (Pointer) cast!(type)
Changes a pointer type. The first parameter type is specified new pointer type. Returns a self which pointer type was changed.
pointer = Pointer.new('i')
pointer.cast!('I')
- (Object) to_object
Use with the object when an API returns void pointer which is a direct reference to an object.
framework 'Cocoa'
keyboard = TISCopyCurrentKeyboardInputSource()
keyboard_name = TISGetInputSourceProperty(keyboard, KTISPropertyLocalizedName)
keyboard_name.to_object
- (String) type
Returns a pointer type.
pointer = Pointer.new(NSRect.type)
pointer.type #=> "{CGRect={CGPoint=dd}{CGSize=dd}}"
- (Object) value
Get a value at 0 position.
The following code is same meaning.
val = pointer[0]
val = pointer.value