123456789_123456789_123456789_123456789_123456789_

Class: FFI::AutoPointer

Relationships & Source Files
Namespace Children
Classes:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: FFI::Pointer
Defined in: lib/ffi/autopointer.rb

Constant Summary

AbstractMemory - Inherited

LONG_MAX

Pointer - Inherited

NULL, SIZE

Class Method Summary

DataConverter - Extended

from_native

Convert from a native type.

native_type

Get native type.

to_native

Convert to a native type.

Pointer - Inherited

.new

A new instance of Pointer.

.size

Return the size of a pointer on the current platform, in bytes.

Instance Attribute Summary

Pointer - Inherited

AbstractMemory - Inherited

#size_limit?

Return true if self has a size limit.

Instance Method Summary

Pointer - Inherited

#+, #==,
#address

Alias for Pointer#to_i.

#free,
#initialize_copy

This method is internally used by #dup and #clone.

#inspect

Alias for Pointer#to_s.

#order

Get or set self‘s endianness.

#read

Read pointer’s contents as type

#read_array_of_type

Read an array of type of length length.

#read_string

Read pointer’s contents as a string, or the first len bytes of the equivalent string if len is not nil.

#read_string_length

Read the first len bytes of pointer’s contents as a string.

#read_string_to_null

Read pointer’s contents as a string.

#slice, #to_i, #to_ptr, #to_s, #type_size,
#write

Write value of type type to pointer’s content.

#write_array_of_type

Write ary in pointer’s contents as type.

#write_string

Write str in pointer’s contents, or first len bytes if len is not nil.

#write_string_length

Write len first bytes of str in pointer’s contents.

AbstractMemory - Inherited

Constructor Details

.new(pointer, method) ⇒ self .new(pointer, proc) ⇒ self .new(pointer) ⇒ self

Note:

The safest, and therefore preferred, calling idiom is to pass a Method as the second parameter. Example usage:

class PointerHelper
  def self.release(pointer)
    #...
  end
end

p = AutoPointer.new(other_pointer, PointerHelper.method(:release))

The above code will cause PointerHelper#release to be invoked at GC time.

Note:

The last calling idiom (only one parameter) is generally only going to be useful if you subclass AutoPointer, and override #release, which by default does nothing.

Raises:

  • (TypeError)
[ GitHub ]

  
# File 'lib/ffi/autopointer.rb', line 70

def initialize(ptr, proc=nil)
  raise TypeError, "Invalid pointer" if ptr.nil? || !ptr.kind_of?(Pointer) ||
      ptr.kind_of?(MemoryPointer) || ptr.kind_of?(AutoPointer)
  super(ptr.type_size, ptr)

  @releaser = if proc
                if not proc.respond_to?(:call)
                  raise RuntimeError.new("proc must be callable")
                end
                Releaser.new(ptr, proc)

              else
                if not self.class.respond_to?(:release, true)
                  raise RuntimeError.new("no release method defined")
                end
                Releaser.new(ptr, self.class.method(:release))
              end

  ObjectSpace.define_finalizer(self, @releaser)
  self
end

Class Method Details

.self.from_native(ptr, ctx) ⇒ AutoPointer

Create a new AutoPointer.

Override DataConverter#from_native.

[ GitHub ]

  
# File 'lib/ffi/autopointer.rb', line 175

def self.from_native(val, ctx)
  self.new(val)
end

.native_typeType::POINTER

Return native type of AutoPointer.

Override DataConverter#native_type.

Raises:

  • (RuntimeError)

    if class does not implement a #release method

[ GitHub ]

  
# File 'lib/ffi/autopointer.rb', line 161

def self.native_type
  if not self.respond_to?(:release, true)
    raise RuntimeError.new("no release method defined for #{self.inspect}")
  end
  Type::POINTER
end

Instance Attribute Details

#autorelease=(autorelease) ⇒ Boolean (rw)

Set autorelease property. See Autorelease section at Pointer.

Parameters:

  • autorelease (Boolean)

Returns:

  • (Boolean)

    autorelease

Raises:

  • (FrozenError)
[ GitHub ]

  
# File 'lib/ffi/autopointer.rb', line 101

def autorelease=(autorelease)
  raise FrozenError.new("can't modify frozen #{self.class}") if frozen?
  @releaser.autorelease=(autorelease)
end

#autorelease?Boolean (rw)

Get autorelease property. See Autorelease section at Pointer.

Returns:

  • (Boolean)

    autorelease

[ GitHub ]

  
# File 'lib/ffi/autopointer.rb', line 108

def autorelease?
  @releaser.autorelease
end

Instance Method Details

#freenil

Free the pointer.

[ GitHub ]

  
# File 'lib/ffi/autopointer.rb', line 94

def free
  @releaser.free
end