1 //===-- TypeCategoryMap.h ---------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef lldb_TypeCategoryMap_h_
11 #define lldb_TypeCategoryMap_h_
12 
13 #include <functional>
14 #include <list>
15 #include <map>
16 #include <mutex>
17 
18 #include "lldb/lldb-enumerations.h"
19 #include "lldb/lldb-public.h"
20 
21 #include "lldb/DataFormatters/FormattersContainer.h"
22 #include "lldb/DataFormatters/TypeCategory.h"
23 
24 namespace lldb_private {
25 class TypeCategoryMap {
26 private:
27   typedef ConstString KeyType;
28   typedef TypeCategoryImpl ValueType;
29   typedef ValueType::SharedPointer ValueSP;
30   typedef std::list<lldb::TypeCategoryImplSP> ActiveCategoriesList;
31   typedef ActiveCategoriesList::iterator ActiveCategoriesIterator;
32 
33 public:
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(ConstString type_name,
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 
GetCount()79   uint32_t GetCount() { return m_map.size(); }
80 
81   lldb::TypeFormatImplSP GetFormat(FormattersMatchData &match_data);
82 
83   lldb::TypeSummaryImplSP GetSummaryFormat(FormattersMatchData &match_data);
84 
85 #ifndef LLDB_DISABLE_PYTHON
86   lldb::SyntheticChildrenSP
87   GetSyntheticChildren(FormattersMatchData &match_data);
88 #endif
89 
90   lldb::TypeValidatorImplSP GetValidator(FormattersMatchData &match_data);
91 
92 private:
93   class delete_matching_categories {
94     lldb::TypeCategoryImplSP ptr;
95 
96   public:
delete_matching_categories(lldb::TypeCategoryImplSP p)97     delete_matching_categories(lldb::TypeCategoryImplSP p) : ptr(p) {}
98 
operator()99     bool operator()(const lldb::TypeCategoryImplSP &other) {
100       return ptr.get() == other.get();
101     }
102   };
103 
104   std::recursive_mutex m_map_mutex;
105   IFormatChangeListener *listener;
106 
107   MapType m_map;
108   ActiveCategoriesList m_active_categories;
109 
map()110   MapType &map() { return m_map; }
111 
active_list()112   ActiveCategoriesList &active_list() { return m_active_categories; }
113 
mutex()114   std::recursive_mutex &mutex() { return m_map_mutex; }
115 
116   friend class FormattersContainer<KeyType, ValueType>;
117   friend class FormatManager;
118 };
119 } // namespace lldb_private
120 
121 #endif // lldb_TypeCategoryMap_h_
122