Return plain ptr from ITracker::findChild instead of shared_ptr

This is part of wider rework of trackers to avoid `std::shared_ptr`
everywhere.

Related to #2088
This commit is contained in:
Martin Hořeňovský 2020-12-04 21:37:19 +01:00
parent 21c97f2fad
commit 6350851f9a
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
3 changed files with 26 additions and 20 deletions

View File

@ -34,7 +34,7 @@ namespace Catch {
~GeneratorTracker(); ~GeneratorTracker();
static GeneratorTracker& acquire( TrackerContext& ctx, TestCaseTracking::NameAndLocation const& nameAndLocation ) { static GeneratorTracker& acquire( TrackerContext& ctx, TestCaseTracking::NameAndLocation const& nameAndLocation ) {
std::shared_ptr<GeneratorTracker> tracker; GeneratorTracker* tracker;
ITracker& currentTracker = ctx.currentTracker(); ITracker& currentTracker = ctx.currentTracker();
// Under specific circumstances, the generator we want // Under specific circumstances, the generator we want
@ -48,18 +48,22 @@ namespace Catch {
// } // }
// //
// without it, the code above creates 5 nested generators. // without it, the code above creates 5 nested generators.
if (currentTracker.nameAndLocation() == nameAndLocation) { if ( currentTracker.nameAndLocation() == nameAndLocation ) {
auto thisTracker = currentTracker.parent().findChild(nameAndLocation); auto thisTracker =
assert(thisTracker); currentTracker.parent().findChild( nameAndLocation );
assert(thisTracker->isGeneratorTracker()); assert( thisTracker );
tracker = std::static_pointer_cast<GeneratorTracker>(thisTracker); assert( thisTracker->isGeneratorTracker() );
} else if ( TestCaseTracking::ITrackerPtr childTracker = currentTracker.findChild( nameAndLocation ) ) { tracker = static_cast<GeneratorTracker*>( thisTracker );
} else if ( ITracker* childTracker =
currentTracker.findChild( nameAndLocation ) ) {
assert( childTracker ); assert( childTracker );
assert( childTracker->isGeneratorTracker() ); assert( childTracker->isGeneratorTracker() );
tracker = std::static_pointer_cast<GeneratorTracker>( childTracker ); tracker = static_cast<GeneratorTracker*>( childTracker );
} else { } else {
tracker = std::make_shared<GeneratorTracker>( nameAndLocation, ctx, &currentTracker ); auto newTracker = std::make_shared<GeneratorTracker>(
currentTracker.addChild( tracker ); nameAndLocation, ctx, &currentTracker );
tracker = newTracker.get();
currentTracker.addChild( std::move(newTracker) );
} }
if( !tracker->isComplete() ) { if( !tracker->isComplete() ) {

View File

@ -34,7 +34,7 @@ namespace TestCaseTracking {
m_children.push_back( child ); m_children.push_back( child );
} }
ITrackerPtr ITracker::findChild( NameAndLocation const& nameAndLocation ) { ITracker* ITracker::findChild( NameAndLocation const& nameAndLocation ) {
auto it = std::find_if( auto it = std::find_if(
m_children.begin(), m_children.begin(),
m_children.end(), m_children.end(),
@ -43,7 +43,7 @@ namespace TestCaseTracking {
nameAndLocation.location && nameAndLocation.location &&
tracker->nameAndLocation().name == nameAndLocation.name; tracker->nameAndLocation().name == nameAndLocation.name;
} ); } );
return ( it != m_children.end() ) ? *it : nullptr; return ( it != m_children.end() ) ? it->get() : nullptr;
} }
@ -195,17 +195,19 @@ namespace TestCaseTracking {
bool SectionTracker::isSectionTracker() const { return true; } bool SectionTracker::isSectionTracker() const { return true; }
SectionTracker& SectionTracker::acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation ) { SectionTracker& SectionTracker::acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation ) {
std::shared_ptr<SectionTracker> section; SectionTracker* section;
ITracker& currentTracker = ctx.currentTracker(); ITracker& currentTracker = ctx.currentTracker();
if( ITrackerPtr childTracker = currentTracker.findChild( nameAndLocation ) ) { if ( ITracker* childTracker =
currentTracker.findChild( nameAndLocation ) ) {
assert( childTracker ); assert( childTracker );
assert( childTracker->isSectionTracker() ); assert( childTracker->isSectionTracker() );
section = std::static_pointer_cast<SectionTracker>( childTracker ); section = static_cast<SectionTracker*>( childTracker );
} } else {
else { auto newSection = std::make_shared<SectionTracker>(
section = std::make_shared<SectionTracker>( nameAndLocation, ctx, &currentTracker ); nameAndLocation, ctx, &currentTracker );
currentTracker.addChild( section ); section = newSection.get();
currentTracker.addChild( std::move( newSection ) );
} }
if( !ctx.completedCycle() ) if( !ctx.completedCycle() )
section->tryOpen(); section->tryOpen();

View File

@ -75,7 +75,7 @@ namespace TestCaseTracking {
* *
* Returns nullptr if not found. * Returns nullptr if not found.
*/ */
ITrackerPtr findChild( NameAndLocation const& nameAndLocation ); ITracker* findChild( NameAndLocation const& nameAndLocation );
//! Have any children been added? //! Have any children been added?
bool hasChildren() const { bool hasChildren() const {
return !m_children.empty(); return !m_children.empty();