migrated ITracker to std::shared_ptr

This commit is contained in:
Phil Nash 2017-04-25 20:57:43 +01:00
parent 44cccde8b9
commit 0807a6910f

View File

@ -32,7 +32,11 @@ namespace TestCaseTracking {
{} {}
}; };
struct ITracker : SharedImpl<> { struct ITracker;
using ITrackerPtr = std::shared_ptr<ITracker>;
struct ITracker {
virtual ~ITracker(); virtual ~ITracker();
// static queries // static queries
@ -51,8 +55,8 @@ namespace TestCaseTracking {
virtual void fail() = 0; virtual void fail() = 0;
virtual void markAsNeedingAnotherRun() = 0; virtual void markAsNeedingAnotherRun() = 0;
virtual void addChild( Ptr<ITracker> const& child ) = 0; virtual void addChild( ITrackerPtr const& child ) = 0;
virtual ITracker* findChild( NameAndLocation const& nameAndLocation ) = 0; virtual ITrackerPtr findChild( NameAndLocation const& nameAndLocation ) = 0;
virtual void openChild() = 0; virtual void openChild() = 0;
// Debug/ checking // Debug/ checking
@ -68,7 +72,7 @@ namespace TestCaseTracking {
CompletedCycle CompletedCycle
}; };
Ptr<ITracker> m_rootTracker; ITrackerPtr m_rootTracker;
ITracker* m_currentTracker = nullptr; ITracker* m_currentTracker = nullptr;
RunState m_runState = NotStarted; RunState m_runState = NotStarted;
@ -120,13 +124,13 @@ namespace TestCaseTracking {
NameAndLocation m_nameAndLocation; NameAndLocation m_nameAndLocation;
public: public:
TrackerHasName( NameAndLocation const& nameAndLocation ) : m_nameAndLocation( nameAndLocation ) {} TrackerHasName( NameAndLocation const& nameAndLocation ) : m_nameAndLocation( nameAndLocation ) {}
bool operator ()( Ptr<ITracker> const& tracker ) { bool operator ()( ITrackerPtr const& tracker ) {
return return
tracker->nameAndLocation().name == m_nameAndLocation.name && tracker->nameAndLocation().name == m_nameAndLocation.name &&
tracker->nameAndLocation().location == m_nameAndLocation.location; tracker->nameAndLocation().location == m_nameAndLocation.location;
} }
}; };
typedef std::vector<Ptr<ITracker> > Children; typedef std::vector<ITrackerPtr> Children;
NameAndLocation m_nameAndLocation; NameAndLocation m_nameAndLocation;
TrackerContext& m_ctx; TrackerContext& m_ctx;
ITracker* m_parent; ITracker* m_parent;
@ -157,14 +161,14 @@ namespace TestCaseTracking {
} }
virtual void addChild( Ptr<ITracker> const& child ) override { virtual void addChild( ITrackerPtr const& child ) override {
m_children.push_back( child ); m_children.push_back( child );
} }
virtual ITracker* findChild( NameAndLocation const& nameAndLocation ) override { virtual ITrackerPtr findChild( NameAndLocation const& nameAndLocation ) override {
Children::const_iterator it = std::find_if( m_children.begin(), m_children.end(), TrackerHasName( nameAndLocation ) ); Children::const_iterator it = std::find_if( m_children.begin(), m_children.end(), TrackerHasName( nameAndLocation ) );
return( it != m_children.end() ) return( it != m_children.end() )
? it->get() ? *it
: nullptr; : nullptr;
} }
virtual ITracker& parent() override { virtual ITracker& parent() override {
@ -258,16 +262,16 @@ namespace TestCaseTracking {
virtual bool isSectionTracker() const override { return true; } virtual bool isSectionTracker() const override { return true; }
static SectionTracker& acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation ) { static SectionTracker& acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation ) {
SectionTracker* section = nullptr; std::shared_ptr<SectionTracker> section;
ITracker& currentTracker = ctx.currentTracker(); ITracker& currentTracker = ctx.currentTracker();
if( ITracker* childTracker = currentTracker.findChild( nameAndLocation ) ) { if( ITrackerPtr childTracker = currentTracker.findChild( nameAndLocation ) ) {
assert( childTracker ); assert( childTracker );
assert( childTracker->isSectionTracker() ); assert( childTracker->isSectionTracker() );
section = static_cast<SectionTracker*>( childTracker ); section = std::dynamic_pointer_cast<SectionTracker>( childTracker );
} }
else { else {
section = new SectionTracker( nameAndLocation, ctx, &currentTracker ); section = std::make_shared<SectionTracker>( nameAndLocation, ctx, &currentTracker );
currentTracker.addChild( section ); currentTracker.addChild( section );
} }
if( !ctx.completedCycle() ) if( !ctx.completedCycle() )
@ -306,16 +310,16 @@ namespace TestCaseTracking {
virtual bool isIndexTracker() const override { return true; } virtual bool isIndexTracker() const override { return true; }
static IndexTracker& acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation, int size ) { static IndexTracker& acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation, int size ) {
IndexTracker* tracker = nullptr; std::shared_ptr<IndexTracker> tracker;
ITracker& currentTracker = ctx.currentTracker(); ITracker& currentTracker = ctx.currentTracker();
if( ITracker* childTracker = currentTracker.findChild( nameAndLocation ) ) { if( ITrackerPtr childTracker = currentTracker.findChild( nameAndLocation ) ) {
assert( childTracker ); assert( childTracker );
assert( childTracker->isIndexTracker() ); assert( childTracker->isIndexTracker() );
tracker = static_cast<IndexTracker*>( childTracker ); tracker = std::dynamic_pointer_cast<IndexTracker>( childTracker );
} }
else { else {
tracker = new IndexTracker( nameAndLocation, ctx, &currentTracker, size ); tracker = std::make_shared<IndexTracker>( nameAndLocation, ctx, &currentTracker, size );
currentTracker.addChild( tracker ); currentTracker.addChild( tracker );
} }
@ -343,7 +347,7 @@ namespace TestCaseTracking {
}; };
inline ITracker& TrackerContext::startRun() { inline ITracker& TrackerContext::startRun() {
m_rootTracker = new SectionTracker( NameAndLocation( "{root}", CATCH_INTERNAL_LINEINFO ), *this, nullptr ); m_rootTracker = std::make_shared<SectionTracker>( NameAndLocation( "{root}", CATCH_INTERNAL_LINEINFO ), *this, nullptr );
m_currentTracker = nullptr; m_currentTracker = nullptr;
m_runState = Executing; m_runState = Executing;
return *m_rootTracker; return *m_rootTracker;