Bug 1936278 - Prevent search mode chiclet from being dismissed when clicking in page...
[gecko.git] / dom / canvas / ParamTraits_STL.h
blob680eb2c989d693759cc13dff85d8c8dfe60423c4
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"
12 #include <memory>
14 namespace IPC {
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;
30 return true;
34 // -
36 template <typename U, size_t N>
37 struct ParamTraits<U[N]> final {
38 using T = U[N];
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)) {
49 return false;
51 return reader->ReadBytesInto(*out, kByteSize);
55 // -
57 template <class U>
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});
63 if (in) {
64 WriteParam(writer, *in);
68 static bool Read(MessageReader* const reader, T* const out) {
69 bool isSome;
70 if (!ReadParam(reader, &isSome)) return false;
72 if (!isSome) {
73 out->reset();
74 return true;
76 out->emplace();
77 return ReadParam(reader, &**out);
81 } // namespace IPC
83 #endif