From 92db72fb387c7bb1f671f128b15d91d4168aedc9 Mon Sep 17 00:00:00 2001 From: Hammie Date: Sun, 14 Apr 2024 13:34:23 +0200 Subject: [PATCH] Simplify attributes --- src/libs/core/include/attributes.h | 9 ++---- src/libs/core/src/attributes.cpp | 39 +++++++++++------------- src/libs/core/src/internal_functions.cpp | 7 ++--- src/libs/core/src/string_codec.h | 25 ++++++++------- 4 files changed, 37 insertions(+), 43 deletions(-) diff --git a/src/libs/core/include/attributes.h b/src/libs/core/include/attributes.h index 0c4ec8795..592cf2302 100644 --- a/src/libs/core/include/attributes.h +++ b/src/libs/core/include/attributes.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -28,14 +29,9 @@ bool MatchAttributePath(const std::string_view &pattern, const ATTRIBUTES &attri class ATTRIBUTES final { - // TODO: remove with another iteration of rewriting this - friend class COMPILER; - class LegacyProxy; public: - [[deprecated("Pass StringCodec by reference instead")]] explicit ATTRIBUTES(VSTRING_CODEC *p); - explicit ATTRIBUTES(VSTRING_CODEC &p); ATTRIBUTES(const ATTRIBUTES &) = delete; ATTRIBUTES(ATTRIBUTES &&other) noexcept; @@ -86,11 +82,12 @@ class ATTRIBUTES final void SetNameCode(uint32_t n) noexcept; [[nodiscard]] VSTRING_CODEC &GetStringCodec() const noexcept; + void Sort(const std::function &lhs, const std::unique_ptr &rhs)>& pred); + private: ATTRIBUTES(VSTRING_CODEC &string_codec, ATTRIBUTES *parent, const std::string_view &name); ATTRIBUTES(VSTRING_CODEC &string_codec, ATTRIBUTES *parent, uint32_t name_code); - void Release() const noexcept; ATTRIBUTES *CreateNewAttribute(uint32_t name_code); VSTRING_CODEC &stringCodec_; diff --git a/src/libs/core/src/attributes.cpp b/src/libs/core/src/attributes.cpp index a4c517750..8ee08c21d 100644 --- a/src/libs/core/src/attributes.cpp +++ b/src/libs/core/src/attributes.cpp @@ -1,11 +1,8 @@ #include "attributes.h" -#include "string_compare.hpp" -#include "platform/platform.hpp" +#include -ATTRIBUTES::ATTRIBUTES(VSTRING_CODEC *p): ATTRIBUTES(*p) -{ -} +#include "string_compare.hpp" ATTRIBUTES::ATTRIBUTES(ATTRIBUTES &&other) noexcept : stringCodec_(other.stringCodec_), nameCode_(other.stringCodec_.Convert("root")), value_(std::move(other.value_)), @@ -18,17 +15,20 @@ ATTRIBUTES & ATTRIBUTES::operator=(ATTRIBUTES &&other) noexcept stringCodec_ = other.stringCodec_; // Do not update name code // nameCode_ = other.nameCode_; + other.nameCode_ = 1337; value_ = std::move(other.value_); attributes_ = std::move(other.attributes_); // Do not update parent // parent_ = other.parent_; + other.parent_ = (ATTRIBUTES*)0x1; break_ = other.break_; return *this; } ATTRIBUTES::~ATTRIBUTES() { - Release(); + if (break_) + stringCodec_.VariableChanged(); } void ATTRIBUTES::SetBreak(bool set_break) @@ -265,19 +265,14 @@ bool ATTRIBUTES::DeleteAttributeClassX(ATTRIBUTES *pA) } else { - // auto removed_it = std::remove_if(pAttributes.begin(), pAttributes.end(), [&](const auto& - // item){ - // return item.get() == pA; - // }); - // return removed_it != pAttributes.end(); for (uint32_t n = 0; n < attributes_.size(); n++) { - if (attributes_[n].get() == pA) + auto it = std::remove_if(attributes_.begin(), attributes_.end(), [&pA](const auto &attr) { + return attr.get() == pA; + }); + if (it != attributes_.end() ) { - for (auto i = n; i < attributes_.size() - 1; i++) - attributes_[i] = std::move(attributes_[i + 1]); - - attributes_.pop_back(); + attributes_.erase(it); return true; } if (attributes_[n]->DeleteAttributeClassX(pA)) @@ -446,6 +441,12 @@ VSTRING_CODEC & ATTRIBUTES::GetStringCodec() const noexcept return stringCodec_; } +void ATTRIBUTES::Sort( + const std::function &, const std::unique_ptr &)>& pred) +{ + std::sort(std::execution::seq, std::begin(attributes_), std::end(attributes_), pred); +} + ATTRIBUTES * ATTRIBUTES::CreateNewAttribute(uint32_t name_code) { const std::unique_ptr &attr = attributes_.emplace_back(new ATTRIBUTES(stringCodec_, this, name_code)); @@ -480,12 +481,6 @@ ATTRIBUTES ATTRIBUTES::Copy() const return result; } -void ATTRIBUTES::Release() const noexcept -{ - if (break_) - stringCodec_.VariableChanged(); -} - // MatchAttributePath("equipment.*.locator", attribute) bool MatchAttributePath(const std::string_view &pattern, const ATTRIBUTES &attribute) { diff --git a/src/libs/core/src/internal_functions.cpp b/src/libs/core/src/internal_functions.cpp index 9bb37656a..2c0cad9d9 100644 --- a/src/libs/core/src/internal_functions.cpp +++ b/src/libs/core/src/internal_functions.cpp @@ -2569,10 +2569,9 @@ DATA *COMPILER::BC_CallIntFunction(uint32_t func_code, DATA *&pVResult, uint32_t } pA = pV->GetAClass(); - std::sort(std::execution::seq, std::begin(pA->attributes_), std::end(pA->attributes_), - [](const std::unique_ptr &lhs, const std::unique_ptr &rhs) { - return strcmp(lhs->GetThisName(), rhs->GetThisName()) < 0; - }); + pA->Sort([](const std::unique_ptr &lhs, const std::unique_ptr &rhs) { + return strcmp(lhs->GetThisName(), rhs->GetThisName()) < 0; + }); break; } diff --git a/src/libs/core/src/string_codec.h b/src/libs/core/src/string_codec.h index a2892f7ef..dd23a99c5 100644 --- a/src/libs/core/src/string_codec.h +++ b/src/libs/core/src/string_codec.h @@ -6,29 +6,26 @@ struct HTSUBELEMENT { - char *pStr; - uint32_t dwHashCode; + char *pStr = nullptr; + uint32_t dwHashCode{}; }; struct HTELEMENT { - HTSUBELEMENT *pElements; - uint32_t nStringsNum; + HTSUBELEMENT *pElements = nullptr; + uint32_t nStringsNum{}; }; class STRING_CODEC : public VSTRING_CODEC { - uint32_t nHTIndex; - uint32_t nHTEIndex; - uint32_t nStringsNum; + uint32_t nHTIndex{}; + uint32_t nHTEIndex{}; + uint32_t nStringsNum{}; HTELEMENT HTable[HASH_TABLE_SIZE]{}; public: - STRING_CODEC() : nHTIndex(0), nHTEIndex(0) - { - nStringsNum = 0; - } + STRING_CODEC() = default; ~STRING_CODEC() override { @@ -132,6 +129,12 @@ class STRING_CODEC : public VSTRING_CODEC { return "INVALID SCC"; } + if (HTable[nTableIndex].pElements == nullptr) { + return "INVALID SC ELEMENTS"; + } + if (HTable[nTableIndex].pElements[n].pStr == nullptr) { + return "INVALID SC VALUE"; + } return HTable[nTableIndex].pElements[n].pStr; }