package ctypes

  1. Overview
  2. Docs
Combinators for binding to C libraries without writing any C

Install

Dune Dependency

Authors

Maintainers

Sources

0.23.0.tar.gz
sha256=cae47d815b27dd4c824a007f1145856044542fe2588d23a443ef4eefec360bf1
md5=b1af973ec9cf7867a63714e92df82f2a

doc/ctypes.stubs/Cstubs_structs/module-type-TYPE/index.html

Module type Cstubs_structs.TYPESource

include Ctypes_types.TYPE

Values representing C types

Sourcetype 'a typ

The type of values representing C types. There are two types associated with each typ value: the C type used to store and pass values, and the corresponding OCaml type. The type parameter indicates the OCaml type, so a value of type t typ is used to read and write OCaml values of type t. There are various uses of typ values, including

  • constructing function types for binding native functions using Foreign.foreign
  • constructing pointers for reading and writing locations in C-managed storage using ptr
  • describing the fields of structured types built with structure and union.

The void type

Sourceval void : unit typ

Value representing the C void type. Void values appear in OCaml as the unit type, so using void in an argument or result type specification produces a function which accepts or returns unit.

Dereferencing a pointer to void is an error, as in C, and will raise IncompleteType.

Scalar types

The scalar types consist of the Arithmetic types and the Pointer types.

Arithmetic types

The arithmetic types consist of the signed and unsigned integer types (including character types) and the floating types. There are values representing both exact-width integer types (of 8, 16, 32 and 64 bits) and types whose size depend on the platform (signed and unsigned short, int, long, long long).

Sourceval char : char typ

Value representing the C type char.

Signed integer types
Sourceval schar : int typ

Value representing the C type signed char.

Sourceval short : int typ

Value representing the C type (signed) short.

Sourceval int : int typ

Value representing the C type (signed) int.

Value representing the C type (signed) long.

Value representing the C type (signed) long long.

Sourceval nativeint : nativeint typ

Value representing the C type (signed) int.

Sourceval int8_t : int typ

Value representing an 8-bit signed integer C type.

Sourceval int16_t : int typ

Value representing a 16-bit signed integer C type.

Sourceval int32_t : int32 typ

Value representing a 32-bit signed integer C type.

Sourceval int64_t : int64 typ

Value representing a 64-bit signed integer C type.

Sourceval intptr_t : Intptr.t typ

Value representing the C type intptr_t.

Sourceval ptrdiff_t : Ptrdiff.t typ

Value representing the C type ptrdiff_t.

Sourceval camlint : int typ

Value representing an integer type with the same storage requirements as an OCaml int.

Unsigned integer types

Value representing the C type unsigned char.

Sourceval bool : bool typ

Value representing the C type bool.

Value representing an 8-bit unsigned integer C type.

Sourceval uint16_t : Unsigned.uint16 typ

Value representing a 16-bit unsigned integer C type.

Sourceval uint32_t : Unsigned.uint32 typ

Value representing a 32-bit unsigned integer C type.

Sourceval uint64_t : Unsigned.uint64 typ

Value representing a 64-bit unsigned integer C type.

Value representing the C type size_t, an alias for one of the unsigned integer types. The actual size and alignment requirements for size_t vary between platforms.

Value representing the C type unsigned short.

Value representing the C type int.

Value representing the C type unsigned int.

Value representing the C type unsigned long.

Value representing the C type unsigned long long.

Sourceval uintptr_t : Uintptr.t typ

Value representing the C type uintptr_t.

Floating types
Sourceval float : float typ

Value representing the C single-precision float type.

Sourceval double : float typ

Value representing the C type double.

Sourceval ldouble : LDouble.t typ

Value representing the C type long double.

Complex types
Sourceval complex32 : Complex.t typ

Value representing the C99 single-precision float complex type.

Sourceval complex64 : Complex.t typ

Value representing the C99 double-precision double complex type.

Sourceval complexld : ComplexL.t typ

Value representing the C99 long-double-precision long double complex type.

Pointer types
C-compatible pointers
Sourceval ptr : 'a typ -> 'a Ctypes_static.ptr typ

Construct a pointer type from an existing type (called the reference type).

Sourceval ptr_opt : 'a typ -> 'a Ctypes_static.ptr option typ

Construct a pointer type from an existing type (called the reference type). This behaves like ptr, except that null pointers appear in OCaml as None.

Sourceval string : string typ

A high-level representation of the string type.

On the C side this behaves like char *; on the OCaml side values read and written using string are simply native OCaml strings.

To avoid problems with the garbage collector, values passed using string are copied into immovable C-managed storage before being passed to C.

The string type representation is suitable for use in function argument types such as the following:

string @-> returning int

where the lifetime of the C-managed storage does not need to extend beyond the duration of the function call. However, it is not suitable for use in struct or union fields

field s "x" string

because it does not provide a way to manage the lifetime of the C-managed storage.

Sourceval string_opt : string option typ

A high-level representation of the string type. This behaves like string, except that null pointers appear in OCaml as None.

OCaml pointers
Sourceval ocaml_string : string Ctypes_static.ocaml typ

Value representing the directly mapped storage of an OCaml string.

Sourceval ocaml_bytes : bytes Ctypes_static.ocaml typ

Value representing the directly mapped storage of an OCaml byte array.

Array types

C array types
Sourceval array : int -> 'a typ -> 'a Ctypes_static.carray typ

Construct a sized array type from a length and an existing type (called the element type).

