TGAHNK YOU C++ VERY COOL

This commit is contained in:
alison wright 2019-10-08 23:43:35 -04:00
parent dab41d87ef
commit 881fa8c9bd
7 changed files with 184 additions and 164 deletions

View File

@ -25,6 +25,9 @@ inline constexpr std::nullopt_t None{std::nullopt};
template<typename T> template<typename T>
using Option = std::optional<T>; using Option = std::optional<T>;
template<typename... Ts>
using Variant = std::variant<Ts...>;
struct Error : public std::runtime_error { struct Error : public std::runtime_error {
using std::runtime_error::runtime_error; using std::runtime_error::runtime_error;
}; };
@ -122,17 +125,20 @@ static inline QDebug operator<<(QDebug debug, std::string const &t) {
return debug; return debug;
} }
template<typename T>
static inline QDebug operator<<(QDebug debug, std::unique_ptr<T> const &t) {
debug << *t;
return debug;
}
template<typename T0, typename... Ts> template<typename T0, typename... Ts>
static inline QDebug operator<<(QDebug debug, static inline QDebug operator<<(QDebug debug, Variant<T0, Ts...> const &t) {
std::variant<T0, Ts...> const &t) {
std::visit([&](auto &&arg) {debug << arg;}, t); std::visit([&](auto &&arg) {debug << arg;}, t);
return debug; return debug;
} }
template<typename T>
static inline QDebug operator<<(QDebug debug, Option<T> const &t) {
if(t) {
debug << *t;
} else {
debug << "None";
}
return debug;
}
// EOF // EOF

View File

