hikyuu2/hikyuu_cpp/hikyuu/Block.cpp

118 lines
2.5 KiB
C++
Raw Normal View History

2016-04-03 00:08:31 +08:00
/*
* Block.cpp
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 <boost/algorithm/string.hpp>
#include "StockManager.h"
namespace hku {
HKU_API std::ostream& operator <<(std::ostream &os, const Block& blk) {
string strip(", ");
os << "Block(" << blk.category() << strip << blk.name() << ")";
return os;
}
Block::Block() {
}
Block::~Block() {
}
Block::Block(const string& category, const string& name) {
m_data = shared_ptr<Data>(new Data);
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) {
if (this == &block || m_data == block.m_data)
return *this;
m_data = block.m_data;
return *this;
}
bool Block::have(const string& market_code) const {
if (!m_data) return false;
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 {
if (!m_data) return false;
return m_data->m_stockDict.count(stock.market_code()) ? true : false;
}
Stock Block::get(const string& market_code) const {
Stock result;
if (!m_data)
return result;
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;
}
bool Block::add(const Stock& stock) {
if (stock.isNull() || have(stock))
return false;
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) {
StockManager& sm = StockManager::instance();
Stock stock = sm.getStock(market_code);
if (stock.isNull() || have(stock))
return false;
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) {
if (!have(market_code)) {
return false;
}
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) {
if (!have(stock)) {
return false;
}
m_data->m_stockDict.erase(stock.market_code());
return true;
}
} /* namespace hku */