Skip to content
Snippets Groups Projects
Plugins.cpp 6.75 KiB
Newer Older
//==========================================================================
Markus Frank's avatar
Markus Frank committed
//  AIDA Detector description implementation 
//--------------------------------------------------------------------------
Markus Frank's avatar
Markus Frank committed
// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
// For the licensing terms see $DD4hepINSTALL/LICENSE.
// For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
// Author     : M.Frank
//
//==========================================================================

// Framework include files
#include "DD4hep/Plugins.h"
#if defined(DD4HEP_ROOT_VERSION_5)
#include "DD4hep/detail/Plugins.inl"
#include <cstdlib>
Markus Frank's avatar
Markus Frank committed
using namespace dd4hep;
Markus Frank's avatar
Markus Frank committed

namespace {
  inline int* s_debug_value()   {
    static int s_debug_value = ::getenv("DD4HEP_TRACE") == 0 ? 0 : 1;
    return &s_debug_value;
  }
}

bool PluginService::debug()  {
  return *s_debug_value() ? true : false;
}

bool PluginService::setDebug(bool new_value)   {
  int *ptr = s_debug_value();
  bool old_value = *ptr;
  *ptr = new_value ? 1 : 0;
  return old_value;
}

#if !defined(DD4HEP_PARSERS_NO_ROOT) && DD4HEP_ROOT_VERSION_5
PluginDebug::PluginDebug(int dbg)
Markus Frank's avatar
Markus Frank committed
  m_debug = ROOT::Reflex::PluginService::Debug();
  ROOT::Reflex::PluginService::SetDebug(dbg);