Bigarray types
Sourceval bigarray : < element : 'a ; layout : Bigarray_compat.c_layout ; ba_repr : 'b ; dims : 'dims ; bigarray : 'bigarray ; carray : _ > Ctypes_static.bigarray_class -> 'dims -> ('a, 'b) Bigarray_compat.kind -> 'bigarray typ

Construct a sized C-layout bigarray type representation from a bigarray class, the dimensions, and the Bigarray_compat.kind.

Sourceval fortran_bigarray : < element : 'a ; layout : Bigarray_compat.fortran_layout ; ba_repr : 'b ; dims : 'dims ; bigarray : 'bigarray ; carray : _ > Ctypes_static.bigarray_class -> 'dims -> ('a, 'b) Bigarray_compat.kind -> 'bigarray typ

Construct a sized Fortran-layout bigarray type representation from a bigarray class, the dimensions, and the Bigarray_compat.kind.

Sourceval typ_of_bigarray_kind : ('a, 'b) Bigarray_compat.kind -> 'a typ

typ_of_bigarray_kind k is the type corresponding to the Bigarray kind k.

Struct and union types

Sourcetype ('a, 't) field
Sourceval structure : string -> 's Ctypes_static.structure typ

Construct a new structure type. The type value returned is incomplete and can be updated using field until it is passed to seal, at which point the set of fields is fixed.

The type ('_s structure typ) of the expression returned by the call structure tag includes a weak type variable, which can be explicitly instantiated to ensure that the OCaml values representing different C structure types have incompatible types. Typical usage is as follows:

type tagname

let tagname : tagname structure typ = structure "tagname"

Sourceval union : string -> 's Ctypes_static.union typ

Construct a new union type. This behaves analogously to structure; fields are added with field.

Sourceval field : 't typ -> string -> 'a typ -> ('a, ('s, [< `Struct | `Union ]) Ctypes_static.structured as 't) field

field ty label ty' adds a field of type ty' with label label to the structure or union type ty and returns a field value that can be used to read and write the field in structure or union instances (e.g. using getf and setf).

Attempting to add a field to a union type that has been sealed with seal is an error, and will raise ModifyingSealedType.

Sourceval seal : (_, [< `Struct | `Union ]) Ctypes_static.structured typ -> unit

seal t completes the struct or union type t so that no further fields can be added. Struct and union types must be sealed before they can be used in a way that involves their size or alignment; see the documentation for IncompleteType for further details.

View types

Sourceval view : ?format_typ:((Format.formatter -> unit) -> Format.formatter -> unit) -> ?format:(Format.formatter -> 'b -> unit) -> read:('a -> 'b) -> write:('b -> 'a) -> 'a typ -> 'b typ

view ~read:r ~write:w t creates a C type representation t' which behaves like t except that values read using t' are subsequently transformed using the function r and values written using t' are first transformed using the function w.

For example, given suitable definitions of string_of_char_ptr and char_ptr_of_string, the type representation

view ~read:string_of_char_ptr ~write:char_ptr_of_string (ptr char)

can be used to pass OCaml strings directly to and from bound C functions, or to read and write string members in structs and arrays. (In fact, the string type representation is defined in exactly this way.)

The optional argument format_typ is used by the Ctypes.format_typ and string_of_typ functions to print the type at the top level and elsewhere. If format_typ is not supplied the printer for t is used instead.

The optional argument format is used by the Ctypes.format and string_of functions to print the values. If format_val is not supplied the printer for t is used instead.

Sourceval typedef : 'a typ -> string -> 'a typ

typedef t name creates a C type representation t' which is equivalent to t except its name is printed as name.

This is useful when generating C stubs involving "anonymous" types, for example: typedef struct { int f } typedef_name;

Abstract types

Sourceval abstract : name:string -> size:int -> alignment:int -> 'a Ctypes_static.abstract typ

Create an abstract type specification from the size and alignment requirements for the type.

Injection of concrete types

Sourceval lift_typ : 'a Ctypes_static.typ -> 'a typ

lift_typ t turns a concrete type representation into an abstract type representation.

For example, retrieving struct layout from C involves working with an abstract representation of types which do not support operations such as sizeof. The lift_typ function makes it possible to use concrete type representations wherever such abstract type representations are needed.

Function types

Abstract interface to C function type descriptions

Sourcetype 'a fn = 'a Ctypes_static.fn

The type of values representing C function types. A value of type t fn can be used to bind to C functions and to describe type of OCaml functions passed to C.

Sourceval (@->) : 'a typ -> 'b fn -> ('a -> 'b) fn

Construct a function type from a type and an existing function type. This corresponds to prepending a parameter to a C function parameter list. For example,

int @-> ptr void @-> returning float

describes a function type that accepts two arguments -- an integer and a pointer to void -- and returns a float.

Sourceval returning : 'a typ -> 'a fn

Give the return type of a C function. Note that returning is intended to be used together with (@->); see the documentation for (@->) for an example.

Sourcetype 'a static_funptr = 'a Ctypes_static.static_funptr

Function pointer types

The type of values representing C function pointer types.

Sourceval static_funptr : 'a fn -> 'a Ctypes_static.static_funptr typ

Construct a function pointer type from an existing function type (called the reference type).

Sourceval const : 'a typ -> 'a typ
Sourcetype 'a const
Sourceval constant : string -> 'a typ -> 'a const
Sourceval enum : string -> ?typedef:bool -> ?unexpected:(int64 -> 'a) -> ('a * int64 const) list -> 'a typ
OCaml

Innovation. Community. Security.