UnionMember.h (1410B)
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 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 /* A class for holding the members of a union. */ 8 9 #ifndef mozilla_dom_UnionMember_h 10 #define mozilla_dom_UnionMember_h 11 12 #include <utility> 13 14 #include "mozilla/Alignment.h" 15 #include "mozilla/Attributes.h" 16 17 namespace mozilla::dom { 18 19 // The union type has an enum to keep track of which of its UnionMembers has 20 // been constructed. 21 template <class T> 22 class UnionMember { 23 AlignedStorage2<T> mStorage; 24 25 // Copy construction can't be supported because C++ requires that any enclosed 26 // T be initialized in a way C++ knows about -- that is, by |new| or similar. 27 UnionMember(const UnionMember&) = delete; 28 29 public: 30 UnionMember() = default; 31 ~UnionMember() = default; 32 33 template <typename... Args> 34 T& SetValue(Args&&... args) { 35 new (mStorage.addr()) T(std::forward<Args>(args)...); 36 return *mStorage.addr(); 37 } 38 39 T& Value() { return *mStorage.addr(); } 40 const T& Value() const { return *mStorage.addr(); } 41 void Destroy() { mStorage.addr()->~T(); } 42 } MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS; 43 44 } // namespace mozilla::dom 45 46 #endif // mozilla_dom_UnionMember_h