Module type Types.TYPE
include Ctypes_types.TYPE
type 'a typThe type of values representing C types. There are two types associated with each
typvalue: 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 typet typis used to read and write OCaml values of typet. There are various uses oftypvalues, including- constructing function types for binding native functions using
Foreign.foreign
- constructing pointers for reading and writing locations in C-managed storage using
ptr
- constructing function types for binding native functions using
The void type
val void : unit typValue 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).
val char : char typValue representing the C type
char.
Signed integer types
val schar : int typValue representing the C type
signed char.
val short : int typValue representing the C type (
signed)short.
val int : int typValue representing the C type (
signed)int.
val long : Signed.long typValue representing the C type (
signed)long.
val llong : Signed.llong typValue representing the C type (
signed)long long.
val nativeint : nativeint typValue representing the C type (
signed)int.
val int8_t : int typValue representing an 8-bit signed integer C type.
val int16_t : int typValue representing a 16-bit signed integer C type.
val int32_t : int32 typValue representing a 32-bit signed integer C type.
val int64_t : int64 typValue representing a 64-bit signed integer C type.
val camlint : int typValue representing an integer type with the same storage requirements as an OCaml
int.
Unsigned integer types
val uchar : Unsigned.uchar typValue representing the C type
unsigned char.
val bool : bool typValue representing the C type
bool.
val uint8_t : Unsigned.uint8 typValue representing an 8-bit unsigned integer C type.
val uint16_t : Unsigned.uint16 typValue representing a 16-bit unsigned integer C type.
val uint32_t : Unsigned.uint32 typValue representing a 32-bit unsigned integer C type.
val uint64_t : Unsigned.uint64 typValue representing a 64-bit unsigned integer C type.
val size_t : Unsigned.size_t typValue representing the C type
size_t, an alias for one of the unsigned integer types. The actual size and alignment requirements forsize_tvary between platforms.
val ushort : Unsigned.ushort typValue representing the C type
unsigned short.
val sint : Signed.sint typValue representing the C type
int.
val uint : Unsigned.uint typValue representing the C type
unsigned int.
val ulong : Unsigned.ulong typValue representing the C type
unsigned long.
val ullong : Unsigned.ullong typValue representing the C type
unsigned long long.
module Uintptr : Unsigned.SFloating types
val float : float typValue representing the C single-precision
floattype.
val double : float typValue representing the C type
double.
Complex types
val complex32 : Stdlib.Complex.t typValue representing the C99 single-precision
float complextype.
val complex64 : Stdlib.Complex.t typValue representing the C99 double-precision
double complextype.
val complexld : ComplexL.t typValue representing the C99 long-double-precision
long double complextype.
Pointer types
C-compatible pointers
val ptr : 'a typ -> 'a Ctypes_static.ptr typConstruct a pointer type from an existing type (called the reference type).
val ptr_opt : 'a typ -> 'a Ctypes_static.ptr option typConstruct a pointer type from an existing type (called the reference type). This behaves like
ptr, except that null pointers appear in OCaml asNone.
val string : string typA high-level representation of the string type.
On the C side this behaves like
char *; on the OCaml side values read and written usingstringare simply native OCaml strings.To avoid problems with the garbage collector, values passed using
stringare copied into immovable C-managed storage before being passed to C.When the memory is not owned by the C code, -- i.e. when creating or initializing a struct in OCaml before passing it to C -- then the
stringview isn't a good choice, because there's no way to manage the lifetime of the C copy of the generated OCaml string.
OCaml pointers
val ocaml_string : string Ctypes_static.ocaml typValue representing the directly mapped storage of an OCaml string.
val ocaml_bytes : Stdlib.Bytes.t Ctypes_static.ocaml typValue representing the directly mapped storage of an OCaml byte array.
Array types
C array types
val array : int -> 'a typ -> 'a Ctypes_static.carray typConstruct a sized array type from a length and an existing type (called the element type).
Bigarray types
val bigarray : < element : 'a; layout : Bigarray.c_layout; ba_repr : 'b; dims : 'dims; bigarray : 'bigarray; carray : _; > Ctypes_static.bigarray_class -> 'dims -> ('a, 'b) Bigarray.kind -> 'bigarray typConstruct a sized C-layout bigarray type representation from a bigarray class, the dimensions, and the
Bigarray.kind.
val fortran_bigarray : < element : 'a; layout : Bigarray.fortran_layout; ba_repr : 'b; dims : 'dims; bigarray : 'bigarray; carray : _; > Ctypes_static.bigarray_class -> 'dims -> ('a, 'b) Bigarray.kind -> 'bigarray typConstruct a sized Fortran-layout bigarray type representation from a bigarray class, the dimensions, and the
Bigarray.kind.
val typ_of_bigarray_kind : ('a, 'b) Bigarray.kind -> 'a typtyp_of_bigarray_kind kis the type corresponding to the Bigarray kindk.
Struct and union types
val structure : string -> 's Ctypes_static.structure typConstruct a new structure type. The type value returned is incomplete and can be updated using
fielduntil it is passed toseal, at which point the set of fields is fixed.The type (
'_s structure typ) of the expression returned by the callstructure tagincludes 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 tagnamelet tagname : tagname structure typ = structure "tagname"
val union : string -> 's Ctypes_static.union typConstruct a new union type. This behaves analogously to
structure; fields are added withfield.
val field : 't typ -> string -> 'a typ -> ('a, ('s, [< `Struct | `Union ]) Ctypes_static.structured as t) fieldfield ty label ty'adds a field of typety'with labellabelto the structure or union typetyand returns a field value that can be used to read and write the field in structure or union instances (e.g. usinggetfandsetf).Attempting to add a field to a union type that has been sealed with
sealis an error, and will raiseModifyingSealedType.
val seal : (_, [< `Struct | `Union ]) Ctypes_static.structured typ -> unitseal tcompletes the struct or union typetso 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 forIncompleteTypefor further details.
View types
val view : ?format_typ:((Stdlib.Format.formatter -> unit) -> Stdlib.Format.formatter -> unit) -> ?format:(Stdlib.Format.formatter -> 'b -> unit) -> read:('a -> 'b) -> write:('b -> 'a) -> 'a typ -> 'b typview ~read:r ~write:w tcreates a C type representationt'which behaves liketexcept that values read usingt'are subsequently transformed using the functionrand values written usingt'are first transformed using the functionw.For example, given suitable definitions of
string_of_char_ptrandchar_ptr_of_string, the type representationview ~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
stringtype representation is defined in exactly this way.)The optional argument
format_typis used by theCtypes.format_typandstring_of_typfunctions to print the type at the top level and elsewhere. Ifformat_typis not supplied the printer fortis used instead.The optional argument
formatis used by theCtypes.formatandstring_offunctions to print the values. Ifformat_valis not supplied the printer fortis used instead.
Abstract types
val abstract : name:string -> size:int -> alignment:int -> 'a Ctypes_static.abstract typCreate an abstract type specification from the size and alignment requirements for the type.
Injection of concrete types
val lift_typ : 'a Ctypes_static.typ -> 'a typlift_typ tturns 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. Thelift_typfunction makes it possible to use concrete type representations wherever such abstract type representations are needed.
Function types
type 'a fn= 'a Ctypes_static.fnThe type of values representing C function types. A value of type
t fncan be used to bind to C functions and to describe type of OCaml functions passed to C.
val (@->) : 'a typ -> 'b fn -> ('a -> 'b) fnConstruct 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 floatdescribes a function type that accepts two arguments -- an integer and a pointer to void -- and returns a float.
val returning : 'a typ -> 'a fnGive the return type of a C function. Note that
returningis intended to be used together with(@->); see the documentation for(@->)for an example.
type 'a static_funptr= 'a Ctypes_static.static_funptrFunction pointer types
The type of values representing C function pointer types.
val static_funptr : 'a fn -> 'a Ctypes_static.static_funptr typConstruct a function pointer type from an existing function type (called the reference type).
val constant : string -> 'a typ -> 'a constconstant name typretrieves the value of the compile-time constantnameof typetyp. It can be used to retrieve enum constants, #defined values and other integer constant expressions.The type
typmust be either an integer type such asbool,char,int,uint8, etc., or a view (or perhaps multiple views) where the underlying type is an integer type.When the value of the constant cannot be represented in the type there will typically be a diagnostic from either the C compiler or the OCaml compiler. For example, gcc will say
warning: overflow in implicit constant conversion
val enum : string -> ?typedef:bool -> ?unexpected:(int64 -> 'a) -> ('a * int64 const) list -> 'a typenum name ?unexpected alistbuilds a type representation for the enum namedname. The size and alignment are retrieved so that the resulting type can be used everywhere an integer type can be used: as an array element or struct member, as an argument or return value, etc.The value
alistis an association list of OCaml values and values retrieved by theconstantfunction. For example, to expose the enumenum letters { A, B, C = 10, D };
you might first retrieve the values of the enumeration constants:
let a = constant "A" int64_t and b = constant "B" int64_t and c = constant "C" int64_t and d = constant "D" int64_tand then build the enumeration type
let letters = enum "letters" [ `A, a; `B, b; `C, c; `D, d; ] ~unexpected:(fun i -> `E i)The
unexpectedfunction specifies the value to return in the case that some unexpected value is encountered -- for example, if a function with the return type 'enum letters' actually returns the value-1.The optional flag
typedefspecifies whether the first argument,name, indicates an tag or an alias. Iftypedefisfalse(the default) thennameis treated as an enumeration tag:enum letters { ... }If
typedefistruethennameis instead treated as an alias:typedef enum { ... } letters