DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PluginCreators.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation for LCD
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
15 #include "DD4hep/PluginCreators.h"
16 #include "DD4hep/Primitives.h"
17 #include "DD4hep/Printout.h"
18 #include "DD4hep/Plugins.h"
19 
20 // C/C++ include files
21 #include <cstring>
22 
24 namespace DD4hep {
25 
26  static inline ComponentCast* component(void* p) { return (ComponentCast*)p; }
27 
28  void* createProcessor(Geometry::LCDD& lcdd, int argc, char** argv, void* (*cast)(void*)) {
29  void* processor = 0;
30  if ( argc < 2 ) {
31  except("createProcessor","++ DD4hep-plugins: No processor creator name given!");
32  }
33  for(int i=0; i<argc; ++i) {
34  if ( 0 == ::strncmp(argv[i],"-processor",4) ) {
35  std::vector<char*> args;
36  std::string fac = argv[++i];
37  for(int j=++i; j<argc && argv[j] &&
38  0 != ::strncmp(argv[j],"-processor",4) &&
39  0 != ::strncmp(argv[j],"-end-processor",8); ++j)
40  args.push_back(argv[j]);
41  int num_arg = int(args.size());
42  args.push_back(0);
43  processor = PluginService::Create<void*>(fac,&lcdd,num_arg,&args[0]);
44  if ( !processor ) {
45  PluginDebug dbg;
46  processor = PluginService::Create<void*>(fac, &lcdd, argc, argv);
47  if ( !processor ) {
48  except("createProcessor","DD4hep-plugins: Failed to locate plugin %s. \n%s.",
49  fac.c_str(), dbg.missingFactory(fac).c_str());
50  }
51  }
52  if ( cast ) {
53  void* obj = cast(processor);
54  if ( obj ) return obj;
55  ComponentCast* c = component(processor);
56  invalidHandleAssignmentError(typeid(cast),typeid(*c));
57  }
58  }
59  }
60  if ( !processor ) {
61  except("createProcessor",
62  "DD4hep-plugins: Found arguments in plugin call, but could not make any sense of them: %s",
63  arguments(argc,argv).c_str());
64  }
65  return processor;
66  }
67 
68  void* createPlugin(const std::string& factory, Geometry::LCDD& lcdd, int argc, char** argv, void* (*cast)(void*)) {
69  void* object = PluginService::Create<void*>(factory, &lcdd, argc, argv);
70  if ( !object ) {
71  PluginDebug dbg;
72  object = PluginService::Create<void*>(factory, &lcdd, argc, argv);
73  if ( !object ) {
74  except("ConditionsManager","DD4hep-plugins: Failed to locate plugin %s. \n%s.",
75  factory.c_str(), dbg.missingFactory(factory).c_str());
76  }
77  }
78  if ( cast ) {
79  void* obj = cast(object);
80  if ( obj ) return obj;
81  ComponentCast* c = component(object);
82  invalidHandleAssignmentError(typeid(cast),typeid(*c));
83  }
84  return object;
85  }
86 
88  void* createPlugin(const std::string& factory, Geometry::LCDD& lcdd, void* (*cast)(void*)) {
89  char* argv[] = {0};
90  int argc = 0;
91  return createPlugin(factory, lcdd, argc, argv, cast);
92  }
94  void* createPlugin(const std::string& factory,
95  Geometry::LCDD& lcdd,
96  const std::string& arg,
97  void* (*cast)(void*)) {
98  char* argv[] = { (char*)arg.c_str(), 0 };
99  int argc = 1;
100  return createPlugin(factory, lcdd, argc, argv, cast);
101  }
102 
103 }
std::string missingFactory(const std::string &name) const
Helper to check factory existence.
Definition: Plugins.cpp:147
int except(const std::string &src, const std::string &fmt,...)
Calls the display action with ERROR and throws an std::runtime_error exception.
Definition: Printout.cpp:217
void invalidHandleAssignmentError(const std::type_info &from, const std::type_info &to)
Throw exception when handles are badly assigned.
Definition: Primitives.cpp:195
Helper to debug plugin manager calls.
Definition: Plugins.h:77
static ComponentCast * component(void *p)
void * createPlugin(const std::string &factory, Geometry::LCDD &lcdd, int argc, char **argv, void *(*cast)(void *))
Class to perform dynamic casts using unknown pointers.
Definition: Primitives.h:168
void * createProcessor(Geometry::LCDD &lcdd, int argc, char **argv, void *(*cast)(void *))
The main interface to the DD4hep detector description package.
Definition: LCDD.h:82
std::string arguments(int argc, char **argv)
Helper function to serialize argument list to a single string.
Definition: Printout.cpp:96