UnionMember.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* -*- Mode: C++; tab-width: 8; 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. /* A class for holding the members of a union. */
  6. #ifndef mozilla_dom_UnionMember_h
  7. #define mozilla_dom_UnionMember_h
  8. #include "mozilla/Alignment.h"
  9. namespace mozilla {
  10. namespace dom {
  11. // The union type has an enum to keep track of which of its UnionMembers has
  12. // been constructed.
  13. template<class T>
  14. class UnionMember
  15. {
  16. AlignedStorage2<T> mStorage;
  17. public:
  18. T& SetValue()
  19. {
  20. new (mStorage.addr()) T();
  21. return *mStorage.addr();
  22. }
  23. template <typename T1>
  24. T& SetValue(const T1& aValue)
  25. {
  26. new (mStorage.addr()) T(aValue);
  27. return *mStorage.addr();
  28. }
  29. template<typename T1, typename T2>
  30. T& SetValue(const T1& aValue1, const T2& aValue2)
  31. {
  32. new (mStorage.addr()) T(aValue1, aValue2);
  33. return *mStorage.addr();
  34. }
  35. T& Value()
  36. {
  37. return *mStorage.addr();
  38. }
  39. const T& Value() const
  40. {
  41. return *mStorage.addr();
  42. }
  43. void Destroy()
  44. {
  45. mStorage.addr()->~T();
  46. }
  47. };
  48. } // namespace dom
  49. } // namespace mozilla
  50. #endif // mozilla_dom_UnionMember_h