hikyuu2/hikyuu_cpp/hikyuu/Block.h

174 lines
4.1 KiB
C++
Raw Normal View History

2016-04-03 00:08:31 +08:00
/*
* Block.h
*
* Created on: 201528
2016-04-14 01:36:53 +08:00
* Author: fasiondog
2016-04-03 00:08:31 +08:00
*/
#pragma once
2016-04-03 00:08:31 +08:00
#ifndef BLOCK_H_
#define BLOCK_H_
#include "StockMapIterator.h"
namespace hku {
2017-09-29 01:37:12 +08:00
/**
*
* @ingroup StockManage
*/
2016-04-03 00:08:31 +08:00
class HKU_API Block {
public:
2024-05-20 22:39:27 +08:00
Block() noexcept;
Block(const string& category, const string& name) noexcept;
Block(const string& category, const string& name, const string& indexCode) noexcept;
2024-04-23 18:04:05 +08:00
Block(const Block&) noexcept;
Block(Block&&) noexcept;
2024-04-23 18:04:05 +08:00
Block& operator=(const Block&) noexcept;
Block& operator=(Block&&) noexcept;
2016-04-03 00:08:31 +08:00
virtual ~Block();
typedef StockMapIterator const_iterator;
const_iterator begin() const {
const_iterator iter;
if (m_data)
iter = StockMapIterator(m_data->m_stockDict.begin());
return iter;
}
const_iterator end() const {
const_iterator iter;
if (m_data)
iter = StockMapIterator(m_data->m_stockDict.end());
return iter;
}
2024-05-21 01:04:52 +08:00
bool operator==(const Block& blk) const {
2016-04-03 00:08:31 +08:00
return m_data == blk.m_data;
}
2024-05-21 01:04:52 +08:00
bool operator!=(const Block& blk) const {
2016-04-03 00:08:31 +08:00
return m_data != blk.m_data;
}
2017-09-29 01:37:12 +08:00
/** 获取板块类别 */
2016-04-03 00:08:31 +08:00
string category() const {
return m_data ? m_data->m_category : "";
}
2017-09-29 01:37:12 +08:00
/** 获取板块名称 */
2016-04-03 00:08:31 +08:00
string name() const {
return m_data ? m_data->m_name : "";
}
2017-09-29 01:37:12 +08:00
/** 设置板块类别 */
2016-04-03 00:08:31 +08:00
void category(const string& category) {
if (!m_data)
m_data = make_shared<Data>();
2016-04-03 00:08:31 +08:00
m_data->m_category = category;
}
2017-09-29 01:37:12 +08:00
/** 设置名称 */
2016-04-03 00:08:31 +08:00
void name(const string& name) {
if (!m_data)
m_data = make_shared<Data>();
2016-04-03 00:08:31 +08:00
m_data->m_name = name;
}
2017-09-29 01:37:12 +08:00
/** 是否包含指定的证券 */
2016-04-03 00:08:31 +08:00
bool have(const string& market_code) const;
2017-09-29 01:37:12 +08:00
/** 是否包含指定的证券 */
2016-04-03 00:08:31 +08:00
bool have(const Stock& stock) const;
2017-09-29 01:37:12 +08:00
/** 获取指定的证券 */
Stock get(const string& market_code) const;
/** 获取指定的证券 */
2016-04-03 00:08:31 +08:00
Stock operator[](const string& market_code) const {
return get(market_code);
}
2017-09-29 01:37:12 +08:00
2023-10-19 18:35:40 +08:00
/** 获取板块下所有证券 */
StockList getStockList(
std::function<bool(const Stock&)>&& filter = std::function<bool(const Stock&)>()) const;
2023-10-19 18:35:40 +08:00
2017-09-29 01:37:12 +08:00
/** 加入指定证券 */
2016-04-03 00:08:31 +08:00
bool add(const Stock& stock);
2017-09-29 01:37:12 +08:00
/** 加入指定证券 */
bool add(const string& market_code);
2024-05-20 22:39:27 +08:00
/**
*
* @param stocks
* @return true
* @return false
*/
bool add(const StockList& stocks);
/**
*
* @param market_codes
* @return true
* @return false
*/
bool add(const StringList& market_codes);
2017-09-29 01:37:12 +08:00
/** 移除指定证券 */
bool remove(const string& market_code);
/** 移除指定证券 */
2016-04-03 00:08:31 +08:00
bool remove(const Stock& stock);
2017-09-29 01:37:12 +08:00
/** 包含的证券数量 */
2016-04-03 00:08:31 +08:00
size_t size() const {
return m_data ? m_data->m_stockDict.size() : 0;
}
2017-09-29 01:37:12 +08:00
/** 是否为空 */
2016-04-03 00:08:31 +08:00
bool empty() const {
return size() ? false : true;
}
2017-09-29 01:37:12 +08:00
/** 清除包含的所有证券 */
2016-04-03 00:08:31 +08:00
void clear() {
2019-11-10 19:45:57 +08:00
if (m_data)
m_data->m_stockDict.clear();
2016-04-03 00:08:31 +08:00
}
2024-04-23 18:04:05 +08:00
/** 获取对应的指数,可能为空 Stock */
Stock getIndexStock() const {
return m_data ? m_data->m_indexStock : Stock();
}
/** 设置对应的指数 */
void setIndexStock(const Stock& stk);
2016-04-03 00:08:31 +08:00
private:
struct HKU_API Data {
string m_category;
string m_name;
2024-04-23 18:04:05 +08:00
Stock m_indexStock; // 对应指数,可能不存在
2016-04-03 00:08:31 +08:00
StockMapIterator::stock_map_t m_stockDict;
};
shared_ptr<Data> m_data;
};
2017-09-29 01:37:12 +08:00
/** @ingroup StockManage */
2016-04-03 00:08:31 +08:00
typedef vector<Block> BlockList;
2019-11-10 19:45:57 +08:00
HKU_API std::ostream& operator<<(std::ostream& os, const Block&);
2016-04-03 00:08:31 +08:00
2024-05-21 01:04:52 +08:00
/**
* @brief StockManager Block
* @param category
* @param name
* @return HKU_API
*/
HKU_API Block getBlock(const string& category, const string& name);
2016-04-03 00:08:31 +08:00
} /* namespace hku */
#endif /* BLOCK_H_ */