ocaml-ctypes
ocaml-ctypes copied to clipboard
Cstubs.Types.TYPE.const consumption
What functions can consume Cstubs.Types.TYPE.const and return something useful?
Only Cstubs.Types.TYPE.enum as far as I can tell.
What if you have constants that were not in an enum? For instance, a group of macros does not appear to have a way to be mapped into variant constructors. Have a missed something?
What if you have constants that were not in an enum? For instance, a group of macros does not appear to have a way to be mapped into variant constructors. Have a missed something?
I think for the moment you have to process the results of the macros outside the functor:
module Type_bindings(T: Cstubs.Types.TYPE) = struct
let macro_a = constant "MACRO_A" int64
and macro_b = constant "MACRO_B" int64
and macro_c = constant "MACRO_C" int64
end
[...]
(* in some other file *)
module Bindings = Type_bindings(Generated_type_stubs)
(* Bindings.macro_{a|b|c} are now int64 values, which you can map to
variant contructors etc. *)
More generally, const is currently pretty limited and could perhaps be relocated to the more general Ctypes_types.TYPE signature, since there are quite a few other situations where it could be used to retrieve and use compile-time information:
-
The
arrayconstructor could have the typeval array : int const -> 'a typ -> 'a carray typso that we could construct arrays with lengths that are known at compile-time:
#define TABLE_ENTRIES 10 struct table { int id; struct entry *entries[TABLE_ENTRIES]; }; -
Similarly, the
abstractconstructor could take itssizeandalignmentarguments asconstvalues. -
The
sizeofandalignmentfunctions could returnint constso that they could be computed in generated C code and used to construct other static elements
We could also make const an applicative, which should make it possible to move various computations back inside the bindings functor.
I need a constant_opt (or macro_opt?) function for macros that may not be defined which would introduce an #ifdef and return None if the macro is missing.