Fix errors in handling of std::shared_ptrs.

We can't reset to a pointer owned by another std::shared_ptr.
This commit is contained in:
Jeff Young 2022-11-22 13:14:00 +00:00
parent 226529235c
commit 50089ce558
2 changed files with 33 additions and 27 deletions

View File

@ -90,8 +90,8 @@ public:
std::shared_ptr<const CN_ANCHOR> GetSourceNode() const { return m_source; } std::shared_ptr<const CN_ANCHOR> GetSourceNode() const { return m_source; }
std::shared_ptr<const CN_ANCHOR> GetTargetNode() const { return m_target; } std::shared_ptr<const CN_ANCHOR> GetTargetNode() const { return m_target; }
void ResetSourceNode( const CN_ANCHOR* aNode ) { m_source.reset( aNode ); } void SetSourceNode( const std::shared_ptr<const CN_ANCHOR>& aNode ) { m_source = aNode; }
void ResetTargetNode( const CN_ANCHOR* aNode ) { m_target.reset( aNode ); } void SetTargetNode( const std::shared_ptr<const CN_ANCHOR>& aNode ) { m_target = aNode; }
void SetWeight( unsigned weight ) { m_weight = weight; } void SetWeight( unsigned weight ) { m_weight = weight; }
unsigned GetWeight() const { return m_weight; } unsigned GetWeight() const { return m_weight; }

View File

@ -331,7 +331,9 @@ void RN_NET::compute()
void RN_NET::optimizeRNEdges() void RN_NET::optimizeRNEdges()
{ {
auto optimizeZoneAnchor = auto optimizeZoneAnchor =
[&]( const VECTOR2I& aPos, const LSET& aLayerSet, const CN_ANCHOR*& aAnchor ) [&]( const VECTOR2I& aPos, const LSET& aLayerSet,
const std::shared_ptr<const CN_ANCHOR>& aAnchor,
std::function<void(const std::shared_ptr<const CN_ANCHOR>&)> setOptimizedTo )
{ {
SEG::ecoord closest_dist_sq = ( aAnchor->Pos() - aPos ).SquaredEuclideanNorm(); SEG::ecoord closest_dist_sq = ( aAnchor->Pos() - aPos ).SquaredEuclideanNorm();
VECTOR2I closest_pt; VECTOR2I closest_pt;
@ -360,16 +362,14 @@ void RN_NET::optimizeRNEdges()
} }
if( closest_item ) if( closest_item )
{ setOptimizedTo( closest_item->AddAnchor( closest_pt ) );
aAnchor = closest_item->AddAnchor( closest_pt ).get();
return true;
}
return false;
}; };
auto optimizeZoneToZoneAnchors = auto optimizeZoneToZoneAnchors =
[&]( const CN_ANCHOR*& a, const CN_ANCHOR*& b ) [&]( const std::shared_ptr<const CN_ANCHOR>& a,
const std::shared_ptr<const CN_ANCHOR>& b,
std::function<void(const std::shared_ptr<const CN_ANCHOR>&)> setOptimizedATo,
std::function<void(const std::shared_ptr<const CN_ANCHOR>&)> setOptimizedBTo )
{ {
for( CN_ITEM* itemA : a->Item()->ConnectedItems() ) for( CN_ITEM* itemA : a->Item()->ConnectedItems() )
{ {
@ -397,42 +397,48 @@ void RN_NET::optimizeRNEdges()
VECTOR2I ptA; VECTOR2I ptA;
shapeA->Collide( shapeB, startDist + 10, nullptr, &ptA ); shapeA->Collide( shapeB, startDist + 10, nullptr, &ptA );
a = zoneLayerA->AddAnchor( ptA ).get(); setOptimizedATo( zoneLayerA->AddAnchor( ptA ) );
VECTOR2I ptB; VECTOR2I ptB;
shapeB->Collide( shapeA, startDist + 10, nullptr, &ptB ); shapeB->Collide( shapeA, startDist + 10, nullptr, &ptB );
b = zoneLayerB->AddAnchor( ptB ).get(); setOptimizedBTo( zoneLayerB->AddAnchor( ptB ) );
return true;
} }
} }
} }
return false;
}; };
for( CN_EDGE& edge : m_rnEdges ) for( CN_EDGE& edge : m_rnEdges )
{ {
const CN_ANCHOR* source = edge.GetSourceNode().get(); const std::shared_ptr<const CN_ANCHOR>& source = edge.GetSourceNode();
const CN_ANCHOR* target = edge.GetTargetNode().get(); const std::shared_ptr<const CN_ANCHOR>& target = edge.GetTargetNode();
if( source->ConnectedItemsCount() == 0 ) if( source->ConnectedItemsCount() == 0 )
{ {
if( optimizeZoneAnchor( source->Pos(), source->Parent()->GetLayerSet(), target ) ) optimizeZoneAnchor( source->Pos(), source->Parent()->GetLayerSet(), target,
edge.ResetTargetNode( target ); [&]( const std::shared_ptr<const CN_ANCHOR>& optimized )
{
edge.SetTargetNode( optimized );
} );
} }
else if( target->ConnectedItemsCount() == 0 ) else if( target->ConnectedItemsCount() == 0 )
{ {
if( optimizeZoneAnchor( target->Pos(), target->Parent()->GetLayerSet(), source ) ) optimizeZoneAnchor( target->Pos(), target->Parent()->GetLayerSet(), source,
edge.ResetSourceNode( source ); [&]( const std::shared_ptr<const CN_ANCHOR>& optimized )
{
edge.SetSourceNode( optimized );
} );
} }
else else
{ {
if( optimizeZoneToZoneAnchors( source, target ) ) optimizeZoneToZoneAnchors( source, target,
[&]( const std::shared_ptr<const CN_ANCHOR>& optimized )
{ {
edge.ResetSourceNode( source ); edge.SetSourceNode( optimized );
edge.ResetTargetNode( target ); },
} [&]( const std::shared_ptr<const CN_ANCHOR>& optimized )
{
edge.SetTargetNode( optimized );
} );
} }
} }
} }