IPDLStructMember.h (1292B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_ipc_IPDLStructMember_h 8 #define mozilla_ipc_IPDLStructMember_h 9 10 #include <type_traits> 11 #include <utility> 12 #include "mozilla/Attributes.h" 13 14 namespace mozilla::ipc { 15 16 // For types which are trivially default constructible, like `int`, force the 17 // value constructor by wrapping the type in this struct. This is an 18 // implementation detail which will be hidden by the generated IPDL compiler 19 // code. 20 template <typename T> 21 struct IPDLStructMemberWrapper { 22 template <typename... Args> 23 MOZ_IMPLICIT IPDLStructMemberWrapper(Args&&... aArgs) 24 : mVal(std::forward<Args>(aArgs)...) {} 25 MOZ_IMPLICIT operator T&() { return mVal; } 26 MOZ_IMPLICIT operator const T&() const { return mVal; } 27 T mVal{}; 28 }; 29 30 template <typename T> 31 using IPDLStructMember = 32 std::conditional_t<std::is_trivially_default_constructible_v<T>, 33 IPDLStructMemberWrapper<T>, T>; 34 35 } // namespace mozilla::ipc 36 37 #endif // mozilla_ipc_IPDLStructMember_h