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;
|
|
|
|
}
|
|
|
|
|
2019-11-10 03:27:57 +08:00
|
|
|
Block::Block() {}
|
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
|
|
|
|
2019-11-10 03:27:57 +08:00
|
|
|
Block::Block(const string& category, const string& name) : m_data(make_shared<Data>()) {
|
2016-04-03 00:08:31 +08:00
|
|
|
m_data->m_category = category;
|
|
|
|
m_data->m_name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
Block::Block(const Block& block) {
|
|
|
|
if (m_data == block.m_data)
|
|
|
|
return;
|
|
|
|
m_data = block.m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
Block& Block::operator=(const Block& block) {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-10-19 18:35:40 +08:00
|
|
|
vector<Stock> Block::getAllStocks() const {
|
|
|
|
vector<Stock> ret;
|
|
|
|
ret.reserve(size());
|
|
|
|
auto iter = m_data->m_stockDict.begin();
|
|
|
|
for (; iter != m_data->m_stockDict.end(); ++iter) {
|
|
|
|
ret.emplace_back(iter->second);
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
m_data = shared_ptr<Data>(new Data);
|
|
|
|
|
|
|
|
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)
|
|
|
|
m_data = shared_ptr<Data>(new Data);
|
|
|
|
|
|
|
|
m_data->m_stockDict[stock.market_code()] = stock;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace hku */
|