1 //===-- TypeCategoryMap.h ---------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_DATAFORMATTERS_TYPECATEGORYMAP_H
10 #define LLDB_DATAFORMATTERS_TYPECATEGORYMAP_H
11 
12 #include <functional>
13 #include <list>
14 #include <map>
15 #include <mutex>
16 
17 #include "lldb/lldb-enumerations.h"
18 #include "lldb/lldb-public.h"
19 
20 #include "lldb/DataFormatters/FormatClasses.h"
21 #include "lldb/DataFormatters/FormattersContainer.h"
22 #include "lldb/DataFormatters/TypeCategory.h"
23 
24 namespace lldb_private {
25 class TypeCategoryMap {
26 private:
27   typedef std::list<lldb::TypeCategoryImplSP> ActiveCategoriesList;
28   typedef ActiveCategoriesList::iterator ActiveCategoriesIterator;
29 
30 public:
31   typedef ConstString KeyType;
32   typedef TypeCategoryImpl ValueType;
33   typedef ValueType::SharedPointer ValueSP;
34   typedef std::map<KeyType, ValueSP> MapType;
35   typedef MapType::iterator MapIterator;
36   typedef std::function<bool(const ValueSP &)> ForEachCallback;
37 
38   typedef uint32_t Position;
39 
40   static const Position First = 0;
41   static const Position Default = 1;
42   static const Position Last = UINT32_MAX;
43 
44   TypeCategoryMap(IFormatChangeListener *lst);
45 
46   void Add(KeyType name, const ValueSP &entry);
47 
48   bool Delete(KeyType name);
49 
50   bool Enable(KeyType category_name, Position pos = Default);
51 
52   bool Disable(KeyType category_name);
53 
54   bool Enable(ValueSP category, Position pos = Default);
55 
56   bool Disable(ValueSP category);
57 
58   void EnableAllCategories();
59 
60   void DisableAllCategories();
61 
62   void Clear();
63 
64   bool Get(KeyType name, ValueSP &entry);
65 
66   bool Get(uint32_t pos, ValueSP &entry);
67 
68   void ForEach(ForEachCallback callback);
69 
70   lldb::TypeCategoryImplSP GetAtIndex(uint32_t);
71 
72   bool
73   AnyMatches(const FormattersMatchCandidate &candidate_type,
74              TypeCategoryImpl::FormatCategoryItems items =
75                  TypeCategoryImpl::ALL_ITEM_TYPES,
76              bool only_enabled = true, const char **matching_category = nullptr,
77              TypeCategoryImpl::FormatCategoryItems *matching_type = nullptr);
78 
79   uint32_t GetCount() { return m_map.size(); }
80 
81   template <typename ImplSP> void Get(FormattersMatchData &, ImplSP &);
82 
83 private:
84   class delete_matching_categories {
85     lldb::TypeCategoryImplSP ptr;
86 
87   public:
88     delete_matching_categories(lldb::TypeCategoryImplSP p)
89         : ptr(std::move(p)) {}
90 
91     bool operator()(const lldb::TypeCategoryImplSP &other) {
92       return ptr.get() == other.get();
93     }
94   };
95 
96   std::recursive_mutex m_map_mutex;
97   IFormatChangeListener *listener;
98 
99   MapType m_map;
100   ActiveCategoriesList m_active_categories;
101 
102   MapType &map() { return m_map; }
103 
104   ActiveCategoriesList &active_list() { return m_active_categories; }
105 
106   std::recursive_mutex &mutex() { return m_map_mutex; }
107 };
108 } // namespace lldb_private
109 
110 #endif // LLDB_DATAFORMATTERS_TYPECATEGORYMAP_H
111