1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef MOZILLA_PARAMTRAITS_STL_H
7 #define MOZILLA_PARAMTRAITS_STL_H
9 #include "ipc/IPCMessageUtils.h"
10 #include "mozilla/ipc/IPDLParamTraits.h"
16 template <typename U
, size_t N
>
17 struct ParamTraits
<std::array
<U
, N
>> final
{
18 using T
= std::array
<U
, N
>;
20 static void Write(MessageWriter
* const writer
, const T
& in
) {
21 for (const auto& v
: in
) {
22 WriteParam(writer
, v
);
26 static bool Read(MessageReader
* const reader
, T
* const out
) {
27 for (auto& v
: *out
) {
28 if (!ReadParam(reader
, &v
)) return false;
36 template <typename U
, size_t N
>
37 struct ParamTraits
<U
[N
]> final
{
39 static constexpr size_t kByteSize
= sizeof(U
) * N
;
41 static_assert(std::is_trivial
<U
>::value
);
43 static void Write(MessageWriter
* const writer
, const T
& in
) {
44 writer
->WriteBytes(in
, kByteSize
);
47 static bool Read(MessageReader
* const reader
, T
* const out
) {
48 if (!reader
->HasBytesAvailable(kByteSize
)) {
51 return reader
->ReadBytesInto(*out
, kByteSize
);
58 struct ParamTraits
<std::optional
<U
>> final
{
59 using T
= std::optional
<U
>;
61 static void Write(MessageWriter
* const writer
, const T
& in
) {
62 WriteParam(writer
, bool{in
});
64 WriteParam(writer
, *in
);
68 static bool Read(MessageReader
* const reader
, T
* const out
) {
70 if (!ReadParam(reader
, &isSome
)) return false;
77 return ReadParam(reader
, &**out
);