public CCodeFunction generate_enum_to_string_function(ValaEnum en) { var to_string_name = "%s_to_string".printf(get_ccode_lower_case_name(en, null)); var to_string_func = new CCodeFunction(to_string_name, "const char*"); to_string_func.add_parameter(new CCodeParameter("value", get_ccode_name(en))); push_function(to_string_func); ccode.add_declaration("const char *", new CCodeVariableDeclarator("str")); ccode.open_switch(new CCodeIdentifier("value")); foreach (EnumValue enum_value in en.get_values()) { string dbus_value = get_dbus_value(enum_value, enum_value.name); ccode.add_case(new CCodeIdentifier(get_ccode_name(enum_value))); ccode.add_assignment(new CCodeIdentifier("str"), new CCodeConstant("\"%s\"".printf(dbus_value))); ccode.add_break(); } ccode.close(); ccode.add_return(new CCodeIdentifier("str")); pop_function(); return(to_string_func); }
public CCodeFunction generate_enum_from_string_function(ValaEnum en) { var from_string_name = "%s_from_string".printf(get_ccode_lower_case_name(en, null)); var from_string_func = new CCodeFunction(from_string_name, get_ccode_name(en)); from_string_func.add_parameter(new CCodeParameter("str", "const char*")); from_string_func.add_parameter(new CCodeParameter("error", "GError**")); push_function(from_string_func); ccode.add_declaration(get_ccode_name(en), CCodeVariableDeclarator.zero("value", new CCodeConstant("0"))); bool firstif = true; foreach (EnumValue enum_value in en.get_values()) { string dbus_value = get_dbus_value(enum_value, enum_value.name); var string_comparison = new CCodeFunctionCall(new CCodeIdentifier("strcmp")); string_comparison.add_argument(new CCodeIdentifier("str")); string_comparison.add_argument(new CCodeConstant("\"%s\"".printf(dbus_value))); var cond = new CCodeBinaryExpression(CCodeBinaryOperator.EQUALITY, string_comparison, new CCodeConstant("0")); if (firstif) { ccode.open_if(cond); firstif = false; } else { ccode.else_if(cond); } ccode.add_assignment(new CCodeIdentifier("value"), new CCodeIdentifier(get_ccode_name(enum_value))); } ccode.add_else(); var set_error = new CCodeFunctionCall(new CCodeIdentifier("g_set_error")); set_error.add_argument(new CCodeIdentifier("error")); set_error.add_argument(new CCodeIdentifier("G_DBUS_ERROR")); set_error.add_argument(new CCodeIdentifier("G_DBUS_ERROR_INVALID_ARGS")); set_error.add_argument(new CCodeConstant("\"Invalid value for enum `%s'\"".printf(get_ccode_name(en)))); ccode.add_expression(set_error); ccode.close(); ccode.add_return(new CCodeIdentifier("value")); pop_function(); return(from_string_func); }