Class: Capnp::Struct

Inherits:
Object
  • Object
show all
Extended by:
T::Helpers, T::Sig
Defined in:
lib/capnp/runtime/struct.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, data_size, pointers, pointers_size) ⇒ Struct

Returns a new instance of Struct.



12
13
14
15
16
17
# File 'lib/capnp/runtime/struct.rb', line 12

def initialize(data, data_size, pointers, pointers_size)
  @data = data
  @data_size = data_size
  @pointers = pointers
  @pointers_size = pointers_size
end

Class Method Details

.decode_pointer(pointer_ref) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/capnp/runtime/struct.rb', line 23

def self.decode_pointer(pointer_ref)
  # Fetch pointer data from buffer
  data = pointer_ref.read_bytes(0, Capnp::WORD_SIZE)
  parts = T.cast(data.unpack("l<S<S<"), [Integer, Integer, Integer])

  # Check the type of the pointer
  pointer_type = parts[0] & 0b11
  raise Capnp::Error.new("Pointer has type #{pointer_type}") unless pointer_type == 0

  # Shift offset to remove type bits
  parts[0] >>= 2

  parts
end

.from_pointer(pointer_ref) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/capnp/runtime/struct.rb', line 39

def self.from_pointer(pointer_ref)
  # Process far pointers
  pointer_ref, content_ref = pointer_ref.segment.message.dereference_pointer(pointer_ref)

  # Decode the pointer
  offset_words, data_words, pointers_words = decode_pointer(pointer_ref)

  # Check for empty struct
  return new(Capnp::Reference::EMPTY, 0, Capnp::Reference::EMPTY, 0) if offset_words == -1

  # Check for NULL pointer
  return nil if offset_words.zero? && data_words.zero? && pointers_words.zero?

  # Extract data section
  data_size = data_words * Capnp::WORD_SIZE
  if content_ref.nil?
    data_offset = (offset_words + 1) * Capnp::WORD_SIZE
    data_ref = pointer_ref.offset_position(data_offset)
  else
    data_ref = content_ref
  end

  # Extract pointers section
  pointers_size = pointers_words * Capnp::WORD_SIZE
  pointers_ref = data_ref.offset_position(data_size)

  new(data_ref, data_size, pointers_ref, pointers_size)
end

Instance Method Details

#to_objObject



69
70
# File 'lib/capnp/runtime/struct.rb', line 69

def to_obj
end