errors with pooling

This commit is contained in:
jorenchik
2024-10-06 11:52:23 +03:00
parent 28c1841bdc
commit a53eb59900
2 changed files with 88 additions and 12 deletions

View File

@@ -2,14 +2,17 @@
#include <format> #include <format>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <locale>
#include <qabstractbutton.h> #include <qabstractbutton.h>
#include <qboxlayout.h> #include <qboxlayout.h>
#include <qlabel.h> #include <qlabel.h>
#include <qlayoutitem.h> #include <qlayoutitem.h>
#include <qmainwindow.h> #include <qmainwindow.h>
#include <qnamespace.h>
#include <qobjectdefs.h> #include <qobjectdefs.h>
#include <qsizepolicy.h> #include <qsizepolicy.h>
#include <qtoolbutton.h> #include <qtoolbutton.h>
#include <qwidget.h>
#include <qwindow.h> #include <qwindow.h>
#include <qwindowdefs.h> #include <qwindowdefs.h>
#include <regex> #include <regex>
@@ -35,7 +38,6 @@
#include <QListView> #include <QListView>
#include <QWindow> #include <QWindow>
#include <qabstractitemmodel.h> #include <qabstractitemmodel.h>
#include <system_error>
#include "api.h" #include "api.h"
#include "parser.h" #include "parser.h"
@@ -140,7 +142,6 @@ public:
} }
}; };
QString workingPath = "/home/jorenchik/Code/mdemory/memorybase"; QString workingPath = "/home/jorenchik/Code/mdemory/memorybase";
std::string currentPath = ""; std::string currentPath = "";
QList<Mdem*> mdems = QList<Mdem*>(); QList<Mdem*> mdems = QList<Mdem*>();
@@ -337,6 +338,52 @@ void SwitchPage(int pageIdx) {
std::regex lastPathElementExp = std::regex("(.+\\/)*(.+)"); std::regex lastPathElementExp = std::regex("(.+\\/)*(.+)");
struct ErrorView {
QWidget box;
QVBoxLayout layout;
QLabel label;
};
std::vector<ErrorView*> errorPool;
std::vector<ErrorView*> errorViews;
#define ERROR_POOL_CHUNK 50
ErrorView *makeErrorView() {
auto errorView = new ErrorView;
errorView->box.setObjectName("error-box");
errorView->box.setLayout(&errorView->layout);
errorView->box.setMinimumHeight(30);
errorView->box.setStyleSheet(
"QWidget#error-box {"
"border: 1px solid red;"
"}"
);
errorView->layout.addWidget(&errorView->label);
return errorView;
};
ErrorView* acquireError() {
if (errorPool.size() <= 0) {
for (int i = 0; i < ERROR_POOL_CHUNK; ++i) {
auto errorView = makeErrorView();
errorPool.push_back(errorView);
hMdemScroll->addWidget(&errorView->box);
errorView->box.hide();
}
}
auto item = errorPool.back();
errorPool.pop_back();
errorViews.push_back(item);
std::cout << std::format("Acquired, current pool size: {}\n", errorPool.size());
return item;
}
void releaseError(ErrorView** item) {
errorPool.push_back(*item);
(*item) = nullptr;
std::cout << std::format("Released, current pool size: {}\n", errorPool.size());
}
void loadMdem() { void loadMdem() {
auto file = std::ifstream(currentPath); auto file = std::ifstream(currentPath);
std::string content; std::string content;
@@ -349,7 +396,23 @@ void loadMdem() {
delete question; delete question;
} }
questions.clear(); questions.clear();
std::smatch matches;
auto filenameMatched = std::regex_search(currentPath, matches, lastPathElementExp);
auto filename = matches[2];
deckListLabel->setText(
QString::fromStdString(std::format("mdem: {}", filename.str()))
);
while (errorViews.size() > 0) {
auto errorView = errorViews.back();
errorViews.pop_back();
errorView->box.hide();
releaseError(&errorView);
}
if (res.error == "") { if (res.error == "") {
time_t trainedAt; time_t trainedAt;
if (res.value.lastTrainedAt == 0) { if (res.value.lastTrainedAt == 0) {
trainedAt = 0; trainedAt = 0;
@@ -360,13 +423,27 @@ void loadMdem() {
questions = res.value.questions; questions = res.value.questions;
makePages(); makePages();
SwitchPage(0); SwitchPage(0);
std::smatch matches;
auto filename = std::regex_search(currentPath, matches, lastPathElementExp);
deckListLabel->setText(
QString::fromStdString(std::format("mdem: {}", matches[2].str()))
);
} else { } else {
std::cout << std::format("Compilation error: {}", res.error) << std::endl; std::cout << std::format("Compilation error: {}", res.error) << std::endl;
// Show errors.
hMdemScroll->removeItem(mdemSpacer);
auto errorView = acquireError();
errorView->label.setText(
QString::fromStdString(
std::format(
"Error while transpiling {}: {} ({}:{})",
filename.str(),
res.error,
res.row,
res.column
)
)
);
errorView->box.show();
hMdemScroll->addWidget(&errorView->box);
hMdemScroll->addItem(mdemSpacer);
for (auto mdem: mdems) { for (auto mdem: mdems) {
if (mdem->wMdem->isVisible()) { if (mdem->wMdem->isVisible()) {
mdem->wMdem->hide(); mdem->wMdem->hide();

View File

@@ -244,7 +244,6 @@ std::vector<QStandardItem*> itemPool;
#define ITEM_POOL_CHUNK 200 #define ITEM_POOL_CHUNK 200
QStandardItem* acquireItem() { QStandardItem* acquireItem() {
if (itemPool.size() <= 0) { if (itemPool.size() <= 0) {
for (int i = 0; i < ITEM_POOL_CHUNK; ++i) { for (int i = 0; i < ITEM_POOL_CHUNK; ++i) {