Skip to content

Commit c565ded

Browse files
jcardonneiMeaNz
authored andcommitted
fix: sonar issues
1 parent 2842262 commit c565ded

5 files changed

Lines changed: 140 additions & 136 deletions

File tree

editor/src/DocumentWindows/AssetManager/AssetManagerWindow.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ namespace nexo::editor {
194194
// search functionality
195195
void drawSearchBar();
196196
void drawAdvancedSearchPanel();
197+
void drawStatusFilters();
197198
void drawSearchSuggestions();
198199
void updateSearchFilter();
199200
void applySearch();

editor/src/DocumentWindows/AssetManager/SearchBar.cpp

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "IconsFontAwesome.h"
1717
#include <imgui.h>
1818
#include <imgui_internal.h>
19+
#include <array>
1920

2021
namespace nexo::editor {
2122

@@ -39,29 +40,28 @@ namespace nexo::editor {
3940
ImGuiInputTextFlags_CallbackHistory |
4041
ImGuiInputTextFlags_EnterReturnsTrue;
4142

42-
bool searchChanged = false;
4343
if (ImGui::InputTextWithHint("##SearchInput", "Search assets...",
4444
m_searchBuffer.data(), m_searchBuffer.capacity(),
4545
inputFlags,
46-
[](ImGuiInputTextCallbackData* data) -> int {
46+
[](ImGuiInputTextCallbackData* data) {
4747
auto* window = static_cast<AssetManagerWindow*>(data->UserData);
48+
4849
if (data->EventFlag == ImGuiInputTextFlags_CallbackEdit) {
49-
// Update the search buffer with current text
5050
window->m_searchBuffer = std::string(data->Buf, data->BufTextLen);
5151
window->handleSearchInput();
52-
} else if (data->EventFlag == ImGuiInputTextFlags_CallbackHistory) {
53-
// Handle up/down arrows for search history
54-
if (data->EventKey == ImGuiKey_UpArrow) {
55-
auto history = window->m_searchHistory.getRecentSearches(1);
56-
if (!history.empty()) {
57-
data->DeleteChars(0, data->BufTextLen);
58-
data->InsertChars(0, history[0].c_str());
59-
}
52+
return 0;
53+
}
54+
55+
if (data->EventFlag == ImGuiInputTextFlags_CallbackHistory && data->EventKey == ImGuiKey_UpArrow) {
56+
auto history = window->m_searchHistory.getRecentSearches(1);
57+
if (!history.empty()) {
58+
data->DeleteChars(0, data->BufTextLen);
59+
data->InsertChars(0, history[0].c_str());
6060
}
6161
}
62+
6263
return 0;
6364
}, this)) {
64-
searchChanged = true;
6565
applySearch();
6666
}
6767

@@ -118,9 +118,9 @@ namespace nexo::editor {
118118
// Search mode
119119
ImGui::Text("Search in:");
120120
ImGui::SameLine();
121-
const char* searchModes[] = { "Name Only", "Tags Only", "Description", "All Fields" };
122-
int currentMode = static_cast<int>(m_searchCriteria.searchMode);
123-
if (ImGui::Combo("##SearchMode", &currentMode, searchModes, IM_ARRAYSIZE(searchModes))) {
121+
std::array<const char*, 4> searchModes = { "Name Only", "Tags Only", "Description", "All Fields" };
122+
if (auto currentMode = static_cast<int>(m_searchCriteria.searchMode);
123+
ImGui::Combo("##SearchMode", &currentMode, searchModes.data(), searchModes.size())) {
124124
m_searchCriteria.searchMode = static_cast<assets::SearchMode>(currentMode);
125125
updateSearchFilter();
126126
}
@@ -129,9 +129,9 @@ namespace nexo::editor {
129129
ImGui::Text("Asset Types:");
130130
ImGui::Indent();
131131
for (int i = 1; i < static_cast<int>(assets::AssetType::_COUNT); ++i) {
132-
assets::AssetType type = static_cast<assets::AssetType>(i);
133-
bool isSelected = m_searchCriteria.allowedTypes.contains(type);
134-
if (ImGui::Checkbox(assets::getAssetTypeName(type), &isSelected)) {
132+
auto type = static_cast<assets::AssetType>(i);
133+
if (bool isSelected = m_searchCriteria.allowedTypes.contains(type);
134+
ImGui::Checkbox(assets::getAssetTypeName(type), &isSelected)) {
135135
if (isSelected) {
136136
m_searchCriteria.allowedTypes.insert(type);
137137
} else {
@@ -149,8 +149,8 @@ namespace nexo::editor {
149149
ImGui::Separator();
150150
ImGui::Text("File Size:");
151151

152-
float minSize = m_searchCriteria.minSize.value_or(0) / 1024.0f; // Convert to KB
153-
float maxSize = m_searchCriteria.maxSize.value_or(0) / 1024.0f;
152+
float minSize = static_cast<float>(m_searchCriteria.minSize.value_or(0)) / 1024.0f;
153+
float maxSize = static_cast<float>(m_searchCriteria.maxSize.value_or(0)) / 1024.0f;
154154

155155
ImGui::PushItemWidth(100);
156156
if (ImGui::DragFloat("Min (KB)##MinSize", &minSize, 1.0f, 0.0f, FLT_MAX, "%.1f")) {
@@ -164,33 +164,7 @@ namespace nexo::editor {
164164
}
165165
ImGui::PopItemWidth();
166166

167-
// Status filters
168-
ImGui::Separator();
169-
ImGui::Text("Asset Status:");
170-
171-
if (ImGui::Checkbox("Loaded Only", &m_searchCriteria.onlyLoaded)) {
172-
if (m_searchCriteria.onlyLoaded) {
173-
m_searchCriteria.onlyUnloaded = false;
174-
m_searchCriteria.onlyErrored = false;
175-
}
176-
updateSearchFilter();
177-
}
178-
ImGui::SameLine();
179-
if (ImGui::Checkbox("Unloaded Only", &m_searchCriteria.onlyUnloaded)) {
180-
if (m_searchCriteria.onlyUnloaded) {
181-
m_searchCriteria.onlyLoaded = false;
182-
m_searchCriteria.onlyErrored = false;
183-
}
184-
updateSearchFilter();
185-
}
186-
ImGui::SameLine();
187-
if (ImGui::Checkbox("Errors Only", &m_searchCriteria.onlyErrored)) {
188-
if (m_searchCriteria.onlyErrored) {
189-
m_searchCriteria.onlyLoaded = false;
190-
m_searchCriteria.onlyUnloaded = false;
191-
}
192-
updateSearchFilter();
193-
}
167+
drawStatusFilters();
194168

195169
// Options
196170
ImGui::Separator();
@@ -349,6 +323,35 @@ namespace nexo::editor {
349323
}
350324
}
351325

326+
void AssetManagerWindow::drawStatusFilters() {
327+
ImGui::Separator();
328+
ImGui::Text("Asset Status:");
329+
330+
if (ImGui::Checkbox("Loaded Only", &m_searchCriteria.onlyLoaded)) {
331+
if (m_searchCriteria.onlyLoaded) {
332+
m_searchCriteria.onlyUnloaded = false;
333+
m_searchCriteria.onlyErrored = false;
334+
}
335+
updateSearchFilter();
336+
}
337+
ImGui::SameLine();
338+
if (ImGui::Checkbox("Unloaded Only", &m_searchCriteria.onlyUnloaded)) {
339+
if (m_searchCriteria.onlyUnloaded) {
340+
m_searchCriteria.onlyLoaded = false;
341+
m_searchCriteria.onlyErrored = false;
342+
}
343+
updateSearchFilter();
344+
}
345+
ImGui::SameLine();
346+
if (ImGui::Checkbox("Errors Only", &m_searchCriteria.onlyErrored)) {
347+
if (m_searchCriteria.onlyErrored) {
348+
m_searchCriteria.onlyLoaded = false;
349+
m_searchCriteria.onlyUnloaded = false;
350+
}
351+
updateSearchFilter();
352+
}
353+
}
354+
352355
std::vector<assets::GenericAssetRef> AssetManagerWindow::getFilteredAssets() const {
353356
std::vector<assets::GenericAssetRef> allAssets = assets::AssetCatalog::getInstance().getAssets();
354357

editor/src/context/SearchHistory.hpp

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <fstream>
2222
#include <filesystem>
2323
#include <algorithm>
24+
#include <ranges>
25+
#include <loguru.hpp>
2426
#include "assets/SearchCriteria.hpp"
2527
#include "json.hpp"
2628

@@ -35,7 +37,7 @@ namespace nexo::editor {
3537
SearchHistoryEntry() = default;
3638

3739
SearchHistoryEntry(const std::string& text, const assets::SearchCriteria& crit)
38-
: searchText(text), criteria(crit), timestamp(std::chrono::system_clock::now()), useCount(1) {}
40+
: searchText(text), criteria(crit), timestamp(std::chrono::system_clock::now()) {}
3941
};
4042

4143
class SearchHistory {
@@ -52,6 +54,11 @@ namespace nexo::editor {
5254
saveHistory();
5355
}
5456

57+
SearchHistory(const SearchHistory&) = delete;
58+
SearchHistory& operator=(const SearchHistory&) = delete;
59+
SearchHistory(SearchHistory&&) = default;
60+
SearchHistory& operator=(SearchHistory&&) = default;
61+
5562
/**
5663
* @brief Add a search to history
5764
* @param searchText The search text
@@ -61,12 +68,10 @@ namespace nexo::editor {
6168
if (searchText.empty()) return;
6269

6370
// Check if search already exists
64-
auto it = std::find_if(m_history.begin(), m_history.end(),
71+
if (auto it = std::ranges::find_if(m_history,
6572
[&searchText](const SearchHistoryEntry& entry) {
6673
return entry.searchText == searchText;
67-
});
68-
69-
if (it != m_history.end()) {
74+
}); it != m_history.end()) {
7075
// Update existing entry
7176
it->useCount++;
7277
it->timestamp = std::chrono::system_clock::now();
@@ -107,7 +112,7 @@ namespace nexo::editor {
107112
*/
108113
[[nodiscard]] std::vector<std::string> getPopularSearches(size_t maxCount = 10) const {
109114
std::vector<SearchHistoryEntry> sorted(m_history.begin(), m_history.end());
110-
std::sort(sorted.begin(), sorted.end(),
115+
std::ranges::sort(sorted,
111116
[](const SearchHistoryEntry& a, const SearchHistoryEntry& b) {
112117
return a.useCount > b.useCount;
113118
});
@@ -164,13 +169,9 @@ namespace nexo::editor {
164169
* @param searchText The search text to remove
165170
*/
166171
void removeSearch(const std::string& searchText) {
167-
m_history.erase(
168-
std::remove_if(m_history.begin(), m_history.end(),
169-
[&searchText](const SearchHistoryEntry& entry) {
170-
return entry.searchText == searchText;
171-
}),
172-
m_history.end()
173-
);
172+
std::erase_if(m_history, [&searchText](const SearchHistoryEntry& entry) {
173+
return entry.searchText == searchText;
174+
});
174175
saveHistory();
175176
}
176177

@@ -180,12 +181,10 @@ namespace nexo::editor {
180181
* @return Optional containing the criteria if found
181182
*/
182183
[[nodiscard]] std::optional<assets::SearchCriteria> getCriteriaForSearch(const std::string& searchText) const {
183-
auto it = std::find_if(m_history.begin(), m_history.end(),
184+
if (auto it = std::ranges::find_if(m_history,
184185
[&searchText](const SearchHistoryEntry& entry) {
185186
return entry.searchText == searchText;
186-
});
187-
188-
if (it != m_history.end()) {
187+
}); it != m_history.end()) {
189188
return it->criteria;
190189
}
191190
return std::nullopt;
@@ -197,7 +196,7 @@ namespace nexo::editor {
197196

198197
[[nodiscard]] std::vector<SearchHistoryEntry> sortByRecency() const {
199198
std::vector<SearchHistoryEntry> sorted(m_history.begin(), m_history.end());
200-
std::sort(sorted.begin(), sorted.end(),
199+
std::ranges::sort(sorted,
201200
[](const SearchHistoryEntry& a, const SearchHistoryEntry& b) {
202201
return a.timestamp > b.timestamp;
203202
});
@@ -241,8 +240,12 @@ namespace nexo::editor {
241240
if (m_history.size() >= MAX_HISTORY_SIZE) break;
242241
}
243242
}
244-
} catch (const std::exception&) {
245-
// Ignore errors loading history
243+
} catch (const nlohmann::json::exception& e) {
244+
LOG_F(WARNING, "Failed to parse search history JSON: %s", e.what());
245+
} catch (const std::filesystem::filesystem_error& e) {
246+
LOG_F(WARNING, "Filesystem error loading search history: %s", e.what());
247+
} catch (const std::exception& e) {
248+
LOG_F(WARNING, "Unexpected error loading search history: %s", e.what());
246249
}
247250
}
248251

@@ -276,8 +279,12 @@ namespace nexo::editor {
276279
if (file.is_open()) {
277280
file << j.dump(2);
278281
}
279-
} catch (const std::exception&) {
280-
// Ignore errors saving history
282+
} catch (const nlohmann::json::exception& e) {
283+
LOG_F(WARNING, "Failed to serialize search history to JSON: %s", e.what());
284+
} catch (const std::filesystem::filesystem_error& e) {
285+
LOG_F(WARNING, "Filesystem error saving search history: %s", e.what());
286+
} catch (const std::exception& e) {
287+
LOG_F(WARNING, "Unexpected error saving search history: %s", e.what());
281288
}
282289
}
283290

@@ -287,7 +294,7 @@ namespace nexo::editor {
287294

288295
[[nodiscard]] static std::string toLower(const std::string& str) {
289296
std::string lower = str;
290-
std::transform(lower.begin(), lower.end(), lower.begin(),
297+
std::ranges::transform(lower, lower.begin(),
291298
[](unsigned char c) { return std::tolower(c); });
292299
return lower;
293300
}

engine/src/assets/Asset.hpp

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#pragma once
2020

2121
#include <Texture.hpp>
22+
#include <array>
2223
#include <boost/uuid/basic_random_generator.hpp>
2324
#include <boost/uuid/nil_generator.hpp>
2425
#include <boost/uuid/uuid.hpp>
@@ -49,8 +50,8 @@ namespace nexo::assets {
4950
* @brief Array of asset type names
5051
* @note The order of the array must match the order of the AssetType enum.
5152
*/
52-
constexpr const char* AssetTypeNames[] = {"UNKNOWN", "TEXTURE", "MATERIAL", "MODEL", "SOUND",
53-
"MUSIC", "FONT", "SHADER", "SCRIPT"};
53+
constexpr std::array<const char*, 9> AssetTypeNames = {{"UNKNOWN", "TEXTURE", "MATERIAL", "MODEL", "SOUND",
54+
"MUSIC", "FONT", "SHADER", "SCRIPT"}};
5455

5556
static_assert(static_cast<int>(AssetType::_COUNT) == std::size(AssetTypeNames),
5657
"AssetTypeNames array size must match AssetType enum size");
@@ -115,16 +116,16 @@ namespace nexo::assets {
115116
class AssetImporter;
116117

117118
struct AssetMetadata {
118-
AssetType type; //< Asset type
119-
AssetStatus status; //< Asset status
120-
uint64_t referenceCount; //< Number of references to the asset
121-
AssetID id; //< Unique identifier
122-
AssetLocation location; //< Location of the asset
123-
std::chrono::system_clock::time_point creationTime; //< Asset creation time
124-
std::chrono::system_clock::time_point modificationTime; //< Last modification time
125-
size_t fileSize = 0; //< File size in bytes
126-
std::vector<std::string> tags; //< Asset tags for categorization
127-
std::string description; //< Asset description
119+
AssetType type = AssetType::UNKNOWN; //< Asset type
120+
AssetStatus status = AssetStatus::UNLOADED; //< Asset status
121+
uint64_t referenceCount = 0; //< Number of references to the asset
122+
AssetID id = boost::uuids::nil_uuid(); //< Unique identifier
123+
AssetLocation location = AssetLocation("default"); //< Location of the asset
124+
std::chrono::system_clock::time_point creationTime = std::chrono::system_clock::now(); //< Asset creation time
125+
std::chrono::system_clock::time_point modificationTime = std::chrono::system_clock::now(); //< Last modification time
126+
size_t fileSize = 0; //< File size in bytes
127+
std::vector<std::string> tags = {}; //< Asset tags for categorization
128+
std::string description = ""; //< Asset description
128129
};
129130

130131
/**
@@ -146,18 +147,7 @@ namespace nexo::assets {
146147
[[nodiscard]] virtual bool isErrored() const = 0;
147148

148149
protected:
149-
explicit IAsset()
150-
: m_metadata({.type = AssetType::UNKNOWN,
151-
.status = AssetStatus::UNLOADED,
152-
.referenceCount = 0,
153-
.id = boost::uuids::nil_uuid(),
154-
.location = AssetLocation("default"),
155-
.creationTime = std::chrono::system_clock::now(),
156-
.modificationTime = std::chrono::system_clock::now(),
157-
.fileSize = 0,
158-
.tags = {},
159-
.description = ""})
160-
{}
150+
explicit IAsset() = default;
161151

162152
public:
163153
AssetMetadata m_metadata;
@@ -183,6 +173,7 @@ namespace nexo::assets {
183173

184174
~Asset() override = default;
185175

176+
using IAsset::getMetadata;
186177
[[nodiscard]] const AssetMetadata& getMetadata() const override
187178
{
188179
return m_metadata;

0 commit comments

Comments
 (0)