From 3e0549e4c9e5ed2ef9109d03ac494b60626eeb4b Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Wed, 16 Sep 2020 16:36:33 +0200 Subject: [PATCH] Move the XML writer to its own file --- src/CMakeLists.txt | 1 + src/sfizz/Synth.cpp | 21 +++------------------ src/sfizz/utility/XmlHelpers.h | 30 ++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 18 deletions(-) create mode 100644 src/sfizz/utility/XmlHelpers.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4adbe3fb..7e04487e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -29,6 +29,7 @@ set (SFIZZ_HEADERS sfizz/Debug.h sfizz/utility/NumericId.h sfizz/utility/SpinMutex.h + sfizz/utility/XmlHelpers.h sfizz/modulations/ModId.h sfizz/modulations/ModKey.h sfizz/modulations/ModKeyHash.h diff --git a/src/sfizz/Synth.cpp b/src/sfizz/Synth.cpp index c984acae..1fe4ee9f 100644 --- a/src/sfizz/Synth.cpp +++ b/src/sfizz/Synth.cpp @@ -19,6 +19,7 @@ #include "modulations/sources/Controller.h" #include "modulations/sources/LFO.h" #include "modulations/sources/FlexEnvelope.h" +#include "utility/XmlHelpers.h" #include "pugixml.hpp" #include "absl/algorithm/container.h" #include "absl/memory/memory.h" @@ -1301,25 +1302,9 @@ std::string sfz::Synth::exportMidnam(absl::string_view model) const } } - /// - struct string_writer : pugi::xml_writer { - std::string result; - - string_writer() - { - result.reserve(8192); - } - - void write(const void* data, size_t size) override - { - result.append(static_cast(data), size); - } - }; - - /// - string_writer writer; + string_xml_writer writer; doc.save(writer); - return std::move(writer.result); + return std::move(writer.str()); } const sfz::Region* sfz::Synth::getRegionView(int idx) const noexcept diff --git a/src/sfizz/utility/XmlHelpers.h b/src/sfizz/utility/XmlHelpers.h new file mode 100644 index 00000000..11932299 --- /dev/null +++ b/src/sfizz/utility/XmlHelpers.h @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: BSD-2-Clause + +// This code is part of the sfizz library and is licensed under a BSD 2-clause +// license. You should have receive a LICENSE.md file along with the code. +// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz + +#pragma once +#include +#include + +class string_xml_writer : public pugi::xml_writer { +public: + explicit string_xml_writer(size_t capacity = 8192) + { + result_.reserve(capacity); + } + + void write(const void* data, size_t size) override + { + result_.append(static_cast(data), size); + } + + std::string& str() noexcept + { + return result_; + } + +private: + std::string result_; +};