Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:36

0001 
0002 // Copyright 2021, Jefferson Science Associates, LLC.
0003 // Subject to the terms in the LICENSE file found in the top-level directory.
0004 
0005 
0006 #include "JAutoActivator.h"
0007 
0008 JAutoActivator::JAutoActivator() {
0009     SetTypeName("JAutoActivator");
0010     SetCallbackStyle(CallbackStyle::ExpertMode);
0011 }
0012 
0013 void JAutoActivator::AddAutoActivatedFactory(string factory_name, string factory_tag) {
0014     m_auto_activated_factories.push_back({std::move(factory_name), std::move(factory_tag)});
0015 }
0016 
0017 /// Converts "ObjName:TagName" into ("ObjName", "TagName")
0018 ///      and "name::space::ObjName:TagName" into ("name::space::ObjName", "TagName")
0019 std::pair<std::string, std::string> JAutoActivator::Split(std::string factory_name) {
0020 
0021     std::string::size_type pos = -1;
0022     while (true) {
0023         // Find the next colon
0024         pos = factory_name.find(':', pos+1);
0025 
0026         if (pos == std::string::npos) {
0027             // There are no colons at all remaining. Everything is objname
0028             return std::make_pair(factory_name, "");
0029         }
0030         else if (factory_name[pos+1] == ':') {
0031             // Else we found a double colon, which is part of the objname namespace. Keep going.
0032             pos += 1;
0033         }
0034         else {
0035             // We found the first single colon, which has to delimit the objname from the tag. Stop.
0036             return std::make_pair(factory_name.substr(0, pos), factory_name.substr(pos+1));
0037         }
0038     }
0039 }
0040 
0041 void JAutoActivator::Init() {
0042 
0043     string autoactivate_conf;
0044     if (GetApplication()->GetParameter("autoactivate", autoactivate_conf)) {
0045         try {
0046             if (!autoactivate_conf.empty()) {
0047                 // Loop over comma separated list of factories to auto activate
0048                 vector <string> myfactories;
0049                 string &str = autoactivate_conf;
0050                 unsigned int cutAt;
0051                 while ((cutAt = str.find(",")) != (unsigned int) str.npos) {
0052                     if (cutAt > 0)myfactories.push_back(str.substr(0, cutAt));
0053                     str = str.substr(cutAt + 1);
0054                 }
0055                 if (str.length() > 0)myfactories.push_back(str);
0056 
0057                 // Loop over list of factory strings (which could be in factory:tag
0058                 // form) and parse the strings as needed in order to add them to
0059                 // the auto activate list.
0060                 for (unsigned int i = 0; i < myfactories.size(); i++) {
0061                     auto pair = Split(myfactories[i]);
0062                     AddAutoActivatedFactory(pair.first, pair.second);
0063                 }
0064             }
0065         }
0066         catch (...) {
0067             LOG_ERROR(GetLogger()) << "Error parsing parameter 'autoactivate'. Found: " << autoactivate_conf << LOG_END;
0068             throw JException("AutoActivator could not parse parameter 'autoactivate'");
0069         }
0070     }
0071 }
0072 
0073 void JAutoActivator::Process(const JEvent& event) {
0074     for (const auto &pair: m_auto_activated_factories) {
0075         auto name = pair.first;
0076         auto tag = pair.second;
0077         auto factory = event.GetFactory(name, tag);
0078         if (factory != nullptr) {
0079             factory->Create(event.shared_from_this()); // This will do nothing if factory is already created
0080         }
0081         else {
0082             LOG_ERROR(GetLogger()) << "Could not find factory with typename=" << name << ", tag=" << tag << LOG_END;
0083             throw JException("AutoActivator could not find factory with typename=%s, tag=%s", name.c_str(), tag.c_str());
0084         }
0085     }
0086 }
0087