Skip to content

Commit

Permalink
use qobject_cast where possible over dynamic cast
Browse files Browse the repository at this point in the history
Its faster and has a compile time check for qobject
  • Loading branch information
guruofquality committed Oct 20, 2018
1 parent 95be885 commit c16d07c
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 83 deletions.
12 changes: 6 additions & 6 deletions AffinitySupport/AffinityZonesDock.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2014-2017 Josh Blum
// Copyright (c) 2014-2018 Josh Blum
// SPDX-License-Identifier: BSL-1.0

#include "MainWindow/IconUtils.hpp"
Expand Down Expand Up @@ -88,7 +88,7 @@ QStringList AffinityZonesDock::zones(void) const
QStringList zones;
for (int i = 0; i < _editorsTabs->count(); i++)
{
auto editor = dynamic_cast<AffinityZoneEditor *>(_editorsTabs->widget(i));
auto editor = qobject_cast<AffinityZoneEditor *>(_editorsTabs->widget(i));
assert(editor != nullptr);
zones.push_back(editor->zoneName());
}
Expand All @@ -99,7 +99,7 @@ QColor AffinityZonesDock::zoneToColor(const QString &zone)
{
for (int i = 0; i < _editorsTabs->count(); i++)
{
auto editor = dynamic_cast<AffinityZoneEditor *>(_editorsTabs->widget(i));
auto editor = qobject_cast<AffinityZoneEditor *>(_editorsTabs->widget(i));
assert(editor != nullptr);
if (zone == editor->zoneName()) return editor->color();
}
Expand All @@ -110,7 +110,7 @@ QJsonObject AffinityZonesDock::zoneToConfig(const QString &zone)
{
for (int i = 0; i < _editorsTabs->count(); i++)
{
auto editor = dynamic_cast<AffinityZoneEditor *>(_editorsTabs->widget(i));
auto editor = qobject_cast<AffinityZoneEditor *>(_editorsTabs->widget(i));
assert(editor != nullptr);
if (zone == editor->zoneName()) return editor->getCurrentConfig();
}
Expand Down Expand Up @@ -198,7 +198,7 @@ void AffinityZonesDock::updateTabColors(void)
{
for (int i = 0; i < _editorsTabs->count(); i++)
{
auto editor = dynamic_cast<AffinityZoneEditor *>(_editorsTabs->widget(i));
auto editor = qobject_cast<AffinityZoneEditor *>(_editorsTabs->widget(i));
_editorsTabs->setTabIcon(i, colorToWidgetIcon(editor->color()));
}
}
Expand All @@ -216,7 +216,7 @@ void AffinityZonesDock::saveAffinityZoneEditorsState(void)

for (int i = 0; i < _editorsTabs->count(); i++)
{
auto editor = dynamic_cast<AffinityZoneEditor *>(_editorsTabs->widget(i));
auto editor = qobject_cast<AffinityZoneEditor *>(_editorsTabs->widget(i));
assert(editor != nullptr);
const auto jsonDoc = QJsonDocument(editor->getCurrentConfig());
const auto value = jsonDoc.toJson(QJsonDocument::Compact);
Expand Down
6 changes: 3 additions & 3 deletions EvalEngine/EvalEngine.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2014-2017 Josh Blum
// Copyright (c) 2014-2018 Josh Blum
// SPDX-License-Identifier: BSL-1.0

#include "EvalEngine.hpp"
Expand Down Expand Up @@ -86,7 +86,7 @@ void EvalEngine::submitTopology(const GraphObjectList &graphObjects)
BlockInfos blockInfos;
for (auto obj : graphObjects)
{
auto block = dynamic_cast<GraphBlock *>(obj);
auto block = qobject_cast<GraphBlock *>(obj);
if (block == nullptr) continue;
_blockEvalMapper->setMapping(block, block);
connect(block, SIGNAL(triggerEvalEvent(void)), _blockEvalMapper, SLOT(map(void)));
Expand Down Expand Up @@ -114,7 +114,7 @@ void EvalEngine::submitActivateTopology(const bool active)

void EvalEngine::submitBlock(QObject *obj)
{
auto block = dynamic_cast<GraphBlock *>(obj);
auto block = qobject_cast<GraphBlock *>(obj);
assert(block != nullptr);
QMetaObject::invokeMethod(_impl, "submitBlock", Qt::QueuedConnection, Q_ARG(BlockInfo, blockToBlockInfo(block)));
}
Expand Down
14 changes: 7 additions & 7 deletions EvalEngine/TopologyTraversal.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2014-2017 Josh Blum
// Copyright (c) 2014-2018 Josh Blum
// SPDX-License-Identifier: BSL-1.0

#include "TopologyEval.hpp"
Expand All @@ -23,8 +23,8 @@ static std::vector<GraphConnectionEndpoint> traverseInputEps(
if (it != traversed.end()) return inputEndpoints;
traversed.push_back(inputEp);

auto inputBlock = dynamic_cast<GraphBlock *>(inputEp.getObj().data());
auto inputBreaker = dynamic_cast<GraphBreaker *>(inputEp.getObj().data());
auto inputBlock = qobject_cast<GraphBlock *>(inputEp.getObj().data());
auto inputBreaker = qobject_cast<GraphBreaker *>(inputEp.getObj().data());

if (inputBlock != nullptr)
{
Expand All @@ -36,7 +36,7 @@ static std::vector<GraphConnectionEndpoint> traverseInputEps(
auto nodeName = inputBreaker->getNodeName();
for (auto graphObject : graphObjects)
{
auto breaker = dynamic_cast<GraphBreaker *>(graphObject);
auto breaker = qobject_cast<GraphBreaker *>(graphObject);
if (breaker == nullptr) continue;
if (not breaker->isEnabled()) continue;
if (breaker->getNodeName() != nodeName) continue;
Expand All @@ -45,7 +45,7 @@ static std::vector<GraphConnectionEndpoint> traverseInputEps(
//this is the recursive part
for (auto graphSubObject : graphObjects)
{
auto connection = dynamic_cast<GraphConnection *>(graphSubObject);
auto connection = qobject_cast<GraphConnection *>(graphSubObject);
if (connection == nullptr) continue;
if (not connection->isEnabled()) continue;
if (connection->getOutputEndpoint().getObj() != breaker) continue;
Expand All @@ -69,7 +69,7 @@ ConnectionInfos TopologyEval::getConnectionInfo(const GraphObjectList &graphObje
ConnectionInfos connections;
for (auto graphObject : graphObjects)
{
auto connection = dynamic_cast<GraphConnection *>(graphObject);
auto connection = qobject_cast<GraphConnection *>(graphObject);
if (connection == nullptr) continue;
if (not connection->isEnabled()) continue;
if (not connection->getInputEndpoint().isValid()) continue;
Expand All @@ -81,7 +81,7 @@ ConnectionInfos TopologyEval::getConnectionInfo(const GraphObjectList &graphObje

//ignore connections from output breakers
//we will come back to them from the block to breaker to block path
auto outputBreaker = dynamic_cast<GraphBreaker *>(outputEp.getObj().data());
auto outputBreaker = qobject_cast<GraphBreaker *>(outputEp.getObj().data());
if (outputBreaker != nullptr) continue;

for (const auto &subEp : traverseInputEps(inputEp, graphObjects))
Expand Down
6 changes: 3 additions & 3 deletions GraphEditor/GraphDraw.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2013-2017 Josh Blum
// Copyright (c) 2013-2018 Josh Blum
// SPDX-License-Identifier: BSL-1.0

#include "GraphEditor/GraphDraw.hpp"
Expand Down Expand Up @@ -31,7 +31,7 @@

GraphDraw::GraphDraw(QWidget *parent):
QGraphicsView(parent),
_graphEditor(dynamic_cast<GraphEditor *>(parent)),
_graphEditor(qobject_cast<GraphEditor *>(parent)),
_zoomScale(1.0),
_selectionState(0)
{
Expand Down Expand Up @@ -220,7 +220,7 @@ void GraphDraw::updateEnabledActions(void)
//and enable/disable the actions in the move graph objects submenu
for (auto child : mainMenu->moveGraphObjectsMenu->children())
{
auto action = dynamic_cast<QAction *>(child);
auto action = qobject_cast<QAction *>(child);
if (action != nullptr) action->setEnabled(selectedNoC);
}
}
Expand Down
12 changes: 6 additions & 6 deletions GraphEditor/GraphDrawSelection.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2013-2017 Josh Blum
// Copyright (c) 2013-2018 Josh Blum
// SPDX-License-Identifier: BSL-1.0

#include "GraphEditor/GraphDraw.hpp"
Expand Down Expand Up @@ -243,10 +243,10 @@ GraphObjectList GraphDraw::getGraphObjects(const int selectionFlags)
{
auto o = dynamic_cast<GraphObject *>(child);
if (o == nullptr) continue;
if (((selectionFlags & GRAPH_BLOCK) != 0) and (dynamic_cast<GraphBlock *>(o) != nullptr)) l.push_back(o);
if (((selectionFlags & GRAPH_BREAKER) != 0) and (dynamic_cast<GraphBreaker *>(o) != nullptr)) l.push_back(o);
if (((selectionFlags & GRAPH_CONNECTION) != 0) and (dynamic_cast<GraphConnection *>(o) != nullptr)) l.push_back(o);
if (((selectionFlags & GRAPH_WIDGET) != 0) and (dynamic_cast<GraphWidget *>(o) != nullptr)) l.push_back(o);
if (((selectionFlags & GRAPH_BLOCK) != 0) and (qobject_cast<GraphBlock *>(o) != nullptr)) l.push_back(o);
if (((selectionFlags & GRAPH_BREAKER) != 0) and (qobject_cast<GraphBreaker *>(o) != nullptr)) l.push_back(o);
if (((selectionFlags & GRAPH_CONNECTION) != 0) and (qobject_cast<GraphConnection *>(o) != nullptr)) l.push_back(o);
if (((selectionFlags & GRAPH_WIDGET) != 0) and (qobject_cast<GraphWidget *>(o) != nullptr)) l.push_back(o);
}
return l;
}
Expand Down Expand Up @@ -339,7 +339,7 @@ QString GraphDraw::getSelectionDescription(const int selectionFlags)
//if a single connection is selected, pretty print its endpoint IDs
if (selected.size() == 1)
{
auto conn = dynamic_cast<GraphConnection *>(selected.at(0));
auto conn = qobject_cast<GraphConnection *>(selected.at(0));
if (conn == nullptr) return selected.at(0)->getId();
return tr("%1[%2] to %3[%4]").arg(
conn->getOutputEndpoint().getObj()->getId(),
Expand Down
36 changes: 18 additions & 18 deletions GraphEditor/GraphEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static const size_t POLL_WIDGET_CHANGES_MS = 1000;
GraphEditor::GraphEditor(QWidget *parent):
QTabWidget(parent),
_logger(Poco::Logger::get("PothosFlow.GraphEditor")),
_parentTabWidget(dynamic_cast<QTabWidget *>(parent)),
_parentTabWidget(qobject_cast<QTabWidget *>(parent)),
_moveGraphObjectsMapper(new QSignalMapper(this)),
_insertGraphWidgetsMapper(new QSignalMapper(this)),
_stateManager(new GraphStateManager(this)),
Expand Down Expand Up @@ -281,7 +281,7 @@ GraphConnection *GraphEditor::makeConnection(const GraphConnectionEndpoint &ep0,
//duplicate check
for (auto obj : this->getGraphObjects(GRAPH_CONNECTION))
{
auto conn = dynamic_cast<GraphConnection *>(obj);
auto conn = qobject_cast<GraphConnection *>(obj);
assert(conn != nullptr);
if (
(conn->getOutputEndpoint() == ep0 and conn->getInputEndpoint() == ep1) or
Expand Down Expand Up @@ -312,7 +312,7 @@ static GraphBreaker *findInputBreaker(GraphEditor *editor, const GraphConnection
{
for (auto obj : editor->getGraphObjects(GRAPH_CONNECTION))
{
auto conn = dynamic_cast<GraphConnection *>(obj);
auto conn = qobject_cast<GraphConnection *>(obj);
assert(conn != nullptr);
if (not (conn->getOutputEndpoint().getObj()->scene() == conn->getInputEndpoint().getObj()->scene())) continue;
if (not (conn->getOutputEndpoint() == ep)) continue;
Expand All @@ -321,7 +321,7 @@ static GraphBreaker *findInputBreaker(GraphEditor *editor, const GraphConnection
const auto &pairs = conn->getSigSlotPairs();
if (not pairs.empty() and pairs.at(0).first != signalName) continue;
}
auto breaker = dynamic_cast<GraphBreaker *>(conn->getInputEndpoint().getObj().data());
auto breaker = qobject_cast<GraphBreaker *>(conn->getInputEndpoint().getObj().data());
if (breaker == nullptr) continue;
return breaker;
}
Expand All @@ -346,7 +346,7 @@ void GraphEditor::handleMoveGraphObjects(const int index)
std::vector<GraphConnection *> boundaryConnections;
for (auto obj : this->getGraphObjects(GRAPH_CONNECTION))
{
auto conn = dynamic_cast<GraphConnection *>(obj);
auto conn = qobject_cast<GraphConnection *>(obj);
assert(conn != nullptr);

//Connection has the same endpoints, so make sure that the parent is corrected to the endpoint
Expand Down Expand Up @@ -414,7 +414,7 @@ void GraphEditor::handleMoveGraphObjects(const int index)
for (auto obj : this->getGraphObjects(GRAPH_BREAKER))
{
if (obj->draw() != epIn.getObj()->draw()) continue;
auto outBreaker = dynamic_cast<GraphBreaker *>(obj);
auto outBreaker = qobject_cast<GraphBreaker *>(obj);
assert(outBreaker != nullptr);
if (outBreaker->isInput()) continue;
if (outBreaker->getNodeName() != name) continue;
Expand Down Expand Up @@ -451,7 +451,7 @@ void GraphEditor::handleAddBlock(const QJsonObject &blockDesc)
QPointF where(std::rand()%100, std::rand()%100);

//determine where, a nice point on the visible drawing area sort of upper left
auto view = dynamic_cast<QGraphicsView *>(this->currentWidget());
auto view = qobject_cast<QGraphicsView *>(this->currentWidget());
where += view->mapToScene(this->size().width()/4, this->size().height()/4);

this->handleAddBlock(blockDesc, where);
Expand Down Expand Up @@ -515,7 +515,7 @@ void GraphEditor::handleCreateOutputBreaker(void)

void GraphEditor::handleInsertGraphWidget(QObject *obj)
{
auto block = dynamic_cast<GraphBlock *>(obj);
auto block = qobject_cast<GraphBlock *>(obj);
assert(block != nullptr);
assert(block->isGraphWidget());

Expand Down Expand Up @@ -667,7 +667,7 @@ void GraphEditor::handlePaste(void)
}

//determine an acceptable position to center the paste
auto view = dynamic_cast<QGraphicsView *>(this->currentWidget());
auto view = qobject_cast<QGraphicsView *>(this->currentWidget());
auto pastePos = view->mapToScene(view->mapFromGlobal(QCursor::pos()));
if (not view->sceneRect().contains(pastePos))
{
Expand Down Expand Up @@ -833,7 +833,7 @@ void GraphEditor::handleSetEnabled(const bool enb)
for (auto obj : draw->getObjectsSelected())
{
//stash the graph widget's actual block
auto graphWidget = dynamic_cast<GraphWidget *>(obj);
auto graphWidget = qobject_cast<GraphWidget *>(obj);
if (graphWidget != nullptr) obj = graphWidget->getGraphBlock();
if (obj->isEnabled() != enb) objs.insert(obj);
}
Expand Down Expand Up @@ -887,7 +887,7 @@ void GraphEditor::handleAffinityZoneClicked(const QString &zone)

for (auto obj : draw->getObjectsSelected(GRAPH_BLOCK))
{
auto block = dynamic_cast<GraphBlock *>(obj);
auto block = qobject_cast<GraphBlock *>(obj);
assert(block != nullptr);
block->setAffinityZone(zone);
}
Expand All @@ -898,7 +898,7 @@ void GraphEditor::handleAffinityZoneChanged(const QString &zone)
{
for (auto obj : this->getGraphObjects(GRAPH_BLOCK))
{
auto block = dynamic_cast<GraphBlock *>(obj);
auto block = qobject_cast<GraphBlock *>(obj);
assert(block != nullptr);
if (block->getAffinityZone() == zone) block->changed();
}
Expand Down Expand Up @@ -938,7 +938,7 @@ void GraphEditor::handleBlockDisplayModeChange(void)
{
for (auto obj : this->getGraphObjects(GRAPH_BLOCK))
{
auto block = dynamic_cast<GraphBlock *>(obj);
auto block = qobject_cast<GraphBlock *>(obj);
assert(block != nullptr);
block->changed();
}
Expand All @@ -962,7 +962,7 @@ void GraphEditor::handleBlockXcrement(const int adj)
GraphObjectList changedObjects;
for (auto obj : draw->getObjectsSelected(GRAPH_BLOCK))
{
auto block = dynamic_cast<GraphBlock *>(obj);
auto block = qobject_cast<GraphBlock *>(obj);
assert(block != nullptr);
for (const auto &propKey : block->getProperties())
{
Expand Down Expand Up @@ -1096,14 +1096,14 @@ void GraphEditor::updateGraphEditorMenus(void)
bool hasGraphWidgetsToInsert = false;
for (auto obj : this->getGraphObjects(GRAPH_BLOCK))
{
auto block = dynamic_cast<GraphBlock *>(obj);
auto block = qobject_cast<GraphBlock *>(obj);
assert(block != nullptr);
if (not block->isGraphWidget()) continue;

//does block have an active graph display?
for (auto subObj : this->getGraphObjects(GRAPH_WIDGET))
{
auto display = dynamic_cast<GraphWidget *>(subObj);
auto display = qobject_cast<GraphWidget *>(subObj);
assert(display != nullptr);
if (display->getGraphBlock() == block) goto next_block;
}
Expand All @@ -1123,7 +1123,7 @@ void GraphEditor::updateGraphEditorMenus(void)

GraphDraw *GraphEditor::getGraphDraw(const int index) const
{
auto draw = dynamic_cast<GraphDraw *>(this->widget(index));
auto draw = qobject_cast<GraphDraw *>(this->widget(index));
assert(draw != nullptr);
return draw;
}
Expand Down Expand Up @@ -1201,7 +1201,7 @@ void GraphEditor::handlePollWidgetTimer(void)
QStringList changedIds;
for (auto obj : this->getGraphObjects(GRAPH_WIDGET))
{
auto graphWidget = dynamic_cast<const GraphWidget *>(obj);
auto graphWidget = qobject_cast<const GraphWidget *>(obj);
assert(graphWidget != nullptr);
if (not graphWidget->didWidgetStateChange()) continue;
auto graphBlock = graphWidget->getGraphBlock();
Expand Down
4 changes: 2 additions & 2 deletions GraphEditor/GraphEditorExport.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2016-2017 Josh Blum
// Copyright (c) 2016-2018 Josh Blum
// SPDX-License-Identifier: BSL-1.0

#include "GraphEditor/GraphEditor.hpp"
Expand Down Expand Up @@ -66,7 +66,7 @@ void GraphEditor::exportToJSONTopology(const QString &fileName)
std::map<size_t, const GraphBlock*> uidToBlock;
for (const auto *obj : graphObjects)
{
const auto block = dynamic_cast<const GraphBlock *>(obj);
const auto block = qobject_cast<const GraphBlock *>(obj);
if (block == nullptr) continue;
if (not block->isEnabled()) continue;
if (block->isGraphWidget()) continue;
Expand Down
Loading

0 comments on commit c16d07c

Please sign in to comment.