ui: fixed crash that had a chance to occur when showing source files of cdb/cbp/sonargraph project

This commit is contained in:
mlangkabel
2018-08-27 16:15:18 +02:00
parent 9bce2de57b
commit e8a4830229
4 changed files with 96 additions and 37 deletions
+1
View File
@@ -595,6 +595,7 @@ add_files(
utility/ScopedFunctor.cpp
utility/ScopedFunctor.h
utility/ScopedSwitcher.h
utility/SingleValueCache.h
utility/TimeStamp.cpp
utility/TimeStamp.h
utility/tracing.cpp
+48
View File
@@ -0,0 +1,48 @@
#ifndef SINGLE_VALUE_CACHE_H
#define SINGLE_VALUE_CACHE_H
#include <functional>
template <typename ValType>
class SingleValueCache
{
public:
SingleValueCache(std::function<ValType()> calculator);
ValType getValue();
void clear();
private:
std::function<ValType()> m_calculator;
ValType m_value;
bool m_hasValue;
};
template <typename ValType>
SingleValueCache<ValType>::SingleValueCache(std::function<ValType()> calculator)
: m_calculator(calculator)
, m_hasValue(false)
{
}
template <typename ValType>
ValType SingleValueCache<ValType>::getValue()
{
if (!m_hasValue)
{
m_value = m_calculator();
m_hasValue = true;
}
return m_value;
}
template <typename ValType>
void SingleValueCache<ValType>::clear()
{
if (m_hasValue)
{
m_value = ValType();
m_hasValue = false;
}
}
#endif // SINGLE_VALUE_CACHE_H
@@ -128,6 +128,14 @@ QtProjectWizzardContentPathCDB::QtProjectWizzardContentPathCDB(
)
: QtProjectWizzardContentPath(window)
, m_settings(settings)
, m_filePaths([&]()
{
return utility::getAsRelativeIfShorter(
utility::toVector(SourceGroupCxxCdb(m_settings).getAllSourceFilePaths()),
m_settings->getProjectDirectoryPath()
);
}
)
{
setTitleString("Compilation Database (compile_commands.json)");
setHelpString(
@@ -172,14 +180,11 @@ void QtProjectWizzardContentPathCDB::load()
{
m_picker->setText(QString::fromStdWString(m_settings->getCompilationDatabasePath().wstr()));
m_filePaths = utility::getAsRelativeIfShorter(
utility::toVector(SourceGroupCxxCdb(m_settings).getAllSourceFilePaths()),
m_settings->getProjectDirectoryPath()
);
m_filePaths.clear();
if (m_fileCountLabel)
{
m_fileCountLabel->setText("<b>" + QString::number(m_filePaths.size()) + "</b> source files were found in the compilation database.");
m_fileCountLabel->setText("<b>" + QString::number(getFilePaths().size()) + "</b> source files were found in the compilation database.");
}
}
@@ -190,9 +195,7 @@ void QtProjectWizzardContentPathCDB::save()
std::vector<FilePath> QtProjectWizzardContentPathCDB::getFilePaths() const
{
const_cast<QtProjectWizzardContentPathCDB*>(this)->load();
return m_filePaths;
return m_filePaths.getValue();
}
QString QtProjectWizzardContentPathCDB::getFileNamesTitle() const
@@ -235,6 +238,14 @@ QtProjectWizzardContentCodeblocksProjectPath::QtProjectWizzardContentCodeblocksP
)
: QtProjectWizzardContentPath(window)
, m_settings(settings)
, m_filePaths([&]()
{
return utility::getAsRelativeIfShorter(
utility::toVector(SourceGroupCxxCodeblocks(m_settings).getAllSourceFilePaths()),
m_settings->getProjectDirectoryPath()
);
}
)
{
setTitleString("Code::Blocks Project (.cbp)");
setHelpString(
@@ -278,14 +289,11 @@ void QtProjectWizzardContentCodeblocksProjectPath::load()
{
m_picker->setText(QString::fromStdWString(m_settings->getCodeblocksProjectPath().wstr()));
m_filePaths = utility::getAsRelativeIfShorter(
utility::toVector(SourceGroupCxxCodeblocks(m_settings).getAllSourceFilePaths()),
m_settings->getProjectDirectoryPath()
);
m_filePaths.clear();
if (m_fileCountLabel)
{
m_fileCountLabel->setText("<b>" + QString::number(m_filePaths.size()) + "</b> source files were found in the Code::Blocks project.");
m_fileCountLabel->setText("<b>" + QString::number(getFilePaths().size()) + "</b> source files were found in the Code::Blocks project.");
}
}
@@ -296,9 +304,7 @@ void QtProjectWizzardContentCodeblocksProjectPath::save()
std::vector<FilePath> QtProjectWizzardContentCodeblocksProjectPath::getFilePaths() const
{
const_cast<QtProjectWizzardContentCodeblocksProjectPath*>(this)->load();
return m_filePaths;
return m_filePaths.getValue();
}
QString QtProjectWizzardContentCodeblocksProjectPath::getFileNamesTitle() const
@@ -346,6 +352,24 @@ QtProjectWizzardContentSonargraphProjectPath::QtProjectWizzardContentSonargraphP
, m_settings(settings)
, m_settingsCxxSonargraph(settingsCxxSonargraph)
, m_settingsWithSonargraphProjectPath(settingsWithSonargraphProjectPath)
, m_filePaths([&]()
{
std::set<FilePath> allSourceFilePaths;
if (std::shared_ptr<SourceGroupSettingsCxxSonargraph> settings = std::dynamic_pointer_cast<SourceGroupSettingsCxxSonargraph>(m_settings))
{
allSourceFilePaths = SourceGroupCxxSonargraph(settings).getAllSourceFilePaths();
}
else if (std::shared_ptr<SourceGroupSettingsJavaSonargraph> settings = std::dynamic_pointer_cast<SourceGroupSettingsJavaSonargraph>(m_settings))
{
allSourceFilePaths = SourceGroupJavaSonargraph(settings).getAllSourceFilePaths();
}
return utility::getAsRelativeIfShorter(
utility::toVector(allSourceFilePaths),
m_settings->getProjectDirectoryPath()
);
}
)
{
setTitleString("Sonargraph Project (system.sonargraph)");
setHelpString(
@@ -389,24 +413,11 @@ void QtProjectWizzardContentSonargraphProjectPath::load()
{
m_picker->setText(QString::fromStdWString(m_settingsWithSonargraphProjectPath->getSonargraphProjectPath().wstr()));
std::set<FilePath> allSourceFilePaths;
if (std::shared_ptr<SourceGroupSettingsCxxSonargraph> settings = std::dynamic_pointer_cast<SourceGroupSettingsCxxSonargraph>(m_settings))
{
allSourceFilePaths = SourceGroupCxxSonargraph(settings).getAllSourceFilePaths();
}
else if (std::shared_ptr<SourceGroupSettingsJavaSonargraph> settings = std::dynamic_pointer_cast<SourceGroupSettingsJavaSonargraph>(m_settings))
{
allSourceFilePaths = SourceGroupJavaSonargraph(settings).getAllSourceFilePaths();
}
m_filePaths = utility::getAsRelativeIfShorter(
utility::toVector(allSourceFilePaths),
m_settings->getProjectDirectoryPath()
);
m_filePaths.clear();
if (m_fileCountLabel)
{
m_fileCountLabel->setText("<b>" + QString::number(m_filePaths.size()) + "</b> source files were found in the Sonargraph project.");
m_fileCountLabel->setText("<b>" + QString::number(getFilePaths().size()) + "</b> source files were found in the Sonargraph project.");
}
}
@@ -445,9 +456,7 @@ bool QtProjectWizzardContentSonargraphProjectPath::check()
std::vector<FilePath> QtProjectWizzardContentSonargraphProjectPath::getFilePaths() const
{
const_cast<QtProjectWizzardContentSonargraphProjectPath*>(this)->load();
return m_filePaths;
return m_filePaths.getValue();
}
QString QtProjectWizzardContentSonargraphProjectPath::getFileNamesTitle() const
@@ -4,6 +4,7 @@
#include <set>
#include "qt/window/project_wizzard/QtProjectWizzardContent.h"
#include "utility/SingleValueCache.h"
class QCheckBox;
class QComboBox;
@@ -76,7 +77,7 @@ private:
std::shared_ptr<SourceGroupSettingsCxxCdb> m_settings;
QLabel* m_fileCountLabel;
std::vector<FilePath> m_filePaths;
mutable SingleValueCache<std::vector<FilePath>> m_filePaths;
};
@@ -108,7 +109,7 @@ private:
std::shared_ptr<SourceGroupSettingsCxxCodeblocks> m_settings;
QLabel* m_fileCountLabel;
std::vector<FilePath> m_filePaths;
mutable SingleValueCache<std::vector<FilePath>> m_filePaths;
};
@@ -145,7 +146,7 @@ private:
std::shared_ptr<SourceGroupSettingsCxxSonargraph> m_settingsCxxSonargraph;
std::shared_ptr<SourceGroupSettingsWithSonargraphProjectPath> m_settingsWithSonargraphProjectPath;
QLabel* m_fileCountLabel;
std::vector<FilePath> m_filePaths;
mutable SingleValueCache<std::vector<FilePath>> m_filePaths;
};