shared_library_type Derived Type

type, public :: shared_library_type

Handle on a loaded shared library. (Specification)


Type-Bound Procedures

procedure, public :: close => lib_close

Unload the library

  • private subroutine lib_close(self, err)

    Unload the library. Procedure pointers obtained from it become invalid. Closing an unloaded handle is a no-op.

    Arguments

    Type IntentOptional Attributes Name
    class(shared_library_type), intent(inout) :: self

    Handle on the library

    type(state_type), intent(out), optional :: err

    Optional state return flag. On error, if not requested, the code stops

procedure, public :: data => lib_data

Address of an exported variable

  • private function lib_data(self, name, err) result(ptr)

    Address of an exported variable, to be used with c_f_pointer.

    Arguments

    Type IntentOptional Attributes Name
    class(shared_library_type), intent(in) :: self

    Handle on a loaded library

    character(len=*), intent(in) :: name

    Name of the exported variable

    type(state_type), intent(out), optional :: err

    Optional state return flag. On error, if not requested, the code stops

    Return Value type(c_ptr)

    Address of the variable, c_null_ptr on error

procedure, public :: filename => lib_filename

Path of the file that was actually loaded

  • private pure function lib_filename(self) result(fname)

    Path of the file that was actually loaded ("" if none).

    Arguments

    Type IntentOptional Attributes Name
    class(shared_library_type), intent(in) :: self

    Handle on a library

    Return Value character(len=:), allocatable

    Path of the loaded file

procedure, public :: has_symbol => lib_has_symbol

Test whether a symbol is exported

  • private function lib_has_symbol(self, name) result(found)

    .true. if the library is loaded and exports name.

    Arguments

    Type IntentOptional Attributes Name
    class(shared_library_type), intent(in) :: self

    Handle on a library

    character(len=*), intent(in) :: name

    Name of the symbol to look for

    Return Value logical

    Whether the symbol is exported

procedure, public :: is_loaded => lib_is_loaded

Test whether a library is currently loaded

  • private pure function lib_is_loaded(self) result(loaded)

    Whether a library is currently loaded into this handle.

    Arguments

    Type IntentOptional Attributes Name
    class(shared_library_type), intent(in) :: self

    Handle on a library

    Return Value logical

    Whether a library is loaded

procedure, public :: open => lib_open

Load a shared library into this handle

  • private subroutine lib_open(self, name, path, global, err)

    Load a shared library into an existing handle.

    Arguments

    Type IntentOptional Attributes Name
    class(shared_library_type), intent(inout) :: self

    Handle on the library. Must not be loaded already

    character(len=*), intent(in) :: name

    Library name, file name or path

    character(len=*), intent(in), optional :: path

    Optional directory to search in, instead of the OS search rules

    logical, intent(in), optional :: global

    Optional RTLD_GLOBAL flag. Ignored on Windows

    type(state_type), intent(out), optional :: err

    Optional state return flag. On error, if not requested, the code stops

procedure, public :: symbol => lib_symbol

Address of an exported procedure

  • private function lib_symbol(self, name, err) result(fptr)

    Address of an exported procedure, to be used with c_f_procpointer.

    Arguments

    Type IntentOptional Attributes Name
    class(shared_library_type), intent(in) :: self

    Handle on a loaded library

    character(len=*), intent(in) :: name

    Name of the exported procedure

    type(state_type), intent(out), optional :: err

    Optional state return flag. On error, if not requested, the code stops

    Return Value type(c_funptr)

    Address of the procedure, c_null_funptr on error

Source Code

    type :: shared_library_type
        private
        !> OS handle returned by `dlopen` / `LoadLibraryExW`
        type(c_ptr) :: handle = c_null_ptr
        !> Path of the file that was actually loaded
        character(len=:), allocatable :: path
    contains
        !> Load a shared library into this handle
        procedure :: open       => lib_open
        !> Address of an exported procedure
        procedure :: symbol     => lib_symbol
        !> Address of an exported variable
        procedure :: data       => lib_data
        !> Test whether a symbol is exported
        procedure :: has_symbol => lib_has_symbol
        !> Unload the library
        procedure :: close      => lib_close
        !> Test whether a library is currently loaded
        procedure :: is_loaded  => lib_is_loaded
        !> Path of the file that was actually loaded
        procedure :: filename   => lib_filename
    end type shared_library_type