From dcf83d7782b5aba0a6af3e9a909e29f7d5fec115 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 26 Aug 2019 20:20:20 -0400 Subject: [PATCH] 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. --- src/athena/DNAYaml.cpp | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/athena/DNAYaml.cpp b/src/athena/DNAYaml.cpp index 3422a6f..f68c2b0 100644 --- a/src/athena/DNAYaml.cpp +++ b/src/athena/DNAYaml.cpp @@ -432,31 +432,39 @@ YAMLDocWriter::YAMLDocWriter(const char* classType, athena::io::IStreamReader* r YAMLDocWriter::~YAMLDocWriter() { yaml_emitter_delete(&m_emitter); } bool YAMLDocWriter::finish(athena::io::IStreamWriter* fout) { + const auto error = [this] { + HandleYAMLEmitterError(&m_emitter); + return false; + }; + yaml_event_t event = {}; - if (fout) + if (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.data.document_start.implicit = true; - if (!yaml_emitter_emit(&m_emitter, &event)) - goto err; - if (!RecursiveFinish(&m_emitter, *m_rootNode)) + if (!yaml_emitter_emit(&m_emitter, &event)) { + return error(); + } + if (!RecursiveFinish(&m_emitter, *m_rootNode)) { return false; + } event.type = YAML_DOCUMENT_END_EVENT; event.data.document_end.implicit = true; - if (!yaml_emitter_emit(&m_emitter, &event)) - goto err; + if (!yaml_emitter_emit(&m_emitter, &event)) { + return error(); + } - if (!yaml_emitter_close(&m_emitter) || !yaml_emitter_flush(&m_emitter)) - goto err; + if (!yaml_emitter_close(&m_emitter) || !yaml_emitter_flush(&m_emitter)) { + return error(); + } return true; -err: - HandleYAMLEmitterError(&m_emitter); - return false; } YAMLDocWriter::RecordRAII YAMLDocWriter::enterSubRecord(const char* name) {