Skip to content

the zero-overhead smart pointer that's never invalid

Notifications You must be signed in to change notification settings

FatihBAKIR/track_ptr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

track_ptr

the zero-overhead smart pointer that's never invalid

std::vector<Widget> widgets(1);
widgets.front().val = "hello";

track_ptr<Widget> w_p = get_ptr(widgets.front());
Widget* danger = &widgets.front();

std::cout << w_p->val << '\n'; // prints "hello"
std::cout << danger->val << '\n'; // prints "hello"

widgets.resize(1000);
widgets.front().val += " world";

std::cout << w_p->val << '\n'; // prints "hello world"
std::cout << danger->val << '\n'; // probably dies

never invalid:

  • track_ptrs are updated when the object they point to is moved from
  • If a tracked object is destructed before any pointer to it, those pointers point automatically to nullptr, not to a segmentation fault

zero overhead:

  • track_ptrs are linked together, so no matter how many pointers point to the same object, there is no dynamic memory allocation
  • Objects of types that can be tracked store 3 pointers

Usage

To allow the objects of a type T to be tracked, make it inherit from tracked:

class Widget : public tracked
{
  ...
}

tracked is a non-polymorphic class that notifies pointers upon moving or destruction

warning: make sure you call its assignment operators if you are implementing your own assignment operators like:

Widget& Widget::operator=(Widget&& rhs) //ditto with copy assingment
{
  ...
  tracked::operator=(std::move(rhs));
}

About

the zero-overhead smart pointer that's never invalid

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published