Skip to content

Commit

Permalink
Introduced a new type GOBool3 for working with three-valued booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg68 committed Jan 17, 2024
1 parent ca45a49 commit f856002
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
45 changes: 45 additions & 0 deletions src/core/GOBool3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2006 Milan Digital Audio LLC
* Copyright 2009-2024 GrandOrgue contributors (see AUTHORS)
* License GPL-2.0 or later
* (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html).
*/

#ifndef GOBOOL3_H
#define GOBOOL3_H

#include <cstdint>

// 3-value booleans
enum GOBool3 : int8_t {
BOOL3_UNDEF = -1,
BOOL3_FALSE = 0, // must be the same as ``(int8_t) false``
BOOL3_TRUE = 1, // must be the same as ``(int8_t) true``
BOOL3_MIN = BOOL3_UNDEF,
BOOL3_MAX = BOOL3_TRUE
};

// conversion from any number and bool
template <typename T> GOBool3 to_bool3(T from) { return (GOBool3)(int8_t)from; }

#define CONVERT_BOOL3_TO_BOOL(fromBool3, defaultBoolExpr) \
fromBool3 >= BOOL3_FALSE ? (bool)(int8_t)(fromBool3) : defaultBoolExpr

// conversion with lazy calculation of default
template <typename F> inline bool to_bool(GOBool3 from, F defaultFunc) {
return CONVERT_BOOL3_TO_BOOL(from, defaultFunc());
}

// conversion with a simple default
inline bool to_bool(GOBool3 from, bool defaultValue = false) {
/*
we don't implement it as
``return to_bool(from, [&]() { return defaultValue; });``
because the performance reason: gcc does't inline calling a trivial lambda
*/
return CONVERT_BOOL3_TO_BOOL(from, defaultValue);
}

#undef CONVERT_BOOL3_TO_BOOL

#endif /* GOBOOL3_H */
21 changes: 10 additions & 11 deletions src/core/config/GOConfigReader.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2006 Milan Digital Audio LLC
* Copyright 2009-2023 GrandOrgue contributors (see AUTHORS)
* Copyright 2009-2024 GrandOrgue contributors (see AUTHORS)
* License GPL-2.0 or later
* (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html).
*/
Expand All @@ -11,8 +11,9 @@
#include <wx/intl.h>
#include <wx/log.h>

#include "GOBool3.h"
#include "GOConfigReaderDB.h"
#include "GOUtil.h"
#include "config/GOConfigReaderDB.h"

GOConfigReader::GOConfigReader(
GOConfigReaderDB &cfg, bool strict, bool hw1Check)
Expand Down Expand Up @@ -156,15 +157,15 @@ bool GOConfigReader::ReadBoolean(
return ReadBoolean(type, group, key, required, false);
}

int GOConfigReader::ReadBooleanTriple(
GOBool3 GOConfigReader::ReadBooleanTriple(
GOSettingType type,
const wxString &group,
const wxString &key,
bool required) {
wxString value;

if (!Read(type, group, key, required, value))
return -1;
return BOOL3_UNDEF;

if (value.length() > 0 && value[value.length() - 1] == ' ') {
if (m_Strict)
Expand All @@ -176,19 +177,19 @@ int GOConfigReader::ReadBooleanTriple(
value.Trim();
}
if (value == wxT("Y") || value == wxT("y"))
return 1;
return BOOL3_TRUE;
if (value == wxT("N") || value == wxT("n"))
return 0;
return BOOL3_FALSE;
value.MakeUpper();
wxLogWarning(
_("Strange boolean value for section '%s' entry '%s': %s"),
group.c_str(),
key.c_str(),
value.c_str());
if (value.Length() && value[0] == wxT('Y'))
return 1;
return BOOL3_TRUE;
else if (value.Length() && value[0] == wxT('N'))
return 0;
return BOOL3_FALSE;

wxString error;
error.Printf(
Expand All @@ -205,9 +206,7 @@ bool GOConfigReader::ReadBoolean(
const wxString &key,
bool required,
bool defaultValue) {
int tripleValue = ReadBooleanTriple(type, group, key, required);

return tripleValue < 0 ? defaultValue : (tripleValue);
return to_bool(ReadBooleanTriple(type, group, key, required), defaultValue);
}

GOLogicalColour GOConfigReader::ReadColor(
Expand Down
13 changes: 11 additions & 2 deletions src/core/config/GOConfigReader.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2006 Milan Digital Audio LLC
* Copyright 2009-2023 GrandOrgue contributors (see AUTHORS)
* Copyright 2009-2024 GrandOrgue contributors (see AUTHORS)
* License GPL-2.0 or later
* (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html).
*/
Expand All @@ -13,6 +13,7 @@
#include <wx/hashmap.h>
#include <wx/string.h>

#include "GOBool3.h"
#include "GOLogicalColour.h"

class GOConfigReaderDB;
Expand Down Expand Up @@ -47,7 +48,7 @@ class GOConfigReader {
/**
* Reads a triple-value boolean (-1 - not defined, 0 - false, 1 - true)
*/
int ReadBooleanTriple(
GOBool3 ReadBooleanTriple(
GOSettingType type,
const wxString &group,
const wxString &key,
Expand Down Expand Up @@ -127,6 +128,14 @@ class GOConfigReader {
int nmax,
bool required,
int defaultValue);
GOBool3 ReadBool3FromInt(
GOSettingType type,
const wxString &group,
const wxString &key,
bool required) {
return to_bool3(ReadInteger(
type, group, key, BOOL3_MIN, BOOL3_MAX, required, BOOL3_UNDEF));
}
int ReadLong(
GOSettingType type,
const wxString &group,
Expand Down

0 comments on commit f856002

Please sign in to comment.