Warning, file /jana2/src/libraries/JANA/Services/JServiceLocator.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006 #ifndef _JSERVICELOCATOR_H_
0007 #define _JSERVICELOCATOR_H_
0008
0009
0010 #include <map>
0011 #include <typeinfo>
0012 #include <typeindex>
0013 #include <assert.h>
0014 #include <memory>
0015 #include <mutex>
0016
0017 #include <JANA/JException.h>
0018 #include "JANA/Utils/JTypeInfo.h"
0019 #include <JANA/JServiceFwd.h>
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 class JServiceLocator {
0032
0033 std::map<std::type_index, std::shared_ptr<JService>> underlying;
0034 std::mutex mutex;
0035
0036 public:
0037
0038
0039 template<typename T>
0040 void provide(std::shared_ptr<T> t) {
0041
0042
0043
0044
0045
0046 std::lock_guard<std::mutex> lock(mutex);
0047 auto svc = std::dynamic_pointer_cast<JService>(t);
0048 assert(svc != nullptr);
0049 svc->SetTypeName(JTypeInfo::demangle<T>());
0050 underlying[std::type_index(typeid(T))] = svc;
0051 }
0052
0053 template<typename T>
0054 std::shared_ptr<T> get() {
0055
0056
0057 auto iter = underlying.find(std::type_index(typeid(T)));
0058 if (iter == underlying.end()) {
0059 std::ostringstream oss;
0060 oss << "Service not found: '" << JTypeInfo::demangle<T>() << "'. Did you forget to include a plugin?" << std::endl;
0061 throw JException(oss.str());
0062 }
0063 auto svc = iter->second;
0064 svc->DoInit(this);
0065
0066
0067
0068 auto svc_typed = std::static_pointer_cast<T>(svc);
0069 return svc_typed;
0070 }
0071
0072 void InitAllServices() {
0073
0074
0075
0076
0077 for (auto& entry : underlying) {
0078 entry.second->DoInit(this);
0079 }
0080 }
0081 };
0082
0083 #endif