hikyuu2/hikyuu_cpp/hikyuu/Block.cpp

110 lines
2.7 KiB
C++
Raw 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;
}
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) {
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 {
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;
}
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) {
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);
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) {
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;
}
} /* namespace hku */