Bug 1936278 - Prevent search mode chiclet from being dismissed when clicking in page...
[gecko.git] / dom / canvas / ParamTraits_IsEnumCase.h
blobd0fea3494e67546beb6d4fa768db8ec390398fde
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"
13 namespace IPC {
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.
22 template <class T>
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);
28 return true;
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);
35 if (!e) return false;
36 *out = *e;
37 return true;
41 } // namespace IPC
43 #endif