1 //===-- SBTypeFormat.cpp ------------------------------------------*- C++
2 //-*-===//
3 //
4 // The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #include "lldb/API/SBTypeFormat.h"
12
13 #include "lldb/API/SBStream.h"
14
15 #include "lldb/DataFormatters/DataVisualization.h"
16
17 using namespace lldb;
18 using namespace lldb_private;
19
SBTypeFormat()20 SBTypeFormat::SBTypeFormat() : m_opaque_sp() {}
21
SBTypeFormat(lldb::Format format,uint32_t options)22 SBTypeFormat::SBTypeFormat(lldb::Format format, uint32_t options)
23 : m_opaque_sp(
24 TypeFormatImplSP(new TypeFormatImpl_Format(format, options))) {}
25
SBTypeFormat(const char * type,uint32_t options)26 SBTypeFormat::SBTypeFormat(const char *type, uint32_t options)
27 : m_opaque_sp(TypeFormatImplSP(new TypeFormatImpl_EnumType(
28 ConstString(type ? type : ""), options))) {}
29
SBTypeFormat(const lldb::SBTypeFormat & rhs)30 SBTypeFormat::SBTypeFormat(const lldb::SBTypeFormat &rhs)
31 : m_opaque_sp(rhs.m_opaque_sp) {}
32
~SBTypeFormat()33 SBTypeFormat::~SBTypeFormat() {}
34
IsValid() const35 bool SBTypeFormat::IsValid() const { return m_opaque_sp.get() != NULL; }
36
GetFormat()37 lldb::Format SBTypeFormat::GetFormat() {
38 if (IsValid() && m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat)
39 return ((TypeFormatImpl_Format *)m_opaque_sp.get())->GetFormat();
40 return lldb::eFormatInvalid;
41 }
42
GetTypeName()43 const char *SBTypeFormat::GetTypeName() {
44 if (IsValid() && m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeEnum)
45 return ((TypeFormatImpl_EnumType *)m_opaque_sp.get())
46 ->GetTypeName()
47 .AsCString("");
48 return "";
49 }
50
GetOptions()51 uint32_t SBTypeFormat::GetOptions() {
52 if (IsValid())
53 return m_opaque_sp->GetOptions();
54 return 0;
55 }
56
SetFormat(lldb::Format fmt)57 void SBTypeFormat::SetFormat(lldb::Format fmt) {
58 if (CopyOnWrite_Impl(Type::eTypeFormat))
59 ((TypeFormatImpl_Format *)m_opaque_sp.get())->SetFormat(fmt);
60 }
61
SetTypeName(const char * type)62 void SBTypeFormat::SetTypeName(const char *type) {
63 if (CopyOnWrite_Impl(Type::eTypeEnum))
64 ((TypeFormatImpl_EnumType *)m_opaque_sp.get())
65 ->SetTypeName(ConstString(type ? type : ""));
66 }
67
SetOptions(uint32_t value)68 void SBTypeFormat::SetOptions(uint32_t value) {
69 if (CopyOnWrite_Impl(Type::eTypeKeepSame))
70 m_opaque_sp->SetOptions(value);
71 }
72
GetDescription(lldb::SBStream & description,lldb::DescriptionLevel description_level)73 bool SBTypeFormat::GetDescription(lldb::SBStream &description,
74 lldb::DescriptionLevel description_level) {
75 if (!IsValid())
76 return false;
77 else {
78 description.Printf("%s\n", m_opaque_sp->GetDescription().c_str());
79 return true;
80 }
81 }
82
operator =(const lldb::SBTypeFormat & rhs)83 lldb::SBTypeFormat &SBTypeFormat::operator=(const lldb::SBTypeFormat &rhs) {
84 if (this != &rhs) {
85 m_opaque_sp = rhs.m_opaque_sp;
86 }
87 return *this;
88 }
89
operator ==(lldb::SBTypeFormat & rhs)90 bool SBTypeFormat::operator==(lldb::SBTypeFormat &rhs) {
91 if (!IsValid())
92 return !rhs.IsValid();
93 return m_opaque_sp == rhs.m_opaque_sp;
94 }
95
IsEqualTo(lldb::SBTypeFormat & rhs)96 bool SBTypeFormat::IsEqualTo(lldb::SBTypeFormat &rhs) {
97 if (!IsValid())
98 return !rhs.IsValid();
99
100 if (GetFormat() == rhs.GetFormat())
101 return GetOptions() == rhs.GetOptions();
102 else
103 return false;
104 }
105
operator !=(lldb::SBTypeFormat & rhs)106 bool SBTypeFormat::operator!=(lldb::SBTypeFormat &rhs) {
107 if (!IsValid())
108 return !rhs.IsValid();
109 return m_opaque_sp != rhs.m_opaque_sp;
110 }
111
GetSP()112 lldb::TypeFormatImplSP SBTypeFormat::GetSP() { return m_opaque_sp; }
113
SetSP(const lldb::TypeFormatImplSP & typeformat_impl_sp)114 void SBTypeFormat::SetSP(const lldb::TypeFormatImplSP &typeformat_impl_sp) {
115 m_opaque_sp = typeformat_impl_sp;
116 }
117
SBTypeFormat(const lldb::TypeFormatImplSP & typeformat_impl_sp)118 SBTypeFormat::SBTypeFormat(const lldb::TypeFormatImplSP &typeformat_impl_sp)
119 : m_opaque_sp(typeformat_impl_sp) {}
120
CopyOnWrite_Impl(Type type)121 bool SBTypeFormat::CopyOnWrite_Impl(Type type) {
122 if (!IsValid())
123 return false;
124
125 if (m_opaque_sp.unique() &&
126 ((type == Type::eTypeKeepSame) ||
127 (type == Type::eTypeFormat &&
128 m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat) ||
129 (type == Type::eTypeEnum &&
130 m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeEnum)))
131 return true;
132
133 if (type == Type::eTypeKeepSame) {
134 if (m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat)
135 type = Type::eTypeFormat;
136 else
137 type = Type::eTypeEnum;
138 }
139
140 if (type == Type::eTypeFormat)
141 SetSP(
142 TypeFormatImplSP(new TypeFormatImpl_Format(GetFormat(), GetOptions())));
143 else
144 SetSP(TypeFormatImplSP(
145 new TypeFormatImpl_EnumType(ConstString(GetTypeName()), GetOptions())));
146
147 return true;
148 }
149