OpenTwin 0.1
OpenTwin
 
Loading...
Searching...
No Matches
IconManager.hpp
Go to the documentation of this file.
1
4// ###########################################################################################################################################################################################################################################################################################################################
5
6#pragma once
7
8// OpenTwin header
10
11template <class T>
12std::shared_ptr<T>& ot::IconManager::getOrCreate(const QString& _subPath, std::map<QString, std::shared_ptr<T>>& _dataMap, std::shared_ptr<T>& _default) {
13 std::lock_guard<std::mutex> lock(this->m_mutex);
14
15 // Find existing
16 std::optional<std::shared_ptr<T>*> existing = this->getWhileLocked<T>(_subPath, _dataMap);
17 if (existing.has_value()) {
18 if (existing.value()) {
19 return *existing.value();
20 }
21 }
22
23 // Find new
24 QString path = this->findFullPath(_subPath);
25 if (path.isEmpty()) {
26 OT_LOG_E("Icon \"" + _subPath.toStdString() + "\" not found");
27 return _default;
28 }
29
30 // Create and store new
31 std::shared_ptr<T> newShared(new T(path));
32 auto result = _dataMap.insert_or_assign(_subPath, newShared);
33 return result.first->second;
34}
35
36template <class T>
37std::optional<std::shared_ptr<T>*> ot::IconManager::get(const QString& _subPath, std::map<QString, std::shared_ptr<T>>& _dataMap) {
38 std::lock_guard<std::mutex> lock(this->m_mutex);
39 return this->getWhileLocked<T>(_subPath, _dataMap);
40}
41
42template <class T>
43std::optional<std::shared_ptr<T>*> ot::IconManager::getWhileLocked(const QString& _subPath, std::map<QString, std::shared_ptr<T>>& _dataMap) {
44 // Find existing
45 const auto& it = _dataMap.find(_subPath);
46 if (it != _dataMap.end()) {
47 return &it->second;
48 }
49 else {
50 return std::nullopt;
51 }
52}
#define OT_LOG_E(___text)
Log a error message according to the service logger configuration.
Definition Logger.h:186
int result
Definition dllmain.cpp:82