From 3dbf28b7b48bc83f3417ad724fd9eb697d51a0f4 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Fri, 5 Jun 2015 07:33:39 -0700 Subject: [PATCH] use lambdas to remove some repeated code --- .../entities/src/EntityScriptingInterface.cpp | 92 ++++++------------- .../entities/src/EntityScriptingInterface.h | 1 + 2 files changed, 29 insertions(+), 64 deletions(-) diff --git a/libraries/entities/src/EntityScriptingInterface.cpp b/libraries/entities/src/EntityScriptingInterface.cpp index 80f6e1069c..f6f3b05c4d 100644 --- a/libraries/entities/src/EntityScriptingInterface.cpp +++ b/libraries/entities/src/EntityScriptingInterface.cpp @@ -451,11 +451,9 @@ bool EntityScriptingInterface::setAllVoxels(QUuid entityID, int value) { } -QUuid EntityScriptingInterface::addAction(QString actionTypeString, QUuid entityID, QVariantMap arguments) { - QUuid actionID = QUuid::createUuid(); - +bool EntityScriptingInterface::actionWorker(QUuid entityID, std::function actor) { if (!_entityTree) { - return QUuid(); + return false; } _entityTree->lockForWrite(); @@ -463,28 +461,35 @@ QUuid EntityScriptingInterface::addAction(QString actionTypeString, QUuid entity EntitySimulation* simulation = _entityTree->getSimulation(); EntityItemPointer entity = _entityTree->findEntityByEntityItemID(entityID); if (!entity) { - qDebug() << "addAction -- unknown entity" << entityID; + qDebug() << "actionWorker -- unknown entity" << entityID; _entityTree->unlock(); - return QUuid(); + return false; } if (!simulation) { - qDebug() << "addAction -- no simulation" << entityID; + qDebug() << "actionWorker -- no simulation" << entityID; _entityTree->unlock(); - return QUuid(); + return false; } - EntityActionType actionType = EntityActionInterface::actionTypeFromString(actionTypeString); - - if (actionType == ACTION_TYPE_NONE) { - _entityTree->unlock(); - return QUuid(); - } - - EntityActionPointer action = simulation->actionFactory(actionType, actionID, entity, arguments); + bool success = actor(simulation, entity); _entityTree->unlock(); + return success; +} - if (action) { + + +QUuid EntityScriptingInterface::addAction(QString actionTypeString, QUuid entityID, QVariantMap arguments) { + QUuid actionID = QUuid::createUuid(); + bool success = actionWorker(entityID, [actionID, actionTypeString, entityID, arguments](EntitySimulation* simulation, + EntityItemPointer entity) { + EntityActionType actionType = EntityActionInterface::actionTypeFromString(actionTypeString); + if (actionType == ACTION_TYPE_NONE) { + return false; + } + return simulation->actionFactory(actionType, actionID, entity, arguments); + }); + if (success) { return actionID; } return QUuid(); @@ -492,55 +497,14 @@ QUuid EntityScriptingInterface::addAction(QString actionTypeString, QUuid entity bool EntityScriptingInterface::updateAction(QUuid entityID, QUuid actionID, QVariantMap arguments) { - if (!_entityTree) { - return false; - } - - _entityTree->lockForWrite(); - - EntitySimulation* simulation = _entityTree->getSimulation(); - EntityItemPointer entity = _entityTree->findEntityByEntityItemID(entityID); - if (!entity) { - qDebug() << "updateAction -- unknown entity" << entityID; - _entityTree->unlock(); - return false; - } - - if (!simulation) { - qDebug() << "updateAction -- no simulation" << entityID; - _entityTree->unlock(); - return false; - } - - bool result = entity->updateAction(simulation, actionID, arguments); - _entityTree->unlock(); - return result; + return actionWorker(entityID, [entityID, actionID,arguments](EntitySimulation* simulation, EntityItemPointer entity) { + return entity->updateAction(simulation, actionID, arguments); + }); } bool EntityScriptingInterface::deleteAction(QUuid entityID, QUuid actionID) { - if (!_entityTree) { - return false; - } - - _entityTree->lockForWrite(); - - EntitySimulation* simulation = _entityTree->getSimulation(); - EntityItemPointer entity = _entityTree->findEntityByEntityItemID(entityID); - if (!entity) { - qDebug() << "updateAction -- unknown entity" << entityID; - _entityTree->unlock(); - return false; - } - - if (!simulation) { - qDebug() << "updateAction -- no simulation" << entityID; - _entityTree->unlock(); - return false; - } - - bool result = entity->removeAction(simulation, actionID); - - _entityTree->unlock(); - return result; + return actionWorker(entityID, [entityID, actionID](EntitySimulation* simulation, EntityItemPointer entity) { + return entity->removeAction(simulation, actionID); + }); } diff --git a/libraries/entities/src/EntityScriptingInterface.h b/libraries/entities/src/EntityScriptingInterface.h index 38cd38c569..5c81ab15dd 100644 --- a/libraries/entities/src/EntityScriptingInterface.h +++ b/libraries/entities/src/EntityScriptingInterface.h @@ -155,6 +155,7 @@ signals: void clearingEntities(); private: + bool actionWorker(QUuid entityID, std::function actor); bool setVoxels(QUuid entityID, std::function actor); void queueEntityMessage(PacketType packetType, EntityItemID entityID, const EntityItemProperties& properties);