Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ngscopeclient/scopehal-apps
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 4c57f744808f
Choose a base ref
...
head repository: ngscopeclient/scopehal-apps
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 40e2c17ac799
Choose a head ref
  • 2 commits
  • 7 files changed
  • 1 contributor

Commits on Oct 30, 2020

  1. Copy the full SHA
    6196c2e View commit details
  2. Merge pull request #266 from azonenberg/kathi_dev

    Reworked preference construction
    nshcat authored Oct 30, 2020
    Copy the full SHA
    40e2c17 View commit details
1 change: 1 addition & 0 deletions src/glscopeclient/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -53,6 +53,7 @@ add_executable(glscopeclient
PreferenceTree.cpp
PreferenceManager.cpp
PreferenceDialog.cpp
PreferenceSchema.cpp
ProtocolAnalyzerWindow.cpp
ScopeApp.cpp
ScopeSyncWizard.cpp
150 changes: 109 additions & 41 deletions src/glscopeclient/Preference.cpp
Original file line number Diff line number Diff line change
@@ -47,24 +47,81 @@ namespace impl

}

PreferenceBuilder&& PreferenceBuilder::IsVisible(bool isVisible) &&
PreferenceBuilder PreferenceBuilder::Invisible() &&
{
this->m_pref.m_isVisible = isVisible;
this->m_pref.m_isVisible = false;
return move(*this);
}

PreferenceBuilder&& PreferenceBuilder::WithUnit(Unit::UnitType type) &&
PreferenceBuilder PreferenceBuilder::Label(std::string label) &&
{
this->m_pref.m_unit = Unit{ type };
this->m_pref.m_label = std::move(label);
return move(*this);
}

Preference&& PreferenceBuilder::Build() &&
PreferenceBuilder PreferenceBuilder::Description(std::string description) &&
{
this->m_pref.m_description = std::move(description);
return move(*this);
}

PreferenceBuilder PreferenceBuilder::Unit(Unit::UnitType type) &&
{
this->m_pref.m_unit = ::Unit{ type };
return move(*this);
}

Preference PreferenceBuilder::Build() &&
{
return move(this->m_pref);
}
}

void EnumMapping::AddEnumMember(const std::string& name, base_type value)
{
if(this->m_forwardMap.count(name) != 0)
throw std::runtime_error("Enum mapping already contains given enum value");

this->m_forwardMap.insert(std::make_pair(name, value));
this->m_backwardMap.insert(std::make_pair(value, name));
this->m_names.push_back(name);
}

const std::string& EnumMapping::GetName(base_type value)
{
const auto it = this->m_backwardMap.find(value);

if(it == this->m_backwardMap.end())
throw std::runtime_error("Enum mapping doesnt contain requested entry");

return it->second;
}

EnumMapping::base_type EnumMapping::GetValue(const std::string& name)
{
const auto it = this->m_forwardMap.find(name);

if(it == this->m_forwardMap.end())
throw std::runtime_error("Enum mapping doesnt contain requested entry");

return it->second;
}

const std::vector<std::string>& EnumMapping::GetNames()
{
return this->m_names;
}

void Preference::SetLabel(std::string label)
{
this->m_label = std::move(label);
}

void Preference::SetDescription(std::string description)
{
this->m_description = std::move(description);
}

