-
Notifications
You must be signed in to change notification settings - Fork 97
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
Does the C stubs enum supports C flags usage ? #598
Comments
There's no direct support for flag sets in You might consider
|
Ok, thanks @yallop. |
Sorry to reopen this but I would like that you have a look at what I have done: I use Cstubs constant and I create a view to deal with flags list. First I wrote this in my bindings module: (** Flags for a Function_info struct. *)
type flags =
| Is_method (** is a method. *)
| Is_constructor (** is a constructor. *)
| Is_getter (** is a getter of a Property_info. *)
| Is_setter (** is a setter of a Property_info. *)
| Wraps_vfunc (** represents a virtual function. *)
| Throws (** the function may throw an error. *)
module Enums = functor (T : Cstubs.Types.TYPE) -> struct
(** Other stuff *)
let gi_function_is_method = T.constant "GI_FUNCTION_IS_METHOD" T.int64_t
let gi_function_is_constructor = T.constant "GI_FUNCTION_IS_CONSTRUCTOR" T.int64_t
let gi_function_is_getter = T.constant "GI_FUNCTION_IS_GETTER" T.int64_t
let gi_function_is_setter = T.constant "GI_FUNCTION_IS_SETTER" T.int64_t
let gi_function_wraps_vfunc = T.constant "GI_FUNCTION_WRAPS_VFUNC" T.int64_t
let gi_function_throws = T.constant "GI_FUNCTION_THROWS" T.int64_t
end I have a Stubs.ml file:
And in my Function_info.ml file, I have the following: open Ctypes
open Foreign
open Stubs
type t
let functioninfo : t structure typ = structure "Function_info"
let all_flags : (int64 * Bindings.flags) list= [
gi_function_is_method, Is_method;
gi_function_is_constructor, Is_constructor;
gi_function_is_getter, Is_getter;
gi_function_is_setter, Is_setter;
gi_function_wraps_vfunc, Wraps_vfunc;
gi_function_throws, Throws;
]
let flags_of_int64 v =
let open Int64 in
let rec build_flags_list allf acc =
match allf with
| [] -> acc
| (i, f) :: q -> if ((logand v i) <> zero) then build_flags_list q (f :: acc)
else build_flags_list q acc
in build_flags_list all_flags []
let int64_of_flags (f : Bindings.flags list) =
let open Int64 in
let bitwise_or = fun acc value ->
let (i, _f) = List.find (fun (i', f') -> value = f') all_flags in logor acc i
in
List.fold_left bitwise_or Int64.zero f
let flags =
view int64_t
~read:flags_of_int64
~write:int64_of_flags
let get_flags =
foreign "g_function_info_get_flags"
(ptr functioninfo @-> returning flags) I can compile it and simple tests seem to be fine. |
The problem is the Your But if you use |
This is true, and the problem can be fixed by using So if you have a C definition like this: enum flags { ... }; then you might add the following to your bindings functor: let enum_flags = enum "flags" [] ~unexpected:(fun x -> x) When you link everything together you'll end up with a value of the type let flags = view enum_flags ~read:... ~write:... |
thanks to both of you. |
I would like to know if using the Cstubs enum in order to bind a C enum values that are used as flags (ie ored value) is the good way to do?
For example this function:
returns a set of values that are bitwise ored. Previously, I created my bindings like this:
And it returns a list of flags.Is there a way to deal with ored values with the Cstubs enum type?
The text was updated successfully, but these errors were encountered: