Skip to content

any_tidy_ptr

Tony Van Eerd edited this page Dec 4, 2019 · 3 revisions

Source: any_tidy_ptr.h

For all your self-tidying pointer needs.

any_tidy_ptr<T> is basically unique_ptr<T, std::function<void(T*)>>.

Why?

Sometimes you want to manage some memory and clean it up when you are done, but you don't really care where it came from - that's SEP - Someone Else's Problem.

Motivational example: an Image class that accepts pixels allocated from various sources.

any_tidy_ptr<Foo> p0; // empty
any_tidy_ptr<Foo> p1(new Foo); // just like unique_ptr<Foo>
Foo local;
any_tidy_ptr<Foo> p2(&local, nullptr); // no deleter
any_tidy_ptr<Foo> p3(myFooAlloc(), myFooDelete);  // custom deleter
std::shared_ptr<Foo> sp = std::make_shared<Foo>();
any_tidy_ptr<Foo> p4 = sp; // copies sp, deleter will sp.reset()
std::unique_ptr<Foo> up = std::make_unique<Foo>();
any_tidy_ptr<Foo> p5 = std::move(up);
Clone this wiki locally