DNAYaml: Remove use of goto in finish()

We can just wrap the error code within a lambda and call it in the error
control paths, eliminating the need for goto.
This commit is contained in:
Lioncash 2019-08-26 20:20:20 -04:00
parent e8d6c2abe7
commit dcf83d7782
1 changed files with 21 additions and 13 deletions

View File

@ -432,31 +432,39 @@ YAMLDocWriter::YAMLDocWriter(const char* classType, athena::io::IStreamReader* r
YAMLDocWriter::~YAMLDocWriter() { yaml_emitter_delete(&m_emitter); } YAMLDocWriter::~YAMLDocWriter() { yaml_emitter_delete(&m_emitter); }
bool YAMLDocWriter::finish(athena::io::IStreamWriter* fout) { bool YAMLDocWriter::finish(athena::io::IStreamWriter* fout) {
const auto error = [this] {
HandleYAMLEmitterError(&m_emitter);
return false;
};
yaml_event_t event = {}; yaml_event_t event = {};
if (fout) if (fout) {
yaml_emitter_set_output(&m_emitter, (yaml_write_handler_t*)YAMLAthenaWriter, fout); yaml_emitter_set_output(&m_emitter, (yaml_write_handler_t*)YAMLAthenaWriter, fout);
if (!yaml_emitter_open(&m_emitter)) }
goto err; if (!yaml_emitter_open(&m_emitter)) {
return error();
}
event.type = YAML_DOCUMENT_START_EVENT; event.type = YAML_DOCUMENT_START_EVENT;
event.data.document_start.implicit = true; event.data.document_start.implicit = true;
if (!yaml_emitter_emit(&m_emitter, &event)) if (!yaml_emitter_emit(&m_emitter, &event)) {
goto err; return error();
if (!RecursiveFinish(&m_emitter, *m_rootNode)) }
if (!RecursiveFinish(&m_emitter, *m_rootNode)) {
return false; return false;
}
event.type = YAML_DOCUMENT_END_EVENT; event.type = YAML_DOCUMENT_END_EVENT;
event.data.document_end.implicit = true; event.data.document_end.implicit = true;
if (!yaml_emitter_emit(&m_emitter, &event)) if (!yaml_emitter_emit(&m_emitter, &event)) {
goto err; return error();
}
if (!yaml_emitter_close(&m_emitter) || !yaml_emitter_flush(&m_emitter)) if (!yaml_emitter_close(&m_emitter) || !yaml_emitter_flush(&m_emitter)) {
goto err; return error();
}
return true; return true;
err:
HandleYAMLEmitterError(&m_emitter);
return false;
} }
YAMLDocWriter::RecordRAII YAMLDocWriter::enterSubRecord(const char* name) { YAMLDocWriter::RecordRAII YAMLDocWriter::enterSubRecord(const char* name) {