File: container_class_methods.rb

package info (click to toggle)
ruby-gir-ffi 0.16.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 924 kB
  • sloc: ruby: 6,849; makefile: 4
file content (38 lines) | stat: -rw-r--r-- 851 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true

module GLib
  # Common methods for container classes: Array, PtrArray, List, SList and
  # HashTable.
  module ContainerClassMethods
    def wrap(typespec, ptr)
      # HACK: wrap and from are almost the same!
      ptr = case ptr
            when nil
              nil
            when FFI::Pointer
              ptr
            when self, GirFFI::BoxedBase
              ptr.to_ptr
            end

      super(ptr).tap do |container|
        container&.reset_typespec typespec
      end
    end

    def from(typespec = :void, obj)
      case obj
      when nil
        nil
      when FFI::Pointer
        wrap typespec, obj
      when self
        obj.reset_typespec typespec
      when GirFFI::BoxedBase
        wrap typespec, obj.to_ptr
      else
        from_enumerable typespec, obj
      end
    end
  end
end