hikyuu2/hikyuu_cpp/hikyuu/data_driver/DataDriverFactory.cpp

148 lines
4.0 KiB
C++
Raw Normal View History

2015-01-07 01:26:14 +08:00
/*
* DatabaseDriverFactory.cpp
*
* Created on: 2012-8-14
* Author: fasiondog
*/
#include <boost/algorithm/string.hpp>
2019-08-16 01:17:24 +08:00
#include "base_info/sqlite/SQLiteBaseInfoDriver.h"
2016-04-03 00:08:31 +08:00
#include "block_info/qianlong/QLBlockInfoDriver.h"
2017-10-20 02:11:57 +08:00
#include "kdata/hdf5/H5KDataDriver.h"
2015-01-07 01:26:14 +08:00
#include "DataDriverFactory.h"
#include "KDataDriver.h"
namespace hku {
map<string, BaseInfoDriverPtr> default_baseinfo_driver() {
map<string, BaseInfoDriverPtr> result;
2019-08-16 01:17:24 +08:00
result["SQLITE3"] = make_shared<SQLiteBaseInfoDriver>();
return result;
}
2015-01-07 01:26:14 +08:00
map<string, BlockInfoDriverPtr> default_block_driver() {
map<string, BlockInfoDriverPtr> result;
result["QIANLONG"] = make_shared<QLBlockInfoDriver>();
2015-01-07 01:26:14 +08:00
return result;
}
2017-10-20 02:11:57 +08:00
map<string, KDataDriverPtr> default_kdata_driver() {
map<string, KDataDriverPtr> result;
result["HDF5"] = make_shared<H5KDataDriver>();
return result;
}
map<string, BaseInfoDriverPtr> DataDriverFactory::m_baseInfoDrivers(default_baseinfo_driver());
map<string, BlockInfoDriverPtr> DataDriverFactory::m_blockDrivers(default_block_driver());
2017-10-20 02:11:57 +08:00
map<string, KDataDriverPtr> DataDriverFactory::m_kdataDrivers(default_kdata_driver());
map<Parameter, KDataDriverPtr> DataDriverFactory::m_param_kdataDrivers;
2015-01-07 01:26:14 +08:00
void DataDriverFactory
::regBaseInfoDriver(const BaseInfoDriverPtr& driver) {
string new_type(driver->name());
to_upper(new_type);
m_baseInfoDrivers[new_type] = driver;
2016-04-03 00:08:31 +08:00
}
2015-01-07 01:26:14 +08:00
void DataDriverFactory::removeBaseInfoDriver(const string& name) {
string new_type(name);
to_upper(new_type);
m_baseInfoDrivers.erase(new_type);
}
BaseInfoDriverPtr DataDriverFactory
::getBaseInfoDriver(const Parameter& params) {
map<string, BaseInfoDriverPtr>::const_iterator iter;
string type = params.get<string>("type");
to_upper(type);
iter = m_baseInfoDrivers.find(type);
BaseInfoDriverPtr result;
if (iter != m_baseInfoDrivers.end()) {
result = iter->second;
2017-10-20 02:11:57 +08:00
result->init(params);
}
return result;
}
void DataDriverFactory::regBlockDriver(const BlockInfoDriverPtr& driver) {
string name(driver->name());
to_upper(name);
m_blockDrivers[name] = driver;
}
void DataDriverFactory::removeBlockDriver(const string& name) {
string new_name(name);
to_upper(new_name);
m_blockDrivers.erase(new_name);
}
BlockInfoDriverPtr DataDriverFactory::getBlockDriver(const Parameter& params) {
BlockInfoDriverPtr result;
map<string, BlockInfoDriverPtr>::const_iterator iter;
string name = params.get<string>("type");
to_upper(name);
iter = m_blockDrivers.find(name);
if (iter != m_blockDrivers.end()) {
result = iter->second;
2017-10-20 02:11:57 +08:00
result->init(params);
}
return result;
}
void DataDriverFactory::regKDataDriver(const KDataDriverPtr& driver) {
string new_type(driver->name());
to_upper(new_type);
m_kdataDrivers[new_type] = driver;
}
void DataDriverFactory::removeKDataDriver(const string& name) {
string new_name(name);
to_upper(new_name);
m_kdataDrivers.erase(new_name);
auto iter = m_param_kdataDrivers.begin();
for (; iter != m_param_kdataDrivers.end(); ++iter) {
string new_type(iter->first.get<string>("type"));
to_upper(new_type);
if (new_type == new_name) {
break;
}
}
if (iter != m_param_kdataDrivers.end()) {
m_param_kdataDrivers.erase(iter);
}
}
KDataDriverPtr DataDriverFactory::getKDataDriver(const Parameter& params) {
KDataDriverPtr result;
auto param_iter = m_param_kdataDrivers.find(params);
if (param_iter != m_param_kdataDrivers.end()) {
result = param_iter->second;
return result;
}
string name;
try {
name = params.get<string>("type");
} catch (...) {
return result;
}
to_upper(name);
auto iter = m_kdataDrivers.find(name);
if (iter != m_kdataDrivers.end()) {
result = iter->second;
result->init(params);
m_param_kdataDrivers[params] = result;
}
return result;
}
2015-01-07 01:26:14 +08:00
} /* namespace hku */