ladybird/Userland/Libraries/LibWeb/WebDriver/Response.h
Timothy Flynn 9b483625e6 LibIPC+Everywhere: Change IPC decoders to construct values in-place
Currently, the generated IPC decoders will default-construct the type to
be decoded, then pass that value by reference to the concrete decoder.
This, of course, requires that the type is default-constructible. This
was an issue for decoding Variants, which had to require the first type
in the Variant list is Empty, to ensure it is default constructible.

Further, this made it possible for values to become uninitialized in
user-defined decoders.

This patch makes the decoder interface such that the concrete decoders
themselves contruct the decoded type upon return from the decoder. To do
so, the default decoders in IPC::Decoder had to be moved to the IPC
namespace scope, as these decoders are now specializations instead of
overloaded methods (C++ requires specializations to be in a namespace
scope).
2022-12-26 09:36:16 +01:00

56 lines
1.5 KiB
C++

/*
* Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/JsonValue.h>
#include <AK/Variant.h>
#include <LibIPC/Forward.h>
#include <LibWeb/WebDriver/Error.h>
namespace Web::WebDriver {
// FIXME: Ideally, this could be `using Response = ErrorOr<JsonValue, Error>`, but that won't be
// default-constructible, which is a requirement for the generated IPC.
struct Response {
Response() = default;
Response(JsonValue&&);
Response(Error&&);
JsonValue& value() { return m_value_or_error.template get<JsonValue>(); }
JsonValue const& value() const { return m_value_or_error.template get<JsonValue>(); }
Error& error() { return m_value_or_error.template get<Error>(); }
Error const& error() const { return m_value_or_error.template get<Error>(); }
bool is_error() const { return m_value_or_error.template has<Error>(); }
JsonValue release_value() { return move(value()); }
Error release_error() { return move(error()); }
template<typename... Visitors>
decltype(auto) visit(Visitors&&... visitors) const
{
return m_value_or_error.visit(forward<Visitors>(visitors)...);
}
private:
// Note: Empty is only a possible state until the Response has been decoded by IPC.
Variant<Empty, JsonValue, Error> m_value_or_error;
};
}
namespace IPC {
template<>
bool encode(Encoder&, Web::WebDriver::Response const&);
template<>
ErrorOr<Web::WebDriver::Response> decode(Decoder&);
}