SBTypeSynthetic.cpp revision 344779
1//===-- SBTypeSynthetic.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/SBTypeSynthetic.h"
12
13#include "lldb/API/SBStream.h"
14
15#include "lldb/DataFormatters/DataVisualization.h"
16
17using namespace lldb;
18using namespace lldb_private;
19
20#ifndef LLDB_DISABLE_PYTHON
21
22SBTypeSynthetic::SBTypeSynthetic() : m_opaque_sp() {}
23
24SBTypeSynthetic SBTypeSynthetic::CreateWithClassName(const char *data,
25                                                     uint32_t options) {
26  if (!data || data[0] == 0)
27    return SBTypeSynthetic();
28  return SBTypeSynthetic(ScriptedSyntheticChildrenSP(
29      new ScriptedSyntheticChildren(options, data, "")));
30}
31
32SBTypeSynthetic SBTypeSynthetic::CreateWithScriptCode(const char *data,
33                                                      uint32_t options) {
34  if (!data || data[0] == 0)
35    return SBTypeSynthetic();
36  return SBTypeSynthetic(ScriptedSyntheticChildrenSP(
37      new ScriptedSyntheticChildren(options, "", data)));
38}
39
40SBTypeSynthetic::SBTypeSynthetic(const lldb::SBTypeSynthetic &rhs)
41    : m_opaque_sp(rhs.m_opaque_sp) {}
42
43SBTypeSynthetic::~SBTypeSynthetic() {}
44
45bool SBTypeSynthetic::IsValid() const { return m_opaque_sp.get() != NULL; }
46
47bool SBTypeSynthetic::IsClassCode() {
48  if (!IsValid())
49    return false;
50  const char *code = m_opaque_sp->GetPythonCode();
51  return (code && *code);
52}
53
54bool SBTypeSynthetic::IsClassName() {
55  if (!IsValid())
56    return false;
57  return !IsClassCode();
58}
59
60const char *SBTypeSynthetic::GetData() {
61  if (!IsValid())
62    return NULL;
63  if (IsClassCode())
64    return m_opaque_sp->GetPythonCode();
65  else
66    return m_opaque_sp->GetPythonClassName();
67}
68
69void SBTypeSynthetic::SetClassName(const char *data) {
70  if (IsValid() && data && *data)
71    m_opaque_sp->SetPythonClassName(data);
72}
73
74void SBTypeSynthetic::SetClassCode(const char *data) {
75  if (IsValid() && data && *data)
76    m_opaque_sp->SetPythonCode(data);
77}
78
79uint32_t SBTypeSynthetic::GetOptions() {
80  if (!IsValid())
81    return lldb::eTypeOptionNone;
82  return m_opaque_sp->GetOptions();
83}
84
85void SBTypeSynthetic::SetOptions(uint32_t value) {
86  if (!CopyOnWrite_Impl())
87    return;
88  m_opaque_sp->SetOptions(value);
89}
90
91bool SBTypeSynthetic::GetDescription(lldb::SBStream &description,
92                                     lldb::DescriptionLevel description_level) {
93  if (m_opaque_sp) {
94    description.Printf("%s\n", m_opaque_sp->GetDescription().c_str());
95    return true;
96  }
97  return false;
98}
99
100lldb::SBTypeSynthetic &SBTypeSynthetic::
101operator=(const lldb::SBTypeSynthetic &rhs) {
102  if (this != &rhs) {
103    m_opaque_sp = rhs.m_opaque_sp;
104  }
105  return *this;
106}
107
108bool SBTypeSynthetic::operator==(lldb::SBTypeSynthetic &rhs) {
109  if (!IsValid())
110    return !rhs.IsValid();
111  return m_opaque_sp == rhs.m_opaque_sp;
112}
113
114bool SBTypeSynthetic::IsEqualTo(lldb::SBTypeSynthetic &rhs) {
115  if (!IsValid())
116    return !rhs.IsValid();
117
118  if (m_opaque_sp->IsScripted() != rhs.m_opaque_sp->IsScripted())
119    return false;
120
121  if (IsClassCode() != rhs.IsClassCode())
122    return false;
123
124  if (strcmp(GetData(), rhs.GetData()))
125    return false;
126
127  return GetOptions() == rhs.GetOptions();
128}
129
130bool SBTypeSynthetic::operator!=(lldb::SBTypeSynthetic &rhs) {
131  if (!IsValid())
132    return !rhs.IsValid();
133  return m_opaque_sp != rhs.m_opaque_sp;
134}
135
136lldb::ScriptedSyntheticChildrenSP SBTypeSynthetic::GetSP() {
137  return m_opaque_sp;
138}
139
140void SBTypeSynthetic::SetSP(
141    const lldb::ScriptedSyntheticChildrenSP &TypeSynthetic_impl_sp) {
142  m_opaque_sp = TypeSynthetic_impl_sp;
143}
144
145SBTypeSynthetic::SBTypeSynthetic(
146    const lldb::ScriptedSyntheticChildrenSP &TypeSynthetic_impl_sp)
147    : m_opaque_sp(TypeSynthetic_impl_sp) {}
148
149bool SBTypeSynthetic::CopyOnWrite_Impl() {
150  if (!IsValid())
151    return false;
152  if (m_opaque_sp.unique())
153    return true;
154
155  ScriptedSyntheticChildrenSP new_sp(new ScriptedSyntheticChildren(
156      m_opaque_sp->GetOptions(), m_opaque_sp->GetPythonClassName(),
157      m_opaque_sp->GetPythonCode()));
158
159  SetSP(new_sp);
160
161  return true;
162}
163
164#endif // LLDB_DISABLE_PYTHON
165