1//===-- ScriptedInterface.h -------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_INTERPRETER_INTERFACES_SCRIPTEDINTERFACE_H
10#define LLDB_INTERPRETER_INTERFACES_SCRIPTEDINTERFACE_H
11
12#include "lldb/Core/StructuredDataImpl.h"
13#include "lldb/Target/ExecutionContext.h"
14#include "lldb/Utility/LLDBLog.h"
15#include "lldb/Utility/Log.h"
16#include "lldb/Utility/UnimplementedError.h"
17#include "lldb/lldb-private.h"
18
19#include "llvm/Support/Compiler.h"
20
21#include <string>
22
23namespace lldb_private {
24class ScriptedInterface {
25public:
26  ScriptedInterface() = default;
27  virtual ~ScriptedInterface() = default;
28
29  StructuredData::GenericSP GetScriptObjectInstance() {
30    return m_object_instance_sp;
31  }
32
33  virtual llvm::SmallVector<llvm::StringLiteral> GetAbstractMethods() const = 0;
34
35  template <typename Ret>
36  static Ret ErrorWithMessage(llvm::StringRef caller_name,
37                              llvm::StringRef error_msg, Status &error,
38                              LLDBLog log_caterogy = LLDBLog::Process) {
39    LLDB_LOGF(GetLog(log_caterogy), "%s ERROR = %s", caller_name.data(),
40              error_msg.data());
41    std::string full_error_message =
42        llvm::Twine(caller_name + llvm::Twine(" ERROR = ") +
43                    llvm::Twine(error_msg))
44            .str();
45    if (const char *detailed_error = error.AsCString())
46      full_error_message +=
47          llvm::Twine(llvm::Twine(" (") + llvm::Twine(detailed_error) +
48                      llvm::Twine(")"))
49              .str();
50    error.SetErrorString(full_error_message);
51    return {};
52  }
53
54  template <typename T = StructuredData::ObjectSP>
55  bool CheckStructuredDataObject(llvm::StringRef caller, T obj, Status &error) {
56    if (!obj)
57      return ErrorWithMessage<bool>(caller, "Null Structured Data object",
58                                    error);
59
60    if (!obj->IsValid()) {
61      return ErrorWithMessage<bool>(caller, "Invalid StructuredData object",
62                                    error);
63    }
64
65    if (error.Fail())
66      return ErrorWithMessage<bool>(caller, error.AsCString(), error);
67
68    return true;
69  }
70
71protected:
72  StructuredData::GenericSP m_object_instance_sp;
73};
74} // namespace lldb_private
75#endif // LLDB_INTERPRETER_INTERFACES_SCRIPTEDINTERFACE_H
76