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: 98e3f4ce4711
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: 036c181c1484
Choose a head ref
  • 1 commit
  • 3 files changed
  • 1 contributor

Commits on Jun 22, 2020

  1. Implemented Preference class

    nshcat committed Jun 22, 2020
    Copy the full SHA
    036c181 View commit details
Showing with 193 additions and 0 deletions.
  1. +1 −0 glscopeclient/CMakeLists.txt
  2. +77 −0 glscopeclient/Preference.cpp
  3. +115 −0 glscopeclient/Preference.h
1 change: 1 addition & 0 deletions glscopeclient/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ add_executable(glscopeclient
InstrumentConnectionDialog.cpp
OscilloscopeWindow.cpp
Program.cpp
Preference.cpp
ProtocolAnalyzerWindow.cpp
ProtocolDecoderDialog.cpp
ScopeApp.cpp
77 changes: 77 additions & 0 deletions glscopeclient/Preference.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/***********************************************************************************************************************
* *
* ANTIKERNEL v0.1 *
* *
* Copyright (c) 2012-2018 Andrew D. Zonenberg *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
* following conditions are met: *
* *
* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
* following disclaimer. *
* *
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
* following disclaimer in the documentation and/or other materials provided with the distribution. *
* *
* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
* derived from this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
***********************************************************************************************************************/

/**
@file
@author Katharina B.
@brief Implementation of Preference
*/

#include <stdexcept>

#include "Preference.h"

const std::string& Preference::get_identifier() const
{
return m_identifier;
}

const std::string& Preference::get_description() const
{
return m_description;
}

PreferenceType Preference::get_type() const
{
return m_type;
}

bool Preference::get_bool() const
{
if(m_type != PreferenceType::Boolean)
throw std::runtime_error("Preference type mismatch");

return get_value_raw<bool>();
}

double Preference::get_real() const
{
if(m_type != PreferenceType::Real)
throw std::runtime_error("Preference type mismatch");

return get_value_raw<double>();
}

const std::string& Preference::get_string() const
{
if(m_type != PreferenceType::String)
throw std::runtime_error("Preference type mismatch");

return get_value_raw<std::string>();
}
115 changes: 115 additions & 0 deletions glscopeclient/Preference.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/***********************************************************************************************************************
* *
* ANTIKERNEL v0.1 *
* *
* Copyright (c) 2012-2020 Andrew D. Zonenberg *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
* following conditions are met: *
* *
* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
* following disclaimer. *
* *
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
* following disclaimer in the documentation and/or other materials provided with the distribution. *
* *
* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
* derived from this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
***********************************************************************************************************************/

/**
@file
@author Katharina B.
@brief Basic preference class and auxilliary types
*/

#ifndef Preference_H
#define Preference_H

#include <string>
#include <type_traits>
#include <utility>

constexpr std::size_t max(std::size_t a, std::size_t b)
{
return (a > b) ? a : b;
}

enum class PreferenceType
{
Boolean,
String,
Real
};

class Preference
{
private:
using PreferenceValue = typename std::aligned_union<
max(max(sizeof(bool), sizeof(double)), sizeof(std::string)),
bool, std::string, double
>::type;

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

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

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

public:
Preference(const Preference&) = delete;
Preference(Preference&&) = default;

Preference& operator=(const Preference&) = delete;
Preference& operator=(Preference&&) = default;

public:
const std::string& get_identifier() const;
const std::string& get_description() const;
PreferenceType get_type() const;
bool get_bool() const;
double get_real() const;
const std::string& get_string() const;

private:
template<typename T>
const T& get_value_raw() const
{
return *reinterpret_cast<const T*>(&m_value);
}

private:
std::string m_identifier;
std::string m_description;
PreferenceType m_type;
PreferenceValue m_value;
};

#endif // Preference_H