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_ISENUMCASE_H
7 #define MOZILLA_PARAMTRAITS_ISENUMCASE_H
9 #include "ipc/IPCMessageUtils.h"
10 #include "IsEnumCase.h"
11 #include "mozilla/ipc/IPDLParamTraits.h"
16 `IsEnumCase(T) -> bool` guarantees that we never have false negatives or false
17 positives due to adding or removing enum cases to enums, and forgetting to
18 update their serializations. Also, it allows enums to be non-continguous, unlike
19 ContiguousEnumSerializer.
23 struct ParamTraits_IsEnumCase
{
24 static bool Write(MessageWriter
* const writer
, const T
& in
) {
25 MOZ_ASSERT(mozilla::IsEnumCase(in
));
26 const auto shadow
= static_cast<std::underlying_type_t
<T
>>(in
);
27 WriteParam(writer
, shadow
);
31 static bool Read(MessageReader
* const reader
, T
* const out
) {
32 auto shadow
= std::underlying_type_t
<T
>{};
33 if (!ReadParam(reader
, &shadow
)) return false;
34 const auto e
= mozilla::AsEnumCase
<T
>(shadow
);