Skip to content

Commit

Permalink
code clean up: dynamic_cast reference whenever possible, to get autom…
Browse files Browse the repository at this point in the history
…atic failure
  • Loading branch information
jafl committed May 17, 2024
1 parent c4cc478 commit 3a70170
Show file tree
Hide file tree
Showing 75 changed files with 495 additions and 694 deletions.
20 changes: 8 additions & 12 deletions libj2dplot/code/J2DPlotFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,31 +164,27 @@ J2DPlotFunction::Receive
{
if (sender == itsVarList && message.Is(JVariableList::kVarInserted))
{
const auto* info = dynamic_cast<const JVariableList::VarInserted*>(&message);
assert( info != nullptr );
info->AdjustIndex(&itsXIndex);
auto& info = dynamic_cast<const JVariableList::VarInserted&>(message);
info.AdjustIndex(&itsXIndex);
}

else if (sender == itsVarList && message.Is(JVariableList::kVarRemoved))
{
const auto* info = dynamic_cast<const JVariableList::VarRemoved*>(&message);
assert( info != nullptr );
const bool ok = info->AdjustIndex(&itsXIndex);
auto& info = dynamic_cast<const JVariableList::VarRemoved&>(message);
const bool ok = info.AdjustIndex(&itsXIndex);
assert( ok ); // client must ensure this
}

else if (sender == itsVarList && message.Is(JVariableList::kVarMoved))
{
const auto* info = dynamic_cast<const JVariableList::VarMoved*>(&message);
assert( info != nullptr );
info->AdjustIndex(&itsXIndex);
auto& info = dynamic_cast<const JVariableList::VarMoved&>(message);
info.AdjustIndex(&itsXIndex);
}

else if (sender == itsVarList && message.Is(JVariableList::kVarValueChanged))
{
const auto* info = dynamic_cast<const JVariableList::VarValueChanged*>(&message);
assert( info != nullptr );
if (info->GetVarIndex() != itsXIndex)
auto& info = dynamic_cast<const JVariableList::VarValueChanged&>(message);
if (info.GetVarIndex() != itsXIndex)
{
UpdateFunction();
BroadcastCurveChanged();
Expand Down
5 changes: 2 additions & 3 deletions libj2dplot/code/JX2DCurveOptionsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,9 @@ JX2DCurveOptionsDialog::Receive
{
if (sender == itsNameList && message.Is(JX2DCurveNameList::kNameSelected))
{
const auto* info = dynamic_cast<const JX2DCurveNameList::NameSelected*>(&message);
assert(info != nullptr);
auto& info = dynamic_cast<const JX2DCurveNameList::NameSelected&>(message);
SaveSettings();
itsCurrentIndex = info->GetIndex();
itsCurrentIndex = info.GetIndex();
AdjustWidgets();
}

Expand Down
27 changes: 11 additions & 16 deletions libjcore/code/JAliasArray.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ JAliasArray<T>::SearchSortedOTI
JIndex* data = const_cast<JIndex*>(itsIndexArray->GetCArray()); \
for (JIndex _i=0; _i<count; _i++) \
{ \
m->AdjustIndex(data + _i); \
m.AdjustIndex(data + _i); \
}

template <class T>
Expand All @@ -411,27 +411,25 @@ JAliasArray<T>::Receive
{
if (sender == itsData && message.Is(JListT::kItemsInserted))
{
auto* m = dynamic_cast<const JListT::ItemsInserted*>(&message);
assert( m != nullptr );
auto& m = dynamic_cast<const JListT::ItemsInserted&>(message);
ADJUST_INDEX(m);

for (JIndex i = m->GetFirstIndex(); i <= m->GetLastIndex(); i++)
for (JIndex i = m.GetFirstIndex(); i <= m.GetLastIndex(); i++)
{
itsIndexArray->InsertSorted(i);
}

SetItemCount(GetItemCount() + m->GetCount());
SetItemCount(GetItemCount() + m.GetCount());
}
else if (sender == itsData && message.Is(JListT::kItemsRemoved))
{
auto* m = dynamic_cast<const JListT::ItemsRemoved*>(&message);
assert( m != nullptr );
auto& m = dynamic_cast<const JListT::ItemsRemoved&>(message);

const JSize count = itsIndexArray->GetItemCount();
for (JIndex i=count; i>=1; i--)
{
JIndex index = itsIndexArray->GetItem(i);
if (m->AdjustIndex(&index))
if (m.AdjustIndex(&index))
{
itsIndexArray->SetItem(i, index);
}
Expand All @@ -441,28 +439,25 @@ JAliasArray<T>::Receive
}
}

SetItemCount(GetItemCount() - m->GetCount());
SetItemCount(GetItemCount() - m.GetCount());
}
else if (sender == itsData && message.Is(JListT::kItemMoved))
{
auto* m = dynamic_cast<const JListT::ItemMoved*>(&message);
assert( m != nullptr );
auto& m = dynamic_cast<const JListT::ItemMoved&>(message);
ADJUST_INDEX(m);
}
else if (sender == itsData && message.Is(JListT::kItemsSwapped))
{
auto* m = dynamic_cast<const JListT::ItemsSwapped*>(&message);
assert( m != nullptr );
auto& m = dynamic_cast<const JListT::ItemsSwapped&>(message);
ADJUST_INDEX(m);
}
else if (sender == itsData && message.Is(JListT::kItemsChanged))
{
// Sort() would be O(N log N). This method is O(N) + O(log N) = O(N).

auto* m = dynamic_cast<const JListT::ItemsChanged*>(&message);
assert( m != nullptr );
auto& m = dynamic_cast<const JListT::ItemsChanged&>(message);

for (JIndex j=m->GetFirstIndex(); j<=m->GetLastIndex(); j++)
for (JIndex j=m.GetFirstIndex(); j<=m.GetLastIndex(); j++)
{
const JSize count = itsIndexArray->GetItemCount();
for (JIndex i=1; i<=count; i++)
Expand Down
40 changes: 16 additions & 24 deletions libjcore/code/JAuxTableData.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -116,60 +116,52 @@ JAuxTableData<T>::Receive

if (sender == itsTable && message.Is(JTableData::kRowsInserted))
{
auto* info = dynamic_cast<const JTableData::RowsInserted*>(&message);
assert( info != nullptr );
JPackedTableData<T>::InsertRows(info->GetFirstIndex(), info->GetCount());
auto& info = dynamic_cast<const JTableData::RowsInserted&>(message);
JPackedTableData<T>::InsertRows(info.GetFirstIndex(), info.GetCount());
}

else if (sender == itsTable && message.Is(JTableData::kRowDuplicated))
{
auto* info = dynamic_cast<const JTableData::RowDuplicated*>(&message);
assert( info != nullptr );
JPackedTableData<T>::DuplicateRow(info->GetOrigIndex(), info->GetNewIndex());
auto& info = dynamic_cast<const JTableData::RowDuplicated&>(message);
JPackedTableData<T>::DuplicateRow(info.GetOrigIndex(), info.GetNewIndex());
}

else if (sender == itsTable && message.Is(JTableData::kRowsRemoved))
{
auto* info = dynamic_cast<const JTableData::RowsRemoved*>(&message);
assert( info != nullptr );
JPackedTableData<T>::RemoveNextRows(info->GetFirstIndex(), info->GetCount());
auto& info = dynamic_cast<const JTableData::RowsRemoved&>(message);
JPackedTableData<T>::RemoveNextRows(info.GetFirstIndex(), info.GetCount());
}

else if (sender == itsTable && message.Is(JTableData::kRowMoved))
{
auto* info = dynamic_cast<const JTableData::RowMoved*>(&message);
assert( info != nullptr );
JPackedTableData<T>::MoveRow(info->GetOrigIndex(), info->GetNewIndex());
auto& info = dynamic_cast<const JTableData::RowMoved&>(message);
JPackedTableData<T>::MoveRow(info.GetOrigIndex(), info.GetNewIndex());
}

// columns changed

else if (sender == itsTable && message.Is(JTableData::kColsInserted))
{
auto* info = dynamic_cast<const JTableData::ColsInserted*>(&message);
assert( info != nullptr );
JPackedTableData<T>::InsertCols(info->GetFirstIndex(), info->GetCount());
auto& info = dynamic_cast<const JTableData::ColsInserted&>(message);
JPackedTableData<T>::InsertCols(info.GetFirstIndex(), info.GetCount());
}

else if (sender == itsTable && message.Is(JTableData::kColDuplicated))
{
auto* info = dynamic_cast<const JTableData::ColDuplicated*>(&message);
assert( info != nullptr );
JPackedTableData<T>::DuplicateCol(info->GetOrigIndex(), info->GetNewIndex());
auto& info = dynamic_cast<const JTableData::ColDuplicated&>(message);
JPackedTableData<T>::DuplicateCol(info.GetOrigIndex(), info.GetNewIndex());
}

else if (sender == itsTable && message.Is(JTableData::kColsRemoved))
{
auto* info = dynamic_cast<const JTableData::ColsRemoved*>(&message);
assert( info != nullptr );
JPackedTableData<T>::RemoveNextCols(info->GetFirstIndex(), info->GetCount());
auto& info = dynamic_cast<const JTableData::ColsRemoved&>(message);
JPackedTableData<T>::RemoveNextCols(info.GetFirstIndex(), info.GetCount());
}

else if (sender == itsTable && message.Is(JTableData::kColMoved))
{
auto* info = dynamic_cast<const JTableData::ColMoved*>(&message);
assert( info != nullptr );
JPackedTableData<T>::MoveCol(info->GetOrigIndex(), info->GetNewIndex());
auto& info = dynamic_cast<const JTableData::ColMoved&>(message);
JPackedTableData<T>::MoveCol(info.GetOrigIndex(), info.GetNewIndex());
}

// everything changed
Expand Down
7 changes: 3 additions & 4 deletions libjcore/code/JBroadcasterMessageIterator.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ JBroadcasterMessageIterator<T>::ListChanged

if (message.Is(JListT::kItemsInserted))
{
auto* info = dynamic_cast<const JListT::ItemsInserted*>(&message);
assert( info != nullptr );
if (JListIterator<T>::GetCursor() == 0 && info->GetFirstIndex() == 1)
auto& info = dynamic_cast<const JListT::ItemsInserted&>(message);
if (JListIterator<T>::GetCursor() == 0 && info.GetFirstIndex() == 1)
{
JListIterator<T>::MoveTo(JListT::kStartAfter, info->GetCount());
JListIterator<T>::MoveTo(JListT::kStartAfter, info.GetCount());
}
}
}
Expand Down
49 changes: 20 additions & 29 deletions libjcore/code/JBufferTableData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,71 +62,62 @@ JBufferTableData::Receive

if (sender == data && message.Is(JTableData::kRectChanged))
{
auto* info = dynamic_cast<const JTableData::RectChanged*>(&message);
assert( info != nullptr );
UpdateRect(info->GetRect());
auto& info = dynamic_cast<const JTableData::RectChanged&>(message);
UpdateRect(info.GetRect());
}

// rows changed

else if (sender == data && message.Is(JTableData::kRowsInserted))
{
auto* info = dynamic_cast<const JTableData::RowsInserted*>(&message);
assert( info != nullptr );
InsertRows(info->GetFirstIndex(), info->GetCount());
UpdateRows(info->GetFirstIndex(), info->GetCount());
auto& info = dynamic_cast<const JTableData::RowsInserted&>(message);
InsertRows(info.GetFirstIndex(), info.GetCount());
UpdateRows(info.GetFirstIndex(), info.GetCount());
}

else if (sender == data && message.Is(JTableData::kRowDuplicated))
{
auto* info = dynamic_cast<const JTableData::RowDuplicated*>(&message);
assert( info != nullptr );
DuplicateRow(info->GetOrigIndex(), info->GetNewIndex());
auto& info = dynamic_cast<const JTableData::RowDuplicated&>(message);
DuplicateRow(info.GetOrigIndex(), info.GetNewIndex());
}

else if (sender == data && message.Is(JTableData::kRowsRemoved))
{
auto* info = dynamic_cast<const JTableData::RowsRemoved*>(&message);
assert( info != nullptr );
RemoveNextRows(info->GetFirstIndex(), info->GetCount());
auto& info = dynamic_cast<const JTableData::RowsRemoved&>(message);
RemoveNextRows(info.GetFirstIndex(), info.GetCount());
}

else if (sender == data && message.Is(JTableData::kRowMoved))
{
auto* info = dynamic_cast<const JTableData::RowMoved*>(&message);
assert( info != nullptr );
MoveRow(info->GetOrigIndex(), info->GetNewIndex());
auto& info = dynamic_cast<const JTableData::RowMoved&>(message);
MoveRow(info.GetOrigIndex(), info.GetNewIndex());
}

// columns changed

else if (sender == data && message.Is(JTableData::kColsInserted))
{
auto* info = dynamic_cast<const JTableData::ColsInserted*>(&message);
assert( info != nullptr );
InsertCols(info->GetFirstIndex(), info->GetCount());
UpdateCols(info->GetFirstIndex(), info->GetCount());
auto& info = dynamic_cast<const JTableData::ColsInserted&>(message);
InsertCols(info.GetFirstIndex(), info.GetCount());
UpdateCols(info.GetFirstIndex(), info.GetCount());
}

else if (sender == data && message.Is(JTableData::kColDuplicated))
{
auto* info = dynamic_cast<const JTableData::ColDuplicated*>(&message);
assert( info != nullptr );
DuplicateCol(info->GetOrigIndex(), info->GetNewIndex());
auto& info = dynamic_cast<const JTableData::ColDuplicated&>(message);
DuplicateCol(info.GetOrigIndex(), info.GetNewIndex());
}

else if (sender == data && message.Is(JTableData::kColsRemoved))
{
auto* info = dynamic_cast<const JTableData::ColsRemoved*>(&message);
assert( info != nullptr );
RemoveNextCols(info->GetFirstIndex(), info->GetCount());
auto& info = dynamic_cast<const JTableData::ColsRemoved&>(message);
RemoveNextCols(info.GetFirstIndex(), info.GetCount());
}

else if (sender == data && message.Is(JTableData::kColMoved))
{
auto* info = dynamic_cast<const JTableData::ColMoved*>(&message);
assert( info != nullptr );
MoveCol(info->GetOrigIndex(), info->GetNewIndex());
auto& info = dynamic_cast<const JTableData::ColMoved&>(message);
MoveCol(info.GetOrigIndex(), info.GetNewIndex());
}

// something else
Expand Down
10 changes: 4 additions & 6 deletions libjcore/code/JContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,13 @@ JContainer::Receive
{
if (sender == itsList && message.Is(JListT::kItemsInserted))
{
auto* info = dynamic_cast<const JListT::ItemsInserted*>(&message);
assert( info != nullptr );
SetItemCount(GetItemCount() + info->GetCount());
auto& info = dynamic_cast<const JListT::ItemsInserted&>(message);
SetItemCount(GetItemCount() + info.GetCount());
}
else if (sender == itsList && message.Is(JListT::kItemsRemoved))
{
auto* info = dynamic_cast<const JListT::ItemsRemoved*>(&message);
assert( info != nullptr );
SetItemCount(GetItemCount() - info->GetCount());
auto& info = dynamic_cast<const JListT::ItemsRemoved&>(message);
SetItemCount(GetItemCount() - info.GetCount());
}
else
{
Expand Down
18 changes: 8 additions & 10 deletions libjcore/code/JListIterator.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -529,24 +529,22 @@ JListIterator<T>::ListChanged
{
if (message.Is(JListT::kItemsInserted))
{
auto* info = dynamic_cast<const JListT::ItemsInserted*>(&message);
assert( info != nullptr );
if (itsCursorPosition >= info->GetFirstIndex())
auto& info = dynamic_cast<const JListT::ItemsInserted&>(message);
if (itsCursorPosition >= info.GetFirstIndex())
{
itsCursorPosition += info->GetCount();
itsCursorPosition += info.GetCount();
}
}
else if (message.Is(JListT::kItemsRemoved))
{
auto* info = dynamic_cast<const JListT::ItemsRemoved*>(&message);
assert( info != nullptr );
if (itsCursorPosition > info->GetLastIndex())
auto& info = dynamic_cast<const JListT::ItemsRemoved&>(message);
if (itsCursorPosition > info.GetLastIndex())
{
itsCursorPosition -= info->GetCount();
itsCursorPosition -= info.GetCount();
}
else if (itsCursorPosition >= info->GetFirstIndex())
else if (itsCursorPosition >= info.GetFirstIndex())
{
itsCursorPosition = info->GetFirstIndex()-1;
itsCursorPosition = info.GetFirstIndex()-1;
}
}
else if (message.Is(JListT::kCopied))
Expand Down
Loading

0 comments on commit 3a70170

Please sign in to comment.