dom/bindings/UnionMember.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/bindings/UnionMember.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,59 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/* A class for holding the members of a union. */
    1.11 +
    1.12 +#ifndef mozilla_dom_UnionMember_h
    1.13 +#define mozilla_dom_UnionMember_h
    1.14 +
    1.15 +#include "mozilla/Alignment.h"
    1.16 +
    1.17 +namespace mozilla {
    1.18 +namespace dom {
    1.19 +
    1.20 +// The union type has an enum to keep track of which of its UnionMembers has
    1.21 +// been constructed.
    1.22 +template<class T>
    1.23 +class UnionMember
    1.24 +{
    1.25 +  AlignedStorage2<T> mStorage;
    1.26 +
    1.27 +public:
    1.28 +  T& SetValue()
    1.29 +  {
    1.30 +    new (mStorage.addr()) T();
    1.31 +    return *mStorage.addr();
    1.32 +  }
    1.33 +  template <typename T1>
    1.34 +  T& SetValue(const T1& aValue)
    1.35 +  {
    1.36 +    new (mStorage.addr()) T(aValue);
    1.37 +    return *mStorage.addr();
    1.38 +  }
    1.39 +  template<typename T1, typename T2>
    1.40 +  T& SetValue(const T1& aValue1, const T2& aValue2)
    1.41 +  {
    1.42 +    new (mStorage.addr()) T(aValue1, aValue2);
    1.43 +    return *mStorage.addr();
    1.44 +  }
    1.45 +  T& Value()
    1.46 +  {
    1.47 +    return *mStorage.addr();
    1.48 +  }
    1.49 +  const T& Value() const
    1.50 +  {
    1.51 +    return *mStorage.addr();
    1.52 +  }
    1.53 +  void Destroy()
    1.54 +  {
    1.55 +    mStorage.addr()->~T();
    1.56 +  }
    1.57 +};
    1.58 +
    1.59 +} // namespace dom
    1.60 +} // namespace mozilla
    1.61 +
    1.62 +#endif // mozilla_dom_UnionMember_h

mercurial