dom/bindings/Nullable.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
     2 /* vim: set ts=2 sw=2 et tw=79: */
     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 file,
     5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #ifndef mozilla_dom_Nullable_h
     8 #define mozilla_dom_Nullable_h
    10 #include "mozilla/Assertions.h"
    11 #include "nsTArrayForwardDeclare.h"
    12 #include "mozilla/Move.h"
    14 namespace mozilla {
    15 namespace dom {
    17 // Support for nullable types
    18 template <typename T>
    19 struct Nullable
    20 {
    21 private:
    22   // mIsNull MUST COME FIRST because otherwise the casting in our array
    23   // conversion operators would shift where it is found in the struct.
    24   bool mIsNull;
    25   T mValue;
    27 public:
    28   Nullable()
    29     : mIsNull(true)
    30   {}
    32   explicit Nullable(T aValue)
    33     : mIsNull(false)
    34     , mValue(aValue)
    35   {}
    37   explicit Nullable(Nullable<T>&& aOther)
    38     : mIsNull(aOther.mIsNull)
    39     , mValue(mozilla::Move(aOther.mValue))
    40   {}
    42   Nullable(const Nullable<T>& aOther)
    43     : mIsNull(aOther.mIsNull)
    44     , mValue(aOther.mValue)
    45   {}
    47   void operator=(const Nullable<T>& aOther)
    48   {
    49     mIsNull = aOther.mIsNull;
    50     mValue = aOther.mValue;
    51   }
    53   void SetValue(T aValue) {
    54     mValue = aValue;
    55     mIsNull = false;
    56   }
    58   // For cases when |T| is some type with nontrivial copy behavior, we may want
    59   // to get a reference to our internal copy of T and work with it directly
    60   // instead of relying on the copying version of SetValue().
    61   T& SetValue() {
    62     mIsNull = false;
    63     return mValue;
    64   }
    66   void SetNull() {
    67     mIsNull = true;
    68   }
    70   const T& Value() const {
    71     MOZ_ASSERT(!mIsNull);
    72     return mValue;
    73   }
    75   T& Value() {
    76     MOZ_ASSERT(!mIsNull);
    77     return mValue;
    78   }
    80   bool IsNull() const {
    81     return mIsNull;
    82   }
    84   bool Equals(const Nullable<T>& aOtherNullable) const
    85   {
    86     return (mIsNull && aOtherNullable.mIsNull) ||
    87            (!mIsNull && !aOtherNullable.mIsNull &&
    88             mValue == aOtherNullable.mValue);
    89   }
    91   bool operator==(const Nullable<T>& aOtherNullable) const
    92   {
    93     return Equals(aOtherNullable);
    94   }
    96   bool operator!=(const Nullable<T>& aOtherNullable) const
    97   {
    98     return !Equals(aOtherNullable);
    99   }
   101   // Make it possible to use a const Nullable of an array type with other
   102   // array types.
   103   template<typename U>
   104   operator const Nullable< nsTArray<U> >&() const {
   105     // Make sure that T is ok to reinterpret to nsTArray<U>
   106     const nsTArray<U>& arr = mValue;
   107     (void)arr;
   108     return *reinterpret_cast<const Nullable< nsTArray<U> >*>(this);
   109   }
   110   template<typename U>
   111   operator const Nullable< FallibleTArray<U> >&() const {
   112     // Make sure that T is ok to reinterpret to FallibleTArray<U>
   113     const FallibleTArray<U>& arr = mValue;
   114     (void)arr;
   115     return *reinterpret_cast<const Nullable< FallibleTArray<U> >*>(this);
   116   }
   117 };
   119 } // namespace dom
   120 } // namespace mozilla
   122 #endif /* mozilla_dom_Nullable_h */

mercurial