【问题标题】:OCaml ctypes idiomatic way to deal with out parametersOCaml ctypes 处理输出参数的惯用方式
【发布时间】:2017-05-09 03:40:41
【问题描述】:

我正在为一些使用 out-parameter 习惯用法并返回错误代码的 C 函数编写 OCaml 包装器。我一直在使用Ctypes.allocate_n 在OCaml 端分配一个C 数组来包装它们。然后将内容复制到 OCaml 类型中。

我觉得我正在解决Ctypes 或其他模块已经以另一种方式解决的问题,这是一个示例。

gethostname(2) 具有以下类型:

int gethostname(char *name, size_t len);

这里是 out_parameter.mli 用于包装的 gethostname 函数。

val gethostname : int -> [> `Ok of string | `Error of int];;

这是实现

open Core.Std;;
let (@->) = Ctypes.(@->);;
let returning = Ctypes.returning;;
open Foreign;;

let gethostname size =
  let size' = Unsigned.Size_t.of_int size in
  let c_gethostname =
    foreign "gethostname" (Ctypes.ptr Ctypes.char @-> Ctypes.size_t @-> returning Ctypes.int) in
  let buf = Ctypes.allocate_n Ctypes.char ~count:size in
  let err = c_gethostname buf size' in
  match err with
  | 0 -> (
    `Ok (Ctypes.string_from_ptr buf ~length:size)
    )
  | _ -> `Error err;;

let main () =
  Printf.printf "%s\n" (match gethostname 1000 with
  | `Ok hostname -> hostname
  | `Error _ -> "error getting hostname");;

let () = main ();;

为了完整起见,我用这个命令编译了out_parameter.native

$ corebuild -pkg ctypes.foreign out_parameter.native

代码确实有效并返回主机名,去掉了尾随的空字节。

$ ./out_parameter.native
MY-HOSTNAME

$ ./out_parameter.native | sed -e 's/\x0/@/g'
MY-HOSTNAME

【问题讨论】:

    标签: ocaml


    【解决方案1】:

    看起来您的代码可以正常工作并且是惯用的。有两种表示错误的方法:

    • 您可以抛出异常。
    • 您可以使用结果类型。如果你这样做,我建议使用“新”内置 result 类型(已在 result 包中为旧版本的 OCaml 反向移植)。

    一般而言,您负责编写支持该错误约定的代码(代码中的match err 部分)。通常可以编写几个组合器来减少样板。

    但是,如果您使用Cstubs(代码生成),ctypes 提供了一些快捷方式:

    • errno_policy 可以实现 libc 的 errno 约定。
    • concurrency_policy 可以调整 C 代码,使其与给定的并发模型(lwt 等)相对应。这与错误约定无关,但有些相关。

    如果您坚持使用 Foreign(这使您的构建系统比 Cstubs 更简单),那么我建议您手动完成,或者如果您有很多类似的功能,请提取几个组合器。

    【讨论】:

      猜你喜欢
      • 2013-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 2021-02-26
      • 2021-11-24
      • 2017-09-23
      • 1970-01-01
      相关资源
      最近更新 更多