mirror of
https://github.com/RGBCube/serenity
synced 2025-05-20 14:15:07 +00:00

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).
48 lines
952 B
C++
48 lines
952 B
C++
/*
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <AK/DeprecatedString.h>
|
|
#include <LibGfx/Size.h>
|
|
#include <LibIPC/Decoder.h>
|
|
#include <LibIPC/Encoder.h>
|
|
|
|
namespace Gfx {
|
|
|
|
template<>
|
|
DeprecatedString IntSize::to_deprecated_string() const
|
|
{
|
|
return DeprecatedString::formatted("[{}x{}]", m_width, m_height);
|
|
}
|
|
|
|
template<>
|
|
DeprecatedString FloatSize::to_deprecated_string() const
|
|
{
|
|
return DeprecatedString::formatted("[{}x{}]", m_width, m_height);
|
|
}
|
|
|
|
}
|
|
|
|
namespace IPC {
|
|
|
|
template<>
|
|
bool encode(Encoder& encoder, Gfx::IntSize const& size)
|
|
{
|
|
encoder << size.width() << size.height();
|
|
return true;
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<Gfx::IntSize> decode(Decoder& decoder)
|
|
{
|
|
auto width = TRY(decoder.decode<int>());
|
|
auto height = TRY(decoder.decode<int>());
|
|
return Gfx::IntSize { width, height };
|
|
}
|
|
|
|
}
|
|
|
|
template class Gfx::Size<int>;
|
|
template class Gfx::Size<float>;
|