TypedPointer represents a :pointer (FFI type) that is a specific struct type. You can use TypedPointer( SomeStructClass ) instead of :pointer in these situations:
As the type for NiceFFI::Struct#layout to create type-smart accessors.
As the return type for NiceFFI::Library#attach_function to wrap the returned pointer.
Create a new TypedPointer whose type is the given struct class.
type must be a class (not an instance) which is a descendent of FFI::Struct (or is FFI::Struct itself).
options must be a Hash of zero or more options. The current meaningful options are:
:autorelease - If false, instances of NiceStruct and OpaqueStruct (and subclasses) created via this TypedPointer will be passed {:autorelease => false} to disable automatic memory management. Use this for return values of functions that should not be autoreleased.
# File lib/nice-ffi/typedpointer.rb, line 55 def initialize( type, options={} ) # unless type.is_a? Class and type.ancestors.include? FFI::Struct # raise TypeError, "#{self.class} only wraps FFI::Struct and subclasses." # end @type = type options = {:autorelease => true}.merge!( options ) @autorelease = options[:autorelease] end
# File lib/nice-ffi/typedpointer.rb, line 90 def to_s "#<#{self.class}[ #{@type} ]>" end
Unwrap (i.e. extract the pointer) from a struct of this type.
# File lib/nice-ffi/typedpointer.rb, line 82 def unwrap( struct ) unless struct.is_a? @type raise TypeError, "#{self.class}[ #{@type} ] cannot unwrap #{struct.type}" end struct.to_ptr end
Wrap a FFI::Pointer in a new struct of this type.
# File lib/nice-ffi/typedpointer.rb, line 68 def wrap( pointer ) unless pointer.is_a? FFI::Pointer raise TypeError, "#{self.class}[ #{@type} ] cannot wrap #{pointer.type}" end if @type.included_modules.include?( NiceFFI::AutoRelease ) @type.new( pointer, :autorelease => @autorelease ) else @type.new( pointer ) end end
Generated with the Darkfish Rdoc Generator 2.