@ -6,148 +6,150 @@
#include <QIcon> #include <QIcon>
namespace Arc { namespace Arc {
Option<ArcType> getArchiveType(std::istream &st) noexcept {
Option<ArchiveType> getArchiveType(std::istream &st) noexcept { try {
try { auto pos = st.tellg();
auto pos = st.tellg(); auto magic = readBytes<4>(st);
auto magic = readBytes<4>(st); st.seekg(pos);
st.seekg(pos); if(magic == std::array{'P', 'A', 'C', 'K'}) {return ArcType::Pack;}
if(magic == std::array{'P', 'A', 'C', 'K'}) {return ArcPack;} if(magic == std::array{'W', 'A', 'D', '2'}) {return ArcType::Wad2;}
if(magic == std::array{'W', 'A', 'D', '2'}) {return ArcWad2;} } catch(...) {
} catch(...) { }
return None;
} }
return None;
}
Dir readArchive(std::istream &st) { Dir readArchive(std::istream &st) {
switch(auto v = getArchiveType(st); switch(auto v = getArchiveType(st);
v.has_value() ? *v : throw FileFormatError("not an archive")) { v.has_value() ? *v : throw FileFormatError("not an archive")) {
case ArcPack: return readPack(st); case ArcType::Pack: return readPack(st);
case ArcWad2: return readWad2(st); case ArcType::Wad2: return readWad2(st);
}
} }
}
Dir::iterator Dir::findNode(std::string const &name) { Node *Dir::findNode(std::string const &name) {
return std::find_if(begin(), end(), [&name](Node const &node) { auto it = std::find_if(begin(), end(), [&name](Node &node) {
return node.name == name; return node.name == name;
}); });
} return it != end() ? &*it : nullptr;
}
Node::Node(Dir &&f, std::string &&n, FileType t) : Node::Node(Dir &&f, std::string &&n, FileType t) :
super_type(std::move(f)), superType(std::move(f)),
name(std::move(n)), name(std::move(n)),
type(t) type(t)
{ {
} }
Node::Node(File &&f, std::string &&n, FileType t) : Node::Node(File &&f, std::string &&n, FileType t) :
super_type(std::move(f)), superType(std::move(f)),
name(std::move(n)), name(std::move(n)),
type(t) type(t)
{ {
} }
Model::Model(QObject *parent) : Model::Model(QObject *parent) :
QAbstractItemModel{parent}, QAbstractItemModel{parent},
m_dir{nullptr} m_dir{nullptr}
{ {
} }
Model::~Model() { Model::~Model() {
} }
QVariant Model::data(QModelIndex const &index, int role) const { QVariant Model::data(QModelIndex const &index, int role) const {
if(!index.isValid()) { if(!index.isValid()) {
return QVariant{};
}
auto node = static_cast<Node *>(index.internalPointer());
auto col = Column(index.column());
switch(role) {
case Qt::DecorationRole:
if(col == Column::Name) {
auto icon =
std::holds_alternative<Dir>(*node) ? "folder"
: "text-x-generic";
return QVariant{QIcon::fromTheme(icon)};
}
break;
case Qt::DisplayRole:
switch(col) {
case Column::Size:
if(auto file = std::get_if<File>(node)) {
return QVariant{QString::number(file->size())};
}
break;
case Column::Type:
return QVariant{tr(enumToString(node->type))};
case Column::Name:
return QVariant{tr(node->name.data())};
}
case Qt::UserRole:
return QVariant::fromValue(static_cast<void *>(node));
default:
break;
}
return QVariant{}; return QVariant{};
} }
auto node = static_cast<Node const *>(index.internalPointer()); Qt::ItemFlags Model::flags(QModelIndex const &index) const {
if(!index.isValid()) {
switch(role) { return Qt::NoItemFlags;
case Qt::DecorationRole: } else {
if(index.column() == ColumnName) { return Qt::ItemIsSelectable |
auto icon = Qt::ItemIsDragEnabled |
std::holds_alternative<Dir>(*node) ? "folder" Qt::ItemIsEnabled |
: "text-x-generic"; Qt::ItemNeverHasChildren;
return QVariant{QIcon::fromTheme(icon)};
}
break;
case Qt::DisplayRole:
switch(index.column()) {
case ColumnSize:
if(auto file = std::get_if<File>(node)) {
return QVariant{QString::number(file->size())};
}
break;
case ColumnType:
return QVariant{tr(enumToString(node->type))};
case ColumnName:
return QVariant{tr(node->name.data())};
}
default:
break;
}
return QVariant{};
}
Qt::ItemFlags Model::flags(QModelIndex const &index) const {
if(!index.isValid()) {
return Qt::NoItemFlags;
} else {
return Qt::ItemIsSelectable |
Qt::ItemIsDragEnabled |
Qt::ItemIsEnabled |
Qt::ItemNeverHasChildren;
}
}
QVariant Model::headerData(int section,
Qt::Orientation orientation,
int role) const {
if(orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch(section) {
case ColumnSize: return QVariant{tr("Size")};
case ColumnType: return QVariant{tr("Type")};
case ColumnName: return QVariant{tr("Name")};
} }
} }
return QVariant{};
}
QModelIndex Model::index(int row, int col, QModelIndex const &parent) const { QVariant Model::headerData(int section,
if(!m_dir || !hasIndex(row, col, parent) || row > m_dir->size()) { Qt::Orientation ori,
int role) const {
if(ori == Qt::Horizontal && role == Qt::DisplayRole) {
switch(Column(section)) {
case Column::Size: return QVariant{tr("Size")};
case Column::Type: return QVariant{tr("Type")};
case Column::Name: return QVariant{tr("Name")};
}
}
return QVariant{};
}
QModelIndex Model::index(int row,
int col,
QModelIndex const &parent) const {
if(!m_dir || !hasIndex(row, col, parent) || row > m_dir->size()) {
return QModelIndex{};
} else {
return createIndex(row, col, &m_dir->at(row));
}
}
QModelIndex Model::parent(QModelIndex const &) const {
return QModelIndex{}; return QModelIndex{};
} else {
// despite index data being const, this function does not take a const
// pointer, which is very annoying!
return createIndex(row, col, const_cast<Node *>(&m_dir->at(row)));
} }
}
QModelIndex Model::parent(QModelIndex const &) const { int Model::rowCount(QModelIndex const &) const {
return QModelIndex{}; return m_dir ? m_dir->size() : 0;
}
int Model::rowCount(QModelIndex const &) const {
return m_dir ? m_dir->size() : 0;
}
int Model::columnCount(QModelIndex const &) const {
return enumMax<Column>();
}
Dir *Model::dir() const {
return m_dir;
}
void Model::setDir(Dir *dir) {
if(dir != m_dir) {
m_dir = dir;
emit layoutChanged();
emit dirChanged(dir);
} }
}
int Model::columnCount(QModelIndex const &) const {
return enumMax<Column>();
}
Dir *Model::dir() const {
return m_dir;
}
void Model::setDir(Dir *dir) {
if(dir != m_dir) {
m_dir = dir;
emit layoutChanged();
emit dirChanged(dir);
}
}
} }
// EOF // EOF

