module Google::Protobuf::Internal::PointerHelper

Public Instance Methods

descriptor_from_file_def(file_def, pointer) click to toggle source

Utility code to defensively walk the object graph from a file_def to the pool, and either retrieve the wrapper object for the given pointer or create one. Assumes that the caller is the wrapper class for the given pointer and that it implements ‘private_constructor`.

# File lib/google/protobuf/ffi/internal/pointer_helper.rb, line 16
def descriptor_from_file_def(file_def, pointer)
  raise RuntimeError.new "FileDef is nil" if file_def.nil?
  raise RuntimeError.new "FileDef is null" if file_def.null?
  pool_def = Google::Protobuf::FFI.file_def_pool file_def
  raise RuntimeError.new "PoolDef is nil" if pool_def.nil?
  raise RuntimeError.new "PoolDef is null" if pool_def.null?
  pool = Google::Protobuf::OBJECT_CACHE.get(pool_def.address)
  raise "Cannot find pool in ObjectCache!" if pool.nil?
  descriptor = pool.descriptor_class_by_def[pointer.address]
  if descriptor.nil?
    pool.descriptor_class_by_def[pointer.address] = private_constructor(pointer, pool)
  else
    descriptor
  end
end