From 73a6189e9d16ab80783043e1f12ba209f7c9afe0 Mon Sep 17 00:00:00 2001 From: fasiondog Date: Sun, 25 Feb 2024 21:03:21 +0800 Subject: [PATCH] =?UTF-8?q?KData=20=E4=BC=98=E5=8C=96=20(continue)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hikyuu_cpp/hikyuu/KData.cpp | 9 ++++---- hikyuu_cpp/hikyuu/KData.h | 23 +++++++++++-------- hikyuu_cpp/hikyuu/KDataImp.h | 10 +++++--- .../hikyuu/utilities/test_Parameter.cpp | 16 +++++++++++++ 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/hikyuu_cpp/hikyuu/KData.cpp b/hikyuu_cpp/hikyuu/KData.cpp index b0c1482f..21ec15c7 100644 --- a/hikyuu_cpp/hikyuu/KData.cpp +++ b/hikyuu_cpp/hikyuu/KData.cpp @@ -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()) {} + +KData::KData(const Stock& stock, const KQuery& query) +: m_imp(make_shared(stock, query)) {} bool KData::operator==(const KData& thr) const { return this == &thr || m_imp == thr.m_imp || diff --git a/hikyuu_cpp/hikyuu/KData.h b/hikyuu_cpp/hikyuu/KData.h index 91ffabee..26ac5c8d 100644 --- a/hikyuu_cpp/hikyuu/KData.h +++ b/hikyuu_cpp/hikyuu/KData.h @@ -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(); + 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(); + return m_imp->getQuery(); } inline Stock KData::getStock() const { - return m_imp ? m_imp->getStock() : Null(); + 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 { diff --git a/hikyuu_cpp/hikyuu/KDataImp.h b/hikyuu_cpp/hikyuu/KDataImp.h index f6681c6b..2970ed70 100644 --- a/hikyuu_cpp/hikyuu/KDataImp.h +++ b/hikyuu_cpp/hikyuu/KDataImp.h @@ -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(); diff --git a/hikyuu_cpp/unit_test/hikyuu/utilities/test_Parameter.cpp b/hikyuu_cpp/unit_test/hikyuu/utilities/test_Parameter.cpp index 2bc9831d..d61fac97 100644 --- a/hikyuu_cpp/unit_test/hikyuu/utilities/test_Parameter.cpp +++ b/hikyuu_cpp/unit_test/hikyuu/utilities/test_Parameter.cpp @@ -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("k", k); + + /** @arg 验证是否可以多次读取 KData,防止移动语义影响 */ + auto ek = param.get("k"); + CHECK_EQ(ek, k); + + auto ek2 = param.get("k"); + CHECK_EQ(ek2, k); +} + #if HKU_SUPPORT_SERIALIZATION /** @par 检测点 */ TEST_CASE("test_Parameter_serialize") {