From 2cb7daf66d328e5d1de515ddceeaa20bf37a52e6 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 22 May 2018 19:27:32 +0100 Subject: Multi-index Use Boost multi-index instead of bimap to gain access to custom compators, specific the transparent ones, so we can expose a string_view interface to them. --- slicer/slicer/modelParts.cpp | 7 +++++++ slicer/slicer/modelParts.h | 4 ++++ slicer/slicer/modelPartsTypes.cpp | 28 +++++++++++++++------------- slicer/slicer/modelPartsTypes.h | 1 - slicer/slicer/modelPartsTypes.impl.h | 26 ++++++++++++++++---------- 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/slicer/slicer/modelParts.cpp b/slicer/slicer/modelParts.cpp index 3736156..702bc93 100644 --- a/slicer/slicer/modelParts.cpp +++ b/slicer/slicer/modelParts.cpp @@ -1,5 +1,6 @@ #include "modelParts.h" #include +#include namespace Slicer { const Metadata emptyMetadata; @@ -90,5 +91,11 @@ namespace Slicer { { ch(this->name, modelPart, this); } + + bool + case_less::operator()(std::string_view && a, std::string_view && b) const + { + return boost::ilexicographical_compare(a, b); + } } diff --git a/slicer/slicer/modelParts.h b/slicer/slicer/modelParts.h index 7f5cd6a..6e13fc9 100644 --- a/slicer/slicer/modelParts.h +++ b/slicer/slicer/modelParts.h @@ -118,6 +118,10 @@ namespace Slicer { const std::string name; }; + struct DLL_PUBLIC case_less { + bool operator()(std::string_view && lhs, std::string_view && rhs) const; + }; + class DLL_PUBLIC ModelPart : public std::enable_shared_from_this { public: virtual ~ModelPart() = default; diff --git a/slicer/slicer/modelPartsTypes.cpp b/slicer/slicer/modelPartsTypes.cpp index bca297f..58e8caf 100644 --- a/slicer/slicer/modelPartsTypes.cpp +++ b/slicer/slicer/modelPartsTypes.cpp @@ -4,8 +4,13 @@ #include namespace Slicer { - typedef std::map ClassRefMap; - typedef boost::bimap ClassNameMap; + typedef std::map> ClassRefMap; + typedef boost::multi_index_container< + std::pair, + boost::multi_index::indexed_by< + boost::multi_index::ordered_unique, const std::string, &std::pair::first>, std::less<>>, + boost::multi_index::ordered_unique, const std::string, &std::pair::second>, std::less<>> + >> ClassNameMap; static void createClassMaps() __attribute__((constructor(208))); static void deleteClassMaps() __attribute__((destructor(208))); @@ -41,9 +46,10 @@ namespace Slicer { const std::string & ModelPartForComplexBase::ToModelTypeName(const std::string & name) { - auto mapped = classNameMap()->right.find(name); - if (mapped != classNameMap()->right.end()) { - return mapped->second; + auto & right = classNameMap()->get<1>(); + auto mapped = right.find(name); + if (mapped != right.end()) { + return mapped->first; } return name; } @@ -51,8 +57,9 @@ namespace Slicer { const std::string & ModelPartForComplexBase::ToExchangeTypeName(const std::string & name) { - auto mapped = classNameMap()->left.find(name); - if (mapped != classNameMap()->left.end()) { + auto & left = classNameMap()->get<0>(); + auto mapped = left.find(name); + if (mapped != left.end()) { return mapped->second; } return name; @@ -163,7 +170,7 @@ namespace Slicer { { Slicer::classRefMap()->erase(className); if (typeName) { - Slicer::classNameMap()->left.erase(className); + classNameMap()->get<0>().erase(className); } } ModelPartPtr ModelPartForComplexBase::getSubclassModelPart(const std::string & name, void * m) @@ -185,11 +192,6 @@ namespace Slicer { return "::" + std::string(buf.get()); } - std::string ModelPartForComplexBase::hookNameLower(const HookCommon & h) - { - return boost::algorithm::to_lower_copy(h.name); - } - void ModelPartForOptionalBase::OnEachChild(const ChildHandler & ch) { if (this->hasModel()) { diff --git a/slicer/slicer/modelPartsTypes.h b/slicer/slicer/modelPartsTypes.h index ee5a083..e105f08 100644 --- a/slicer/slicer/modelPartsTypes.h +++ b/slicer/slicer/modelPartsTypes.h @@ -131,7 +131,6 @@ namespace Slicer { static void unregisterClass(const std::string & className, const std::string * typeName); static TypeId GetTypeId(const std::string & id, const std::string & className); static std::string demangle(const char * mangled); - static std::string hookNameLower(const HookCommon &); static const std::string & ToExchangeTypeName(const std::string &); static const std::string & ToModelTypeName(const std::string &); diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h index f8e2ba6..79e8645 100644 --- a/slicer/slicer/modelPartsTypes.impl.h +++ b/slicer/slicer/modelPartsTypes.impl.h @@ -11,7 +11,6 @@ #include #include #include -#include #define CUSTOMMODELPARTFOR(Type, BaseModelPart, ModelPartType) \ template<> ModelPartPtr ModelPart::CreateFor() { return std::make_shared(nullptr); } \ @@ -323,8 +322,8 @@ namespace Slicer { HookPtr, boost::multi_index::indexed_by< boost::multi_index::sequenced<>, - boost::multi_index::ordered_non_unique>, - boost::multi_index::ordered_non_unique>>> { + boost::multi_index::ordered_non_unique, std::less<>>, + boost::multi_index::ordered_non_unique, case_less>>> { }; template @@ -543,7 +542,12 @@ namespace Slicer { // ModelPartForEnum template - class ModelPartForEnum::Enumerations : public boost::bimap { + class ModelPartForEnum::Enumerations : public boost::multi_index_container< + std::pair, + boost::multi_index::indexed_by< + boost::multi_index::ordered_unique, const T, &std::pair::first>>, + boost::multi_index::ordered_unique, const std::string, &std::pair::second>, std::less<>> + >> { }; template @@ -561,18 +565,20 @@ namespace Slicer { template T ModelPartForEnum::lookup(const std::string & val) { - auto i = enumerations.right.find(val); - if (i == enumerations.right.end()) { - throw InvalidEnumerationSymbol(val, typeid(T).name()); + auto & right = enumerations.template get<1>(); + auto i = right.find(val); + if (i == right.end()) { + throw InvalidEnumerationSymbol(std::string(val), typeid(T).name()); } - return i->second; + return i->first; } template const std::string & ModelPartForEnum::lookup(T val) { - auto i = enumerations.left.find(val); - if (i == enumerations.left.end()) { + auto & left = enumerations.template get<0>(); + auto i = left.find(val); + if (i == left.end()) { throw InvalidEnumerationValue((::Ice::Int)val, typeid(T).name()); } return i->second; -- cgit v1.2.3