From: Princeton Ferro Date: Sun, 9 May 2021 19:55:37 +0000 (-0400) Subject: ccode: Allow appending `#elif`, `#else` cases to CCodeIfSection X-Git-Tag: 0.53.1~64 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=994b4cb078643d9bb1842fa60ecb69891e1e7b87;p=thirdparty%2Fvala.git ccode: Allow appending `#elif`, `#else` cases to CCodeIfSection --- 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); + } }