Skip to content

Interface Declarations

Interoperable Types

YAMLHandler

This object is an interoperable abstract type to access YAML configuration object. The configuration object object is the "root" of the YAML file. allowing access to YAML::Node objects. We never acces the YAML::Node object directly in this interface because there is no need to do so.

  • c_obj: opaque pointer to C++ YAML::Node context manager
  type YAMLHandler
    type(c_ptr) :: ptr
  end type YAMLHandler

YAMLMap

Value-key pairs (or hash mappings) of YAML entries.

  • ptr: opaque pointer to C Map struct
  • idx: array position number of this map instance; index starts at zero.
  • labels: list of keys available in a map instance
  type YAMLMap
    type(c_ptr) :: ptr
    integer :: idx
    type(StringsType), allocatable :: labels(:)
  contains
    procedure :: destroy => map_destroy
    procedure :: value_int => map_value_int
    procedure :: value_str => map_value_str
    procedure :: value_double => map_value_double
    procedure :: value_int_1d => map_value_int_1d
    procedure :: value_double_1d => map_value_double_1d
    procedure :: value_double_2d => map_value_double_2d
    procedure :: value_int_2d => map_value_integer_2d
    procedure :: value_sequence => map_value_sequence
    procedure :: value_map => map_value_map
  end type YAMLMap

YAMLField

Type for retriving any terminating field in the YAML file. Variable c_obj is a generic type void*,and the c_type_enum is used for casting the c_obj void* to the correct type.

  • c_obj: opaque pointer to C++ YAML::Node context manager
  • m: matrix row size; used exclusively when c_obj is a double** or int**
  • n: matrix column size
  • c_type_enum: Enumeration corresponding data type passed between the C wrapper and Fortran
  type, bind(c) :: YAMLField
    type(c_ptr) :: c_obj = c_null_ptr  !
    integer(c_int) :: m = -9999
    integer(c_int) :: n = -9999
    integer(c_int) :: c_type_enum
  end type YAMLField

YAMLSequence

A YAMLSequence is a list of YAMLMap under the hood.

  • ptr: opaque pointer to C Sequence struct
  • size: sequence size; must be positive
  type YAMLSequence
    type(c_ptr) :: ptr
    integer :: size
  contains
    procedure :: destroy => sequence_destroy
    procedure :: element => sequence_get_element
  end type YAMLSequence

YAMLElement

Value-key pairs (or hash mappings) of YAML entries.

  • isValid: boolean flag informing users the element is valid and initialized.
  type, extends(YAMLMap) :: YAMLElement  ! this is a map / struct list
    logical :: isValid = .false.
  contains
    procedure :: destroy => element_destroy
  end type YAMLElement

Public Functions / Subroutines

yaml_open_file

Opens a YAML file and return a YAMLHander instance. Subroutine yaml_close_file must be called on each file opened with this function.

  • Returns: YAMLHandler object
  • fpath: Path to YAML file. Can be absolute or relative.
Source
    function yaml_open_file(fpath) result(handler)
      character(len=*), intent(in) :: fpath

      character(256), pointer :: fstr  ! @todo: dynamically allocated instead?
      type(c_ptr) :: cstr
      type(YAMLHandler) :: handler
      integer :: n
      integer :: i = 0

      n = len(fpath) + 1

      allocate(fstr)

      do i = 1,n - 1
        fstr(i:i) = fpath(i:i)
      end do

      fstr(n:n) = C_NULL_CHAR
      cstr = C_LOC(fstr)

      handler%ptr = c_yaml_load_file(cstr)

      deallocate(fstr)
      return
    end function yaml_open_file

yaml_start_from_map

Return a YAML sequence. A sequnce is an array or list. Opens a YAML file and return a YAMLHander instance. Subroutine yaml_close_file must be called on each file opened with this subroutine.

  • Returns: YAMLMap
  • key: Path to YAML file. Can be absolute or relative.
Source
    function yaml_start_from_map(handler, key) result(mapper)
      type(YAMLHandler), intent(in) :: handler
      character(len=*), intent(in) :: key

      character(len=256), target :: arr
      type(c_ptr) :: ptr
      type(YAMLMap) :: mapper

      arr = trim(key)//C_NULL_CHAR
      ptr = C_LOC(arr)

      mapper%ptr = c_yaml_start_from_map(handler%ptr, ptr)
      mapper%idx = 0
      call p_set_map_labels(mapper)
      return
    end function yaml_start_from_map

yaml_start_from_sequence

Return a YAML sequence. A sequnce is an array or list. Opens a YAML file and return a YAMLHander instance. Subroutine yaml_close_file must be called on each file opened with this subroutine.

  • Returns [YAMLSequence}(#yamlsequence)]
  • key: lead starting sequence key
  • handler: YAMLHandler object
Source
    function yaml_start_from_sequence(handler, key) result(seq)
      type(YAMLHandler), intent(in) :: handler
      character(len=*), intent(in) :: key

      character(len=256), target :: arr
      type(c_ptr) :: ptr
      type(YAMLSequence) :: seq

      arr = trim(key)//C_NULL_CHAR
      ptr = C_LOC(arr)

      seq%ptr = c_yaml_start_from_sequence(handler%ptr, ptr)
      seq%size = c_yaml_get_sequence_size(seq%ptr)
      return
    end function yaml_start_from_sequence

yaml_close_file

Closes the YAML file and deallocated any memory associated with the YAML instance.

Source
    subroutine yaml_close_file(handler)
      type(YAMLHandler), intent(inout) :: handler

      call c_yaml_destroy_domain(handler%ptr)
    end subroutine yaml_close_file