hikyuu2/hikyuu_cpp/hikyuu/Block.cpp

164 lines
4.1 KiB
C++
Raw Permalink Normal View History

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: 201528
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 {
if (!block.m_data)
2016-04-03 00:08:31 +08:00
return;
m_data = block.m_data;
}
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 {
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;
}
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 {
HKU_IF_RETURN(!m_data, false);
2016-04-03 00:08:31 +08:00
string query_str = market_code;
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 {
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;
HKU_IF_RETURN(!m_data, result);
2016-04-03 00:08:31 +08:00
string query_str = market_code;
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;
}
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();
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) {
HKU_IF_RETURN(stock.isNull() || have(stock), false);
2016-04-03 00:08:31 +08:00
if (!m_data)
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);
HKU_IF_RETURN(stock.isNull() || have(stock), false);
2016-04-03 00:08:31 +08:00
if (!m_data)
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) {
HKU_IF_RETURN(!have(market_code), false);
2016-04-03 00:08:31 +08:00
string query_str = market_code;
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) {
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)
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 */