C++ (Qt) ItemUniquePointer take(ItemObserverPointer item) { ItemUniquePointer result; const auto pred = [item](const ItemUniquePointer &p) { return item.get() == p.get(); }; const auto it = std::find_if(m_children.begin(), m_children.end(), pred); if (it != m_children.end()) { std::swap(result, *it); result->m_parent = nullptr; m_children.erase(it); } return result; } void DeleteItem( ItemObserverPointer & item ){ (void)item->parent()->take(item);}
C++ (Qt)const auto pred = [item](const ItemUniquePointer &p) { return item.get() == p.get(); };const auto it = std::find_if(m_children.begin(), m_children.end(), pred);
C++ (Qt)void DeleteItem( ItemObserverPointer & item ){ (void)item->parent()->take(item);}
C++ (Qt)qDeleteAll(treeWidget->selectedItems());
C++ (Qt)Engine * Car::ReplaceEngine( Engine * engine ){ QMutexLocker lock(&mMutex); Q_ASSERT(engine && engine->GetOwner() == 0); Engine * old = mEngine.take(); old->SetOwner(0); mEngine.reset(engine); engine->SetOwner(this); return old;}...void Monitor::SetEngine( Engine * engine ){ QMutexLocker lock(&Car::mMutex); if (engine == mEngine) return; if (mEngine) mEngine->RemMonitor(this); mEngine = engine; if (mEngine) mEngine->AddMonitor(this);}
C++ (Qt)SingleEngine Car::replaceEngine(SingleEngine engine){ using std::swap; swap(m_engine, engine); return engine;}...void Monitor::setEngine(const WeakEngine& engine){ m_engine = engine; }
C++ (Qt) using std::swap;
C++ (Qt)ItemObserverPointer item1 = ...;ItemObserverPointer item2 = ...;DeleteItem(item1); // это удалит item1 и item2DeleteItem(item2); // а это вылет