const string& Preference::GetIdentifier() const
{
return m_identifier;
@@ -142,7 +199,7 @@ const std::string& Preference::GetString() const

void Preference::CleanUp()
{
if(m_type == PreferenceType::String)
if(m_hasValue && m_type == PreferenceType::String)
(reinterpret_cast<string*>(&m_value))->~basic_string();
}

@@ -171,27 +228,31 @@ void Preference::MoveFrom(Preference& other)
m_label = move(other.m_label);
m_isVisible = move(other.m_isVisible);
m_unit = move(other.m_unit);
m_hasValue = move(other.m_hasValue);

switch(other.m_type)
if(m_hasValue)
{
case PreferenceType::Boolean:
Construct<bool>(other.GetBool());
break;

case PreferenceType::Real:
Construct<double>(other.GetReal());
break;

case PreferenceType::String:
Construct<string>(move(other.GetValueRaw<string>()));
break;

case PreferenceType::Color:
Construct<impl::Color>(move(other.GetValueRaw<impl::Color>()));
break;

default:
break;
switch(other.m_type)
{
case PreferenceType::Boolean:
Construct<bool>(other.GetBool());
break;

case PreferenceType::Real:
Construct<double>(other.GetReal());
break;

case PreferenceType::String:
Construct<string>(move(other.GetValueRaw<string>()));
break;

case PreferenceType::Color:
Construct<impl::Color>(move(other.GetValueRaw<impl::Color>()));
break;

default:
break;
}
}

other.m_type = PreferenceType::None;
@@ -234,30 +295,37 @@ void Preference::SetColorRaw(const impl::Color& color)
Construct<impl::Color>(color);
}

impl::PreferenceBuilder Preference::New(std::string identifier, std::string label, std::string description, bool defaultValue)
impl::PreferenceBuilder Preference::Real(std::string identifier, double defaultValue)
{
return impl::PreferenceBuilder(
Preference(std::move(identifier), std::move(label), std::move(description), defaultValue)
);
Preference pref(PreferenceType::Real, std::move(identifier));
pref.Construct<double>(defaultValue);

return impl::PreferenceBuilder{ std::move(pref) };
}

impl::PreferenceBuilder Preference::New(std::string identifier, std::string label, std::string description, double defaultValue)
impl::PreferenceBuilder Preference::Bool(std::string identifier, bool defaultValue)
{
return impl::PreferenceBuilder(
Preference(std::move(identifier), std::move(label), std::move(description), defaultValue)
);
Preference pref(PreferenceType::Boolean, std::move(identifier));
pref.Construct<bool>(defaultValue);

return impl::PreferenceBuilder{ std::move(pref) };
}

impl::PreferenceBuilder Preference::New(std::string identifier, std::string label, std::string description, const char* defaultValue)
impl::PreferenceBuilder Preference::String(std::string identifier, std::string defaultValue)
{
return impl::PreferenceBuilder(
Preference(std::move(identifier), std::move(label), std::move(description), defaultValue)
);
Preference pref(PreferenceType::String, std::move(identifier));
pref.Construct<std::string>(defaultValue);

return impl::PreferenceBuilder{ std::move(pref) };
}

impl::PreferenceBuilder Preference::New(std::string identifier, std::string label, std::string description, std::string defaultValue)
impl::PreferenceBuilder Preference::Color(std::string identifier, const Gdk::Color& defaultValue)
{
return impl::PreferenceBuilder(
Preference(std::move(identifier), std::move(label), std::move(description), std::move(defaultValue))
);
Preference pref(PreferenceType::Color, std::move(identifier));
pref.Construct<impl::Color>(impl::Color{
defaultValue.get_red(), defaultValue.get_green(), defaultValue.get_blue()
});

return impl::PreferenceBuilder{ std::move(pref) };
}

98 changes: 54 additions & 44 deletions src/glscopeclient/Preference.h
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@
#include <string>
#include <type_traits>
#include <utility>
#include <type_traits>
#include <cstdint>

#include <giomm.h>
@@ -76,6 +77,42 @@ namespace impl
};
}

class EnumMapping
{
using base_type = signed long long;

public:
EnumMapping()
{

}

public:
void AddEnumMember(const std::string& name, base_type value);
const std::string& GetName(base_type value);
base_type GetValue(const std::string& name);
const std::vector<std::string>& GetNames();

public:
template<
typename E,
typename = typename std::enable_if<
std::is_convertible<E, base_type>::value &&
not std::is_same<E, base_type>::value
>::type
>
void AddEnumMember(const std::string& name, E value)
{
const auto val = static_cast<base_type>(value);
this->AddEnumMember(name, val);
}

protected:
std::map<std::string, base_type> m_forwardMap;
std::map<base_type, std::string> m_backwardMap;
std::vector<std::string> m_names;
};

