[vala/staging: 2/3] ccode: Allow appending `#elif`, `#else` cases to CCodeIfSection




commit 994b4cb078643d9bb1842fa60ecb69891e1e7b87
Author: Princeton Ferro <princetonferro gmail com>
Date:   Sun May 9 15:55:37 2021 -0400

    ccode: Allow appending `#elif`, `#else` cases to CCodeIfSection

 ccode/valaccodeifsection.vala | 42 +++++++++++++++++++++++++++++++++++-------
 1 file changed, 35 insertions(+), 7 deletions(-)
---
diff --git a/ccode/valaccodeifsection.vala b/ccode/valaccodeifsection.vala
index dd51743e0..c89cc26d5 100644
--- a/ccode/valaccodeifsection.vala
+++ b/ccode/valaccodeifsection.vala
@@ -27,24 +27,52 @@ using GLib;
  */
 public class Vala.CCodeIfSection : CCodeFragment {
        /**
-        * The expression
+        * The conditional expression, or null if there is no condition.
         */
-       public string expression { get; set; }
+       public string? expression { get; set; }
 
-       public CCodeIfSection (string expr) {
+       CCodeIfSection? else_section;
+       bool is_else_section;
+
+       public CCodeIfSection (string? expr) {
                expression = expr;
+               is_else_section = false;
+       }
+
+       public unowned CCodeIfSection append_else (string? expr = null) {
+               else_section = new CCodeIfSection (expr);
+               else_section.is_else_section = true;
+               return else_section;
        }
 
        public override void write (CCodeWriter writer) {
-               writer.write_string ("#if ");
-               writer.write_string (expression);
+               if (is_else_section) {
+                       if (expression != null) {
+                               writer.write_string ("#elif ");
+                               writer.write_string (expression);
+                       } else {
+                               writer.write_string ("#else ");
+                       }
+               } else if (expression != null) {
+                       writer.write_string ("#if ");
+                       writer.write_string (expression);
+               }
+               writer.write_newline ();
                foreach (CCodeNode node in get_children ()) {
                        node.write_combined (writer);
                }
-               writer.write_string ("#endif");
-               writer.write_newline ();
+               if (else_section != null) {
+                       else_section.write_combined (writer);
+               } else {
+                       writer.write_string ("#endif");
+                       writer.write_newline ();
+               }
        }
 
        public override void write_declaration (CCodeWriter writer) {
        }
+
+       public override void write_combined (CCodeWriter writer) {
+               write (writer);
+       }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]