\n\n"); fwrite($fp, "#include \"../$name.enum.h\"\n\n"); fwrite($fp, "using std::string;\n"); fwrite($fp, "#include \n"); fwrite($fp, "\n\n"); fwrite($fp, "// ** This needs to be copied to the header\n"); fwrite($fp, $name . " " . $name . "_fromString(const string& value);\n"); fwrite($fp, "string " . $name . "_toString($name value);\n"); fwrite($fp, "\n\n"); fwrite($fp, "auto " . $name . "_fromString(const string& value) -> " . $name . " {\n"); foreach ($values as $v) { fwrite($fp, $tab . "if (value == \"$v\") {\n"); fwrite($fp, $tab . $tab . "return $v;\n"); fwrite($fp, $tab . "}\n"); fwrite($fp, "\n"); } fwrite($fp, $tab . "g_warning(\"Invalid enum value for $name: «%s»\", value.c_str());\n"); fwrite($fp, $tab . "return {$values[0]};\n"); fwrite($fp, "}\n"); //////////////////////////////////////////////////////////////////////////// fwrite($fp, "\n\n"); fwrite($fp, "auto " . $name . "_toString($name value) -> string {\n"); foreach ($values as $v) { fwrite($fp, $tab . "if (value == $v) {\n"); fwrite($fp, $tab . $tab . "return \"$v\";\n"); fwrite($fp, $tab . "}\n"); fwrite($fp, "\n"); } fwrite($fp, $tab . "g_error(\"Invalid enum value for $name: %i\", value);\n"); fwrite($fp, $tab . "return \"\";\n"); fwrite($fp, "}\n"); fclose($fp); print "Generated $output\n"; } function generateEnum($file) { $values = parseEnumFile($file); $name = substr($file, 0, -7); writeCppFile("generated/$name.generated.cpp", $name, $values); }