ParamTraits_IsEnumCase.h (1297B)
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/. */ 5 6 #ifndef MOZILLA_PARAMTRAITS_ISENUMCASE_H 7 #define MOZILLA_PARAMTRAITS_ISENUMCASE_H 8 9 #include "IsEnumCase.h" 10 #include "ipc/IPCMessageUtils.h" 11 12 namespace IPC { 13 14 /* 15 `IsEnumCase(T) -> bool` guarantees that we never have false negatives or false 16 positives due to adding or removing enum cases to enums, and forgetting to 17 update their serializations. Also, it allows enums to be non-continguous, unlike 18 ContiguousEnumSerializer. 19 */ 20 21 template <class T> 22 struct ParamTraits_IsEnumCase { 23 static bool Write(MessageWriter* const writer, const T& in) { 24 MOZ_ASSERT(mozilla::IsEnumCase(in)); 25 const auto shadow = static_cast<std::underlying_type_t<T>>(in); 26 WriteParam(writer, shadow); 27 return true; 28 } 29 30 static bool Read(MessageReader* const reader, T* const out) { 31 auto shadow = std::underlying_type_t<T>{}; 32 if (!ReadParam(reader, &shadow)) return false; 33 const auto e = mozilla::AsEnumCase<T>(shadow); 34 if (!e) return false; 35 *out = *e; 36 return true; 37 } 38 }; 39 40 } // namespace IPC 41 42 #endif