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