void User::AddFriendGuarded(User& newFriend) { ScopeGuard invariantGuard = MakeObjGuard( *this, &User::CheckIfValid, __FUNCTION__, __LINE__ ); (void)invariantGuard; ScopeGuard guard1 = MakeObjGuard( *this, &User::DoSomething ); (void)guard1; friends_.push_back(&newFriend); Loki::ScopeGuard guard = Loki::MakeObjGuard(friends_, &UserCont::pop_back); fCount++; Loki::ScopeGuard guardRef = Loki::MakeGuard(Decrement, Loki::ByRef(fCount)); pDB_->AddFriend(GetName(), newFriend.GetName()); guard.Dismiss(); guardRef.Dismiss(); }
/** @brief Delete the port matching the given name. @return true on success, false on failure. */ bool PortMonitor::DeletePort(const wchar_t* portName) { std::vector<std::wstring>::iterator it; for (it = this->portNames.begin(); it != this->portNames.end(); ++it) { if (wcscmp(it->c_str(), portName) == 0) { if (!this->RemovePortFromRegistry(portName)) return false; Loki::ScopeGuard registryGuard = Loki::MakeGuard(&PortMonitor::AddPortToRegistry, *this, portName); DeletePortIpcRequest request(portName); PipeClient pipeClient; if (!pipeClient.Send(&request)) return false; DeletePortIpcResponse* response = request.GetExactResponse(); if (response->GetReturnCode()) { registryGuard.Dismiss(); this->portNames.erase(it); return true; } else { SetLastError(response->GetErrorCode()); return false; } } } return false; }