View File

@ -12,10 +12,10 @@ namespace Arc {
struct Node; struct Node;
struct Dir; struct Dir;
enum Column { enum class Column {
ColumnSize, Size,
ColumnType, Type,
ColumnName, Name,
}; };
Q_ENUM_NS(Column) Q_ENUM_NS(Column)
@ -30,28 +30,28 @@ namespace Arc {
}; };
Q_ENUM_NS(FileType) Q_ENUM_NS(FileType)
enum ArchiveType { enum class ArcType {
ArcPack, Pack,
ArcWad2, Wad2,
}; };
Q_ENUM_NS(ArchiveType) Q_ENUM_NS(ArcType)
Option<ArchiveType> getArchiveType(std::istream &st) noexcept; Option<ArcType> getArchiveType(std::istream &st) noexcept;
Dir readArchive(std::istream &st); Dir readArchive(std::istream &st);
struct Dir : public std::vector<Node> { struct Dir : public std::vector<Node> {
using std::vector<Node>::vector; using std::vector<Node>::vector;
Dir::iterator findNode(std::string const &name); Node *findNode(std::string const &name);
}; };
struct File : public QByteArray { struct File : public QByteArray {
using QByteArray::QByteArray; using QByteArray::QByteArray;
}; };
struct Node : public std::variant<Dir, File> { struct Node : public Variant<Dir, File> {
using super_type = std::variant<Dir, File>; using superType = Variant<Dir, File>;
Node() = default; Node() = default;
@ -74,13 +74,14 @@ namespace Arc {
QVariant data(QModelIndex const &index, int role) const override; QVariant data(QModelIndex const &index, int role) const override;
Qt::ItemFlags flags(QModelIndex const &index) const override; Qt::ItemFlags flags(QModelIndex const &index) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) QVariant headerData(int section, Qt::Orientation ori, int role)
const override; const override;
QModelIndex index(int row, QModelIndex index(int row,
int col, int col,
QModelIndex const &parent = QModelIndex()) QModelIndex const &parent = QModelIndex())
const override;
QModelIndex parent(QModelIndex const &index = QModelIndex())
const override; const override;
QModelIndex parent(QModelIndex const &index = QModelIndex()) const override;
int rowCount(QModelIndex const &index = QModelIndex()) const override; int rowCount(QModelIndex const &index = QModelIndex()) const override;
int columnCount(QModelIndex const &index = QModelIndex()) const override; int columnCount(QModelIndex const &index = QModelIndex()) const override;
@ -100,4 +101,12 @@ Q_DECLARE_METATYPE(Arc::Dir)
Q_DECLARE_METATYPE(Arc::File) Q_DECLARE_METATYPE(Arc::File)
Q_DECLARE_METATYPE(Arc::Node) Q_DECLARE_METATYPE(Arc::Node)
static inline QDebug operator<<(QDebug debug, Arc::Node const &t) {
debug << "Arc::Node(" << t.name << ", ";
std::visit([&](auto &&arg) {debug << arg;},
static_cast<Variant<Arc::Dir, Arc::File>>(t));
debug << ")";
return debug;
}
// EOF // EOF

View File

@ -59,7 +59,7 @@ static PackEntry readPackEntry(std::istream &st) {
return ent; return ent;
} }
static void insertFile(Arc::Dir &dir, std::string name, Arc::File file) { static void insertFile(Arc::Dir &dir, std::string &name, Arc::File &file) {
Option<std::string> next; Option<std::string> next;
if(auto slash = name.find('/'); slash != std::string::npos) { if(auto slash = name.find('/'); slash != std::string::npos) {
@ -68,18 +68,14 @@ static void insertFile(Arc::Dir &dir, std::string name, Arc::File file) {
} }
auto existing = dir.findNode(name); auto existing = dir.findNode(name);
if(next) { if(next) {
auto ref = auto &ref = existing ? *existing
existing != dir.end() : dir.emplace_back(Arc::Dir{}, std::move(name));
? *existing insertFile(std::get<Arc::Dir>(ref), *next, file);
: dir.emplace_back(Arc::Dir{}, std::move(name)); } else if(!existing) {
insertFile(std::get<Arc::Dir>(ref), *std::move(next), std::move(file));
} else {
if(existing != dir.end()) {
throw FileFormatError("duplicate file");
}
dir.emplace_back(std::move(file), std::move(name)); dir.emplace_back(std::move(file), std::move(name));
} else {
throw FileFormatError("duplicate file");
} }
} }
@ -91,7 +87,7 @@ Arc::Dir readPack(std::istream &st) {
for(quint32 i = 0; i < hdr.dirNum; i++) { for(quint32 i = 0; i < hdr.dirNum; i++) {
auto ent = readPackEntry(st); auto ent = readPackEntry(st);
insertFile(root, std::move(ent.name), std::move(ent.file)); insertFile(root, ent.name, ent.file);
} }
return root; return root;

View File

@ -19,7 +19,10 @@ Project::Project(Arc::Dir &&arc, QErrorMessage *errors, QMdiArea *parent) :
setAttribute(Qt::WA_DeleteOnClose); setAttribute(Qt::WA_DeleteOnClose);
showMaximized(); showMaximized();
connect(m_model, &Arc::Model::dirChanged, this, &Project::dirChanged); connect(m_model, &Arc::Model::dirChanged,
this, &Project::dirChanged);
connect(tableView, &QAbstractItemView::doubleClicked,
this, &Project::viewDoubleClicked);
m_sorter->setSourceModel(m_model); m_sorter->setSourceModel(m_model);
tableView->setModel(m_sorter); tableView->setModel(m_sorter);
@ -33,4 +36,12 @@ void Project::dirChanged(Arc::Dir *) {
tableView->resizeColumnsToContents(); tableView->resizeColumnsToContents();
} }
void Project::viewDoubleClicked(QModelIndex const &index) {
auto node = static_cast<Arc::Node *>(index.data(Qt::UserRole)
.value<void *>());
if(auto dir = std::get_if<Arc::Dir>(node)) {
m_model->setDir(dir);
}
}
// EOF // EOF

View File

@ -22,6 +22,7 @@ public:
private slots: private slots:
void dirChanged(Arc::Dir *dir); void dirChanged(Arc::Dir *dir);
void viewDoubleClicked(QModelIndex const &index);
private: private:
Arc::Dir m_arc; Arc::Dir m_arc;

View File

@ -82,11 +82,6 @@ Arc::Dir readWad2(std::istream &st) {
for(quint32 i = 0; i < hdr.dirNum; i++) { for(quint32 i = 0; i < hdr.dirNum; i++) {
auto ent = readWad2Entry(st); auto ent = readWad2Entry(st);
/*
if(root.findNode(ent.name) != root.end()) {
throw FileFormatError("duplicate file");
}
*/
root.emplace_back(std::move(ent.file), std::move(ent.name), ent.type); root.emplace_back(std::move(ent.file), std::move(ent.name), ent.type);
} }