1*75930019STodd Fiala //===-- SBStructuredData.cpp ------------------------------------*- C++ -*-===//
2*75930019STodd Fiala //
3*75930019STodd Fiala //                     The LLVM Compiler Infrastructure
4*75930019STodd Fiala //
5*75930019STodd Fiala // This file is distributed under the University of Illinois Open Source
6*75930019STodd Fiala // License. See LICENSE.TXT for details.
7*75930019STodd Fiala //
8*75930019STodd Fiala //===----------------------------------------------------------------------===//
9*75930019STodd Fiala 
10*75930019STodd Fiala #include "lldb/API/SBStructuredData.h"
11*75930019STodd Fiala 
12*75930019STodd Fiala #include "lldb/API/SBStream.h"
13*75930019STodd Fiala #include "lldb/Core/Error.h"
14*75930019STodd Fiala #include "lldb/Core/Event.h"
15*75930019STodd Fiala #include "lldb/Core/Stream.h"
16*75930019STodd Fiala #include "lldb/Core/StructuredData.h"
17*75930019STodd Fiala #include "lldb/Target/StructuredDataPlugin.h"
18*75930019STodd Fiala 
19*75930019STodd Fiala using namespace lldb;
20*75930019STodd Fiala using namespace lldb_private;
21*75930019STodd Fiala 
22*75930019STodd Fiala #pragma mark --
23*75930019STodd Fiala #pragma mark Impl
24*75930019STodd Fiala 
25*75930019STodd Fiala class SBStructuredData::Impl
26*75930019STodd Fiala {
27*75930019STodd Fiala public:
28*75930019STodd Fiala 
29*75930019STodd Fiala     Impl() :
30*75930019STodd Fiala         m_plugin_wp(),
31*75930019STodd Fiala         m_data_sp()
32*75930019STodd Fiala     {
33*75930019STodd Fiala     }
34*75930019STodd Fiala 
35*75930019STodd Fiala     Impl(const Impl &rhs) = default;
36*75930019STodd Fiala 
37*75930019STodd Fiala     Impl(const EventSP &event_sp) :
38*75930019STodd Fiala        m_plugin_wp(EventDataStructuredData::GetPluginFromEvent(event_sp.get())),
39*75930019STodd Fiala         m_data_sp(EventDataStructuredData::GetObjectFromEvent(event_sp.get()))
40*75930019STodd Fiala     {
41*75930019STodd Fiala     }
42*75930019STodd Fiala 
43*75930019STodd Fiala     ~Impl() = default;
44*75930019STodd Fiala 
45*75930019STodd Fiala     Impl&
46*75930019STodd Fiala     operator =(const Impl &rhs) = default;
47*75930019STodd Fiala 
48*75930019STodd Fiala     bool
49*75930019STodd Fiala     IsValid() const
50*75930019STodd Fiala     {
51*75930019STodd Fiala         return m_data_sp.get() != nullptr;
52*75930019STodd Fiala     }
53*75930019STodd Fiala 
54*75930019STodd Fiala     void
55*75930019STodd Fiala     Clear()
56*75930019STodd Fiala     {
57*75930019STodd Fiala         m_plugin_wp.reset();
58*75930019STodd Fiala         m_data_sp.reset();
59*75930019STodd Fiala     }
60*75930019STodd Fiala 
61*75930019STodd Fiala     SBError
62*75930019STodd Fiala     GetAsJSON(lldb::SBStream &stream) const
63*75930019STodd Fiala     {
64*75930019STodd Fiala         SBError sb_error;
65*75930019STodd Fiala 
66*75930019STodd Fiala         if (!m_data_sp)
67*75930019STodd Fiala         {
68*75930019STodd Fiala             sb_error.SetErrorString("No structured data.");
69*75930019STodd Fiala             return sb_error;
70*75930019STodd Fiala         }
71*75930019STodd Fiala 
72*75930019STodd Fiala         m_data_sp->Dump(stream.ref());
73*75930019STodd Fiala         return sb_error;
74*75930019STodd Fiala     }
75*75930019STodd Fiala 
76*75930019STodd Fiala     lldb::SBError
77*75930019STodd Fiala     GetDescription(lldb::SBStream &stream) const
78*75930019STodd Fiala     {
79*75930019STodd Fiala         SBError sb_error;
80*75930019STodd Fiala 
81*75930019STodd Fiala         if (!m_data_sp)
82*75930019STodd Fiala         {
83*75930019STodd Fiala             sb_error.SetErrorString("Cannot pretty print structured data: "
84*75930019STodd Fiala                                     "no data to print.");
85*75930019STodd Fiala             return sb_error;
86*75930019STodd Fiala         }
87*75930019STodd Fiala 
88*75930019STodd Fiala         // Grab the plugin.
89*75930019STodd Fiala         auto plugin_sp = StructuredDataPluginSP(m_plugin_wp);
90*75930019STodd Fiala         if (!plugin_sp)
91*75930019STodd Fiala         {
92*75930019STodd Fiala             sb_error.SetErrorString("Cannot pretty print structured data: "
93*75930019STodd Fiala                                     "plugin doesn't exist.");
94*75930019STodd Fiala             return sb_error;
95*75930019STodd Fiala         }
96*75930019STodd Fiala 
97*75930019STodd Fiala         // Get the data's description.
98*75930019STodd Fiala         auto error = plugin_sp->GetDescription(m_data_sp, stream.ref());
99*75930019STodd Fiala         if (!error.Success())
100*75930019STodd Fiala             sb_error.SetError(error);
101*75930019STodd Fiala 
102*75930019STodd Fiala         return sb_error;
103*75930019STodd Fiala     }
104*75930019STodd Fiala 
105*75930019STodd Fiala private:
106*75930019STodd Fiala 
107*75930019STodd Fiala     StructuredDataPluginWP    m_plugin_wp;
108*75930019STodd Fiala     StructuredData::ObjectSP  m_data_sp;
109*75930019STodd Fiala 
110*75930019STodd Fiala };
111*75930019STodd Fiala 
112*75930019STodd Fiala #pragma mark --
113*75930019STodd Fiala #pragma mark SBStructuredData
114*75930019STodd Fiala 
115*75930019STodd Fiala 
116*75930019STodd Fiala SBStructuredData::SBStructuredData() :
117*75930019STodd Fiala     m_impl_up(new Impl())
118*75930019STodd Fiala {
119*75930019STodd Fiala }
120*75930019STodd Fiala 
121*75930019STodd Fiala SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs) :
122*75930019STodd Fiala     m_impl_up(new Impl(*rhs.m_impl_up.get()))
123*75930019STodd Fiala {
124*75930019STodd Fiala }
125*75930019STodd Fiala 
126*75930019STodd Fiala SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp) :
127*75930019STodd Fiala     m_impl_up(new Impl(event_sp))
128*75930019STodd Fiala {
129*75930019STodd Fiala }
130*75930019STodd Fiala 
131*75930019STodd Fiala SBStructuredData::~SBStructuredData()
132*75930019STodd Fiala {
133*75930019STodd Fiala }
134*75930019STodd Fiala 
135*75930019STodd Fiala SBStructuredData &
136*75930019STodd Fiala SBStructuredData::operator =(const lldb::SBStructuredData &rhs)
137*75930019STodd Fiala {
138*75930019STodd Fiala     *m_impl_up = *rhs.m_impl_up;
139*75930019STodd Fiala     return *this;
140*75930019STodd Fiala }
141*75930019STodd Fiala 
142*75930019STodd Fiala bool
143*75930019STodd Fiala SBStructuredData::IsValid() const
144*75930019STodd Fiala {
145*75930019STodd Fiala     return m_impl_up->IsValid();
146*75930019STodd Fiala }
147*75930019STodd Fiala 
148*75930019STodd Fiala void
149*75930019STodd Fiala SBStructuredData::Clear()
150*75930019STodd Fiala {
151*75930019STodd Fiala     m_impl_up->Clear();
152*75930019STodd Fiala }
153*75930019STodd Fiala 
154*75930019STodd Fiala SBError
155*75930019STodd Fiala SBStructuredData::GetAsJSON(lldb::SBStream &stream) const
156*75930019STodd Fiala {
157*75930019STodd Fiala     return m_impl_up->GetAsJSON(stream);
158*75930019STodd Fiala }
159*75930019STodd Fiala 
160*75930019STodd Fiala lldb::SBError
161*75930019STodd Fiala SBStructuredData::GetDescription(lldb::SBStream &stream) const
162*75930019STodd Fiala {
163*75930019STodd Fiala     return m_impl_up->GetDescription(stream);
164*75930019STodd Fiala }
165*75930019STodd Fiala 
166