/** @brief Create a new port and associate it with the monitor. @param portName The name to give the new port. @return true on success, false on failure. */ bool PortMonitor::AddPort(const wchar_t* portName) { if (!this->AddPortToRegistry(portName)) return false; Loki::ScopeGuard registryGuard = Loki::MakeGuard( &PortMonitor::RemovePortFromRegistry, *this, portName ); AddPortIpcRequest request(portName); PipeClient pipeClient; if (!pipeClient.Send(&request)) return false; AddPortIpcResponse* response = request.GetExactResponse(); if (response->GetReturnCode()) { registryGuard.Dismiss(); this->portNames.push_back(std::wstring(portName)); return true; } else { SetLastError(response->GetErrorCode()); return false; } }
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; }