Skip to content

Commit

Permalink
Add missing changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mysticial committed Oct 2, 2023
1 parent 84e6dd0 commit 40d0297
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 7 deletions.
6 changes: 5 additions & 1 deletion Common/Cpp/Options/EditableTableOption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace PokemonAutomation{


EditableTableRow::EditableTableRow()
EditableTableRow::EditableTableRow(void*)
: m_index((size_t)0 - 1)
{}
void EditableTableRow::add_option(ConfigOption& option, std::string serialization_string){
Expand Down Expand Up @@ -99,6 +99,10 @@ EditableTableOption::EditableTableOption(
{
restore_defaults();
}
void EditableTableOption::set_default(std::vector<std::unique_ptr<EditableTableRow>> default_value){
SpinLockGuard lg(m_lock);
m_default = std::move(default_value);
}
size_t EditableTableOption::current_rows() const{
SpinLockGuard lg(m_lock);
return m_current.size();
Expand Down
11 changes: 8 additions & 3 deletions Common/Cpp/Options/EditableTableOption.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace PokemonAutomation{
class EditableTableRow{
public:
virtual ~EditableTableRow() = default;
EditableTableRow();
EditableTableRow(void* = nullptr);

// Duplicate/deep-copy the entire row. Does not copy over listeners.
virtual std::unique_ptr<EditableTableRow> clone() const = 0;
Expand Down Expand Up @@ -74,6 +74,7 @@ class EditableTableOption : public ConfigOption{
bool enable_saveload,
std::vector<std::unique_ptr<EditableTableRow>> default_value = {}
);
void set_default(std::vector<std::unique_ptr<EditableTableRow>> default_value);

const std::string& label() const{ return m_label; }

Expand Down Expand Up @@ -135,7 +136,7 @@ class EditableTableOption : public ConfigOption{
private:
const std::string m_label;
const bool m_enable_saveload;
const std::vector<std::unique_ptr<EditableTableRow>> m_default;
std::vector<std::unique_ptr<EditableTableRow>> m_default;

mutable SpinLock m_lock;
uint64_t m_seqnum = 0;
Expand All @@ -159,7 +160,11 @@ class EditableTableOption_t : public EditableTableOption{
}

virtual std::unique_ptr<EditableTableRow> make_row() const override{
return std::unique_ptr<EditableTableRow>(new RowType());
if constexpr (std::is_default_constructible_v<RowType>){
return std::unique_ptr<EditableTableRow>(new RowType());
}else{
return std::unique_ptr<EditableTableRow>(new RowType(this));
}
}
};

Expand Down
148 changes: 148 additions & 0 deletions Common/Cpp/Options/IntegerRangeOption.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/* Integer Range Option
*
* From: https://github.com/PokemonAutomation/Arduino-Source
*
* This option is thread-safe.
*
*/

#include <limits>
#include <atomic>
#include "Common/Cpp/Exceptions.h"
#include "IntegerRangeOption.h"

namespace PokemonAutomation{



template <typename Type>
struct IntegerRangeCell<Type>::Data{
const Type m_lo_min_value;
const Type m_lo_max_value;
const Type m_hi_min_value;
const Type m_hi_max_value;
const Type m_lo_default;
const Type m_hi_default;
std::atomic<Type> m_lo_current;
std::atomic<Type> m_hi_current;

Data(
Type lo_min_value, Type lo_max_value, Type lo_default_value, Type lo_current_value,
Type hi_min_value, Type hi_max_value, Type hi_default_value, Type hi_current_value
)
: m_lo_min_value(lo_min_value), m_lo_max_value(lo_max_value)
, m_hi_min_value(hi_min_value), m_hi_max_value(hi_max_value)
, m_lo_default(lo_default_value)
, m_hi_default(hi_default_value)
, m_lo_current(lo_current_value)
, m_hi_current(hi_current_value)
{
if (lo_min_value > lo_max_value){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Limits are incompatible.");
}
if (hi_min_value > hi_max_value){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Limits are incompatible.");
}
if (!(lo_min_value <= lo_default_value && lo_default_value <= lo_max_value)){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Low default is out of range.");
}
if (!(hi_min_value <= hi_default_value && hi_default_value <= hi_max_value)){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "High default is out of range.");
}
if (lo_default_value > hi_default_value){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Default values are incompatible.");
}
if (lo_current_value > hi_current_value){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Current values are incompatible.");
}
}
};



template <typename Type>
IntegerRangeCell<Type>::~IntegerRangeCell() = default;
template <typename Type>
IntegerRangeCell<Type>::IntegerRangeCell(const IntegerRangeCell& x)
: ConfigOption(x)
, m_data(
CONSTRUCT_TOKEN,
x.lo_min_value(), x.lo_max_value(), x.lo_default_value(), x.lo_current_value(),
x.hi_min_value(), x.hi_max_value(), x.hi_default_value(), x.hi_current_value()
)
{}
template <typename Type>
IntegerRangeCell<Type>::IntegerRangeCell(
LockWhileRunning lock_while_running,
Type lo_min_value, Type lo_max_value, Type lo_default_value, Type lo_current_value,
Type hi_min_value, Type hi_max_value, Type hi_default_value, Type hi_current_value
)
: ConfigOption(lock_while_running)
, m_data(
CONSTRUCT_TOKEN,
lo_min_value, lo_max_value, lo_default_value, lo_current_value,
hi_min_value, hi_max_value, hi_default_value, hi_current_value
)
{}

template <typename Type>
Type IntegerRangeCell<Type>::lo_min_value() const{
return m_data->m_lo_min_value;
}
template <typename Type>
Type IntegerRangeCell<Type>::lo_max_value() const{
return m_data->m_lo_max_value;
}
template <typename Type>
Type IntegerRangeCell<Type>::lo_default_value() const{
return m_data->m_lo_default;
}
template <typename Type>
Type IntegerRangeCell<Type>::lo_current_value() const{
return m_data->m_lo_current.load(std::memory_order_relaxed);
}

template <typename Type>
Type IntegerRangeCell<Type>::hi_min_value() const{
return m_data->m_hi_min_value;

}
template <typename Type>
Type IntegerRangeCell<Type>::hi_max_value() const{
return m_data->m_hi_max_value;
}
template <typename Type>
Type IntegerRangeCell<Type>::hi_default_value() const{
return m_data->m_hi_default;
}
template <typename Type>
Type IntegerRangeCell<Type>::hi_current_value() const{
return m_data->m_hi_current.load(std::memory_order_relaxed);
}

template <typename Type>
void IntegerRangeCell<Type>::set_lo(Type x){
x = std::max(x, lo_min_value());
x = std::min(x, lo_max_value());
x = std::min(x, hi_current_value());
}
template <typename Type>
void IntegerRangeCell<Type>::set_hi(Type x){
x = std::max(x, hi_min_value());
x = std::min(x, hi_max_value());
x = std::max(x, lo_current_value());

}







template class IntegerRangeCell<uint8_t>;



}

61 changes: 61 additions & 0 deletions Common/Cpp/Options/IntegerRangeOption.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* Integer Range Option
*
* From: https://github.com/PokemonAutomation/Arduino-Source
*
* This option is thread-safe.
*
*/

#ifndef PokemonAutomation_Options_IntegerRangeOption_H
#define PokemonAutomation_Options_IntegerRangeOption_H

#include "Common/Cpp/Containers/Pimpl.h"
#include "ConfigOption.h"

namespace PokemonAutomation{


template <typename Type>
class IntegerRangeCell : public ConfigOption{
public:
~IntegerRangeCell();
IntegerRangeCell(const IntegerRangeCell& x);
IntegerRangeCell(
LockWhileRunning lock_while_running,
Type lo_min_value, Type lo_max_value, Type lo_default_value, Type lo_current_value,
Type hi_min_value, Type hi_max_value, Type hi_default_value, Type hi_current_value
);

Type lo_min_value() const;
Type lo_max_value() const;
Type lo_default_value() const;
Type lo_current_value() const;

Type hi_min_value() const;
Type hi_max_value() const;
Type hi_default_value() const;
Type hi_current_value() const;

virtual void set_lo(Type x);
virtual void set_hi(Type x);

virtual void load_json(const JsonValue& json) override;
virtual JsonValue to_json() const override;

std::string check_validity(Type x) const;
virtual std::string check_validity() const override;
virtual void restore_defaults() override;

public:
virtual ConfigWidget* make_QtWidget(QWidget& parent) override;

protected:
struct Data;
Pimpl<Data> m_data;
};



}
#endif

8 changes: 5 additions & 3 deletions Common/Qt/Options/SimpleIntegerWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
#include "ConfigWidget.h"
#include "SimpleIntegerWidget.h"

//#include <iostream>
//using std::cout;
//using std::endl;
// REMOVE
#include <iostream>
using std::cout;
using std::endl;

namespace PokemonAutomation{

Expand All @@ -29,6 +30,7 @@ SimpleIntegerCellWidget<Type>::SimpleIntegerCellWidget(QWidget& parent, SimpleIn
, ConfigWidget(value, *this)
, m_value(value)
{
cout << "sizeHint() = " << this->sizeHint().width() << endl;
connect(
this, &QLineEdit::textChanged,
this, [this](const QString& text){
Expand Down

0 comments on commit 40d0297

Please sign in to comment.