ArtServicePointer.h
Go to the documentation of this file.
1 // ArtServicePointer.h
2 
3 #ifndef ArtServicePointer_H
4 #define ArtServicePointer_H
5 
6 // David Adams
7 // January 2017
8 //
9 // ArtServicePointer provides access to an art service.
10 //
11 // Usage:
12 // MyService* psvc = ArtServicePointer<MyService>()
13 //
14 // Template specializations may be used enable service without
15 // direct use of ServiceHandle, i.e. inside root.
16 
17 // This is the function that is specialized and used by clients.
18 template<class T>
20 
21 #ifdef __CLING__
22 
23 #include <iostream>
24 template<class T>
26  std::cout << "ArtServicePointer: No specialization found for " << typeid(T).name() << std::endl;
27  return nullptr;
28 }
29 
30 #else
31 
32 #include <iostream>
34 
35 // This should not be called directly.
36 template<class T>
38  try {
39  if ( art::ServiceRegistry::isAvailable<T>() ) {
40  return art::ServiceHandle<T>().get();
41  }
42 /*
43  art::ServiceRegistry& reg = art::ServiceRegistry::instance();
44  if ( ! reg.isAvailable<T>() ) return nullptr;
45  return &reg.get<T>();
46 */
47  } catch(const art::Exception& exc) {
48  std::string msg = exc.explain_self();
49  if ( msg.find("no ServiceRegistry has been set") != std::string::npos ) {
50  std::cout << "WARNING: ArtServicePointer: ServiceRegistry not found. Instantiate with" << std::endl;
51  std::cout << " ArtServiceHelper::load(\"myfcl.fcl\")" << std::endl;
52  } else {
53  std::cout << "WARNING: Art exception: \n" << msg;
54  }
55  } catch(...) {
56  std::cout << "ERROR: ArtServicePointer: Unexpected exception." << std::endl;
57  }
58  return nullptr;
59 }
60 
61 template<class T>
63  return GenericArtServicePointer<T>();
64 }
65 
66 #endif
67 
68 #endif
static QCString name
Definition: declinfo.cpp:673
T * ArtServicePointer()
T * GenericArtServicePointer()
void msg(const char *fmt,...)
Definition: message.cpp:107
std::string string
Definition: nybbler.cc:12
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
QTextStream & endl(QTextStream &s)