KData 优化 (continue)

This commit is contained in:
fasiondog 2024-02-25 21:03:21 +08:00
parent 70aa0ad3aa
commit 73a6189e9d
4 changed files with 40 additions and 18 deletions

View File

@ -26,11 +26,10 @@ string KData::toString() const {
return os.str();
}
KData::KData(const Stock& stock, const KQuery& query) {
if (!stock.isNull()) {
m_imp = KDataImpPtr(new KDataImp(stock, query));
}
}
KData::KData() : m_imp(make_shared<KDataImp>()) {}
KData::KData(const Stock& stock, const KQuery& query)
: m_imp(make_shared<KDataImp>(stock, query)) {}
bool KData::operator==(const KData& thr) const {
return this == &thr || m_imp == thr.m_imp ||

View File

@ -21,13 +21,16 @@ class HKU_API Indicator;
*/
class HKU_API KData {
public:
KData() {}
KData();
KData(const KData&);
KData(KData&&);
KData(const Stock& stock, const KQuery& query);
virtual ~KData() {}
KData& operator=(const KData&);
// 移动语义对 KData 没有实际用处,而且会导致 KData 可能存在空 imp 的情况
// 主要是 boost::any_cast 需要,予以保留,但使用时不要到 KData 执行 std::move
KData(KData&&);
KData& operator=(KData&&);
size_t size() const;
@ -188,35 +191,35 @@ inline KRecord KData::getKRecord(Datetime datetime) const {
}
inline size_t KData::getPos(const Datetime& datetime) const {
return m_imp ? m_imp->getPos(datetime) : Null<size_t>();
return m_imp->getPos(datetime);
}
inline size_t KData::size() const {
return m_imp ? m_imp->size() : 0;
return m_imp->size();
}
inline bool KData::empty() const {
return m_imp ? m_imp->empty() : true;
return m_imp->empty();
}
inline KQuery KData::getQuery() const {
return m_imp ? m_imp->getQuery() : Null<KQuery>();
return m_imp->getQuery();
}
inline Stock KData::getStock() const {
return m_imp ? m_imp->getStock() : Null<Stock>();
return m_imp->getStock();
}
inline size_t KData::startPos() const {
return m_imp ? m_imp->startPos() : 0;
return m_imp->startPos();
}
inline size_t KData::endPos() const {
return m_imp ? m_imp->endPos() : 0;
return m_imp->endPos();
}
inline size_t KData::lastPos() const {
return m_imp ? m_imp->lastPos() : 0;
return m_imp->lastPos();
}
inline bool KData::operator!=(const KData& other) const {

View File

@ -19,15 +19,15 @@ public:
KDataImp(const Stock& stock, const KQuery& query);
virtual ~KDataImp();
KQuery getQuery() const {
const KQuery& getQuery() const {
return m_query;
}
Stock getStock() const {
const Stock& getStock() const {
return m_stock;
}
KRecord getKRecord(size_t pos) const {
const KRecord& getKRecord(size_t pos) const {
return m_buffer[pos];
}
@ -45,6 +45,10 @@ public:
size_t getPos(const Datetime& datetime);
const KRecord* data() const {
return m_buffer.data();
}
private:
void _getPosInStock();
void _recoverForward();

View File

@ -144,6 +144,22 @@ TEST_CASE("test_Parameter") {
}
}
/** @par 验证对 KData 的获取 */
TEST_CASE("test_Parameter_KData") {
KData k = getKData("sh000001", KQuery(-10));
CHECK_EQ(k.size(), 10);
Parameter param;
param.set<KData>("k", k);
/** @arg 验证是否可以多次读取 KData防止移动语义影响 */
auto ek = param.get<KData>("k");
CHECK_EQ(ek, k);
auto ek2 = param.get<KData>("k");
CHECK_EQ(ek2, k);
}
#if HKU_SUPPORT_SERIALIZATION
/** @par 检测点 */
TEST_CASE("test_Parameter_serialize") {