Class: Kafka::FFI::OpaquePointer

Inherits:
Object
  • Object
show all
Extended by:
FFI::DataConverter
Defined in:
lib/kafka/ffi/opaque_pointer.rb

Overview

Note:

Kafka has several options for ‘opaque` pointers that get passed to callbacks. Those opaque pointers are not related to this opaque pointer.

OpaquePointer pointer provides a common pattern where we receive a pointer to a struct but don’t care about the structure and need to pass it to functions. OpaquePointer gives type safety by checking types before converting.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pointer) ⇒ OpaquePointer

Returns a new instance of OpaquePointer.



69
70
71
# File 'lib/kafka/ffi/opaque_pointer.rb', line 69

def initialize(pointer)
  @pointer = pointer
end

Instance Attribute Details

#pointerObject (readonly)



17
18
19
# File 'lib/kafka/ffi/opaque_pointer.rb', line 17

def pointer
  @pointer
end

Class Method Details

.by_refObject

Provide ::FFI::Struct API compatility for consistency with attach_function is called with an OpaquePointer.



60
61
62
# File 'lib/kafka/ffi/opaque_pointer.rb', line 60

def by_ref
  self
end

.from_native(value, _ctx) ⇒ nil

Convert from a FFI::Pointer to the implementing class.

Parameters:

  • value (FFI::Pointer)

Returns:

  • (nil)

    Passed ::FFI::Pointer::NULL

  • Instance of the class backed by the pointer.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kafka/ffi/opaque_pointer.rb', line 26

def from_native(value, _ctx)
  if !value.is_a?(::FFI::Pointer)
    raise TypeError, "from_native can only convert from a ::FFI::Pointer to #{self}"
  end

  # The equivalent of a native NULL pointer is nil.
  if value.null?
    return nil
  end

  obj = allocate
  obj.send(:initialize, value)
  obj
end

.inherited(subclass) ⇒ Object



64
65
66
# File 'lib/kafka/ffi/opaque_pointer.rb', line 64

def inherited(subclass)
  subclass.native_type :pointer
end

.to_native(value, _ctx) ⇒ FFI::Pointer

Convert from a Kafka::FFI type to a native FFI type.

Parameters:

  • value (Object)

    Instance to retrieve a pointer for.

Returns:

  • (FFI::Pointer)

    Pointer to the opaque struct



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kafka/ffi/opaque_pointer.rb', line 46

def to_native(value, _ctx)
  if value.nil?
    return ::FFI::Pointer::NULL
  end

  if !value.is_a?(self)
    raise TypeError, "expected a kind of #{self}, was #{value.class}"
  end

  value.pointer
end