1//===-- SBTypeFormat.cpp ------------------------------------------*- 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#include "lldb/lldb-python.h"
11
12#include "lldb/API/SBTypeFormat.h"
13
14#include "lldb/API/SBStream.h"
15
16#include "lldb/DataFormatters/DataVisualization.h"
17
18using namespace lldb;
19using namespace lldb_private;
20
21SBTypeFormat::SBTypeFormat() :
22m_opaque_sp()
23{
24}
25
26SBTypeFormat::SBTypeFormat (lldb::Format format,
27                            uint32_t options)
28: m_opaque_sp(TypeFormatImplSP(new TypeFormatImpl_Format(format,options)))
29{
30}
31
32SBTypeFormat::SBTypeFormat (const char* type,
33                            uint32_t options)
34: m_opaque_sp(TypeFormatImplSP(new TypeFormatImpl_EnumType(ConstString(type ? type : ""),options)))
35{
36}
37
38SBTypeFormat::SBTypeFormat (const lldb::SBTypeFormat &rhs) :
39m_opaque_sp(rhs.m_opaque_sp)
40{
41}
42
43SBTypeFormat::~SBTypeFormat ()
44{
45}
46
47bool
48SBTypeFormat::IsValid() const
49{
50    return m_opaque_sp.get() != NULL;
51}
52
53lldb::Format
54SBTypeFormat::GetFormat ()
55{
56    if (IsValid() && m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat)
57        return ((TypeFormatImpl_Format*)m_opaque_sp.get())->GetFormat();
58    return lldb::eFormatInvalid;
59}
60
61const char*
62SBTypeFormat::GetTypeName ()
63{
64    if (IsValid() && m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeEnum)
65        return ((TypeFormatImpl_EnumType*)m_opaque_sp.get())->GetTypeName().AsCString("");
66    return "";
67}
68
69uint32_t
70SBTypeFormat::GetOptions()
71{
72    if (IsValid())
73        return m_opaque_sp->GetOptions();
74    return 0;
75}
76
77void
78SBTypeFormat::SetFormat (lldb::Format fmt)
79{
80    if (CopyOnWrite_Impl(Type::eTypeFormat))
81        ((TypeFormatImpl_Format*)m_opaque_sp.get())->SetFormat(fmt);
82}
83
84void
85SBTypeFormat::SetTypeName (const char* type)
86{
87    if (CopyOnWrite_Impl(Type::eTypeEnum))
88        ((TypeFormatImpl_EnumType*)m_opaque_sp.get())->SetTypeName(ConstString(type ? type : ""));
89}
90
91void
92SBTypeFormat::SetOptions (uint32_t value)
93{
94    if (CopyOnWrite_Impl(Type::eTypeKeepSame))
95        m_opaque_sp->SetOptions(value);
96}
97
98bool
99SBTypeFormat::GetDescription (lldb::SBStream &description,
100                              lldb::DescriptionLevel description_level)
101{
102    if (!IsValid())
103        return false;
104    else {
105        description.Printf("%s\n",
106                           m_opaque_sp->GetDescription().c_str());
107        return true;
108    }
109}
110
111lldb::SBTypeFormat &
112SBTypeFormat::operator = (const lldb::SBTypeFormat &rhs)
113{
114    if (this != &rhs)
115    {
116        m_opaque_sp = rhs.m_opaque_sp;
117    }
118    return *this;
119}
120
121bool
122SBTypeFormat::operator == (lldb::SBTypeFormat &rhs)
123{
124    if (IsValid() == false)
125        return !rhs.IsValid();
126    return m_opaque_sp == rhs.m_opaque_sp;
127}
128
129bool
130SBTypeFormat::IsEqualTo (lldb::SBTypeFormat &rhs)
131{
132    if (IsValid() == false)
133        return !rhs.IsValid();
134
135    if (GetFormat() == rhs.GetFormat())
136        return GetOptions() == rhs.GetOptions();
137    else
138        return false;
139}
140
141bool
142SBTypeFormat::operator != (lldb::SBTypeFormat &rhs)
143{
144    if (IsValid() == false)
145        return !rhs.IsValid();
146    return m_opaque_sp != rhs.m_opaque_sp;
147}
148
149lldb::TypeFormatImplSP
150SBTypeFormat::GetSP ()
151{
152    return m_opaque_sp;
153}
154
155void
156SBTypeFormat::SetSP (const lldb::TypeFormatImplSP &typeformat_impl_sp)
157{
158    m_opaque_sp = typeformat_impl_sp;
159}
160
161SBTypeFormat::SBTypeFormat (const lldb::TypeFormatImplSP &typeformat_impl_sp) :
162    m_opaque_sp(typeformat_impl_sp)
163{
164}
165
166bool
167SBTypeFormat::CopyOnWrite_Impl(Type type)
168{
169    if (!IsValid())
170        return false;
171
172    if (m_opaque_sp.unique() &&
173        ((type == Type::eTypeKeepSame) ||
174         (type == Type::eTypeFormat && m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat) ||
175         (type == Type::eTypeEnum && m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeEnum))
176        )
177        return true;
178
179    if (type == Type::eTypeKeepSame)
180    {
181        if (m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat)
182            type = Type::eTypeFormat;
183        else
184            type = Type::eTypeEnum;
185    }
186
187    if (type == Type::eTypeFormat)
188        SetSP(TypeFormatImplSP(new TypeFormatImpl_Format(GetFormat(),GetOptions())));
189    else
190        SetSP(TypeFormatImplSP(new TypeFormatImpl_EnumType(ConstString(GetTypeName()),GetOptions())));
191
192    return true;
193}
194