Module: Aws::Crt::ManagedNative

Overview

A mixin module for generic managed native functionality Example:

class C
  include Aws::Crt::ManagedNative
  native_destroy Aws::Crt::Native.method(:test_struct_destroy)

  def initialize
    manage_native { Aws::Crt::Native::test_struct_new() }
  end

  def use_native
    Aws::Crt::Native::test_method(native) #use that getter for native
  end
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(sub_class) ⇒ Object



21
22
23
# File 'lib/aws-crt/managed_native.rb', line 21

def self.included(sub_class)
  sub_class.extend(ClassMethods)
end

Instance Method Details

#manage_native(&block) ⇒ Object

expects a block that returns a :pointer to the native resource that this class manages



27
28
29
30
31
32
33
34
35
36
# File 'lib/aws-crt/managed_native.rb', line 27

def manage_native(&block)
  # check that a destructor has been registered
  unless self.class.instance_variable_get('@destructor')
    raise 'No native destructor registered.  use native_destroy to ' \
          'set the method used to cleanup the native object this ' \
          'class manages.'
  end
  native = block.call
  @native = FFI::AutoPointer.new(native, self.class.method(:on_release))
end

#native(safe: true) ⇒ FFI:Pointer

Parameters:

  • safe (Boolean) (defaults to: true)

    (true) - raise an exception if the native object is not set (has been freed or never created)

Returns:

  • (FFI:Pointer)


41
42
43
44
45
# File 'lib/aws-crt/managed_native.rb', line 41

def native(safe: true)
  raise '@native is unset or has been freed.' if safe && !@native

  @native
end

#native_set?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/aws-crt/managed_native.rb', line 48

def native_set?
  !!@native
end

#releaseObject

Immediately release this instance’s attachment to the underlying resources, without waiting for the garbage collector. Note that underlying resources will remain alive until nothing else is using them.



56
57
58
59
60
61
# File 'lib/aws-crt/managed_native.rb', line 56

def release
  return unless @native

  @native.free
  @native = nil
end