c++ - Store weak pointer to self -


i work codebase partially implemented in love overly complex solutions simple problems (e.g. template classes 2 parameters ever instantiated 1 pair of types). 1 thing did create objects in smart pointer, , have object store weak pointer itself.

class myclass {     //...     boost::weak_ptr<myclass> m_self;     //... };  boost::shared_ptr<myclass> factory::factory::factory::createmyclass() {     boost::shared_ptr<myclass> obj(new myclass(...));     boost::weak_ptr<myclass> p(obj);     obj->storeselfpointer(p);     return obj; } 

the class proceeds use m_self locking , passing around resulting shared pointer.

for life of me, cannot fathom trying accomplish. there pattern or idea explain implementation? looks me pointless , i'd refactor away.

edit: should mention none of places use resulting smart pointer obtained locking m_self retain smart pointer.

a possible use of "design" use m_self.lock() generate shared pointers this.

if remove weak pointer member, reference count hold generated shared pointer this incorrect.

it achieves same std::enable_shared_from_this, interestingly enough, cppreference.com mentions design :

a common implementation enable_shared_from_this hold weak reference (such std::weak_ptr) this. constructors of std::shared_ptr detect presence of enable_shared_from_this base , assign newly created std::shared_ptr internally stored weak reference

and c++ standard, section § 20.8.2.4 10 , mention same possible implementation :

the shared_ptr constructors create unique pointers can detect presence of enable_shared_- from_this base , assign newly created shared_ptr __weak_this member


possible refactoring :

  • if using c++11, can remove std::weak_ptr member, , publicly inherits std::enable_shared_from_this<t>. should retrieve shared pointer calling shared_from_this().

  • if not using c++11 can use boost, use boost::enable_shared_from_this, see boost documentation. should retrieve shared pointer calling shared_from_this().

  • if not using c++11, , can't use boost, can bring proposed implementation of standard code base, short enough :

code : (copied § 20.8.2.4 - 11, remove leading underscores, , want rename it)

template<class t> class enable_shared_from_this {     private:      weak_ptr<t> __weak_this;     protected:      constexpr enable_shared_from_this() : __weak_this() { }      enable_shared_from_this(enable_shared_from_this const &) { }      enable_shared_from_this& operator=(enable_shared_from_this const &) { return *this; }      ~enable_shared_from_this() { }     public:      shared_ptr<t> shared_from_this() { return shared_ptr<t>(__weak_this); }      shared_ptr<t const> shared_from_this() const { return shared_ptr<t const>(__weak_this); } }; 

and use shared_from_this() make shared pointer. if copy code, note constructing shared pointers other means not work. shared pointers constructors need modified (as explain standard quote above).


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -