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--
2375930019STodd Fiala #pragma mark Impl
2475930019STodd Fiala 
25*b9c1b51eSKate Stone class SBStructuredData::Impl {
2675930019STodd Fiala public:
27*b9c1b51eSKate Stone   Impl() : m_plugin_wp(), m_data_sp() {}
2875930019STodd Fiala 
2975930019STodd Fiala   Impl(const Impl &rhs) = default;
3075930019STodd Fiala 
31*b9c1b51eSKate Stone   Impl(const EventSP &event_sp)
32*b9c1b51eSKate Stone       : m_plugin_wp(
33*b9c1b51eSKate Stone             EventDataStructuredData::GetPluginFromEvent(event_sp.get())),
34*b9c1b51eSKate Stone         m_data_sp(EventDataStructuredData::GetObjectFromEvent(event_sp.get())) {
3575930019STodd Fiala   }
3675930019STodd Fiala 
3775930019STodd Fiala   ~Impl() = default;
3875930019STodd Fiala 
39*b9c1b51eSKate Stone   Impl &operator=(const Impl &rhs) = default;
4075930019STodd Fiala 
41*b9c1b51eSKate Stone   bool IsValid() const { return m_data_sp.get() != nullptr; }
4275930019STodd Fiala 
43*b9c1b51eSKate Stone   void Clear() {
4475930019STodd Fiala     m_plugin_wp.reset();
4575930019STodd Fiala     m_data_sp.reset();
4675930019STodd Fiala   }
4775930019STodd Fiala 
48*b9c1b51eSKate Stone   SBError GetAsJSON(lldb::SBStream &stream) const {
4975930019STodd Fiala     SBError sb_error;
5075930019STodd Fiala 
51*b9c1b51eSKate Stone     if (!m_data_sp) {
5275930019STodd Fiala       sb_error.SetErrorString("No structured data.");
5375930019STodd Fiala       return sb_error;
5475930019STodd Fiala     }
5575930019STodd Fiala 
5675930019STodd Fiala     m_data_sp->Dump(stream.ref());
5775930019STodd Fiala     return sb_error;
5875930019STodd Fiala   }
5975930019STodd Fiala 
60*b9c1b51eSKate Stone   lldb::SBError GetDescription(lldb::SBStream &stream) const {
6175930019STodd Fiala     SBError sb_error;
6275930019STodd Fiala 
63*b9c1b51eSKate Stone     if (!m_data_sp) {
6475930019STodd Fiala       sb_error.SetErrorString("Cannot pretty print structured data: "
6575930019STodd Fiala                               "no data to print.");
6675930019STodd Fiala       return sb_error;
6775930019STodd Fiala     }
6875930019STodd Fiala 
6975930019STodd Fiala     // Grab the plugin.
7075930019STodd Fiala     auto plugin_sp = StructuredDataPluginSP(m_plugin_wp);
71*b9c1b51eSKate Stone     if (!plugin_sp) {
7275930019STodd Fiala       sb_error.SetErrorString("Cannot pretty print structured data: "
7375930019STodd Fiala                               "plugin doesn't exist.");
7475930019STodd Fiala       return sb_error;
7575930019STodd Fiala     }
7675930019STodd Fiala 
7775930019STodd Fiala     // Get the data's description.
7875930019STodd Fiala     auto error = plugin_sp->GetDescription(m_data_sp, stream.ref());
7975930019STodd Fiala     if (!error.Success())
8075930019STodd Fiala       sb_error.SetError(error);
8175930019STodd Fiala 
8275930019STodd Fiala     return sb_error;
8375930019STodd Fiala   }
8475930019STodd Fiala 
8575930019STodd Fiala private:
8675930019STodd Fiala   StructuredDataPluginWP m_plugin_wp;
8775930019STodd Fiala   StructuredData::ObjectSP m_data_sp;
8875930019STodd Fiala };
8975930019STodd Fiala 
9075930019STodd Fiala #pragma mark--
9175930019STodd Fiala #pragma mark SBStructuredData
9275930019STodd Fiala 
93*b9c1b51eSKate Stone SBStructuredData::SBStructuredData() : m_impl_up(new Impl()) {}
9475930019STodd Fiala 
95*b9c1b51eSKate Stone SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs)
96*b9c1b51eSKate Stone     : m_impl_up(new Impl(*rhs.m_impl_up.get())) {}
9775930019STodd Fiala 
98*b9c1b51eSKate Stone SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
99*b9c1b51eSKate Stone     : m_impl_up(new Impl(event_sp)) {}
10075930019STodd Fiala 
101*b9c1b51eSKate Stone SBStructuredData::~SBStructuredData() {}
10275930019STodd Fiala 
103*b9c1b51eSKate Stone SBStructuredData &SBStructuredData::
104*b9c1b51eSKate Stone operator=(const lldb::SBStructuredData &rhs) {
10575930019STodd Fiala   *m_impl_up = *rhs.m_impl_up;
10675930019STodd Fiala   return *this;
10775930019STodd Fiala }
10875930019STodd Fiala 
109*b9c1b51eSKate Stone bool SBStructuredData::IsValid() const { return m_impl_up->IsValid(); }
11075930019STodd Fiala 
111*b9c1b51eSKate Stone void SBStructuredData::Clear() { m_impl_up->Clear(); }
11275930019STodd Fiala 
113*b9c1b51eSKate Stone SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const {
11475930019STodd Fiala   return m_impl_up->GetAsJSON(stream);
11575930019STodd Fiala }
11675930019STodd Fiala 
117*b9c1b51eSKate Stone lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const {
11875930019STodd Fiala   return m_impl_up->GetDescription(stream);
11975930019STodd Fiala }
120