void EventHandler::add(std::weak_ptr<EventHandled> handler) { auto fd_ptr = handler.lock(); for (auto fd : fd_ptr->pollfds()) { struct epoll_event event{}; event.events = fd.events; event.data.fd = fd.fd; cb_map[event.data.fd] = [this, handler]() { if (auto tmp = handler.lock()) tmp->handle(*this); }; if (epoll_ctl(epfd, EPOLL_CTL_ADD, event.data.fd, &event) < 0) throw std::runtime_error("Failed to add epoll fd to list.\n"); } }
/** Returns the singleton client or server lobby protocol. */ template<class T> static std::shared_ptr<T> get() { if (std::shared_ptr<LobbyProtocol> lp = m_lobby.lock()) { std::shared_ptr<T> new_type = std::dynamic_pointer_cast<T>(lp); if (new_type) return new_type; } return nullptr; } // get
void Connection::handleWriteTimeout(std::weak_ptr<Connection> weak, const boost::system::error_code& error) { if(error == boost::asio::error::operation_aborted || weak.expired()) return; if(std::shared_ptr<Connection> connection = weak.lock()) { connection->onWriteTimeout(); } }
std::shared_ptr<vector<string>> ConstStrBlobPtr::check( std::size_t pos, const string& msg) const { // test to see whether the vector pointed by the weak_ptr is still around auto ptr = dataptr.lock(); if(!ptr) throw std::runtime_error("unbound ConstStrBlobPtr"); if(pos >= ptr->size()) throw std::out_of_range(msg); return ptr; }
void visitModuleFile(StringRef Filename, serialization::ModuleKind Kind) override { auto *File = CI.getFileManager().getFile(Filename); assert(File && "missing file for loaded module?"); // Only rewrite each module file once. if (!Rewritten.insert(File).second) return; serialization::ModuleFile *MF = CI.getModuleManager()->getModuleManager().lookup(File); assert(File && "missing module file for loaded module?"); // Not interested in PCH / preambles. if (!MF->isModule()) return; auto OS = Out.lock(); assert(OS && "loaded module file after finishing rewrite action?"); (*OS) << "#pragma clang module build "; if (isValidIdentifier(MF->ModuleName)) (*OS) << MF->ModuleName; else { (*OS) << '"'; OS->write_escaped(MF->ModuleName); (*OS) << '"'; } (*OS) << '\n'; // Rewrite the contents of the module in a separate compiler instance. CompilerInstance Instance(CI.getPCHContainerOperations(), &CI.getPreprocessor().getPCMCache()); Instance.setInvocation( std::make_shared<CompilerInvocation>(CI.getInvocation())); Instance.createDiagnostics( new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()), /*ShouldOwnClient=*/true); Instance.getFrontendOpts().DisableFree = false; Instance.getFrontendOpts().Inputs.clear(); Instance.getFrontendOpts().Inputs.emplace_back( Filename, InputKind(InputKind::Unknown, InputKind::Precompiled)); Instance.getFrontendOpts().ModuleFiles.clear(); Instance.getFrontendOpts().ModuleMapFiles.clear(); // Don't recursively rewrite imports. We handle them all at the top level. Instance.getPreprocessorOutputOpts().RewriteImports = false; llvm::CrashRecoveryContext().RunSafelyOnThread([&]() { RewriteIncludesAction Action; Action.OutputStream = OS; Instance.ExecuteAction(Action); }); (*OS) << "#pragma clang module endbuild /*" << MF->ModuleName << "*/\n"; }
//------------------------------------------// // Texture2DAssetLoader::StartupLoader //------------------------------------------// bool Texture2DAssetLoader::StartupLoader(std::weak_ptr<class Renderer> renderer) { if (renderer.lock() == nullptr) { //Fail something return false; } mWeakRenderer = renderer; return true; }
std::shared_ptr<std::vector<std::string>> StrBlobPtr::check( std::size_t i, const std::string &msg) const { auto ret = wptr.lock(); if (!ret) { throw std::runtime_error("unbound StrBlobPtr"); } if (i >= ret->size()) { throw std::out_of_range(msg); } return ret; }
static void av_qsv_log_callback(void *ptr, int level, const char *fmt, va_list vl) { if (auto pQSVLog = g_pQSVLog.lock()) { const int qsv_log_level = log_level_av2rgy(level); if (qsv_log_level >= pQSVLog->getLogLevel() && pQSVLog->logFileAvail()) { char mes[4096]; av_log_format_line(ptr, level, fmt, vl, mes, sizeof(mes), &print_prefix); pQSVLog->write_log(qsv_log_level, char_to_tstring(mes, CP_UTF8).c_str(), true); } } av_log_default_callback(ptr, level, fmt, vl); }
void initialize(std::weak_ptr<scoped_signal_mask>& sigmask_wptr) { static std::mutex mutex_; std::lock_guard<std::mutex> lk(mutex_); sigmask_sptr_ = sigmask_wptr.lock(); if (!sigmask_sptr_) { signal_set sigset{ SignalNumber... }; sigmask_sptr_ = std::make_shared<scoped_signal_mask>(sigset); sigmask_wptr = sigmask_sptr_; } }
/* Notes * * std::weak_ptr * expired - checks whether the referenced object was already deleted lock - creates a shared_ptr that manages the referenced object * std::unique_ptr * ,using this instead of auto_ptr considering: 1 - operators supported(==, !=, < ...),so they can be used/stored in STL which uses/relies on a lot of these operations 2 - array supported, can point to an array */ void f() { if ( !wp.expired()) { auto spt = wp.lock(); std::cout << *spt << "\n"; std::cout << wp.use_count() << "\n"; } else { std::cout << "wp is expired\n"; } }
static std::shared_ptr<UDTSession> getUDTSession(void) { //TODO needs a lock std::shared_ptr<UDTSession> sess = UDTWeakSession.lock(); if (not sess) { sess.reset(new UDTSession()); UDTWeakSession = sess; } return sess; }
void log_gl_error(const gl_error& error) { std::shared_ptr<context> current_locked_context = current_context.lock(); if (current_locked_context && current_locked_context->state().error() == GL_NO_ERROR) { current_locked_context->state().error() = error.error_code(); } log_message(GL_DEBUG_SOURCE_API_KHR, GL_DEBUG_TYPE_ERROR_KHR, error.error_code(), GL_DEBUG_SEVERITY_HIGH_KHR, format("%s: %s", error.error_code_description().c_str(), error.error_msg().c_str())); }
void GenericAudioMixer::setSourceGain(std::weak_ptr<ISource> source, float gain) { auto s = source.lock(); if(s) { auto hash = std::hash<std::shared_ptr<ISource>>()(s); m_inGain[hash] = std::max(0.f, std::min(1.f, gain)); } }
void Renderable2DBox::Render(std::weak_ptr<const RendererBase> renderer) const { std::shared_ptr<const RendererBase> rendererShared = renderer.lock(); if (rendererShared != nullptr) { rendererShared->DrawColoredLine(m_Points[0], m_Points[2], m_Color); rendererShared->DrawColoredLine(m_Points[2], m_Points[3], m_Color); rendererShared->DrawColoredLine(m_Points[3], m_Points[1], m_Color); rendererShared->DrawColoredLine(m_Points[1], m_Points[0], m_Color); } }
void memory_pool::return_to_pool(const std::weak_ptr<memory_pool> &self_weak, std::uint8_t *ptr) { std::shared_ptr<memory_pool> self = self_weak.lock(); if (self) self->return_to_pool(ptr); else { log_debug("dropping memory because the pool has been freed"); delete[] ptr; } }
void ConstraintComponent::setAssociatedNode(std::weak_ptr<kitsune::scenegraph::Node> AssociatedNode) { auto associated = AssociatedNode.lock(); if (!associated) return; associatedNodeComponentAddedListener = associated->addComponentAddedEvent(std::bind(&ConstraintComponent::onNodeAddComponent, this, std::placeholders::_1)); associatedNodeComponentRemovedListener = associated->addComponentRemovedEvent(std::bind(&ConstraintComponent::onNodeRemoveComponent, this, std::placeholders::_1)); createConstraint(); }
std::shared_ptr<mbgl::util::RunLoop> NodeRunLoop() { static std::weak_ptr<mbgl::util::RunLoop> nodeRunLoop; auto loop = nodeRunLoop.lock(); if (!loop) { loop = std::make_shared<mbgl::util::RunLoop>(); nodeRunLoop = loop; } return std::move(loop); }
int GetPlayerList(const Data& data, std::weak_ptr<CPlayer> player) { if (!SendCheck(player)) { return -1; } PBS2CGetPlayerListRes res; res.set_getplayerlistresult(0); PlayerManager::GetInstance().PlayerList2PB(res); player.lock()->GetSocket().lock()->SendBuff((int)TypeS2CGetPlayerListRes, res); return 0; }
std::shared_ptr<ImGui_Wrapper> ImGui_Wrapper::instance(PandaDocument* doc) { static std::weak_ptr<ImGui_Wrapper> ptr; if (ptr.expired()) { auto wrapper = std::shared_ptr<ImGui_Wrapper>(new ImGui_Wrapper(doc)); ptr = wrapper; return wrapper; } else return ptr.lock(); }
void ServicePort::onOpen(std::weak_ptr<ServicePort> weakService, uint16_t port) { LOGt("ServicePort::onOpen()"); if(weakService.expired()) return; if(ServicePort_ptr service = weakService.lock()) { service->open(port); } }
std::experimental::optional<T> get(std::weak_ptr<handle<T>> handle_wp) { auto hid_sp = handle_wp.lock(); if (!hid_sp) { return std::experimental::optional<T>{}; } handle<T>* hid = hid_sp.get(); if(handle_map[hid->index] != hid_sp){ return std::experimental::optional<T>{}; } return std::experimental::make_optional(container[hid->index]); }
bool visot_ff_weak(std::weak_ptr<ff> pWeakFF) { if (pWeakFF.expired() == false) { std::shared_ptr<ff> pFF = pWeakFF.lock(); if (pFF) { return true; } } return false; }
std::shared_ptr<std::vector<std::string>> check(size_type index, string const& message) const { auto blob_ptr = blob_.lock(); if (!blob_ptr) throw std::runtime_error("unbounded shared_ptr"); if (index >= blob_ptr->size()) throw std::out_of_range(message); return blob_ptr; }
std::shared_ptr<ScriptEngine> ScriptEngine::instance() { static std::weak_ptr<ScriptEngine> ptr; if (ptr.expired()) { auto engine = std::shared_ptr<ScriptEngine>(new ScriptEngine()); ptr = engine; return engine; } else return ptr.lock(); }
Language::Ptr Language::Create() { //use slight caching to prevent heavy parsing static std::weak_ptr<Language> instance; if (Language::Ptr res = instance.lock()) { return res; } const Language::Ptr res = MakePtr<LanguageInResources>(); instance = res; return res; }
nCursesProgressWindow::nCursesProgressWindow(std::weak_ptr<Window> parentWnd){ LOG("Progress window constructor : ", this); if(auto spTmp = parentWnd.lock()){ parentWindow = parentWnd; posx = spTmp->getRows()-1; posy = spTmp->getColumns() - width; file_size = dynamic_cast<nCursesDisplayWindow*>(&*spTmp)->file_size; nCursesWhnd = ::newwin(height, width, posx, posy); ::box(nCursesWhnd, 0, 0); }else throw miniReader::Exception::miniRuntimeException( __FILE__, ":", __LINE__, "Error creating object", this); }
static std::shared_ptr<RawCtx> GetResource(const char * resourceName) { rh_rawpak_ctx _ctx = NULL; if( rh_rawpak_open_ctx(handle,resourceName, &_ctx) != 0) throw LookupException( resourceName ); std::shared_ptr<RawCtx> ctx = std::shared_ptr<RawCtx>(wthis.lock(), _ctx); return ctx; }
void SHContainer::insert(std::weak_ptr<lif::Collider> obj) { if (obj.expired()) return; const auto cld = obj.lock(); if (!cld->isActive()) return; const auto ids = _getIdFor(*cld); for (auto id : ids) { buckets[id].emplace_back(obj); } all.emplace_back(obj); }
int LoginCheck(const Data& data, std::weak_ptr<CPlayer> player) { if(player.expired()) { return -1; } std::shared_ptr<CPlayer> playerPtr = player.lock(); if (playerPtr->GetSocket().expired()) { return -1; } PBC2SLoginCheckReq req; req.ParseFromArray(data.buffer, data.bufferLength); PBS2CLoginCheckRes res; if (DBServer::GetInstance().LoginCheck(req.username().c_str(), req.password().c_str(), res) != 0) { return -1; } if (res.logincheckresult() > 0) { player.lock()->GetSocket().lock()->SetLoginID(res.logincheckresult()); DBPlayer playerData; if (DBServer::GetInstance().GetPlayerData(res.logincheckresult(), playerData) == 0) { std::weak_ptr<CTCPClientSocket> socket = player.lock()->GetSocket(); std::weak_ptr<CPlayer> newPlayer = socket.lock()->GetPlayer(); if (!newPlayer.expired()) { DBPlayer oldPlayerData; player.lock()->Player2DB(oldPlayerData); DBServer::GetInstance().SavePlayerData(oldPlayerData); socket.lock()->SetPlayer(std::weak_ptr<CPlayer>()); PlayerManager::GetInstance().RemovePlayer(player.lock()->GetPlayerID()); } newPlayer = PlayerManager::GetInstance().CreatePlayer(playerData.playerid(), socket); if (newPlayer.expired()) { return -1; } newPlayer.lock()->DB2Player(playerData); newPlayer.lock()->SetSocket(socket); socket.lock()->SetPlayer(newPlayer); } } else { player.lock()->GetSocket().lock()->SetPlayer(std::weak_ptr<CPlayer>()); } player.lock()->GetSocket().lock()->SendBuff((int)TypeS2CLoginCheckRes, res); return 0; }
void MAVConnTCPServer::client_closed(std::weak_ptr<MAVConnTCPClient> weak_instp) { if (auto instp = weak_instp.lock()) { bool locked = mutex.try_lock(); logInform(PFXd "Client connection closed, id: %p, address: %s", conn_id, instp.get(), to_string_ss(instp->server_ep).c_str()); client_list.remove(instp); if (locked) mutex.unlock(); } }