From 9000258e8df3bb60af1c6ae731018373f6a40c0c Mon Sep 17 00:00:00 2001 From: Oleg Samarin Date: Sat, 8 Mar 2025 21:31:49 +0300 Subject: [PATCH] Simplified classes --- src/grandorgue/CMakeLists.txt | 4 +- src/grandorgue/GOOrganController.cpp | 7 +- src/grandorgue/yaml/GOYamlInModel.h | 50 ----------- .../{GOYamlInModel.cpp => GOYamlModel.cpp} | 65 ++++++++++++++- src/grandorgue/yaml/GOYamlModel.h | 82 +++++++++++++++++++ src/grandorgue/yaml/GOYamlModelBase.cpp | 45 ---------- src/grandorgue/yaml/GOYamlModelBase.h | 45 ---------- src/grandorgue/yaml/GOYamlOutModel.cpp | 46 ----------- src/grandorgue/yaml/GOYamlOutModel.h | 46 ----------- 9 files changed, 147 insertions(+), 243 deletions(-) delete mode 100644 src/grandorgue/yaml/GOYamlInModel.h rename src/grandorgue/yaml/{GOYamlInModel.cpp => GOYamlModel.cpp} (56%) create mode 100644 src/grandorgue/yaml/GOYamlModel.h delete mode 100644 src/grandorgue/yaml/GOYamlModelBase.cpp delete mode 100644 src/grandorgue/yaml/GOYamlModelBase.h delete mode 100644 src/grandorgue/yaml/GOYamlOutModel.cpp delete mode 100644 src/grandorgue/yaml/GOYamlOutModel.h diff --git a/src/grandorgue/CMakeLists.txt b/src/grandorgue/CMakeLists.txt index 8bfb71ff8..78d342d25 100644 --- a/src/grandorgue/CMakeLists.txt +++ b/src/grandorgue/CMakeLists.txt @@ -221,9 +221,7 @@ sound/GOSoundFilter.cpp sound/GOSoundToneBalanceFilter.cpp updater/GOUpdateChecker.cpp yaml/GOSaveableToYaml.cpp -yaml/GOYamlModelBase.cpp -yaml/GOYamlInModel.cpp -yaml/GOYamlOutModel.cpp +yaml/GOYamlModel.cpp yaml/go-wx-yaml.cpp GOAudioRecorder.cpp GOBitmapCache.cpp diff --git a/src/grandorgue/GOOrganController.cpp b/src/grandorgue/GOOrganController.cpp index 7b3b76789..8a0731008 100644 --- a/src/grandorgue/GOOrganController.cpp +++ b/src/grandorgue/GOOrganController.cpp @@ -64,8 +64,7 @@ #include "sound/GOSoundEngine.h" #include "sound/GOSoundReleaseAlignTable.h" #include "temperaments/GOTemperament.h" -#include "yaml/GOYamlInModel.h" -#include "yaml/GOYamlOutModel.h" +#include "yaml/GOYamlModel.h" #include "yaml/go-wx-yaml.h" #include "go_defs.h" @@ -631,7 +630,7 @@ const wxString &WX_YAML = wxT("yaml"); const wxString WX_GRANDORGUE_COMBINATIONS = "GrandOrgue Combinations"; wxString GOOrganController::ExportCombination(const wxString &fileName) { - GOYamlOutModel yamlOut(GetOrganName(), WX_GRANDORGUE_COMBINATIONS); + GOYamlModel::Out yamlOut(GetOrganName(), WX_GRANDORGUE_COMBINATIONS); yamlOut << *m_setter; yamlOut << *m_DivisionalSetter; @@ -646,7 +645,7 @@ void GOOrganController::LoadCombination(const wxString &file) { const wxString fileExt = fileName.GetExt(); if (fileExt == WX_YAML) { - GOYamlInModel inYaml(GetOrganName(), file, WX_GRANDORGUE_COMBINATIONS); + GOYamlModel::In inYaml(GetOrganName(), file, WX_GRANDORGUE_COMBINATIONS); if (is_to_import_to_this_organ( GetOrganName(), diff --git a/src/grandorgue/yaml/GOYamlInModel.h b/src/grandorgue/yaml/GOYamlInModel.h deleted file mode 100644 index acb5a42fc..000000000 --- a/src/grandorgue/yaml/GOYamlInModel.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2006 Milan Digital Audio LLC - * Copyright 2009-2025 GrandOrgue contributors (see AUTHORS) - * License GPL-2.0 or later - * (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html). - */ - -#ifndef GOYAMLINMODEL_H -#define GOYAMLINMODEL_H - -#include - -#include "GOYamlModelBase.h" - -class GOSaveableToYaml; - -/** - * Getting anything from a yaml file. - * - * The pattern to use: - * - * GOYamlInModel inYaml(organName, fileName, contentType); - * - * inYaml >> saveableToYaml1; - * inYaml >> saveableToYaml2; - */ - -class GOYamlInModel : private GOYamlModelBase { -private: - YAML::Node m_GlobalNode; - -public: - /** - * Construct an instance. If sometimes goes wrong, it throws a wxString with - * the error messag - * @param organName - * @param fileName - * @param contentType - */ - GOYamlInModel( - const wxString &organName, - const wxString &fileName, - const wxString &contentType); - - wxString GetFileOrganName() const { return geFileOrganName(m_GlobalNode); } - - const GOYamlInModel &operator>>(GOSaveableToYaml &saveableObj) const; -}; - -#endif /* GOYAMLINMODEL_H */ diff --git a/src/grandorgue/yaml/GOYamlInModel.cpp b/src/grandorgue/yaml/GOYamlModel.cpp similarity index 56% rename from src/grandorgue/yaml/GOYamlInModel.cpp rename to src/grandorgue/yaml/GOYamlModel.cpp index 8d846108c..b2e9ebcb7 100644 --- a/src/grandorgue/yaml/GOYamlInModel.cpp +++ b/src/grandorgue/yaml/GOYamlModel.cpp @@ -5,12 +5,29 @@ * (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html). */ -#include "GOYamlInModel.h" +#include "GOYamlModel.h" +#include #include #include +#include #include "GOSaveableToYaml.h" +#include "go-wx-yaml.h" +#include "go_defs.h" + +static const uint8_t UTF8_BOM[] = {0xEF, 0xBB, 0xBF}; + +const char *const INFO = "info"; +const char *const CONTENT_TYPE = "content-type"; +const char *const ORGAN_NAME = "organ-name"; +const char *const GRANDORGUE_VERSION = "grandorgue-version"; +const char *const SAVED_TIME = "saved_time"; + +static wxString get_info_field( + const YAML::Node &globalNode, const wxString &fieldName) { + return globalNode[INFO][fieldName].as(); +} static std::vector load_file_bytes(const wxString &filePath) { wxFile file; @@ -56,12 +73,12 @@ static YAML::Node load_yaml_from_file(const wxString &fileName) { return YAML::Load(fileContentInUtf8.data()); } -GOYamlInModel::GOYamlInModel( +GOYamlModel::In::In( const wxString &organName, const wxString &fileName, const wxString &contentType) : m_GlobalNode(load_yaml_from_file(fileName)) { - const wxString fileContentType = getFileContentType(m_GlobalNode); + const wxString fileContentType = get_info_field(m_GlobalNode, CONTENT_TYPE); if (fileContentType != contentType) throw wxString::Format( @@ -71,8 +88,48 @@ GOYamlInModel::GOYamlInModel( contentType); } -const GOYamlInModel &GOYamlInModel::operator>>( +wxString GOYamlModel::In::GetFileOrganName() const { + return get_info_field(m_GlobalNode, ORGAN_NAME); +} + +const GOYamlModel::In &GOYamlModel::In::operator>>( GOSaveableToYaml &saveableObj) const { m_GlobalNode >> saveableObj; return *this; } + +GOYamlModel::Out::Out(const wxString &organName, const wxString &contentType) + : m_GlobalNode(YAML::NodeType::Map) { + YAML::Node infoNode = m_GlobalNode[INFO]; + + infoNode[CONTENT_TYPE] = contentType; + infoNode[ORGAN_NAME] = organName; + infoNode[GRANDORGUE_VERSION] = APP_VERSION; + infoNode[SAVED_TIME] = wxDateTime::Now().Format(); +} + +GOYamlModel::Out &GOYamlModel::Out::operator<<( + const GOSaveableToYaml &saveableObj) { + m_GlobalNode << saveableObj; + return *this; +} + +wxString GOYamlModel::Out::writeTo(const wxString &fileName) { + wxString errMsg; + wxFileOutputStream fOS(fileName); + + if (fOS.IsOk()) { + YAML::Emitter outYaml; + + outYaml << YAML::BeginDoc << m_GlobalNode; + + if ( + !fOS.WriteAll(UTF8_BOM, sizeof(UTF8_BOM)) + || !fOS.WriteAll(outYaml.c_str(), outYaml.size())) + errMsg.Printf( + wxT("Unable to write all the data to the file '%s'"), fileName); + fOS.Close(); + } else + errMsg.Printf(wxT("Unable to open the file '%s' for writing"), fileName); + return errMsg; +} diff --git a/src/grandorgue/yaml/GOYamlModel.h b/src/grandorgue/yaml/GOYamlModel.h new file mode 100644 index 000000000..cccd1c02e --- /dev/null +++ b/src/grandorgue/yaml/GOYamlModel.h @@ -0,0 +1,82 @@ +/* + * Copyright 2006 Milan Digital Audio LLC + * Copyright 2009-2025 GrandOrgue contributors (see AUTHORS) + * License GPL-2.0 or later + * (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html). + */ + +#ifndef GOYAMLMODEL_H +#define GOYAMLMODEL_H + +#include +#include + +class GOSaveableToYaml; + +/** + * A common code used for writting data to and reading data from yaml + */ +class GOYamlModel { +public: + /** + * Getting anything from a yaml file. + * + * The pattern to use: + * + * GOYamlModel::In inYaml(organName, fileName, contentType); + * + * inYaml >> saveableToYaml1; + * inYaml >> saveableToYaml2; + */ + class In { + private: + YAML::Node m_GlobalNode; + + public: + /** + * Construct an instance. If sometimes goes wrong, it throws a wxString with + * the error messag + * @param organName + * @param fileName + * @param contentType + */ + In( + const wxString &organName, + const wxString &fileName, + const wxString &contentType); + + wxString GetFileOrganName() const; + + const In &operator>>(GOSaveableToYaml &saveableObj) const; + }; + + /** + * Putting anything to a yaml file. + * + * The pattern to use: + * + * GOYamlModel::Out outYaml(organName, contentType); + * + * outYaml << saveableToYaml1; + * outYaml << saveableToYaml2; + * + * wxString errorMsg = outYaml.writeTo(fileName) + */ + class Out { + private: + YAML::Node m_GlobalNode; + + public: + Out(const wxString &organName, const wxString &contentType); + + Out &operator<<(const GOSaveableToYaml &saveableObj); + + /** + * Write the current yaml model to the text file. If something goes wrong it + * returns a non empty error message + */ + wxString writeTo(const wxString &fileName); + }; +}; + +#endif /* GOYAMLMODEL_H */ diff --git a/src/grandorgue/yaml/GOYamlModelBase.cpp b/src/grandorgue/yaml/GOYamlModelBase.cpp deleted file mode 100644 index 1783e8bc0..000000000 --- a/src/grandorgue/yaml/GOYamlModelBase.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2006 Milan Digital Audio LLC - * Copyright 2009-2025 GrandOrgue contributors (see AUTHORS) - * License GPL-2.0 or later - * (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html). - */ - -#include "GOYamlModelBase.h" - -#include -#include - -#include "go-wx-yaml.h" -#include "go_defs.h" - -const char *const INFO = "info"; -const char *const CONTENT_TYPE = "content-type"; -const char *const ORGAN_NAME = "organ-name"; -const char *const GRANDORGUE_VERSION = "grandorgue-version"; -const char *const SAVED_TIME = "saved_time"; - -void GOYamlModelBase::putInfoNode( - const wxString &contentType, - const wxString &organName, - YAML::Node &globalNode) { - YAML::Node infoNode = globalNode[INFO]; - - infoNode[CONTENT_TYPE] = contentType; - infoNode[ORGAN_NAME] = organName; - infoNode[GRANDORGUE_VERSION] = APP_VERSION; - infoNode[SAVED_TIME] = wxDateTime::Now().Format(); -} - -static wxString get_info_field( - const YAML::Node &globalNode, const wxString &fieldName) { - return globalNode[INFO][fieldName].as(); -} - -wxString GOYamlModelBase::getFileContentType(const YAML::Node &globalNode) { - return get_info_field(globalNode, CONTENT_TYPE); -} - -wxString GOYamlModelBase::geFileOrganName(const YAML::Node &globalNode) { - return get_info_field(globalNode, ORGAN_NAME); -} diff --git a/src/grandorgue/yaml/GOYamlModelBase.h b/src/grandorgue/yaml/GOYamlModelBase.h deleted file mode 100644 index 73ab63951..000000000 --- a/src/grandorgue/yaml/GOYamlModelBase.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2006 Milan Digital Audio LLC - * Copyright 2009-2025 GrandOrgue contributors (see AUTHORS) - * License GPL-2.0 or later - * (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html). - */ - -#ifndef GOYAMLMODELBASE_H -#define GOYAMLMODELBASE_H - -#include - -namespace YAML { -class Node; -} - -/** - * A common code used for writting data to and reading data from yaml - */ -class GOYamlModelBase { -protected: - /** - * Put the info section to the global node - */ - static void putInfoNode( - const wxString &contentType, - const wxString &organName, - YAML::Node &globalNode); - - /** - * Get the content type of the saved yaml file - * @param globalNode the root yaml node - * @return the content type - */ - static wxString getFileContentType(const YAML::Node &globalNode); - - /** - * Get the organ name the file was saved for - * @param globalNode the root yaml node - * @return the organ name - */ - static wxString geFileOrganName(const YAML::Node &globalNode); -}; - -#endif /* GOYAMLMODELBASE_H */ diff --git a/src/grandorgue/yaml/GOYamlOutModel.cpp b/src/grandorgue/yaml/GOYamlOutModel.cpp deleted file mode 100644 index 4c8b4214e..000000000 --- a/src/grandorgue/yaml/GOYamlOutModel.cpp +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2006 Milan Digital Audio LLC - * Copyright 2009-2025 GrandOrgue contributors (see AUTHORS) - * License GPL-2.0 or later - * (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html). - */ - -#include "GOYamlOutModel.h" - -#include - -#include "GOSaveableToYaml.h" - -static const uint8_t UTF8_BOM[] = {0xEF, 0xBB, 0xBF}; - -GOYamlOutModel::GOYamlOutModel( - const wxString &organName, const wxString &contentType) - : m_GlobalNode(YAML::NodeType::Map) { - putInfoNode(contentType, organName, m_GlobalNode); -} - -GOYamlOutModel &GOYamlOutModel::operator<<( - const GOSaveableToYaml &saveableObj) { - m_GlobalNode << saveableObj; - return *this; -} - -wxString GOYamlOutModel::writeTo(const wxString &fileName) { - wxString errMsg; - wxFileOutputStream fOS(fileName); - - if (fOS.IsOk()) { - YAML::Emitter outYaml; - - outYaml << YAML::BeginDoc << m_GlobalNode; - - if ( - !fOS.WriteAll(UTF8_BOM, sizeof(UTF8_BOM)) - || !fOS.WriteAll(outYaml.c_str(), outYaml.size())) - errMsg.Printf( - wxT("Unable to write all the data to the file '%s'"), fileName); - fOS.Close(); - } else - errMsg.Printf(wxT("Unable to open the file '%s' for writing"), fileName); - return errMsg; -} diff --git a/src/grandorgue/yaml/GOYamlOutModel.h b/src/grandorgue/yaml/GOYamlOutModel.h deleted file mode 100644 index 17181263f..000000000 --- a/src/grandorgue/yaml/GOYamlOutModel.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2006 Milan Digital Audio LLC - * Copyright 2009-2025 GrandOrgue contributors (see AUTHORS) - * License GPL-2.0 or later - * (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html). - */ - -#ifndef GOYAMLOUTFILE_H -#define GOYAMLOUTFILE_H - -#include - -#include "GOYamlModelBase.h" - -class GOSaveableToYaml; - -/** - * Putting anything to a yaml file. - * - * The pattern to use: - * - * GOYamlOutModel outYaml(organName, contentType); - * - * outYaml << saveableToYaml1; - * outYaml << saveableToYaml2; - * - * wxString errorMsg = outYaml.writeTo(fileName) - */ - -class GOYamlOutModel : private GOYamlModelBase { -private: - YAML::Node m_GlobalNode; - -public: - GOYamlOutModel(const wxString &organName, const wxString &contentType); - - GOYamlOutModel &operator<<(const GOSaveableToYaml &saveableObj); - - /** - * Write the current yaml model to the text file. If something goes wrong it - * returns a non empty error message - */ - wxString writeTo(const wxString &fileName); -}; - -#endif /* GOYAMLOUTFILE_H */