diff --git a/lldb/include/lldb/DataFormatters/FormatManager.h b/lldb/include/lldb/DataFormatters/FormatManager.h index 98c5b132c20321..978ad148d6c49d 100644 --- a/lldb/include/lldb/DataFormatters/FormatManager.h +++ b/lldb/include/lldb/DataFormatters/FormatManager.h @@ -34,7 +34,7 @@ namespace lldb_private { // this file's objects directly class FormatManager : public IFormatChangeListener { - typedef FormatMap NamedSummariesMap; + typedef FormattersContainer NamedSummariesMap; typedef TypeCategoryMap::MapType::iterator CategoryMapIterator; public: diff --git a/lldb/include/lldb/DataFormatters/FormattersContainer.h b/lldb/include/lldb/DataFormatters/FormattersContainer.h index d7c531e9618aaf..a30ad920ef45a8 100644 --- a/lldb/include/lldb/DataFormatters/FormattersContainer.h +++ b/lldb/include/lldb/DataFormatters/FormattersContainer.h @@ -104,18 +104,18 @@ class TypeMatcher { } }; -template class FormattersContainer; - -template class FormatMap { +template class FormattersContainer { public: - typedef typename ValueType::SharedPointer ValueSP; + typedef typename std::shared_ptr ValueSP; typedef std::vector> MapType; - typedef typename MapType::iterator MapIterator; typedef std::function ForEachCallback; + typedef typename std::shared_ptr> + SharedPointer; + + friend class TypeCategoryImpl; - FormatMap(IFormatChangeListener *lst) - : m_map(), m_map_mutex(), listener(lst) {} + FormattersContainer(IFormatChangeListener *lst) : listener(lst) {} void Add(TypeMatcher matcher, const ValueSP &entry) { if (listener) @@ -130,9 +130,9 @@ template class FormatMap { listener->Changed(); } - bool Delete(const TypeMatcher &matcher) { + bool Delete(TypeMatcher matcher) { std::lock_guard guard(m_map_mutex); - for (MapIterator iter = m_map.begin(); iter != m_map.end(); ++iter) + for (auto iter = m_map.begin(); iter != m_map.end(); ++iter) if (iter->first.CreatedBySameMatchString(matcher)) { m_map.erase(iter); if (listener) @@ -142,14 +142,18 @@ template class FormatMap { return false; } - void Clear() { + bool Get(ConstString type, ValueSP &entry) { std::lock_guard guard(m_map_mutex); - m_map.clear(); - if (listener) - listener->Changed(); + for (auto &formatter : llvm::reverse(m_map)) { + if (formatter.first.Matches(type)) { + entry = formatter.second; + return true; + } + } + return false; } - bool Get(const TypeMatcher &matcher, ValueSP &entry) { + bool GetExact(TypeMatcher matcher, ValueSP &entry) { std::lock_guard guard(m_map_mutex); for (const auto &pos : m_map) if (pos.first.CreatedBySameMatchString(matcher)) { @@ -159,108 +163,50 @@ template class FormatMap { return false; } - void ForEach(ForEachCallback callback) { - if (callback) { - std::lock_guard guard(m_map_mutex); - for (const auto &pos : m_map) { - const TypeMatcher &type = pos.first; - if (!callback(type, pos.second)) - break; - } - } - } - - uint32_t GetCount() { return m_map.size(); } - - ValueSP GetValueAtIndex(size_t index) { + ValueSP GetAtIndex(size_t index) { std::lock_guard guard(m_map_mutex); if (index >= m_map.size()) return ValueSP(); return m_map[index].second; } - // If caller holds the mutex we could return a reference without copy ctor. - llvm::Optional GetKeyAtIndex(size_t index) { + lldb::TypeNameSpecifierImplSP GetTypeNameSpecifierAtIndex(size_t index) { std::lock_guard guard(m_map_mutex); if (index >= m_map.size()) - return llvm::None; - return m_map[index].first; + return lldb::TypeNameSpecifierImplSP(); + TypeMatcher type_matcher = m_map[index].first; + return std::make_shared( + type_matcher.GetMatchString().GetStringRef(), true); } -protected: - MapType m_map; - std::recursive_mutex m_map_mutex; - IFormatChangeListener *listener; - - MapType &map() { return m_map; } - - std::recursive_mutex &mutex() { return m_map_mutex; } - - friend class FormattersContainer; - friend class FormatManager; -}; - -template class FormattersContainer { -protected: - typedef FormatMap BackEndType; - -public: - typedef std::shared_ptr MapValueType; - typedef typename BackEndType::ForEachCallback ForEachCallback; - typedef typename std::shared_ptr> - SharedPointer; - - friend class TypeCategoryImpl; - - FormattersContainer(IFormatChangeListener *lst) : m_format_map(lst) {} - - void Add(TypeMatcher type, const MapValueType &entry) { - m_format_map.Add(std::move(type), entry); + void Clear() { + std::lock_guard guard(m_map_mutex); + m_map.clear(); + if (listener) + listener->Changed(); } - bool Delete(TypeMatcher type) { return m_format_map.Delete(type); } - - bool Get(ConstString type, MapValueType &entry) { - std::lock_guard guard(m_format_map.mutex()); - for (auto &formatter : llvm::reverse(m_format_map.map())) { - if (formatter.first.Matches(type)) { - entry = formatter.second; - return true; + void ForEach(ForEachCallback callback) { + if (callback) { + std::lock_guard guard(m_map_mutex); + for (const auto &pos : m_map) { + const TypeMatcher &type = pos.first; + if (!callback(type, pos.second)) + break; } } - return false; } - bool GetExact(ConstString type, MapValueType &entry) { - return m_format_map.Get(type, entry); - } - - MapValueType GetAtIndex(size_t index) { - return m_format_map.GetValueAtIndex(index); - } - - lldb::TypeNameSpecifierImplSP GetTypeNameSpecifierAtIndex(size_t index) { - llvm::Optional type_matcher = - m_format_map.GetKeyAtIndex(index); - if (!type_matcher) - return lldb::TypeNameSpecifierImplSP(); - return lldb::TypeNameSpecifierImplSP(new TypeNameSpecifierImpl( - type_matcher->GetMatchString().GetStringRef(), true)); + uint32_t GetCount() { + std::lock_guard guard(m_map_mutex); + return m_map.size(); } - void Clear() { m_format_map.Clear(); } - - void ForEach(ForEachCallback callback) { m_format_map.ForEach(callback); } - - uint32_t GetCount() { return m_format_map.GetCount(); } - protected: - BackEndType m_format_map; - FormattersContainer(const FormattersContainer &) = delete; const FormattersContainer &operator=(const FormattersContainer &) = delete; - bool Get(const FormattersMatchVector &candidates, MapValueType &entry) { + bool Get(const FormattersMatchVector &candidates, ValueSP &entry) { for (const FormattersMatchCandidate &candidate : candidates) { if (Get(candidate.GetTypeName(), entry)) { if (candidate.IsMatch(entry) == false) { @@ -273,6 +219,10 @@ template class FormattersContainer { } return false; } + + MapType m_map; + std::recursive_mutex m_map_mutex; + IFormatChangeListener *listener; }; } // namespace lldb_private diff --git a/lldb/include/lldb/DataFormatters/TypeCategory.h b/lldb/include/lldb/DataFormatters/TypeCategory.h index 4c8a7e14be1296..2662ffc5aeac6d 100644 --- a/lldb/include/lldb/DataFormatters/TypeCategory.h +++ b/lldb/include/lldb/DataFormatters/TypeCategory.h @@ -31,7 +31,7 @@ template class FormatterContainerPair { typedef TypeMatcher ExactMatchMap; typedef TypeMatcher RegexMatchMap; - typedef typename ExactMatchContainer::MapValueType MapValueType; + typedef typename ExactMatchContainer::ValueSP MapValueType; typedef typename ExactMatchContainer::SharedPointer ExactMatchContainerSP; typedef typename RegexMatchContainer::SharedPointer RegexMatchContainerSP; diff --git a/lldb/source/DataFormatters/DataVisualization.cpp b/lldb/source/DataFormatters/DataVisualization.cpp index 82248bb64285d3..ded8bbd90391af 100644 --- a/lldb/source/DataFormatters/DataVisualization.cpp +++ b/lldb/source/DataFormatters/DataVisualization.cpp @@ -169,7 +169,7 @@ DataVisualization::Categories::GetCategoryAtIndex(size_t index) { bool DataVisualization::NamedSummaryFormats::GetSummaryFormat( ConstString type, lldb::TypeSummaryImplSP &entry) { - return GetFormatManager().GetNamedSummaryContainer().Get(type, entry); + return GetFormatManager().GetNamedSummaryContainer().GetExact(type, entry); } void DataVisualization::NamedSummaryFormats::Add(