module GRCommons::DefineMethods

This module provides a way to add FFI methods to the GRBase and GR3Base modules.

Private Instance Methods

define_ffi_methods(ffi_module, prefix: '', default_type: :double) click to toggle source
# File lib/gr_commons/define_methods.rb, line 8
def define_ffi_methods(ffi_module, prefix: '', default_type: :double)
  ffi_module.ffi_methods.each do |method|
    # Use delete_prefix (Ruby >= 2.5)
    method_name = method.to_s.sub(/^#{prefix}/, '')

    # FIXME: Refactoring required

    define_method(method_name) do |*args|
      args.map! do |arg|
        case arg
        when Array
          GRCommonUtils.public_send(default_type, arg)
        when ->(x) { defined?(Numo::NArray) && x.is_a?(Numo::NArray) }
          GRCommonUtils.public_send(default_type, arg)
        else
          arg
        end
      end
      ffi_module.public_send(method, *args)
    end
  end
end