Module NiceFFI::Library
In: lib/nice-ffi/library.rb

A module to be used in place of FFI::Library. It acts mostly like FFI::Library, but with some nice extra features and conveniences to make life easier:

  • load_library method to help find and load libraries from common (or custom) places for the current OS. Use it instead of ffi_lib.
  • attach_function accepts TypedPointers as return type, in which case it wraps the return value of the bound function in the TypedPointer‘s type.
  • Shorthand aliases to improve code readability:

Methods

Public Class methods

Public Instance methods

Try to find and load a library (e.g. "SDL_ttf") into an FFI wrapper module (e.g. SDL::TTF). This method searches in different locations depending on your OS. See PathSet.

Returns the path to the library that was loaded.

Raises LoadError if it could not find or load the library.

optfunc( *args )

Alias for optional_function

Calls the given block, but rescues and prints a warning if FFI::NotFoundError is raised. If warn_message is nil, the error message is printed instead.

This is intended to be used around attach_function for cases where the function may not exist (e.g. because the user has an old version of the library) and the library should continue loading anyway (with the function missing).

Example:

  module Foo
    extend NiceFFI::Library

    load_library( "libfoo" )

    optional( "Warning: Your libfoo doesn't have opt_func()" ) do
      attach_function :opt_func, [], :int
    end
  end

[Validate]