Skip to content

Commit

Permalink
common: Add Bitwise functor for bitwise enums.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoo1 committed Aug 2, 2021
1 parent a8dee0b commit bc80d4b
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions common/bitwise.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
open Ctypes

module type Size = sig
type t
val zero : t
val logor : t -> t -> t
val logand : t -> t -> t
end

module type Elems = sig
type t
module Size : Size
val size : Size.t typ
val desc : (t * Size.t) list
end

module type Enum = sig
type elem
type t
val t : t
end

module Make (E : Elems) : Enum = struct
type elem = E.t
type t = E.t list typ
let t : t =
let read i =
List.filter_map (fun (x, cst) ->
if (E.Size.logand i cst) <> E.Size.zero then
Some x
else None
) E.desc
in
let write items =
List.fold_left (fun i item ->
E.Size.logor (List.assoc item E.desc) i
) E.Size.zero items
in
view ~read ~write E.size
end

0 comments on commit bc80d4b

Please sign in to comment.