mfbt/TypedEnumInternal.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: 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/. */
     7 /* Internal stuff needed by TypedEnum.h and TypedEnumBits.h. */
     9 // NOTE: When we can assume C++11 enum class support and TypedEnum.h goes away,
    10 // we should then consider folding TypedEnumInternal.h into TypedEnumBits.h.
    12 #ifndef mozilla_TypedEnumInternal_h
    13 #define mozilla_TypedEnumInternal_h
    15 #include "mozilla/Attributes.h"
    17 #if defined(__cplusplus)
    19 #if defined(__clang__)
    20    /*
    21     * Per Clang documentation, "Note that marketing version numbers should not
    22     * be used to check for language features, as different vendors use different
    23     * numbering schemes. Instead, use the feature checking macros."
    24     */
    25 #  ifndef __has_extension
    26 #    define __has_extension __has_feature /* compatibility, for older versions of clang */
    27 #  endif
    28 #  if __has_extension(cxx_strong_enums)
    29 #    define MOZ_HAVE_CXX11_ENUM_TYPE
    30 #    define MOZ_HAVE_CXX11_STRONG_ENUMS
    31 #  endif
    32 #elif defined(__GNUC__)
    33 #  if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
    34 #    if MOZ_GCC_VERSION_AT_LEAST(4, 6, 3)
    35 #      define MOZ_HAVE_CXX11_ENUM_TYPE
    36 #      define MOZ_HAVE_CXX11_STRONG_ENUMS
    37 #    endif
    38 #  endif
    39 #elif defined(_MSC_VER)
    40 #  if _MSC_VER >= 1400
    41 #    define MOZ_HAVE_CXX11_ENUM_TYPE
    42 #  endif
    43 #  if _MSC_VER >= 1700
    44 #    define MOZ_HAVE_CXX11_STRONG_ENUMS
    45 #  endif
    46 #endif
    48 namespace mozilla {
    50 /*
    51  * The problem that CastableTypedEnumResult aims to solve is that
    52  * typed enums are not convertible to bool, and there is no way to make them
    53  * be, yet user code wants to be able to write
    54  *
    55  *   if (myFlags & Flags::SOME_PARTICULAR_FLAG)              (1)
    56  *
    57  * There are different approaches to solving this. Most of them require
    58  * adapting user code. For example, we could implement operator! and have
    59  * the user write
    60  *
    61  *   if (!!(myFlags & Flags::SOME_PARTICULAR_FLAG))          (2)
    62  *
    63  * Or we could supply a IsNonZero() or Any() function returning whether
    64  * an enum value is nonzero, and have the user write
    65  *
    66  *   if (Any(Flags & Flags::SOME_PARTICULAR_FLAG))           (3)
    67  *
    68  * But instead, we choose to preserve the original user syntax (1) as it
    69  * is inherently more readable, and to ease porting existing code to typed
    70  * enums. We achieve this by having operator& and other binary bitwise
    71  * operators have as return type a class, CastableTypedEnumResult,
    72  * that wraps a typed enum but adds bool convertibility.
    73  */
    74 template<typename E>
    75 class CastableTypedEnumResult
    76 {
    77   private:
    78     const E mValue;
    80   public:
    81     explicit MOZ_CONSTEXPR CastableTypedEnumResult(E value)
    82       : mValue(value)
    83     {}
    85     MOZ_CONSTEXPR operator E() const { return mValue; }
    87     template<typename DestinationType>
    88     MOZ_EXPLICIT_CONVERSION MOZ_CONSTEXPR
    89     operator DestinationType() const {
    90       return DestinationType(mValue);
    91     }
    93     MOZ_CONSTEXPR bool operator !() const { return !bool(mValue); }
    95 #ifndef MOZ_HAVE_CXX11_STRONG_ENUMS
    96     // This get() method is used to implement a constructor in the
    97     // non-c++11 fallback path for MOZ_BEGIN_ENUM_CLASS, taking a
    98     // CastableTypedEnumResult. If we try to implement it using the
    99     // above conversion operator E(), then at least clang 3.3
   100     // (when forced to take the non-c++11 fallback path) compiles
   101     // this constructor to an infinite recursion. So we introduce this
   102     // get() method, that does exactly the same as the conversion operator,
   103     // to work around this.
   104     MOZ_CONSTEXPR E get() const { return mValue; }
   105 #endif
   106 };
   108 } // namespace mozilla
   110 #endif // __cplusplus
   112 #endif // mozilla_TypedEnumInternal_h

mercurial