[Libguestfs] [PATCH libnbd 8/9] generator: Enhance Flags/OFlags with optional "all flags" mask.

Richard W.M. Jones
2019-08-10T13:02:47Z

Optionally generate a mask which sets all currently known flags.  As
written this commit does nothing, but it makes more sense with
currently proposed changes to nbd_connect_uri.
---
 generator/generator | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/generator/generator b/generator/generator
index a6aea26..fe24738 100755
--- a/generator/generator
+++ b/generator/generator
@@ -898,6 +898,7 @@ and enum = {
 }
 and flags = {
   flag_prefix : string;    (* prefix of each flag name *)
+  all_flags_bitmask : bool; (* if true, generate a bitmask of all flags *)
   flags : (string * int) list (* flag names and their values in C *)
 }
 and permitted_state @@ -930,6 +931,7 @@ let all_enums = [ tls_enum ]
 (* Flags. *)
 let cmd_flags = {
   flag_prefix = "CMD_FLAG";
+  all_flags_bitmask = false;
   flags = [
     "FUA",     1 lsl 0;
     "NO_HOLE", 1 lsl 1;
@@ -3547,13 +3549,18 @@ let generate_include_libnbd_h ()        pr
"\n"
   ) all_enums;
   List.iter (
-    fun { flag_prefix; flags } ->
+    fun { flag_prefix; all_flags_bitmask; flags } ->
       List.iter (
         fun (flag, i) ->
           let flag = sprintf "LIBNBD_%s_%s" flag_prefix flag in
           pr "#define %-40s %d\n" flag i
       ) flags;
-      pr "\n"
+      if all_flags_bitmask then (
+        let all = List.fold_left (lor) 0 (List.map snd flags) in
+        let n = sprintf "LIBNBD_%s_ALL" flag_prefix in
+        pr "#define %-40s %d\n" n all;
+      );
+      pr "\n";
   ) all_flags;
   List.iter (
     fun (n, i) ->
@@ -4606,12 +4613,17 @@ Error.__str__ = _str
       pr "\n"
   ) all_enums;
   List.iter (
-    fun { flag_prefix; flags } ->
+    fun { flag_prefix; all_flags_bitmask; flags } ->
       List.iter (
         fun (flag, i) ->
           let flag = sprintf "%s_%s" flag_prefix flag in
           pr "%-30s = %d\n" flag i
       ) flags;
+      if all_flags_bitmask then (
+        let all = List.fold_left (lor) 0 (List.map snd flags) in
+        let n = sprintf "%s_ALL" flag_prefix in
+        pr "%-30s = %d\n" n all;
+      );
       pr "\n"
   ) all_flags;
   List.iter (fun (n, i) -> pr "%-30s = %d\n" n i) constants;
@@ -4845,13 +4857,15 @@ exception Closed of string
       pr "\n"
   ) all_enums;
   List.iter (
-    fun { flag_prefix; flags } ->
+    fun { flag_prefix; all_flags_bitmask; flags } ->
       pr "module %s : sig\n" flag_prefix;
       pr "  type t =\n";
       List.iter (
         fun (flag, _) ->
           pr "  | %s\n" flag
       ) flags;
+      if all_flags_bitmask then
+        pr "  val all : t list\n";
       pr "end\n";
       pr "\n"
   ) all_flags;
@@ -4948,13 +4962,15 @@ let ()        pr "\n"
   ) all_enums;
   List.iter (
-    fun { flag_prefix; flags } ->
+    fun { flag_prefix; all_flags_bitmask; flags } ->
       pr "module %s = struct\n" flag_prefix;
       pr "  type t =\n";
       List.iter (
         fun (flag, _) ->
           pr "  | %s\n" flag
       ) flags;
+      if all_flags_bitmask then
+        pr "  let all = [%s]\n" (String.concat ";"
(List.map fst flags));
       pr "end\n";
       pr "\n"
   ) all_flags;
--
2.22.0