Files
WFR-lib/include/wfr/WorkflowRuntime.h
2017-04-11 15:49:21 +02:00

209 lines
5.3 KiB
C++
Executable File

#pragma once
#include <wfr/common/types/WfrTypes.h>
#include <string>
#include <map>
#include <wfr/common/exceptions/exceptions.h>
#include <random>
#include <unordered_set>
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<std::string, ConfigEntry> globalConfig;
static std::default_random_engine randomEngine;
static std::unordered_set<std::shared_ptr<wfs::Workflow>> workflowInstances;
static void addWorkflowInstance(const std::shared_ptr<wfs::Workflow>& 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<class T>
static T setGlobalConfig(const std::string& key, const T& value) {
throw exception::wfr_fatal_error("Function \"setGlobalConfig\" not implemented.");
}
template<class T>
static T getGlobalConfig(const std::string& key, const T& defaultValue = T()) {
throw exception::wfr_fatal_error("Function \"getGlobalConfig\" not implemented.");
}
template<class T>
static T getRandomNumber(const T& min, const T& max) {
throw exception::wfr_fatal_error("Function not implemented.");
}
};
template<>
inline bool WorkflowRuntime::setGlobalConfig<bool>(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<uint64_t>(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<int64_t>(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<double>(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<std::string>(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<bool>(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<uint64_t>(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<int64_t>(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<double>(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<std::string>(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<double> 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<wfr_size_t> distribution(min, max);
return distribution(randomEngine);
}
} /* namespace wfr */