PluginDebug::~PluginDebug() noexcept(false) {
Markus Frank's avatar
Markus Frank committed
  ROOT::Reflex::PluginService::SetDebug (m_debug);
string PluginDebug::missingFactory(const string& name) const {
Markus Frank's avatar
Markus Frank committed
  ROOT::Reflex::Scope factories = ROOT::Reflex::Scope::ByName(PLUGINSVC_FACTORY_NS);
  string factoryname = ROOT::Reflex::PluginService::FactoryName(name);
  string msg = "\t\tNo factory for type " + name + " found.\n"
    "\t\tPlease check library load path and/or plugin factory name.";
/// Dummy functions of the plugin service
void* PluginService::getCreator(const std::string&, const std::type_info&)  {  return 0;   }
void  PluginService::addFactory(const std::string&, void*, const std::type_info&, const std::type_info&)  {}

#else   // ROOT 6 or no ROOT at all
Markus Frank's avatar
Markus Frank committed
#if !defined(DD4HEP_PARSERS_NO_ROOT)
Markus Frank's avatar
Markus Frank committed
#else
#include <dlfcn.h>
#endif
#include <cstring>

namespace   {
  struct PluginInterface  {
    int (*getDebug)();
    int (*setDebug)(int new_value);
    void* (*create)(const char* identifier, const char* signature);
    void  (*add)(const char* identifier, 
                 void* creator_stub, 
                 const char* signature, 
                 const char* return_type);
    PluginInterface() noexcept(false);
    static PluginInterface& instance()  noexcept(false)   {
      static PluginInterface s_instance;
      return s_instance;
    }
  };

  template <typename T> 
  static inline T get_func(void* handle, const char* plugin, const char* entry)  {
Markus Frank's avatar
Markus Frank committed
#if !defined(DD4HEP_PARSERS_NO_ROOT)
    PluginService::FuncPointer<Func_t> fun(gSystem->DynFindSymbol(plugin,entry));
    PluginService::FuncPointer<T> fp(fun.fptr.ptr);
    if ( handle ) {}
Markus Frank's avatar
Markus Frank committed
#else
    PluginService::FuncPointer<T> fp(::dlsym(handle, entry));
    if ( !fp.fptr.ptr ) fp.fptr.ptr = ::dlsym(0, entry);
Markus Frank's avatar
Markus Frank committed
#endif
    if ( 0 == fp.fptr.ptr )      {
Markus Frank's avatar
Markus Frank committed
      string err = "dd4hep:PluginService: Failed to access symbol "
        "\""+string(entry)+"\" in plugin library "+string(plugin)+
        " ["+string(::strerror(errno))+"]";
      throw runtime_error(err);
    }
    return fp.fptr.fcn;
  }
  PluginInterface::PluginInterface()  noexcept(false)
    : getDebug(0), setDebug(0), create(0), add(0)
  {
    void* handle = 0;
    const char* plugin_name = ::getenv("DD4HEP_PLUGINMGR");
    if ( 0 == plugin_name )   {
      plugin_name = "libDD4hepGaudiPluginMgr";
    }
#if !defined(DD4HEP_PARSERS_NO_ROOT)
    if ( 0 != gSystem->Load(plugin_name) )   {
      printout(DEBUG,"Plugins","Failed to load plugin manager library: %s",plugin_name);
#else
    handle = ::dlopen(plugin_name, RTLD_LAZY | RTLD_GLOBAL);
#endif
Markus Frank's avatar
Markus Frank committed
    getDebug = get_func< int (*) ()>(handle, plugin_name,"dd4hep_pluginmgr_getdebug");
    setDebug = get_func< int (*) (int)>(handle, plugin_name,"dd4hep_pluginmgr_getdebug");
    create   = get_func< void* (*) (const char*,
Markus Frank's avatar
Markus Frank committed
                                    const char*)>(handle, plugin_name,"dd4hep_pluginmgr_create");
    add      = get_func< void (*) (const char* identifier, 
                                   void* creator_stub, 
                                   const char* signature, 
Markus Frank's avatar
Markus Frank committed
                                   const char* return_type)>(handle, plugin_name,"dd4hep_pluginmgr_add_factory");
Markus Frank's avatar
Markus Frank committed

/// Default constructor
PluginDebug::PluginDebug(int dbg)  noexcept(false) : m_debug(0) {
  m_debug = PluginInterface::instance().setDebug(dbg);
Markus Frank's avatar
Markus Frank committed
}

/// Default destructor
PluginDebug::~PluginDebug()   noexcept(false)   {
  PluginInterface::instance().setDebug(m_debug);
Markus Frank's avatar
Markus Frank committed
}

/// Helper to check factory existence
string PluginDebug::missingFactory(const string& name) const {
  string factoryname = "Create("+name+")";
  string msg = "\t\tNo factory with name " + factoryname + " for type " + name + " found.\n"
    "\t\tPlease check library load path and/or plugin factory name.";
Markus Frank's avatar
Markus Frank committed
  return msg;
}

void* PluginService::getCreator(const std::string& id, const std::type_info& info)  {
  return PluginInterface::instance().create(id.c_str(), info.name());
}

void PluginService::addFactory(const std::string& id, stub_t stub,
                               const std::type_info&  signature_type,
                               const std::type_info&  return_type)
{
  if ( PluginService::debug() )  {
    printout(INFO,"PluginService","+++ Declared factory[%s] with signature %s type:%s.",
             id.c_str(),signature_type.name(),return_type.name());
  }
  PluginInterface::instance().add(id.c_str(),stub,signature_type.name(),return_type.name());
Markus Frank's avatar
Markus Frank committed
#endif

#if !defined(DD4HEP_PARSERS_NO_ROOT)
Markus Frank's avatar
Markus Frank committed
#include "DD4hep/Detector.h"
#include "DD4hep/Handle.h"
#include "DD4hep/GeoHandler.h"
#include "XML/XMLElements.h"
Markus Frank's avatar
Markus Frank committed
DD4HEP_IMPLEMENT_PLUGIN_REGISTRY(NamedObject*, (Detector*,xml::Handle_t*,Ref_t*))
DD4HEP_IMPLEMENT_PLUGIN_REGISTRY(NamedObject*, (Detector*,xml::Handle_t*))
DD4HEP_IMPLEMENT_PLUGIN_REGISTRY(NamedObject*, (Detector*))
DD4HEP_IMPLEMENT_PLUGIN_REGISTRY(TObject*,     (Detector*,xml::Handle_t*))
Markus Frank's avatar
Markus Frank committed
DD4HEP_IMPLEMENT_PLUGIN_REGISTRY(long, (Detector*,xml::Handle_t*))
DD4HEP_IMPLEMENT_PLUGIN_REGISTRY(long, (Detector*,xml::Handle_t const*))
DD4HEP_IMPLEMENT_PLUGIN_REGISTRY(long, (Detector*, int, char**))
DD4HEP_IMPLEMENT_PLUGIN_REGISTRY(long, (Detector*))
DD4HEP_IMPLEMENT_PLUGIN_REGISTRY(long, (Detector*, const GeoHandler*, const std::map<std::string,std::string>*))
Markus Frank's avatar
Markus Frank committed
DD4HEP_IMPLEMENT_PLUGIN_REGISTRY(long, ())
DD4HEP_IMPLEMENT_PLUGIN_REGISTRY(void*, (const char*))
Markus Frank's avatar
Markus Frank committed
DD4HEP_IMPLEMENT_PLUGIN_REGISTRY(void*, (Detector*,int,char**))