Skip to content

Commit

Permalink
Adress some code smells (#110)
Browse files Browse the repository at this point in the history
* fix std namespace usage

* cleanup some unnecessary constructors

* switch typedef to using in C++

* fix const

* change import method and signal names

* fix some uninitialized variables

* use enum class

* use template instead of std::function wherever possible

* more smells

* few more

* and more
  • Loading branch information
Neverous authored Oct 28, 2023
1 parent 1663967 commit 0d34869
Show file tree
Hide file tree
Showing 22 changed files with 229 additions and 225 deletions.
19 changes: 9 additions & 10 deletions include/bootentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class HD
uint64_t partition_size = 0;
uint32_t partition_number = 0;
uint8_t partition_format = 0;
uint8_t signature_type = 9;
EFIBoot::File_path::HD::SIGNATURE signature_type = EFIBoot::File_path::HD::SIGNATURE::NONE;

public:
HD() = default;
Expand Down Expand Up @@ -353,27 +353,27 @@ class End
mutable QString string = "";

public:
EFIBoot::EFIDP_END _subtype = EFIBoot::EFIDP_END_ENTIRE;
uint8_t _subtype = EFIBoot::File_path::End_entire::SUBTYPE;

public:
End() = default;
End(const EFIBoot::File_path::End_instance &)
: _subtype{EFIBoot::EFIDP_END_INSTANCE}
: _subtype{EFIBoot::File_path::End_instance::SUBTYPE}
{
}
End(const EFIBoot::File_path::End_entire &)
: _subtype{EFIBoot::EFIDP_END_ENTIRE}
: _subtype{EFIBoot::File_path::End_entire::SUBTYPE}
{
}
EFIBoot::File_path::ANY toEFIBootFilePath() const
{
switch(_subtype)
{
case EFIBoot::EFIDP_END_INSTANCE:
case EFIBoot::File_path::End_instance::SUBTYPE:
return EFIBoot::File_path::End_instance{};
break;

case EFIBoot::EFIDP_END_ENTIRE:
case EFIBoot::File_path::End_entire::SUBTYPE:
return EFIBoot::File_path::End_entire{};
break;
}
Expand Down Expand Up @@ -412,7 +412,7 @@ class Unknown
QString toString(bool refresh = true) const;
};

typedef std::variant<
using ANY = std::variant<
PCI,
HID,
USB,
Expand All @@ -427,8 +427,7 @@ typedef std::variant<
FirmwareVolume,
BIOSBootSpecification,
End,
Unknown>
ANY;
Unknown>;

inline std::unordered_map<QString, std::function<std::optional<ANY>(const QJsonObject &)>> JSON_readers()
{
Expand Down Expand Up @@ -479,7 +478,7 @@ class BootEntry
QString description = "New entry";
QString error = "";
QString optional_data = "";
uint32_t attributes = EFIBoot::Load_option_attribute::EMPTY;
EFIBoot::Load_option_attribute attributes = EFIBoot::Load_option_attribute::EMPTY;
uint32_t efi_attributes = EFIBoot::EFI_VARIABLE_ATTRIBUTE_DEFAULTS;
uint16_t index = 0;
OptionalDataFormat optional_data_format = OptionalDataFormat::Base64;
Expand Down
2 changes: 1 addition & 1 deletion include/bootentrydelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BootEntryDelegate: public QWidgetItemDelegate<BootEntryWidget, const BootE
BootEntryListModel::Options options{};

public:
BootEntryDelegate();
BootEntryDelegate() = default;
BootEntryDelegate(const BootEntryDelegate &) = delete;
BootEntryDelegate &operator=(const BootEntryDelegate &) = delete;

Expand Down
2 changes: 1 addition & 1 deletion include/bootentryform.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ private slots:
void setAttribute(int state);

private:
uint32_t getAttributes() const;
EFIBoot::Load_option_attribute getAttributes() const;
};
4 changes: 1 addition & 3 deletions include/bootentrylistmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include <QList>
#include <QUndoStack>

typedef std::function<bool(BootEntry &)> Change_fn;

class BootEntryListModel: public QAbstractListModel
{
Q_OBJECT
Expand Down Expand Up @@ -72,7 +70,7 @@ class BootEntryListModel: public QAbstractListModel
void setEntryDescription(const QModelIndex &index, const QString &text);
bool changeEntryOptionalDataFormat(const QModelIndex &index, int format);
void setEntryOptionalData(const QModelIndex &index, const QString &text);
void setEntryAttributes(const QModelIndex &index, uint32_t value);
void setEntryAttributes(const QModelIndex &index, EFIBoot::Load_option_attribute value);
void setEntryNextBoot(const QModelIndex &index, bool value);

Qt::ItemFlags flags(const QModelIndex &index) const override;
Expand Down
22 changes: 17 additions & 5 deletions include/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
template <class Type>
class SetEFIBootDataValueCommand: public QUndoCommand
{
typedef Type EFIBootData::*PropertyPtr;
typedef void (EFIBootData::*SignalPtr)(const Type &);
using PropertyPtr = Type EFIBootData::*;
using SignalPtr = void (EFIBootData::*)(const Type &);

EFIBootData &data;
const QString name;
Expand Down Expand Up @@ -129,10 +129,22 @@ class MoveBootEntryCommand: public QUndoCommand
bool mergeWith(const QUndoCommand *command) override;
};

template <class Type>
struct type_identity
{
using type = Type;
};

template <class Type>
struct underlying_type
{
using type = typename std::conditional_t<std::is_enum_v<Type>, std::underlying_type<Type>, type_identity<Type>>::type;
};

template <class Type>
class SetBootEntryValueCommand: public QUndoCommand
{
typedef Type BootEntry::*PropertyPtr;
using PropertyPtr = Type BootEntry::*;

BootEntryListModel &model;
const QString title;
Expand All @@ -151,7 +163,7 @@ class SetBootEntryValueCommand: public QUndoCommand
, property{property_}
, value{value_}
{
setText(QObject::tr("Change %1 entry \"%2\" %3 to \"%4\"").arg(model.name, title, name).arg(value));
setText(QObject::tr("Change %1 entry \"%2\" %3 to \"%4\"").arg(model.name, title, name).arg(static_cast<typename underlying_type<Type>::type>(value)));
}

SetBootEntryValueCommand(const SetBootEntryValueCommand &) = delete;
Expand Down Expand Up @@ -192,7 +204,7 @@ class SetBootEntryValueCommand: public QUndoCommand
if(value == entry.*property)
setObsolete(true);

setText(QObject::tr("Change %1 entry \"%2\" %3 to \"%4\"").arg(model.name, title, name).arg(entry.*property));
setText(QObject::tr("Change %1 entry \"%2\" %3 to \"%4\"").arg(model.name, title, name).arg(static_cast<typename underlying_type<Type>::type>(entry.*property)));
return true;
}
};
Expand Down
32 changes: 14 additions & 18 deletions include/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,34 +92,30 @@ inline int _tcserror_s(TCHAR *buffer, size_t size, int errnum)
#endif

/* string types */
namespace std
{
typedef basic_string<TCHAR> tstring;
using tstring = std::basic_string<TCHAR>;

typedef basic_string_view<TCHAR> tstring_view;
using tstring_view = std::basic_string_view<TCHAR>;

typedef basic_ostream<TCHAR> tostream;
typedef basic_istream<TCHAR> tistream;
typedef basic_iostream<TCHAR> tiostream;
using tostream = std::basic_ostream<TCHAR>;
using tistream = std::basic_istream<TCHAR>;
using tiostream = std::basic_iostream<TCHAR>;

typedef basic_ifstream<TCHAR> tifstream;
typedef basic_ofstream<TCHAR> tofstream;
typedef basic_fstream<TCHAR> tfstream;
using tifstream = std::basic_ifstream<TCHAR>;
using tofstream = std::basic_ofstream<TCHAR>;
using tfstream = std::basic_fstream<TCHAR>;

typedef basic_stringstream<TCHAR> tstringstream;
using tstringstream = std::basic_stringstream<TCHAR>;

template <class Type>
inline tstring to_tstring(const Type &value)
{
#if defined(UNICODE) || defined(_UNICODE)
return to_wstring(value);
return std::to_wstring(value);
#else
return to_string(value);
return std::to_string(value);
#endif
}

}

const int HEX_BASE = 16;

/* Qt compatibility */
Expand All @@ -137,7 +133,7 @@ struct hash<QString>
#endif

/* QString helpers */
inline std::tstring QStringToStdTString(const QString &string)
inline tstring QStringToStdTString(const QString &string)
{
#if defined(UNICODE) || defined(_UNICODE)
return string.toStdWString();
Expand All @@ -155,7 +151,7 @@ inline QString QStringFromTCharArray(const TCHAR *string)
#endif
}

inline QString QStringFromStdTString(const std::tstring &string)
inline QString QStringFromStdTString(const tstring &string)
{
#if defined(UNICODE) || defined(_UNICODE)
return QString::fromStdWString(string);
Expand All @@ -170,7 +166,7 @@ inline QString toHex(unsigned long long number, int min_width = 0, const QString
return prefix + QString("%1").arg(number, min_width, HEX_BASE, QChar('0')).toUpper();
}

inline bool isxnumber(const std::tstring_view &string)
inline bool isxnumber(const tstring_view &string)
{
#if defined(UNICODE) || defined(_UNICODE)
return std::all_of(std::begin(string), std::end(string), [](char16_t chr)
Expand Down
Loading

0 comments on commit 0d34869

Please sign in to comment.