class Preference
{
friend class impl::PreferenceBuilder;
@@ -87,54 +124,17 @@ class Preference
>::type;

public:
// Taking string as value and then moving is intended
Preference(std::string identifier, std::string label, std::string description, bool defaultValue)
: m_identifier{std::move(identifier)}, m_label{std::move(label)}, m_description{std::move(description)},
m_type{PreferenceType::Boolean}
{
new (&m_value) bool(defaultValue);
}

Preference(std::string identifier, std::string label, std::string description, std::string defaultValue)
: m_identifier{std::move(identifier)}, m_label{std::move(label)}, m_description{std::move(description)},
m_type{PreferenceType::String}
Preference(PreferenceType type, std::string identifier)
: m_identifier{std::move(identifier)}, m_type{type}
{
new (&m_value) std::string(std::move(defaultValue));
}

Preference(std::string identifier, std::string label, std::string description, const char* defaultValue)
: m_identifier{std::move(identifier)}, m_label{std::move(label)}, m_description{std::move(description)},
m_type{PreferenceType::String}
{
new (&m_value) std::string(defaultValue);
}

Preference(std::string identifier, std::string label, std::string description, double defaultValue)
: m_identifier{std::move(identifier)}, m_label{std::move(label)}, m_description{std::move(description)},
m_type{PreferenceType::Real}
{
new (&m_value) double(defaultValue);
}

Preference(std::string identifier, std::string label, std::string description, const Gdk::Color& defaultValue)
: m_identifier{std::move(identifier)}, m_label{std::move(label)}, m_description{std::move(description)},
m_type{PreferenceType::Color}
{
new (&m_value) impl::Color(defaultValue.get_red(), defaultValue.get_green(), defaultValue.get_blue());
}

~Preference()
{
CleanUp();
}

public:
static impl::PreferenceBuilder New(std::string identifier, std::string label, std::string description, bool defaultValue);
static impl::PreferenceBuilder New(std::string identifier, std::string label, std::string description, double defaultValue);
static impl::PreferenceBuilder New(std::string identifier, std::string label, std::string description, const char* defaultValue);
static impl::PreferenceBuilder New(std::string identifier, std::string label, std::string description, std::string defaultValue);
static impl::PreferenceBuilder New(std::string identifier, std::string label, std::string description, const Gdk::Color& defaultValue);

public:
Preference(const Preference&) = delete;
Preference(Preference&& other)
@@ -162,15 +162,21 @@ class Preference
bool GetIsVisible() const;
Gdk::Color GetColor() const;
const impl::Color& GetColorRaw() const;

void SetBool(bool value);
void SetReal(double value);
void SetString(std::string value);
void SetColor(const Gdk::Color& value);
void SetColorRaw(const impl::Color& value);

void SetLabel(std::string label);
void SetDescription(std::string description);
bool HasUnit();
Unit& GetUnit();

public:
static impl::PreferenceBuilder Real(std::string identifier, double defaultValue);
static impl::PreferenceBuilder Bool(std::string identifier, bool defaultValue);
static impl::PreferenceBuilder String(std::string identifier, std::string defaultValue);
static impl::PreferenceBuilder Color(std::string identifier, const Gdk::Color& defaultValue);

private:
template<typename T>
@@ -191,6 +197,7 @@ class Preference
void Construct(T value)
{
new (&m_value) T(std::move(value));
m_hasValue = true;
}

void MoveFrom(Preference& other);
@@ -203,6 +210,7 @@ class Preference
PreferenceValue m_value;
bool m_isVisible{true};
Unit m_unit{Unit::UNIT_COUNTS};
bool m_hasValue{false};
};

namespace impl
@@ -213,9 +221,11 @@ namespace impl
PreferenceBuilder(Preference&& pref);

public:
PreferenceBuilder&& IsVisible(bool isVisible) &&;
PreferenceBuilder&& WithUnit(Unit::UnitType type) &&;
Preference&& Build() &&;
PreferenceBuilder Invisible() &&;
PreferenceBuilder Label(std::string label) &&;
PreferenceBuilder Description(std::string description) &&;
PreferenceBuilder Unit(Unit::UnitType type) &&;
Preference Build() &&;

protected:
Preference m_pref;
Loading