From 7d8b076f71760a5910f74c77ee96370eb111a8a7 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 26 Aug 2023 21:28:40 +0100 Subject: Support case-insensitive OnChild with pre-lowered input --- slicer/db/sqlSelectDeserializer.cpp | 2 +- slicer/slicer/modelParts.cpp | 2 +- slicer/slicer/modelParts.h | 7 +++++-- slicer/slicer/modelPartsTypes.cpp | 10 +++++----- slicer/slicer/modelPartsTypes.h | 14 ++++++++------ slicer/slicer/modelPartsTypes.impl.h | 30 +++++++++++++++++++----------- 6 files changed, 39 insertions(+), 26 deletions(-) diff --git a/slicer/db/sqlSelectDeserializer.cpp b/slicer/db/sqlSelectDeserializer.cpp index 8081d76..e372716 100644 --- a/slicer/db/sqlSelectDeserializer.cpp +++ b/slicer/db/sqlSelectDeserializer.cpp @@ -144,7 +144,7 @@ namespace Slicer { [&c](auto && fmp, auto &&) { assignFromColumn(fmp, c); }, - c.name, nullptr, false); + c.name, nullptr, MatchCase::No); } } } diff --git a/slicer/slicer/modelParts.cpp b/slicer/slicer/modelParts.cpp index 07b4095..2b2a5ef 100644 --- a/slicer/slicer/modelParts.cpp +++ b/slicer/slicer/modelParts.cpp @@ -64,7 +64,7 @@ namespace Slicer { } bool - ModelPart::OnChild(const SubPartHandler &, const std::string_view, const HookFilter &, bool) + ModelPart::OnChild(const SubPartHandler &, const std::string_view, const HookFilter &, MatchCase) { return false; } diff --git a/slicer/slicer/modelParts.h b/slicer/slicer/modelParts.h index 8adc8f2..f86bc92 100644 --- a/slicer/slicer/modelParts.h +++ b/slicer/slicer/modelParts.h @@ -104,6 +104,8 @@ namespace Slicer { Value, }; + enum class MatchCase { Yes, No, No_Prelowered }; + class DLL_PUBLIC HookCommon { public: constexpr HookCommon(std::string_view n, std::string_view nl, const std::string * ns) : @@ -145,7 +147,7 @@ namespace Slicer { virtual void OnEachChild(const ChildHandler &); virtual bool OnAnonChild(const SubPartHandler &, const HookFilter & = HookFilter()); virtual bool OnChild(const SubPartHandler &, std::string_view memberName, const HookFilter & = HookFilter(), - bool matchCase = true); + MatchCase matchCase = MatchCase::Yes); virtual void OnSubclass(const ModelPartHandler &, const std::string &); virtual TypeId GetTypeId() const; virtual std::optional GetTypeIdProperty() const; @@ -173,7 +175,8 @@ namespace Slicer { virtual const std::string & GetRootName() const = 0; bool OnAnonChild(const SubPartHandler &, const HookFilter &) override; - bool OnChild(const SubPartHandler &, std::string_view name, const HookFilter &, bool matchCase = true) override; + bool OnChild(const SubPartHandler &, std::string_view name, const HookFilter &, + MatchCase matchCase = MatchCase::Yes) override; void OnEachChild(const ChildHandler & ch) override; ModelPartType GetType() const override; bool IsOptional() const override; diff --git a/slicer/slicer/modelPartsTypes.cpp b/slicer/slicer/modelPartsTypes.cpp index 673265e..df281b2 100644 --- a/slicer/slicer/modelPartsTypes.cpp +++ b/slicer/slicer/modelPartsTypes.cpp @@ -120,9 +120,9 @@ namespace Slicer { bool ModelPartForRootBase::OnChild( - const SubPartHandler & h, std::string_view name, const HookFilter & hf, bool matchCase) + const SubPartHandler & h, std::string_view name, const HookFilter & hf, MatchCase matchCase) { - if (!optionalCaseEq(name, GetRootName(), matchCase)) { + if (!optionalCaseEq(name, GetRootName(), matchCase == MatchCase::Yes)) { throw IncorrectElementName(std::string {name}); } return OnAnonChild(h, hf); @@ -263,7 +263,7 @@ namespace Slicer { bool ModelPartForOptionalBase::OnChild( - const SubPartHandler & h, std::string_view name, const HookFilter & flt, bool matchCase) + const SubPartHandler & h, std::string_view name, const HookFilter & flt, MatchCase matchCase) { if (this->hasModel()) { return modelPart->OnChild(h, name, flt, matchCase); @@ -325,9 +325,9 @@ namespace Slicer { bool ModelPartForSequenceBase::OnChild( - const SubPartHandler & h, std::string_view name, const HookFilter & flt, bool matchCase) + const SubPartHandler & h, std::string_view name, const HookFilter & flt, MatchCase matchCase) { - if (!name.empty() && !optionalCaseEq(name, GetElementName(), matchCase)) { + if (!name.empty() && !optionalCaseEq(name, GetElementName(), matchCase == MatchCase::Yes)) { throw IncorrectElementName(std::string {name}); } return OnAnonChild(h, flt); diff --git a/slicer/slicer/modelPartsTypes.h b/slicer/slicer/modelPartsTypes.h index 2079195..8a49479 100644 --- a/slicer/slicer/modelPartsTypes.h +++ b/slicer/slicer/modelPartsTypes.h @@ -104,8 +104,8 @@ namespace Slicer { void OnEachChild(const ChildHandler & ch) override; void Complete() override; bool OnAnonChild(const SubPartHandler &, const HookFilter & flt) override; - bool OnChild( - const SubPartHandler &, std::string_view name, const HookFilter & flt, bool matchCase = true) override; + bool OnChild(const SubPartHandler &, std::string_view name, const HookFilter & flt, + MatchCase matchCase = MatchCase::Yes) override; void SetValue(ValueSource && s) override; bool HasValue() const override; bool IsOptional() const override; @@ -160,8 +160,8 @@ namespace Slicer { void OnEachChild(const ChildHandler & ch) override; bool OnAnonChild(const SubPartHandler &, const HookFilter & flt) override; - bool OnChild( - const SubPartHandler &, std::string_view name, const HookFilter & flt, bool matchCase = true) override; + bool OnChild(const SubPartHandler &, std::string_view name, const HookFilter & flt, + MatchCase matchCase = MatchCase::Yes) override; const Metadata & GetMetadata() const override; @@ -246,7 +246,8 @@ namespace Slicer { public: bool HasValue() const override; ModelPartType GetType() const override; - bool OnChild(const SubPartHandler &, std::string_view, const HookFilter &, bool matchCase = true) override; + bool OnChild(const SubPartHandler &, std::string_view, const HookFilter &, + MatchCase matchCase = MatchCase::Yes) override; virtual const std::string & GetElementName() const = 0; static const ModelPartType type; @@ -302,7 +303,8 @@ namespace Slicer { bool OnAnonChild(const SubPartHandler &, const HookFilter &) override; - bool OnChild(const SubPartHandler &, std::string_view name, const HookFilter &, bool matchCase = true) override; + bool OnChild(const SubPartHandler &, std::string_view name, const HookFilter &, + MatchCase matchCase = MatchCase::Yes) override; const Metadata & GetMetadata() const override; diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h index ad1a90f..c5651b6 100644 --- a/slicer/slicer/modelPartsTypes.impl.h +++ b/slicer/slicer/modelPartsTypes.impl.h @@ -388,16 +388,24 @@ namespace Slicer { template bool ModelPartForComplex::OnChild( - const SubPartHandler & ch, std::string_view name, const HookFilter & flt, bool matchCase) - { - if (matchCase) { - return OnChildFromRange(ch, hooks().equal_range(name), flt); - } - else { - std::string i {name}; - to_lower(i); - return OnChildFromRange(ch, hooks().equal_range_lower(i), flt); + const SubPartHandler & ch, std::string_view name, const HookFilter & flt, MatchCase matchCase) + { + switch (matchCase) { + case MatchCase::Yes: + return OnChildFromRange(ch, hooks().equal_range(name), flt); + case MatchCase::No: { + std::string i {name}; + to_lower(i); + return OnChildFromRange(ch, hooks().equal_range_lower(i), flt); + } + case MatchCase::No_Prelowered: + return OnChildFromRange(ch, hooks().equal_range_lower(name), flt); } +#ifdef __cpp_lib_unreachable + std::unreachable(); +#else + __builtin_unreachable(); +#endif } template class DLL_PRIVATE ModelPartForComplex::HookBase : public HookCommon { @@ -684,10 +692,10 @@ namespace Slicer { template bool ModelPartForDictionary::OnChild( - const SubPartHandler & ch, std::string_view name, const HookFilter &, bool matchCase) + const SubPartHandler & ch, std::string_view name, const HookFilter &, MatchCase matchCase) { BOOST_ASSERT(this->Model); - if (!optionalCaseEq(name, pairName, matchCase)) { + if (!optionalCaseEq(name, pairName, matchCase == MatchCase::Yes)) { throw IncorrectElementName(std::string {name}); } ch(ModelPartForDictionaryElementInserter(this->Model), emptyMetadata); -- cgit v1.2.3