Skip to content

Commit

Permalink
Simplified classes
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg68 committed Mar 8, 2025
1 parent 2b5c12e commit 9000258
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 243 deletions.
4 changes: 1 addition & 3 deletions src/grandorgue/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/grandorgue/GOOrganController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand All @@ -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(),
Expand Down
50 changes: 0 additions & 50 deletions src/grandorgue/yaml/GOYamlInModel.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,29 @@
* (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html).
*/

#include "GOYamlInModel.h"
#include "GOYamlModel.h"

#include <wx/datetime.h>
#include <wx/intl.h>
#include <wx/wfstream.h>
#include <yaml-cpp/yaml.h>

#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<wxString>();
}

static std::vector<char> load_file_bytes(const wxString &filePath) {
wxFile file;
Expand Down Expand Up @@ -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(
Expand All @@ -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;
}
82 changes: 82 additions & 0 deletions src/grandorgue/yaml/GOYamlModel.h
Original file line number Diff line number Diff line change
@@ -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 <wx/string.h>
#include <yaml-cpp/yaml.h>

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 */
45 changes: 0 additions & 45 deletions src/grandorgue/yaml/GOYamlModelBase.cpp

This file was deleted.

45 changes: 0 additions & 45 deletions src/grandorgue/yaml/GOYamlModelBase.h

This file was deleted.

Loading

0 comments on commit 9000258

Please sign in to comment.