An open API service indexing awesome lists of open source software.

https://github.com/mateuszkubuszok/weakptrfactory

Minimalistic weak_ptr_factory inspired by Chromium's base::WeakPtrFactory.
https://github.com/mateuszkubuszok/weakptrfactory

Last synced: 7 months ago
JSON representation

Minimalistic weak_ptr_factory inspired by Chromium's base::WeakPtrFactory.

Awesome Lists containing this project

README

        

weak_ptr_factory
===============

[![Build Status](https://travis-ci.org/MateuszKubuszok/WeakPtrFactory.png)](https://travis-ci.org/MateuszKubuszok/WeakPtrFactory)

Minimalistic weak_ptr_factory class inspired by Chromium's base::WeakPtrFactory
(require C++11).

This small project was made as an exercise and for fun. Its practical usage is
at least debatable.

How to use
---------------

Just include `weak/weak_ptr_factory.hpp` or `weak/weak_ptr_factory.hpp` header.
Classes will be available as `weak::weak_ptr` and `weak::weak_ptr_factory`.

If you want to use boost's implementations instead of C++11 std ones use:

#define WEAK_PTR_TOOLS_USE_BOOST
#include "weak_ptr_factory.hpp"

Then it will be available under `weak::weak_ptr_factory`.

Example
---------------

using weak::weak_ptr;
using weak::weak_ptr_factory;

class Test {
weak_ptr_factory weak_ptr_factory_;

public:
explicit Test() :
weak_ptr_factory_(this)
{}

weak_ptr get_weak_ptr() const {
return weak_ptr_factory_.get_weak_ptr();
}

void invalidate_all_ptrs() {
weak_ptr_factory_.invalidate_all_ptrs();
}
};

Factory is initiated with `this` and will create weak_ptrs to its owner with
`get_weak_ptr()` method. On object's destruction all weak_ptrs will be
invalidated.

Problems
---------------

On multithreaded environment one must be careful - since we assume that owner of
a factory manages its lifetime on its own. So it should be used only when we
have certainty that object cannot commit suicide between checking weak_ptr and
calling the object.