175930019STodd Fiala //===-- SBStructuredData.cpp ------------------------------------*- C++ -*-===//
275930019STodd Fiala //
375930019STodd Fiala //                     The LLVM Compiler Infrastructure
475930019STodd Fiala //
575930019STodd Fiala // This file is distributed under the University of Illinois Open Source
675930019STodd Fiala // License. See LICENSE.TXT for details.
775930019STodd Fiala //
875930019STodd Fiala //===----------------------------------------------------------------------===//
975930019STodd Fiala 
1075930019STodd Fiala #include "lldb/API/SBStructuredData.h"
1175930019STodd Fiala 
1275930019STodd Fiala #include "lldb/API/SBStream.h"
1375930019STodd Fiala #include "lldb/Core/Error.h"
1475930019STodd Fiala #include "lldb/Core/Event.h"
1575930019STodd Fiala #include "lldb/Core/Stream.h"
1675930019STodd Fiala #include "lldb/Core/StructuredData.h"
1775930019STodd Fiala #include "lldb/Target/StructuredDataPlugin.h"
1875930019STodd Fiala 
1975930019STodd Fiala using namespace lldb;
2075930019STodd Fiala using namespace lldb_private;
2175930019STodd Fiala 
2275930019STodd Fiala #pragma mark--
23*2ef442c6STodd Fiala #pragma mark StructuredDataImpl
2475930019STodd Fiala 
25*2ef442c6STodd Fiala class StructuredDataImpl {
2675930019STodd Fiala public:
27*2ef442c6STodd Fiala   StructuredDataImpl() : m_plugin_wp(), m_data_sp() {}
2875930019STodd Fiala 
29*2ef442c6STodd Fiala   StructuredDataImpl(const StructuredDataImpl &rhs) = default;
3075930019STodd Fiala 
31*2ef442c6STodd Fiala   StructuredDataImpl(const EventSP &event_sp)
32b9c1b51eSKate Stone       : m_plugin_wp(
33b9c1b51eSKate Stone             EventDataStructuredData::GetPluginFromEvent(event_sp.get())),
34b9c1b51eSKate Stone         m_data_sp(EventDataStructuredData::GetObjectFromEvent(event_sp.get())) {
3575930019STodd Fiala   }
3675930019STodd Fiala 
37*2ef442c6STodd Fiala   ~StructuredDataImpl() = default;
3875930019STodd Fiala 
39*2ef442c6STodd Fiala   StructuredDataImpl &operator=(const StructuredDataImpl &rhs) = default;
4075930019STodd Fiala 
41b9c1b51eSKate Stone   bool IsValid() const { return m_data_sp.get() != nullptr; }
4275930019STodd Fiala 
43b9c1b51eSKate Stone   void Clear() {
4475930019STodd Fiala     m_plugin_wp.reset();
4575930019STodd Fiala     m_data_sp.reset();
4675930019STodd Fiala   }
4775930019STodd Fiala 
48*2ef442c6STodd Fiala   SBError GetAsJSON(lldb_private::Stream &stream) const {
4975930019STodd Fiala     SBError sb_error;
5075930019STodd Fiala 
51b9c1b51eSKate Stone     if (!m_data_sp) {
5275930019STodd Fiala       sb_error.SetErrorString("No structured data.");
5375930019STodd Fiala       return sb_error;
5475930019STodd Fiala     }
5575930019STodd Fiala 
56*2ef442c6STodd Fiala     m_data_sp->Dump(stream);
5775930019STodd Fiala     return sb_error;
5875930019STodd Fiala   }
5975930019STodd Fiala 
60*2ef442c6STodd Fiala   Error GetDescription(lldb_private::Stream &stream) const {
61*2ef442c6STodd Fiala     Error error;
6275930019STodd Fiala 
63b9c1b51eSKate Stone     if (!m_data_sp) {
64*2ef442c6STodd Fiala       error.SetErrorString("Cannot pretty print structured data: "
6575930019STodd Fiala                            "no data to print.");
66*2ef442c6STodd Fiala       return error;
6775930019STodd Fiala     }
6875930019STodd Fiala 
6975930019STodd Fiala     // Grab the plugin.
7075930019STodd Fiala     auto plugin_sp = StructuredDataPluginSP(m_plugin_wp);
71b9c1b51eSKate Stone     if (!plugin_sp) {
72*2ef442c6STodd Fiala       error.SetErrorString("Cannot pretty print structured data: "
7375930019STodd Fiala                            "plugin doesn't exist.");
74*2ef442c6STodd Fiala       return error;
7575930019STodd Fiala     }
7675930019STodd Fiala 
7775930019STodd Fiala     // Get the data's description.
78*2ef442c6STodd Fiala     return plugin_sp->GetDescription(m_data_sp, stream);
7975930019STodd Fiala   }
8075930019STodd Fiala 
8175930019STodd Fiala private:
8275930019STodd Fiala   StructuredDataPluginWP m_plugin_wp;
8375930019STodd Fiala   StructuredData::ObjectSP m_data_sp;
8475930019STodd Fiala };
8575930019STodd Fiala 
8675930019STodd Fiala #pragma mark--
8775930019STodd Fiala #pragma mark SBStructuredData
8875930019STodd Fiala 
89*2ef442c6STodd Fiala SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) {}
9075930019STodd Fiala 
91b9c1b51eSKate Stone SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs)
92*2ef442c6STodd Fiala     : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up.get())) {}
9375930019STodd Fiala 
94b9c1b51eSKate Stone SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
95*2ef442c6STodd Fiala     : m_impl_up(new StructuredDataImpl(event_sp)) {}
9675930019STodd Fiala 
97b9c1b51eSKate Stone SBStructuredData::~SBStructuredData() {}
9875930019STodd Fiala 
99b9c1b51eSKate Stone SBStructuredData &SBStructuredData::
100b9c1b51eSKate Stone operator=(const lldb::SBStructuredData &rhs) {
10175930019STodd Fiala   *m_impl_up = *rhs.m_impl_up;
10275930019STodd Fiala   return *this;
10375930019STodd Fiala }
10475930019STodd Fiala 
105b9c1b51eSKate Stone bool SBStructuredData::IsValid() const { return m_impl_up->IsValid(); }
10675930019STodd Fiala 
107b9c1b51eSKate Stone void SBStructuredData::Clear() { m_impl_up->Clear(); }
10875930019STodd Fiala 
109b9c1b51eSKate Stone SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const {
110*2ef442c6STodd Fiala   return m_impl_up->GetAsJSON(stream.ref());
11175930019STodd Fiala }
11275930019STodd Fiala 
113b9c1b51eSKate Stone lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const {
114*2ef442c6STodd Fiala   Error error = m_impl_up->GetDescription(stream.ref());
115*2ef442c6STodd Fiala   SBError sb_error;
116*2ef442c6STodd Fiala   sb_error.SetError(error);
117*2ef442c6STodd Fiala   return sb_error;
11875930019STodd Fiala }
119