#include "MxlInstanceCache.hpp" #if MXL_MULTIVIEWER_ENABLE_MXL_SDK #include #include #include MxlInstanceCache& MxlInstanceCache::instance() { static MxlInstanceCache cache; return cache; } MxlInstanceHandle* MxlInstanceCache::acquire( const std::string& domain, bool verbose) { for (auto& handle : m_handles) { if (handle.domain == domain) { ++handle.refCount; return &handle; } } mxlInstance inst = mxlCreateInstance(domain.c_str(), nullptr); if (!inst) { return nullptr; } if (verbose) { std::cerr << "MxlInstanceCache: created instance for domain: " << domain << std::endl; } MxlInstanceHandle handle; handle.instance = inst; handle.domain = domain; handle.refCount = 1; m_handles.push_back(handle); return &m_handles.back(); } void MxlInstanceCache::release(MxlInstanceHandle* handle) { if (!handle) { return; } --handle->refCount; if (handle->refCount <= 0) { mxlDestroyInstance(handle->instance); handle->instance = nullptr; m_handles.erase( std::remove_if( m_handles.begin(), m_handles.end(), [](const MxlInstanceHandle& h) { return h.instance == nullptr; }), m_handles.end()); } } #else MxlInstanceCache& MxlInstanceCache::instance() { static MxlInstanceCache cache; return cache; } MxlInstanceHandle* MxlInstanceCache::acquire(const std::string&, bool) { return nullptr; } void MxlInstanceCache::release(MxlInstanceHandle*) { } #endif