Trying to make selection threadsafe

This commit is contained in:
samcake 2017-11-20 17:21:15 -08:00
parent a74372b233
commit aa163db4e6
2 changed files with 15 additions and 4 deletions

View file

@ -72,12 +72,14 @@ bool SelectionScriptingInterface::removeFromSelectedItemsList(const QString& lis
} }
bool SelectionScriptingInterface::clearSelectedItemsList(const QString& listName) { bool SelectionScriptingInterface::clearSelectedItemsList(const QString& listName) {
// QWriteLocker lock(&_selectionListsLock);
_selectedItemsListMap.insert(listName, GameplayObjects()); _selectedItemsListMap.insert(listName, GameplayObjects());
onSelectedItemsListChanged(listName); onSelectedItemsListChanged(listName);
return true; return true;
} }
bool SelectionScriptingInterface::enableListHighlight(const QString& listName, const QVariantMap& highlightStyleValues) { bool SelectionScriptingInterface::enableListHighlight(const QString& listName, const QVariantMap& highlightStyleValues) {
// QWriteLocker lock(&_selectionListsLock);
auto highlightStyle = _highlightedListMap.find(listName); auto highlightStyle = _highlightedListMap.find(listName);
if (highlightStyle == _highlightedListMap.end()) { if (highlightStyle == _highlightedListMap.end()) {
@ -111,6 +113,7 @@ bool SelectionScriptingInterface::enableListHighlight(const QString& listName, c
} }
bool SelectionScriptingInterface::disableListHighlight(const QString& listName) { bool SelectionScriptingInterface::disableListHighlight(const QString& listName) {
// QWriteLocker lock(&_selectionListsLock);
auto highlightStyle = _highlightedListMap.find(listName); auto highlightStyle = _highlightedListMap.find(listName);
if (highlightStyle != _highlightedListMap.end()) { if (highlightStyle != _highlightedListMap.end()) {
// if ((*highlightStyle).isBoundToList()) { // if ((*highlightStyle).isBoundToList()) {
@ -134,6 +137,7 @@ bool SelectionScriptingInterface::disableListHighlight(const QString& listName)
} }
QVariantMap SelectionScriptingInterface::getListHighlightStyle(const QString& listName) const { QVariantMap SelectionScriptingInterface::getListHighlightStyle(const QString& listName) const {
// QReadLocker lock(&_selectionListsLock);
auto highlightStyle = _highlightedListMap.find(listName); auto highlightStyle = _highlightedListMap.find(listName);
if (highlightStyle == _highlightedListMap.end()) { if (highlightStyle == _highlightedListMap.end()) {
return QVariantMap(); return QVariantMap();
@ -143,6 +147,7 @@ QVariantMap SelectionScriptingInterface::getListHighlightStyle(const QString& li
} }
render::HighlightStyle SelectionScriptingInterface::getHighlightStyle(const QString& listName) const { render::HighlightStyle SelectionScriptingInterface::getHighlightStyle(const QString& listName) const {
// QReadLocker lock(&_selectionListsLock);
auto highlightStyle = _highlightedListMap.find(listName); auto highlightStyle = _highlightedListMap.find(listName);
if (highlightStyle == _highlightedListMap.end()) { if (highlightStyle == _highlightedListMap.end()) {
return render::HighlightStyle(); return render::HighlightStyle();
@ -152,6 +157,8 @@ render::HighlightStyle SelectionScriptingInterface::getHighlightStyle(const QStr
} }
template <class T> bool SelectionScriptingInterface::addToGameplayObjects(const QString& listName, T idToAdd) { template <class T> bool SelectionScriptingInterface::addToGameplayObjects(const QString& listName, T idToAdd) {
// QWriteLocker lock(&_selectionListsLock);
GameplayObjects currentList = _selectedItemsListMap.value(listName); GameplayObjects currentList = _selectedItemsListMap.value(listName);
currentList.addToGameplayObjects(idToAdd); currentList.addToGameplayObjects(idToAdd);
_selectedItemsListMap.insert(listName, currentList); _selectedItemsListMap.insert(listName, currentList);
@ -160,6 +167,7 @@ template <class T> bool SelectionScriptingInterface::addToGameplayObjects(const
return true; return true;
} }
template <class T> bool SelectionScriptingInterface::removeFromGameplayObjects(const QString& listName, T idToRemove) { template <class T> bool SelectionScriptingInterface::removeFromGameplayObjects(const QString& listName, T idToRemove) {
// QWriteLocker lock(&_selectionListsLock);
GameplayObjects currentList = _selectedItemsListMap.value(listName); GameplayObjects currentList = _selectedItemsListMap.value(listName);
if (currentList.getContainsData()) { if (currentList.getContainsData()) {
currentList.removeFromGameplayObjects(idToRemove); currentList.removeFromGameplayObjects(idToRemove);
@ -176,10 +184,12 @@ template <class T> bool SelectionScriptingInterface::removeFromGameplayObjects(c
// //
GameplayObjects SelectionScriptingInterface::getList(const QString& listName) { GameplayObjects SelectionScriptingInterface::getList(const QString& listName) {
// QReadLocker lock(&_selectionListsLock);
return _selectedItemsListMap.value(listName); return _selectedItemsListMap.value(listName);
} }
void SelectionScriptingInterface::printList(const QString& listName) { void SelectionScriptingInterface::printList(const QString& listName) {
// QReadLocker lock(&_selectionListsLock);
auto currentList = _selectedItemsListMap.find(listName); auto currentList = _selectedItemsListMap.find(listName);
if (currentList != _selectedItemsListMap.end()) { if (currentList != _selectedItemsListMap.end()) {
if ((*currentList).getContainsData()) { if ((*currentList).getContainsData()) {
@ -212,6 +222,7 @@ void SelectionScriptingInterface::printList(const QString& listName) {
} }
bool SelectionScriptingInterface::removeListFromMap(const QString& listName) { bool SelectionScriptingInterface::removeListFromMap(const QString& listName) {
// QWriteLocker lock(&_selectionListsLock);
if (_selectedItemsListMap.remove(listName)) { if (_selectedItemsListMap.remove(listName)) {
onSelectedItemsListChanged(listName); onSelectedItemsListChanged(listName);
return true; return true;
@ -221,6 +232,7 @@ bool SelectionScriptingInterface::removeListFromMap(const QString& listName) {
} }
void SelectionScriptingInterface::setupHandler(const QString& selectionName) { void SelectionScriptingInterface::setupHandler(const QString& selectionName) {
// QWriteLocker lock(&_selectionListsLock);
auto handler = _handlerMap.find(selectionName); auto handler = _handlerMap.find(selectionName);
if (handler == _handlerMap.end()) { if (handler == _handlerMap.end()) {
handler = _handlerMap.insert(selectionName, new SelectionToSceneHandler()); handler = _handlerMap.insert(selectionName, new SelectionToSceneHandler());
@ -233,6 +245,7 @@ void SelectionScriptingInterface::setupHandler(const QString& selectionName) {
} }
void SelectionScriptingInterface::onSelectedItemsListChanged(const QString& listName) { void SelectionScriptingInterface::onSelectedItemsListChanged(const QString& listName) {
// QWriteLocker lock(&_selectionListsLock);
auto handler = _handlerMap.find(listName); auto handler = _handlerMap.find(listName);
if (handler != _handlerMap.end()) { if (handler != _handlerMap.end()) {
(*handler)->updateSceneFromSelectedList(); (*handler)->updateSceneFromSelectedList();
@ -245,8 +258,6 @@ SelectionToSceneHandler::SelectionToSceneHandler() {
void SelectionToSceneHandler::initialize(const QString& listName) { void SelectionToSceneHandler::initialize(const QString& listName) {
_listName = listName; _listName = listName;
updateSceneFromSelectedList(); updateSceneFromSelectedList();
} }

View file

@ -142,10 +142,9 @@ public:
signals: signals:
void selectedItemsListChanged(const QString& listName); void selectedItemsListChanged(const QString& listName);
// void highlightStyleChanged(const QString& listName);
// void highlightStyleRemoved(const QString& listName);
private: private:
mutable QReadWriteLock _selectionListsLock;
QMap<QString, GameplayObjects> _selectedItemsListMap; QMap<QString, GameplayObjects> _selectedItemsListMap;
QMap<QString, SelectionHighlightStyle> _highlightedListMap; QMap<QString, SelectionHighlightStyle> _highlightedListMap;
@ -156,6 +155,7 @@ private:
void setupHandler(const QString& selectionName); void setupHandler(const QString& selectionName);
}; };