/usr/include/mia-2.2/mia/core/handler.cxx is in libmia-2.2-dev 2.2.7-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | /* -*- mia-c++ -*-
*
* This file is part of MIA - a toolbox for medical image analysis
* Copyright (c) Leipzig, Madrid 1999-2015 Gert Wollny
*
* MIA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MIA; if not, see <http://www.gnu.org/licenses/>.
*
*/
#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <stdexcept>
#include <sstream>
#include <iostream>
#include <iterator>
#include <climits>
#include <boost/regex.hpp>
#include <boost/filesystem/operations.hpp>
#include <mia/core/module.hh>
#include <mia/core/plugin_base.hh>
#include <mia/core/msgstream.hh>
#include <config.h>
NS_MIA_BEGIN
namespace bfs = ::boost::filesystem;
EXPORT_CORE const std::string get_plugin_root();
template <typename I>
TPluginHandler<I>::TPluginHandler():
CPluginHandlerBase(I::PlugData::data_descr, I::PlugType::type_descr)
{
TRACE_FUNCTION;
}
template <typename I>
void TPluginHandler<I>::initialise(const CPluginSearchpath& searchpath)
{
TRACE_FUNCTION;
m_modules = searchpath.find_modules(I::get_data_path_part(), I::get_type_path_part());
// now try to load the interfaces and put them in the map
for (auto i = m_modules.begin(); i != m_modules.end(); ++i) {
try {
CPluginBase *pp = (*i)->get_interface();
if (!pp)
cverr() << "Module '" << (*i)->get_name() << "' doesn't provide an interface\n";
while (pp) {
cvdebug() << "Got type '" << typeid(*pp).name()
<< "', expect '"<< typeid(Interface).name() << "'\n";
Interface *p = dynamic_cast<Interface*>(pp);
CPluginBase *pold = pp;
pp = pp->next_interface();
if (p) {
cvdebug() << "add plugin '" << p->get_name() << "'\n";
auto loaded_plugin_i = m_plugins.find(p->get_name());
if ( loaded_plugin_i == m_plugins.end()) {
p->set_module(*i);
add_plugin_internal(PInterface(p));
// since this module will be used
// keep its module till the final cleanup
(*i)->set_keep_library();
} else {
auto loaded_plugin = loaded_plugin_i->second;
if (loaded_plugin->get_priority() < p->get_priority()) {
cvwarn() << "Plugin with name '" << p->get_name()
<< "' and priority '" << p->get_priority()
<< "' overrides already loaded plugin\n";
p->set_module(*i);
// since this module will be used
// keep its module till the final cleanup
(*i)->set_keep_library();
*i = loaded_plugin->get_module();
add_plugin_internal(PInterface(p));
}else{
cvwarn() << "Plugin with name '" << p->get_name()
<< "' already loaded, and new one has no higher priority.\n";
delete p;
}
// since this module will not be used at all
// unload the according library
(*i)->set_unload_library();
}
}else {
cvdebug() << "discard '" << pold->get_name() << "'\n";
delete pold;
}
}
}
catch(std::invalid_argument& x) {
cvdebug() << "Module '" << (*i)->get_name()
<< "' was not loaded because '"
<< x.what() << "'\n";
}
}
do_initialise();
}
template <typename I>
bool TPluginHandler<I>::add_plugin(PInterface p)
{
bool result = true;
auto loaded_plugin_i = m_plugins.find(p->get_name());
if ( loaded_plugin_i == m_plugins.end()) {
add_plugin_internal(p);
}else{
auto loaded_plugin = loaded_plugin_i->second;
if (loaded_plugin->get_priority() < p->get_priority()) {
cvinfo() << "Plugin with name '" << p->get_name()
<< "' and priority '" << p->get_priority()
<< "' overrides already loaded plugin with priority '"
<< loaded_plugin->get_priority() << "'\n";
add_plugin_internal(p);
// if the plug-in was inported from a module, mark this module for removal
auto old_module = loaded_plugin->get_module();
if (old_module)
old_module->set_unload_library();
}else{
cvinfo() << "Plugin with name '" << p->get_name()
<< "' has lower or equal priority '" << p->get_priority()
<< "' compared to already loaded plugin with priority '"
<< loaded_plugin->get_priority() << "' ... discarding\n";
result = false;
}
}
return result;
}
template <typename I>
void TPluginHandler<I>::add_plugin_internal(PInterface p)
{
m_plugins[p->get_name()] = p;
}
template <typename I>
void TPluginHandler<I>::do_add_dependend_handlers(HandlerHelpMap& handler_map)const
{
for (auto p = begin(); p != end(); ++p)
p->second->add_dependend_handlers(handler_map);
}
template <typename I>
typename TPluginHandler<I>::const_iterator TPluginHandler<I>::begin() const
{
return m_plugins.begin();
}
template <typename I>
typename TPluginHandler<I>::const_iterator TPluginHandler<I>::end() const
{
return m_plugins.end();
}
/**
The destructor frees the modules and plug-ins
*/
template <typename I>
TPluginHandler<I>::~TPluginHandler()
{
}
template <typename I>
size_t TPluginHandler<I>::size() const
{
return m_plugins.size();
}
template <typename I>
const std::string TPluginHandler<I>::get_plugin_names() const
{
std::vector<std::string> names;
for (auto i = m_plugins.begin(); i != m_plugins.end(); ++i)
names.push_back(i->first);
sort(names.begin(), names.end());
std::stringstream outstr;
std::copy(names.begin(), names.end(), std::ostream_iterator<const std::string>(outstr, " "));
return outstr.str();
}
template <typename I>
const std::set<std::string> TPluginHandler<I>::get_set() const
{
std::set<std::string> r;
for (auto i = m_plugins.begin(); i != m_plugins.end(); ++i)
r.insert(i->first);
return r;
}
template <typename I>
typename TPluginHandler<I>::Interface *TPluginHandler<I>::plugin(const char *plugin) const
{
auto p = m_plugins.find(plugin);
if (p == m_plugins.end()) {
std::stringstream msg;
cvdebug() << "Plugin '" << plugin << "' not found in '"
<< I::PlugData::data_descr << "/" << I::PlugType::type_descr << "'\n"
<< " With search path\n"
<< " '" << get_plugin_root();
return nullptr;
}
return p->second.get();
}
template <typename I>
void TPluginHandler<I>::do_print_short_help(std::ostream& os) const
{
for (auto i = begin(); i != end(); ++i) {
i->second->get_short_help(os);
}
}
template <typename I>
void TPluginHandler<I>::do_print_help(std::ostream& os) const
{
for (auto i = begin(); i != end(); ++i) {
i->second->get_help(os);
}
}
template <typename I>
void TPluginHandler<I>::do_get_xml_help(CXMLElement& handlerRoot) const
{
handlerRoot.set_child_text(m_help);
for (auto i = begin(); i != end(); ++i) {
auto pluginRoot = handlerRoot.add_child("plugin");
pluginRoot->set_attribute("name", i->first);
i->second->get_help_xml(*pluginRoot);
}
}
template <typename I>
void TPluginHandler<I>::do_initialise()
{
}
template <typename T>
THandlerSingleton<T>::THandlerSingleton()
{
TRACE_FUNCTION;
assert(!m_is_created);
m_is_created = true;
}
template <typename T>
const T& THandlerSingleton<T>::instance()
{
return do_instance(true);
}
template <typename T>
const T* THandlerSingleton<T>::pointer()
{
return &do_instance(false);
}
template <typename T>
bool THandlerSingleton<T>::add_plugin(typename T::PInterface p)
{
return do_instance(true).add_plugin(p);
}
template <typename T>
T& THandlerSingleton<T>::do_instance(bool require_initialization)
{
TRACE_FUNCTION;
CScopedLock lock(m_creation_mutex);
static THandlerSingleton<T> me;
cvdebug() << "m_is_initialized = " << m_is_initialized << "\n";
if (!m_is_initialized && require_initialization) {
TRACE("Unitialized state");
CScopedLock lock_init(m_initialization_mutex);
if (!m_is_initialized) {
TRACE("Enter locked unitialized state");
lock.release();
cvdebug() << "not yet initialized: second check passed\n";
me.initialise(m_searchpath);
m_is_initialized = true;
}
}
return me;
}
template <typename T>
void THandlerSingleton<T>::set_search_path(const CPluginSearchpath& searchpath)
{
TRACE_FUNCTION;
CScopedLock lock(m_creation_mutex);
cvdebug() << "Set path: "
<< T::Interface::get_data_path_part() << "/"
<< T::Interface::get_type_path_part() << '\n';
if (m_is_created) {
cvinfo() << "THandlerSingleton<"
<< T::Interface::get_data_path_part() << "/"
<< T::Interface::get_type_path_part()
<< ">::set_search_path: handler was already created\n";
}
#if 0 // work around the appearent icc bug, where the static member
//variable is not initialised
::new (&m_searchpath) CPathNameArray;
#endif
// cvdebug() << "searchpath=" << searchpath << "\n";
m_searchpath = searchpath;
}
template <typename T>
CPluginSearchpath THandlerSingleton<T>::m_searchpath;
template <typename T>
bool THandlerSingleton<T>::m_is_created = false;
template <typename T>
bool THandlerSingleton<T>::m_is_initialized = false;
template <typename T>
CMutex THandlerSingleton<T>::m_creation_mutex;
template <typename T>
CMutex THandlerSingleton<T>::m_initialization_mutex;
NS_MIA_END
|