Line data Source code
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 : {
22 : AlignedStorage2<T> mStorage;
23 :
24 : // Copy construction can't be supported because C++ requires that any enclosed
25 : // T be initialized in a way C++ knows about -- that is, by |new| or similar.
26 : UnionMember(const UnionMember&) = delete;
27 :
28 : public:
29 : UnionMember() = default;
30 : ~UnionMember() = default;
31 :
32 324 : T& SetValue()
33 : {
34 324 : new (mStorage.addr()) T();
35 324 : return *mStorage.addr();
36 : }
37 : template <typename T1>
38 0 : T& SetValue(const T1& aValue)
39 : {
40 0 : new (mStorage.addr()) T(aValue);
41 0 : return *mStorage.addr();
42 : }
43 : template<typename T1, typename T2>
44 0 : T& SetValue(const T1& aValue1, const T2& aValue2)
45 : {
46 0 : new (mStorage.addr()) T(aValue1, aValue2);
47 0 : return *mStorage.addr();
48 : }
49 0 : T& Value()
50 : {
51 0 : return *mStorage.addr();
52 : }
53 243 : const T& Value() const
54 : {
55 243 : return *mStorage.addr();
56 : }
57 324 : void Destroy()
58 : {
59 324 : mStorage.addr()->~T();
60 324 : }
61 : };
62 :
63 : } // namespace dom
64 : } // namespace mozilla
65 :
66 : #endif // mozilla_dom_UnionMember_h
|