Module: FFI

Defined in:
lib/ffi/io.rb,
lib/ffi/enum.rb,
ext/ffi_c/ffi.c,
ext/ffi_c/ffi.c,
lib/ffi/union.rb,
lib/ffi/types.rb,
lib/ffi/errno.rb,
lib/ffi/struct.rb,
lib/ffi/pointer.rb,
lib/ffi/library.rb,
lib/ffi/platform.rb,
lib/ffi/variadic.rb,
lib/ffi/autopointer.rb,
lib/ffi/managedstruct.rb,
lib/ffi/tools/generator.rb,
lib/ffi/tools/types_generator.rb,
lib/ffi/tools/const_generator.rb,
lib/ffi/struct_layout_builder.rb,
lib/ffi/tools/struct_generator.rb

Overview

Copyright (C) 2008-2010 Wayne Meissner

All rights reserved.

This file is part of ruby-ffi.

This code is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3 only, as published by the Free Software Foundation.

This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License version 3 for more details.

You should have received a copy of the GNU Lesser General Public License version 3 along with this work. If not, see <www.gnu.org/licenses/>.

Defined Under Namespace

Modules: DataConverter, IO, LastError, Library, NativeType, Platform Classes: AbstractMemory, ArrayType, AutoPointer, Buffer, ConstGenerator, DynamicLibrary, Enum, Enums, Function, FunctionType, Generator, ManagedStruct, MemoryPointer, NotFoundError, NullPointerError, PlatformError, Pointer, StrPtrConverter, Struct, StructByReference, StructByValue, StructGenerator, StructLayout, StructLayoutBuilder, Type, TypesGenerator, Union, VariadicInvoker

Constant Summary

TypeDefs =

FFI::TypeDefs

Document-constant
SizeTypes =
sizeMap = rb_hash_new()
CURRENT_PROCESS =
USE_THIS_PROCESS_AS_LIBRARY = Object.new
CallbackInfo =
rbffi_FunctionTypeClass
FunctionInfo =
rbffi_FunctionTypeClass
NativeLibrary =

backwards compat library

LibraryClass

Class Method Summary (collapse)

Class Method Details

+ (Type) add_typedef(old, add)

Add a definition type to type definitions.

Parameters:

Returns:



32
33
34
# File 'lib/ffi/types.rb', line 32

def self.add_typedef(old, add)
  typedef old, add
end

+ (Numeric) errno

Returns:

  • (Numeric)

See Also:



24
25
26
# File 'lib/ffi/errno.rb', line 24

def self.errno
  FFI::LastError.error
end

+ (nil) errno=(error)

Parameters:

  • error (Numeric)

Returns:

  • (nil)

See Also:



30
31
32
# File 'lib/ffi/errno.rb', line 30

def self.errno=(error)
  FFI::LastError.error = error
end

+ (Type) find_type(name, type_map = nil)

Find a type in type_map (TypeDefs, by default) from a type objet, a type name (symbol). If name is a DataConverter, a new FFI::Type::Mapped is created.

Parameters:

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ffi/types.rb', line 43

def self.find_type(name, type_map = nil)
  if name.is_a?(Type)
    name

  elsif type_map && type_map.has_key?(name)
    type_map[name]

  elsif TypeDefs.has_key?(name)
    TypeDefs[name]

  elsif name.is_a?(DataConverter)
    (type_map || TypeDefs)[name] = Type::Mapped.new(name)
  
  else
    raise TypeError, "unable to resolve type '#{name}'"
  end
end

+ (String) map_library_name(lib)

Transform a generic library name to a platform library name

Examples:

# Linux
FFI.map_library_name 'c'     # -> "libc.so.6"
FFI.map_library_name 'jpeg'  # -> "libjpeg.so"
# Windows
FFI.map_library_name 'c'     # -> "msvcrt.dll"
FFI.map_library_name 'jpeg'  # -> "jpeg.dll"

Parameters:

  • lib (#to_s)

    library name

Returns:

  • (String)

    library name formatted for current platform



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ffi/library.rb', line 34

def self.map_library_name(lib)
  # Mangle the library name to reflect the native library naming conventions
  lib = lib.to_s unless lib.kind_of?(String)
  lib = Library::LIBC if lib == 'c'

  if lib && File.basename(lib) == lib
    lib = Platform::LIBPREFIX + lib unless lib =~ /^#{Platform::LIBPREFIX}/
    r = Platform::IS_GNU ? "\\.so($|\\.[1234567890]+)" : "\\.#{Platform::LIBSUFFIX}$"
    lib += ".#{Platform::LIBSUFFIX}" unless lib =~ /#{r}/
  end

  lib
end

+ (Numeric) type_size(type)

Get type size, in bytes.

Parameters:

  • type

    type is an instance of class accepted by find_type

Returns:

  • (Numeric)


159
160
161
# File 'lib/ffi/types.rb', line 159

def self.type_size(type)
  find_type(type).size
end

+ (Type) typedef(old, add)

Add a definition type to type definitions.

Parameters:

Returns:



27
28
29
# File 'lib/ffi/types.rb', line 27

def self.typedef(old, add)
  TypeDefs[add] = self.find_type(old)
end