1 //===-- FormatCache.cpp ---------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
12 #include "lldb/DataFormatters/FormatCache.h"
15 using namespace lldb_private
;
17 FormatCache::Entry::Entry()
18 : m_format_cached(false), m_summary_cached(false),
19 m_synthetic_cached(false) {}
21 bool FormatCache::Entry::IsFormatCached() { return m_format_cached
; }
23 bool FormatCache::Entry::IsSummaryCached() { return m_summary_cached
; }
25 bool FormatCache::Entry::IsSyntheticCached() { return m_synthetic_cached
; }
27 void FormatCache::Entry::Get(lldb::TypeFormatImplSP
&retval
) {
31 void FormatCache::Entry::Get(lldb::TypeSummaryImplSP
&retval
) {
32 retval
= m_summary_sp
;
35 void FormatCache::Entry::Get(lldb::SyntheticChildrenSP
&retval
) {
36 retval
= m_synthetic_sp
;
39 void FormatCache::Entry::Set(lldb::TypeFormatImplSP format_sp
) {
40 m_format_cached
= true;
41 m_format_sp
= format_sp
;
44 void FormatCache::Entry::Set(lldb::TypeSummaryImplSP summary_sp
) {
45 m_summary_cached
= true;
46 m_summary_sp
= summary_sp
;
49 void FormatCache::Entry::Set(lldb::SyntheticChildrenSP synthetic_sp
) {
50 m_synthetic_cached
= true;
51 m_synthetic_sp
= synthetic_sp
;
54 FormatCache::Entry
&FormatCache::GetEntry(ConstString type
) {
55 auto i
= m_map
.find(type
), e
= m_map
.end();
58 m_map
[type
] = FormatCache::Entry();
62 namespace lldb_private
{
64 template<> bool FormatCache::Entry::IsCached
<lldb::TypeFormatImplSP
>() {
65 return IsFormatCached();
67 template<> bool FormatCache::Entry::IsCached
<lldb::TypeSummaryImplSP
> () {
68 return IsSummaryCached();
70 template<> bool FormatCache::Entry::IsCached
<lldb::SyntheticChildrenSP
>() {
71 return IsSyntheticCached();
74 } // namespace lldb_private
76 template <typename ImplSP
>
77 bool FormatCache::Get(ConstString type
, ImplSP
&format_impl_sp
) {
78 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);
79 auto entry
= GetEntry(type
);
80 if (entry
.IsCached
<ImplSP
>()) {
82 entry
.Get(format_impl_sp
);
86 format_impl_sp
.reset();
90 /// Explicit instantiations for the three types.
93 FormatCache::Get
<lldb::TypeFormatImplSP
>(ConstString
, lldb::TypeFormatImplSP
&);
95 FormatCache::Get
<lldb::TypeSummaryImplSP
>(ConstString
,
96 lldb::TypeSummaryImplSP
&);
98 FormatCache::Get
<lldb::SyntheticChildrenSP
>(ConstString
,
99 lldb::SyntheticChildrenSP
&);
102 void FormatCache::Set(ConstString type
, lldb::TypeFormatImplSP
&format_sp
) {
103 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);
104 GetEntry(type
).Set(format_sp
);
107 void FormatCache::Set(ConstString type
, lldb::TypeSummaryImplSP
&summary_sp
) {
108 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);
109 GetEntry(type
).Set(summary_sp
);
112 void FormatCache::Set(ConstString type
,
113 lldb::SyntheticChildrenSP
&synthetic_sp
) {
114 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);
115 GetEntry(type
).Set(synthetic_sp
);
118 void FormatCache::Clear() {
119 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);