1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/************************************************************************
*
* Copyright (C) 2014-2025 IRCAD France
* Copyright (C) 2014-2018 IHU Strasbourg
*
* This file is part of Sight.
*
* Sight is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sight. If not, see <https://www.gnu.org/licenses/>.
*
***********************************************************************/
#include "service/has_services.hpp"
#include "service/base.hpp"
#include <core/runtime/runtime.hpp>
#include <service/registry.hpp>
namespace sight::service
{
//------------------------------------------------------------------------------
has_services::has_services() noexcept =
default;
//------------------------------------------------------------------------------
has_services::~has_services() noexcept
{
SIGHT_ASSERT(
"Some sub-services were not unregistered, something is probably wrong. "
"Please use unregisterService() or unregisterServices() before destroying the sub-services owner.",
m_sub_services.empty()
);
}
//------------------------------------------------------------------------------
service::base::csptr has_services::get_registered_service(const core::id::type& _id) const
{
service::base::sptr srv;
for(const auto& w_service : m_sub_services)
{
const service::base::sptr& service = w_service.lock();
if(service && (service->get_id() == _id))
{
srv = service;
break;
}
}
return srv;
}
//------------------------------------------------------------------------------
void has_services::unregister_service(const core::id::type& _id)
{
for(auto it_srv = m_sub_services.begin() ; it_srv != m_sub_services.end() ; )
{
const service::base::sptr& service = it_srv->lock();
if(service && (service->get_id() == _id))
{
service->stop().wait();
service::unregister_service(service);
it_srv = m_sub_services.erase(it_srv);
}
else
{
it_srv++;
}
}
}
//------------------------------------------------------------------------------
void has_services::unregister_service(const base::sptr& _service)
{
auto iter = std::find_if(
m_sub_services.begin(),
m_sub_services.end(),
[ = ](const service::base::wptr& _adaptor)
{
return _adaptor.lock() == _service;
});
SIGHT_ASSERT("service '" + _service->get_id() + "' is not registered", iter != m_sub_services.end());
m_sub_services.erase(iter);
if(!_service->stopped())
{
_service->stop().wait();
}
service::unregister_service(_service);
}
//------------------------------------------------------------------------------
service::base::sptr has_services::register_service(const std::string& _impl_type, const std::string& _id)
{
auto srv = service::add(_impl_type, _id);
m_sub_services.push_back(srv);
return srv;
}
//------------------------------------------------------------------------------
void has_services::unregister_services(const std::string& _classname)
{
const std::string classname = core::runtime::filter_id(_classname);
for(auto it_srv = m_sub_services.begin() ; it_srv != m_sub_services.end() ; )
{
const service::base::sptr& srv = it_srv->lock();
if(srv && (classname.empty() || srv->get_classname() == classname))
{
srv->stop().wait();
service::unregister_service(srv);
it_srv = m_sub_services.erase(it_srv);
}
else
{
it_srv++;
}
}
}
//------------------------------------------------------------------------------
} // namespace sight::service
|