================
@@ -297,6 +340,450 @@ class JSONRPCTransport : public IOTransport<Req, Resp, 
Evt> {
   static constexpr llvm::StringLiteral kMessageSeparator = "\n";
 };
 
-} // namespace lldb_private
+/// A handler for the response to an outgoing request.
+template <typename T>
+using Reply =
+    std::conditional_t<std::is_void_v<T>,
+                       llvm::unique_function<void(llvm::Error)>,
+                       llvm::unique_function<void(llvm::Expected<T>)>>;
+
+namespace detail {
+template <typename R, typename P> struct request_t final {
+  using type = llvm::unique_function<void(const P &, Reply<R>)>;
+};
+template <typename R> struct request_t<R, void> final {
+  using type = llvm::unique_function<void(Reply<R>)>;
+};
+template <typename P> struct event_t final {
+  using type = llvm::unique_function<void(const P &)>;
+};
+template <> struct event_t<void> final {
+  using type = llvm::unique_function<void()>;
+};
+} // namespace detail
+
+template <typename R, typename P>
+using OutgoingRequest = typename detail::request_t<R, P>::type;
+
+/// A function to send an outgoing event.
+template <typename P> using OutgoingEvent = typename detail::event_t<P>::type;
+
+/// Creates a request with the given id, method, and optional params.
+template <typename Id, typename Req>
+Req MakeRequest(Id, llvm::StringRef, std::optional<llvm::json::Value>);
+
+/// Creates an error response for a given request.
+template <typename Req, typename Resp>
+Resp MakeResponse(const Req &, llvm::Error);
+
+/// Creates a success response for a given request.
+template <typename Req, typename Resp>
+Resp MakeResponse(const Req &, llvm::json::Value);
+
+/// Creates an event.
+template <typename Evt>
+Evt MakeEvent(llvm::StringRef, std::optional<llvm::json::Value>);
+
+/// Extracts the result value from a response.
+template <typename Resp>
+llvm::Expected<llvm::json::Value> GetResult(const Resp &);
+
+/// Extracts the id from a response.
+template <typename Id, typename Resp> Id GetId(const Resp &);
+
+/// Extracts the method from a request or event.
+template <typename T> llvm::StringRef GetMethod(const T &);
+
+/// Extracts the parameters from a request or event.
+template <typename T> llvm::json::Value GetParams(const T &);
+
+/// Binder collects a table of functions that handle calls.
+///
+/// The wrapper takes care of parsing/serializing responses.
+///
+/// This allows a JSONTransport to handle incoming and outgoing requests and
+/// events.
+///
+/// A simple example could be to a method to a lambda like:
+///
+/// \code{cpp}
+/// Binder binder{transport};
+/// // Binds an incoming request handler.
+/// binder.bind<int, vector<int>>("adder", [](const vector<int> &params) {
+///   int sum = 0;
+///   for (int v : params)
+///     sum += v;
+///   return sum;
+/// });
+/// // Binds an outgoing request handler.
----------------
JDevlieghere wrote:

```suggestion
/// Binds an outgoing request handler.
```

https://github.com/llvm/llvm-project/pull/159160
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to