2016-04-03 00:08:31 +08:00
|
|
|
/*
|
|
|
|
* Block.cpp
|
2019-11-10 03:27:57 +08:00
|
|
|
*
|
2019-04-14 11:05:08 +08:00
|
|
|
* Copyright (c) hikyuu.org
|
2016-04-03 00:08:31 +08:00
|
|
|
*
|
|
|
|
* Created on: 2015年2月8日
|
2016-04-14 01:36:53 +08:00
|
|
|
* Author: fasiondog
|
2016-04-03 00:08:31 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StockManager.h"
|
|
|
|
|
|
|
|
namespace hku {
|
|
|
|
|
2019-11-10 03:27:57 +08:00
|
|
|
HKU_API std::ostream& operator<<(std::ostream& os, const Block& blk) {
|
2016-04-03 00:08:31 +08:00
|
|
|
string strip(", ");
|
|
|
|
os << "Block(" << blk.category() << strip << blk.name() << ")";
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2024-05-20 22:39:27 +08:00
|
|
|
Block::Block() noexcept {}
|
2016-04-03 00:08:31 +08:00
|
|
|
|
2019-11-10 03:27:57 +08:00
|
|
|
Block::~Block() {}
|
2016-04-03 00:08:31 +08:00
|
|
|
|
2024-05-20 22:39:27 +08:00
|
|
|
Block::Block(const string& category, const string& name) noexcept : m_data(make_shared<Data>()) {
|
2016-04-03 00:08:31 +08:00
|
|
|
m_data->m_category = category;
|
|
|
|
m_data->m_name = name;
|
|
|
|
}
|
|
|
|
|
2024-05-20 22:39:27 +08:00
|
|
|
Block::Block(const string& category, const string& name, const string& indexCode) noexcept
|
2024-04-23 18:04:05 +08:00
|
|
|
: Block(category, name) {
|
|
|
|
if (!indexCode.empty()) {
|
|
|
|
m_data->m_indexStock = StockManager::instance().getStock(indexCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Block::Block(const Block& block) noexcept {
|
2024-04-26 18:46:09 +08:00
|
|
|
if (!block.m_data)
|
2016-04-03 00:08:31 +08:00
|
|
|
return;
|
|
|
|
m_data = block.m_data;
|
|
|
|
}
|
|
|
|
|
2024-04-26 18:46:09 +08:00
|
|
|
Block::Block(Block&& block) noexcept {
|
|
|
|
if (!block.m_data)
|
|
|
|
return;
|
|
|
|
m_data = std::move(block.m_data);
|
|
|
|
}
|
|
|
|
|
2024-04-23 18:04:05 +08:00
|
|
|
Block& Block::operator=(const Block& block) noexcept {
|
2020-11-22 00:12:09 +08:00
|
|
|
HKU_IF_RETURN(this == &block || m_data == block.m_data, *this);
|
2016-04-03 00:08:31 +08:00
|
|
|
m_data = block.m_data;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-04-26 18:46:09 +08:00
|
|
|
Block& Block::operator=(Block&& block) noexcept {
|
|
|
|
HKU_IF_RETURN(this == &block || m_data == block.m_data, *this);
|
|
|
|
m_data = std::move(block.m_data);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-04-03 00:08:31 +08:00
|
|
|
bool Block::have(const string& market_code) const {
|
2020-11-22 00:12:09 +08:00
|
|
|
HKU_IF_RETURN(!m_data, false);
|
2016-04-03 00:08:31 +08:00
|
|
|
string query_str = market_code;
|
2019-08-03 01:44:57 +08:00
|
|
|
to_upper(query_str);
|
2016-04-03 00:08:31 +08:00
|
|
|
return m_data->m_stockDict.count(query_str) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Block::have(const Stock& stock) const {
|
2020-11-22 00:12:09 +08:00
|
|
|
HKU_IF_RETURN(!m_data, false);
|
2016-04-03 00:08:31 +08:00
|
|
|
return m_data->m_stockDict.count(stock.market_code()) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stock Block::get(const string& market_code) const {
|
|
|
|
Stock result;
|
2020-11-22 00:12:09 +08:00
|
|
|
HKU_IF_RETURN(!m_data, result);
|
2016-04-03 00:08:31 +08:00
|
|
|
string query_str = market_code;
|
2019-08-03 01:44:57 +08:00
|
|
|
to_upper(query_str);
|
2016-04-03 00:08:31 +08:00
|
|
|
auto iter = m_data->m_stockDict.find(query_str);
|
|
|
|
if (iter != m_data->m_stockDict.end()) {
|
|
|
|
result = iter->second;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-03-29 17:57:06 +08:00
|
|
|
StockList Block::getStockList(std::function<bool(const Stock&)>&& filter) const {
|
|
|
|
StockList ret;
|
2023-10-19 18:35:40 +08:00
|
|
|
ret.reserve(size());
|
|
|
|
auto iter = m_data->m_stockDict.begin();
|
2024-03-29 17:57:06 +08:00
|
|
|
if (filter) {
|
|
|
|
for (; iter != m_data->m_stockDict.end(); ++iter) {
|
|
|
|
if (filter(iter->second)) {
|
|
|
|
ret.emplace_back(iter->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (; iter != m_data->m_stockDict.end(); ++iter) {
|
|
|
|
ret.emplace_back(iter->second);
|
|
|
|
}
|
2023-10-19 18:35:40 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-04-03 00:08:31 +08:00
|
|
|
bool Block::add(const Stock& stock) {
|
2020-11-22 00:12:09 +08:00
|
|
|
HKU_IF_RETURN(stock.isNull() || have(stock), false);
|
2016-04-03 00:08:31 +08:00
|
|
|
if (!m_data)
|
2024-05-30 15:03:51 +08:00
|
|
|
m_data = make_shared<Data>();
|
2016-04-03 00:08:31 +08:00
|
|
|
|
|
|
|
m_data->m_stockDict[stock.market_code()] = stock;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Block::add(const string& market_code) {
|
2019-11-10 03:27:57 +08:00
|
|
|
const StockManager& sm = StockManager::instance();
|
2016-04-03 00:08:31 +08:00
|
|
|
Stock stock = sm.getStock(market_code);
|
2020-11-22 00:12:09 +08:00
|
|
|
HKU_IF_RETURN(stock.isNull() || have(stock), false);
|
2016-04-03 00:08:31 +08:00
|
|
|
if (!m_data)
|
2024-05-30 15:03:51 +08:00
|
|
|
m_data = make_shared<Data>();
|
2016-04-03 00:08:31 +08:00
|
|
|
|
|
|
|
m_data->m_stockDict[stock.market_code()] = stock;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-05-20 22:39:27 +08:00
|
|
|
bool Block::add(const StockList& stocks) {
|
|
|
|
bool success = true;
|
|
|
|
for (const auto& stk : stocks) {
|
|
|
|
success = add(stk);
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Block::add(const StringList& market_codes) {
|
|
|
|
bool success = true;
|
|
|
|
for (const auto& code : market_codes) {
|
|
|
|
success = add(code);
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2016-04-03 00:08:31 +08:00
|
|
|
bool Block::remove(const string& market_code) {
|
2020-11-22 00:12:09 +08:00
|
|
|
HKU_IF_RETURN(!have(market_code), false);
|
2016-04-03 00:08:31 +08:00
|
|
|
string query_str = market_code;
|
2019-08-03 01:44:57 +08:00
|
|
|
to_upper(query_str);
|
2016-04-03 00:08:31 +08:00
|
|
|
m_data->m_stockDict.erase(query_str);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Block::remove(const Stock& stock) {
|
2020-11-22 00:12:09 +08:00
|
|
|
HKU_IF_RETURN(!have(stock), false);
|
2016-04-03 00:08:31 +08:00
|
|
|
m_data->m_stockDict.erase(stock.market_code());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-04-23 18:04:05 +08:00
|
|
|
void Block::setIndexStock(const Stock& stk) {
|
|
|
|
if (!m_data)
|
2024-05-30 15:03:51 +08:00
|
|
|
m_data = make_shared<Data>();
|
2024-04-23 18:04:05 +08:00
|
|
|
m_data->m_indexStock = stk;
|
|
|
|
}
|
|
|
|
|
2024-05-21 01:04:52 +08:00
|
|
|
HKU_API Block getBlock(const string& category, const string& name) {
|
|
|
|
auto& sm = StockManager::instance();
|
|
|
|
return sm.getBlock(category, name);
|
|
|
|
}
|
|
|
|
|
2016-04-03 00:08:31 +08:00
|
|
|
} /* namespace hku */
|