Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to deal with structure as out argument? #16

Open
cedlemo opened this issue Feb 10, 2018 · 0 comments
Open

How to deal with structure as out argument? #16

cedlemo opened this issue Feb 10, 2018 · 0 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@cedlemo
Copy link
Owner

cedlemo commented Feb 10, 2018

An example is the Time_val.t structure.

open Ctypes
open Foreign

type t
let t_typ : t structure typ = structure "Time_val"

let f_tv_sec = field t_typ "tv_sec" (int64_t)
let f_tv_usec = field t_typ "tv_usec" (int64_t)
let _ = seal t_typ

The two functions in the Time_val module:

let to_iso8601 =
  foreign "g_time_val_to_iso8601" (ptr t_typ @-> returning (string_opt))

let from_iso8601 iso_date =
  let time__ptr = allocate t_typ (make t_typ) in
  let from_iso8601_raw =
    foreign "g_time_val_from_iso8601" (string @-> ptr (t_typ) @-> returning bool)
  in
  let ret = from_iso8601_raw iso_date time__ptr in
  let time_ = !@ time__ptr in
  (ret, time_)

And the function in the Core module:

let get_current_time =
  foreign "g_get_current_time" (ptr Time_val.t_typ @-> returning (void))

In the tests:

let test_time_val_from_iso8601 test_ctxt =
  let iso8601 = "2018-02-07T10:39:38Z" in
  match GLib.Time_val.from_iso8601 iso8601 with
  | (false, _) -> assert_equal ~msg:"No time val" true false
  | (true, tv) -> let dt = GLib.Date_time.create_from_timeval_utc (Ctypes.addr tv) in
    let (y,m,d) = GLib.Date_time.get_ymd dt in
    let _ = assert_equal_int32 (Int32.of_int 2018) y in
    let _ = assert_equal_int32 (Int32.of_int 2) m in
    assert_equal_int32 (Int32.of_int 07) d

let test_core_get_current_time_and_to_iso8601 test_ctxt =
  let tv = Ctypes.make GLib.Time_val.t_typ in
  let tv_ptr = Ctypes.addr tv in
  let _ = GLib.Core.get_current_time tv_ptr in
  match GLib.Time_val.to_iso8601 tv_ptr with
  | None -> assert_equal ~msg:"No iso8601 from Timeval" true false
  | Some iso8601 -> let now = GLib.Date_time.create_now_local () in
    let (year, month, day) = GLib.Date_time.get_ymd now in
    let dt = GLib.Date_time.create_from_timeval_utc tv_ptr in
    let (y,m,d) = GLib.Date_time.get_ymd dt in
    let _ = assert_equal_int32 year y in
    let _ = assert_equal_int32 month m in
    assert_equal_int32 day d

The construction of Timeval.from_iso8601 is not clear because, it make locally a structure that is used
as default value for during the allocation of a structure Time_val on the heap then the pointer is passed to the function in order to fill the structure in the heap, then I get the value of the pointer (ie the structure)

Question: why not return a pointer ? Is it possible ?

@cedlemo cedlemo added enhancement New feature or request question Further information is requested labels Feb 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant