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 "mozilla/Alignment.h"
13 
14 namespace mozilla {
15 namespace dom {
16 
17 // The union type has an enum to keep track of which of its UnionMembers has
18 // been constructed.
19 template <class T>
20 class UnionMember {
21   AlignedStorage2<T> mStorage;
22 
23   // Copy construction can't be supported because C++ requires that any enclosed
24   // T be initialized in a way C++ knows about -- that is, by |new| or similar.
25   UnionMember(const UnionMember&) = delete;
26 
27  public:
28   UnionMember() = default;
29   ~UnionMember() = default;
30 
SetValue()31   T& SetValue() {
32     new (mStorage.addr()) T();
33     return *mStorage.addr();
34   }
35   template <typename T1>
SetValue(const T1 & aValue)36   T& SetValue(const T1& aValue) {
37     new (mStorage.addr()) T(aValue);
38     return *mStorage.addr();
39   }
40   template <typename T1, typename T2>
SetValue(const T1 & aValue1,const T2 & aValue2)41   T& SetValue(const T1& aValue1, const T2& aValue2) {
42     new (mStorage.addr()) T(aValue1, aValue2);
43     return *mStorage.addr();
44   }
Value()45   T& Value() { return *mStorage.addr(); }
Value()46   const T& Value() const { return *mStorage.addr(); }
Destroy()47   void Destroy() { mStorage.addr()->~T(); }
48 };
49 
50 }  // namespace dom
51 }  // namespace mozilla
52 
53 #endif  // mozilla_dom_UnionMember_h
54