Class: Google::Protobuf::DescriptorPool

Inherits:
Object
  • Object
show all
Defined in:
lib/google/protobuf/descriptor_dsl.rb,
lib/google/protobuf/ffi/descriptor_pool.rb

Overview

Re-open the class (the rest of the class is implemented in C)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDescriptorPool

Returns a new instance of DescriptorPool.



27
28
29
30
31
32
33
34
# File 'lib/google/protobuf/ffi/descriptor_pool.rb', line 27

def initialize
  @descriptor_pool = ::FFI::AutoPointer.new(Google::Protobuf::FFI.create_descriptor_pool, Google::Protobuf::FFI.method(:free_descriptor_pool))
  @descriptor_class_by_def = {}

  # Should always be the last expression of the initializer to avoid
  # leaking references to this object before construction is complete.
  Google::Protobuf::OBJECT_CACHE.try_add @descriptor_pool.address, self
end

Instance Attribute Details

#descriptor_class_by_defObject

Returns the value of attribute descriptor_class_by_def.



25
26
27
# File 'lib/google/protobuf/ffi/descriptor_pool.rb', line 25

def descriptor_class_by_def
  @descriptor_class_by_def
end

#descriptor_poolObject (readonly)

Returns the value of attribute descriptor_pool.



24
25
26
# File 'lib/google/protobuf/ffi/descriptor_pool.rb', line 24

def descriptor_pool
  @descriptor_pool
end

Class Method Details

.generated_poolObject



60
61
62
# File 'lib/google/protobuf/ffi/descriptor_pool.rb', line 60

def self.generated_pool
  @@generated_pool ||= DescriptorPool.new
end

Instance Method Details

#add_serialized_file(file_contents) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/google/protobuf/ffi/descriptor_pool.rb', line 36

def add_serialized_file(file_contents)
  # Allocate memory sized to file_contents
  memBuf = ::FFI::MemoryPointer.new(:char, file_contents.bytesize)
  # Insert the data
  memBuf.put_bytes(0, file_contents)
  temporary_arena = Google::Protobuf::FFI.create_arena
  file_descriptor_proto = Google::Protobuf::FFI.parse memBuf, file_contents.bytesize, temporary_arena
  raise ArgumentError.new("Unable to parse FileDescriptorProto") if file_descriptor_proto.null?

  status = Google::Protobuf::FFI::Status.new
  file_descriptor = Google::Protobuf::FFI.add_serialized_file @descriptor_pool, file_descriptor_proto, status
  if file_descriptor.null?
    raise TypeError.new("Unable to build file to DescriptorPool: #{Google::Protobuf::FFI.error_message(status)}")
  else
    @descriptor_class_by_def[file_descriptor.address] = FileDescriptor.new file_descriptor, self
  end
end

#build(&block) ⇒ Object



458
459
460
461
462
# File 'lib/google/protobuf/descriptor_dsl.rb', line 458

def build(&block)
  builder = Internal::Builder.new(self)
  builder.instance_eval(&block)
  builder.build
end

#lookup(name) ⇒ Object



54
55
56
57
58
# File 'lib/google/protobuf/ffi/descriptor_pool.rb', line 54

def lookup name
  Google::Protobuf::FFI.lookup_msg(@descriptor_pool, name) ||
    Google::Protobuf::FFI.lookup_enum(@descriptor_pool, name) ||
    Google::Protobuf::FFI.lookup_extension(@descriptor_pool, name)
end