Module: XZ::FiddleHelper

Included in:
LibLZMA
Defined in:
lib/xz/fiddle_helper.rb

Overview

This is an internal API not meant for users of ruby-xz. This mixin modules defines some helper functions on top of Fiddle’s functionality.

Instance Method Summary collapse

Instance Method Details

#dlloadanyof(*names) ⇒ Object

Try loading any of the given names as a shared object. Raises Fiddle::DLError if none can be opened.

Raises:

  • (Fiddle::DLError)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/xz/fiddle_helper.rb', line 74

def dlloadanyof(*names)
  names.each do |name|
    begin
      dlload(name)
    rescue Fiddle::DLError
      # Continue with next one
    else
      # Success
      return name
    end
  end

  raise Fiddle::DLError, "Failed to open any of these shared object files: #{names.join(', ')}"
end

#enum(*args) ⇒ Object

Define constants that have numeric constants assigned as if it was a C enum definition. You can specificy values explicitely or rely on the implicit incrementation; the first implicit value is zero.

Example:

enum :FOO, :BAR, 5, :BAZ

This defines a constant FOO with value 0, BAR with value 5, BAZ with value 6.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/xz/fiddle_helper.rb', line 46

def enum(*args)
  @next_enum_val = 0 # First value of an enum is 0 in C

  args.each_cons(2) do |val1, val2|
    next if val1.respond_to?(:to_int)

    if val2.respond_to?(:to_int)
      const_set(val1, val2.to_int)
      @next_enum_val = val2.to_int + 1
    else
      const_set(val1, @next_enum_val)
      @next_enum_val += 1
    end
  end

  # Cater for the last element in case it is not an explicit
  # value that has already been assigned above.
  unless args.last.respond_to?(:to_int)
    const_set(args.last, @next_enum_val)
  end

  @next_enum_val = 0
  nil
end