C++ (Qt)template<class T>struct CItemRef {...// data std::shared_ptr<T> m_shared; std::weak_ptr<T> m_weak; T * m_raw; };typedef CItemRef<TreeItem> TreeItemRef;
C++ (Qt)template<class T>struct CItemRef { CItemRef( std::shared_ptr<T> sh ) : m_weak(sh), m_raw(sh.get()) { } T * data( void ) const { auto p = m_shared.get(); // locked? if (p) return p; if (m_raw) assert(!m_weak.expired()); return m_raw; } ...// data std::shared_ptr<T> m_shared; std::weak_ptr<T> m_weak; T * m_raw; };
C++ (Qt)template<class T> m_shared.reset();
C++ (Qt)template<class T> auto p = m_shared.get(); // locked? if (p) return p; }
C++ (Qt)bool Lock( void ){ if (!m_shared.get()) m_shared = m_weak.lock(); return m_shared.get();}
C++ (Qt)struct CIem { ... private: TreeItemRef m_parent; std::vector<std::shared_ptr<CItem>> m_children; std::shared_ptr<CItem> m_self; // not null for root};
C++ (Qt)TreeItemRef item = treeView->Root().childAt(0); item.SetName("test");if (CheckSomething(item)) DeleteItem(item); if (!item.IsNull()) item.SetName("done"); if (item.Lock()) {...}
C++ (Qt)if (auto item_data = item.lock()){ item_data->doSomething(); ...}