#pragma once #include #include #include #include #include #include namespace wfr { // Forward declarations namespace wfs { class Workflow; } namespace wfs { class WorkflowTemplate; } class WorkflowRuntime { friend class wfs::WorkflowTemplate; private: struct ConfigEntry { std::string stringData; // std::string and unions don't go well together union { bool boolData; uint64_t uintData; int64_t intData; double doubleData; } data; }; static std::map globalConfig; static std::default_random_engine randomEngine; static std::unordered_set> workflowInstances; static void addWorkflowInstance(const std::shared_ptr& wf) { workflowInstances.insert(wf); } public: static void init(int argc = 0, char** argv = nullptr); static void finish(); static void logMessage( std::string message, const wfr_timestamp_sc_t& timestamp = std::chrono::system_clock::now(), bool noLocking = false ); template static T setGlobalConfig(const std::string& key, const T& value) { throw exception::wfr_fatal_error("Function \"setGlobalConfig\" not implemented."); } template static T getGlobalConfig(const std::string& key, const T& defaultValue = T()) { throw exception::wfr_fatal_error("Function \"getGlobalConfig\" not implemented."); } template static T getRandomNumber(const T& min, const T& max) { throw exception::wfr_fatal_error("Function not implemented."); } }; template<> inline bool WorkflowRuntime::setGlobalConfig(const std::string& key, const bool& value) { bool retval = false; auto e = globalConfig.find(key); if (e != globalConfig.end()) { retval = e->second.data.boolData; e->second.data.boolData = value; } else { ConfigEntry e; e.data.boolData = value; globalConfig.insert({key, e}); } return retval; } template<> inline uint64_t WorkflowRuntime::setGlobalConfig(const std::string& key, const uint64_t& value) { uint64_t retval = 0; auto e = globalConfig.find(key); if (e != globalConfig.end()) { retval = e->second.data.uintData; e->second.data.uintData = value; } else { ConfigEntry e; e.data.uintData = value; globalConfig.insert({key, e}); } return retval; } template<> inline int64_t WorkflowRuntime::setGlobalConfig(const std::string& key, const int64_t& value) { int64_t retval = 0; auto e = globalConfig.find(key); if (e != globalConfig.end()) { retval = e->second.data.intData; e->second.data.intData = value; } else { ConfigEntry e; e.data.intData = value; globalConfig.insert({key, e}); } return retval; } template<> inline double WorkflowRuntime::setGlobalConfig(const std::string& key, const double& value) { double retval = 0; auto e = globalConfig.find(key); if (e != globalConfig.end()) { retval = e->second.data.doubleData; e->second.data.doubleData = value; } else { ConfigEntry e; e.data.doubleData = value; globalConfig.insert({key, e}); } return retval; } template<> inline std::string WorkflowRuntime::getGlobalConfig(const std::string& key, const std::string& defaultValue) { std::string retval = defaultValue; auto e = globalConfig.find(key); if (e != globalConfig.end()) { retval = e->second.stringData; } return retval; } template<> inline bool WorkflowRuntime::getGlobalConfig(const std::string& key, const bool& defaultValue) { bool retval = defaultValue; auto e = globalConfig.find(key); if (e != globalConfig.end()) { retval = e->second.data.boolData; } return retval; } template<> inline uint64_t WorkflowRuntime::getGlobalConfig(const std::string& key, const uint64_t& defaultValue) { uint64_t retval = defaultValue; auto e = globalConfig.find(key); if (e != globalConfig.end()) { retval = e->second.data.uintData; } return retval; } template<> inline int64_t WorkflowRuntime::getGlobalConfig(const std::string& key, const int64_t& defaultValue) { int64_t retval = defaultValue; auto e = globalConfig.find(key); if (e != globalConfig.end()) { retval = e->second.data.intData; } return retval; } template<> inline double WorkflowRuntime::getGlobalConfig(const std::string& key, const double& defaultValue) { double retval = defaultValue; auto e = globalConfig.find(key); if (e != globalConfig.end()) { retval = e->second.data.doubleData; } return retval; } template<> inline std::string WorkflowRuntime::setGlobalConfig(const std::string& key, const std::string& value) { std::string retval; auto e = globalConfig.find(key); if (e != globalConfig.end()) { retval = e->second.stringData; e->second.stringData = value; } else { ConfigEntry e; e.stringData = value; globalConfig.insert({key, e}); } return retval; } template<> inline double WorkflowRuntime::getRandomNumber(const double& min, const double& max) { std::uniform_real_distribution distribution(min, max); return distribution(randomEngine); } template<> inline wfr_size_t WorkflowRuntime::getRandomNumber(const wfr_size_t& min, const wfr_size_t& max) { std::uniform_int_distribution distribution(min, max); return distribution(randomEngine); } } /* namespace wfr */