1
0
mirror of https://github.com/catchorg/Catch2.git synced 2025-01-16 07:08:01 +00:00

Compare commits

...

2 Commits

Author SHA1 Message Date
Martin Hořeňovský
4b51d0dd3b
Remove rest of std::shared_ptr usage in Trackers
Closes #2088
2020-12-05 15:19:23 +01:00
Martin Hořeňovský
6350851f9a
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
2020-12-04 21:37:24 +01:00
3 changed files with 44 additions and 35 deletions

View File

@ -34,7 +34,7 @@ namespace Catch {
~GeneratorTracker();
static GeneratorTracker& acquire( TrackerContext& ctx, TestCaseTracking::NameAndLocation const& nameAndLocation ) {
std::shared_ptr<GeneratorTracker> tracker;
GeneratorTracker* tracker;
ITracker& currentTracker = ctx.currentTracker();
// Under specific circumstances, the generator we want
@ -48,18 +48,23 @@ namespace Catch {
// }
//
// without it, the code above creates 5 nested generators.
if (currentTracker.nameAndLocation() == nameAndLocation) {
auto thisTracker = currentTracker.parent().findChild(nameAndLocation);
assert(thisTracker);
assert(thisTracker->isGeneratorTracker());
tracker = std::static_pointer_cast<GeneratorTracker>(thisTracker);
} else if ( TestCaseTracking::ITrackerPtr childTracker = currentTracker.findChild( nameAndLocation ) ) {
if ( currentTracker.nameAndLocation() == nameAndLocation ) {
auto thisTracker =
currentTracker.parent().findChild( nameAndLocation );
assert( thisTracker );
assert( thisTracker->isGeneratorTracker() );
tracker = static_cast<GeneratorTracker*>( thisTracker );
} else if ( ITracker* childTracker =
currentTracker.findChild( nameAndLocation ) ) {
assert( childTracker );
assert( childTracker->isGeneratorTracker() );
tracker = std::static_pointer_cast<GeneratorTracker>( childTracker );
tracker = static_cast<GeneratorTracker*>( childTracker );
} else {
tracker = std::make_shared<GeneratorTracker>( nameAndLocation, ctx, &currentTracker );
currentTracker.addChild( tracker );
auto newTracker =
Catch::Detail::make_unique<GeneratorTracker>(
nameAndLocation, ctx, &currentTracker );
tracker = newTracker.get();
currentTracker.addChild( std::move(newTracker) );
}
if( !tracker->isComplete() ) {
@ -92,7 +97,7 @@ namespace Catch {
if ( std::find_if(
m_children.begin(),
m_children.end(),
[]( TestCaseTracking::ITrackerPtr tracker ) {
[]( TestCaseTracking::ITrackerPtr const& tracker ) {
return tracker->hasStarted();
} ) != m_children.end() ) {
return false;
@ -101,7 +106,7 @@ namespace Catch {
// No children have started. We need to check if they _can_
// start, and thus we should wait for them, or they cannot
// start (due to filters), and we shouldn't wait for them
auto* parent = m_parent;
ITracker* parent = m_parent;
// This is safe: there is always at least one section
// tracker in a test case tracking tree
while ( !parent->isSectionTracker() ) {
@ -111,7 +116,7 @@ namespace Catch {
"Missing root (test case) level section" );
auto const& parentSection =
static_cast<SectionTracker&>( *parent );
static_cast<SectionTracker const&>( *parent );
auto const& filters = parentSection.getFilters();
// No filters -> no restrictions on running sections
if ( filters.empty() ) {
@ -120,11 +125,11 @@ namespace Catch {
for ( auto const& child : m_children ) {
if ( child->isSectionTracker() &&
std::find( filters.begin(),
filters.end(),
static_cast<SectionTracker&>( *child )
.trimmedName() ) !=
filters.end() ) {
std::find(
filters.begin(),
filters.end(),
static_cast<SectionTracker const&>( *child )
.trimmedName() ) != filters.end() ) {
return true;
}
}

View File

@ -12,7 +12,6 @@
#include <algorithm>
#include <cassert>
#include <memory>
#if defined(__clang__)
# pragma clang diagnostic push
@ -30,11 +29,11 @@ namespace TestCaseTracking {
ITracker::~ITracker() = default;
void ITracker::addChild( ITrackerPtr const& child ) {
m_children.push_back( child );
void ITracker::addChild( ITrackerPtr&& child ) {
m_children.push_back( std::move(child) );
}
ITrackerPtr ITracker::findChild( NameAndLocation const& nameAndLocation ) {
ITracker* ITracker::findChild( NameAndLocation const& nameAndLocation ) {
auto it = std::find_if(
m_children.begin(),
m_children.end(),
@ -43,14 +42,17 @@ namespace TestCaseTracking {
nameAndLocation.location &&
tracker->nameAndLocation().name == nameAndLocation.name;
} );
return ( it != m_children.end() ) ? *it : nullptr;
return ( it != m_children.end() ) ? it->get() : nullptr;
}
ITracker& TrackerContext::startRun() {
using namespace std::string_literals;
m_rootTracker = std::make_shared<SectionTracker>( NameAndLocation( "{root}"s, CATCH_INTERNAL_LINEINFO ), *this, nullptr );
m_rootTracker = Catch::Detail::make_unique<SectionTracker>(
NameAndLocation( "{root}"s, CATCH_INTERNAL_LINEINFO ),
*this,
nullptr );
m_currentTracker = nullptr;
m_runState = Executing;
return *m_rootTracker;
@ -195,17 +197,19 @@ namespace TestCaseTracking {
bool SectionTracker::isSectionTracker() const { return true; }
SectionTracker& SectionTracker::acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation ) {
std::shared_ptr<SectionTracker> section;
SectionTracker* section;
ITracker& currentTracker = ctx.currentTracker();
if( ITrackerPtr childTracker = currentTracker.findChild( nameAndLocation ) ) {
if ( ITracker* childTracker =
currentTracker.findChild( nameAndLocation ) ) {
assert( childTracker );
assert( childTracker->isSectionTracker() );
section = std::static_pointer_cast<SectionTracker>( childTracker );
}
else {
section = std::make_shared<SectionTracker>( nameAndLocation, ctx, &currentTracker );
currentTracker.addChild( section );
section = static_cast<SectionTracker*>( childTracker );
} else {
auto newSection = Catch::Detail::make_unique<SectionTracker>(
nameAndLocation, ctx, &currentTracker );
section = newSection.get();
currentTracker.addChild( std::move( newSection ) );
}
if( !ctx.completedCycle() )
section->tryOpen();

View File

@ -10,10 +10,10 @@
#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <catch2/internal/catch_source_line_info.hpp>
#include <catch2/internal/catch_unique_ptr.hpp>
#include <string>
#include <vector>
#include <memory>
namespace Catch {
namespace TestCaseTracking {
@ -31,7 +31,7 @@ namespace TestCaseTracking {
class ITracker;
using ITrackerPtr = std::shared_ptr<ITracker>;
using ITrackerPtr = Catch::Detail::unique_ptr<ITracker>;
class ITracker {
NameAndLocation m_nameAndLocation;
@ -69,13 +69,13 @@ namespace TestCaseTracking {
virtual void markAsNeedingAnotherRun() = 0;
//! Register a nested ITracker
void addChild( ITrackerPtr const& child );
void addChild( ITrackerPtr&& child );
/**
* Returns ptr to specific child if register with this tracker.
*
* Returns nullptr if not found.
*/
ITrackerPtr findChild( NameAndLocation const& nameAndLocation );
ITracker* findChild( NameAndLocation const& nameAndLocation );
//! Have any children been added?
bool hasChildren() const {
return !m_children.empty();