The Sparta Modeling Framework
Loading...
Searching...
No Matches
sparta::utils::LifeTracker< TrackedObjectT > Class Template Reference

A class that will track the lifetime of the object it points to. More...

#include <LifeTracker.hpp>

Inheritance diagram for sparta::utils::LifeTracker< TrackedObjectT >:
Collaboration diagram for sparta::utils::LifeTracker< TrackedObjectT >:

Public Member Functions

 LifeTracker (TrackedObjectT *obj)
 

Public Attributes

TrackedObjectT * tracked_object
 

Detailed Description

template<class TrackedObjectT>
class sparta::utils::LifeTracker< TrackedObjectT >

A class that will track the lifetime of the object it points to.

This class, which derives from std::shared_ptr, allows a developer to "wrap" a non-shared_ptr object with a lifetime time bomb. Receivers of this class can create a std::weak_ptr to it and check for expiration. See example below and in the Utils test.

class MyVolatileTrackedClass
{
public:
using MyVolatileTrackedClassTracker = sparta::utils::LifeTracker<MyVolatileTrackedClass>;
const MyVolatileTrackedClassTracker & getLifeTracker() const {
return life_tracker_;
}
const uint32_t value = 10;
private:
// This object MUST live inside the class being tracked.
MyVolatileTrackedClassTracker life_tracker_{this};
};
int main()
{
std::weak_ptr<MyVolatileTrackedClass::MyVolatileTrackedClassTracker> wptr;
{
MyVolatileTrackedClass my_object;
wptr = my_object.getLifeTracker();
if(false == wptr.expired()) {
std::cout << "The class is still valid: " <<
wptr.lock()->tracked_object->value << std::endl;
}
}
if(wptr.expired()) {
std::cout << "The class has expired" << std::endl;
}
return 0;
}
A class that will track the lifetime of the object it points to.

Definition at line 67 of file LifeTracker.hpp.

Constructor & Destructor Documentation

◆ LifeTracker()

template<class TrackedObjectT >
sparta::utils::LifeTracker< TrackedObjectT >::LifeTracker ( TrackedObjectT *  obj)
inline

Definition at line 70 of file LifeTracker.hpp.

Member Data Documentation

◆ tracked_object

template<class TrackedObjectT >
TrackedObjectT* sparta::utils::LifeTracker< TrackedObjectT >::tracked_object

Definition at line 75 of file LifeTracker.hpp.


The documentation for this class was generated from the following file: