dom/xslt/base/txStack.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.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef txStack_h___
michael@0 7 #define txStack_h___
michael@0 8
michael@0 9 #include "nsTArray.h"
michael@0 10
michael@0 11 class txStack : private nsTArray<void*>
michael@0 12 {
michael@0 13 public:
michael@0 14 /**
michael@0 15 * Returns the specified object from the top of this stack,
michael@0 16 * without removing it from the stack.
michael@0 17 *
michael@0 18 * @return a pointer to the object that is the top of this stack.
michael@0 19 */
michael@0 20 inline void* peek()
michael@0 21 {
michael@0 22 NS_ASSERTION(!isEmpty(), "peeking at empty stack");
michael@0 23 return !isEmpty() ? ElementAt(Length() - 1) : nullptr;
michael@0 24 }
michael@0 25
michael@0 26 /**
michael@0 27 * Adds the specified object to the top of this stack.
michael@0 28 *
michael@0 29 * @param obj a pointer to the object that is to be added to the
michael@0 30 * top of this stack.
michael@0 31 */
michael@0 32 inline nsresult push(void* aObject)
michael@0 33 {
michael@0 34 return AppendElement(aObject) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
michael@0 35 }
michael@0 36
michael@0 37 /**
michael@0 38 * Removes and returns the specified object from the top of this
michael@0 39 * stack.
michael@0 40 *
michael@0 41 * @return a pointer to the object that was the top of this stack.
michael@0 42 */
michael@0 43 inline void* pop()
michael@0 44 {
michael@0 45 void* object = nullptr;
michael@0 46 NS_ASSERTION(!isEmpty(), "popping from empty stack");
michael@0 47 if (!isEmpty())
michael@0 48 {
michael@0 49 const uint32_t count = Length() - 1;
michael@0 50 object = ElementAt(count);
michael@0 51 RemoveElementAt(count);
michael@0 52 }
michael@0 53 return object;
michael@0 54 }
michael@0 55
michael@0 56 /**
michael@0 57 * Returns true if there are no objects in the stack.
michael@0 58 *
michael@0 59 * @return true if there are no objects in the stack.
michael@0 60 */
michael@0 61 inline bool isEmpty()
michael@0 62 {
michael@0 63 return IsEmpty();
michael@0 64 }
michael@0 65
michael@0 66 /**
michael@0 67 * Returns the number of elements in the Stack.
michael@0 68 *
michael@0 69 * @return the number of elements in the Stack.
michael@0 70 */
michael@0 71 inline int32_t size()
michael@0 72 {
michael@0 73 return Length();
michael@0 74 }
michael@0 75
michael@0 76 private:
michael@0 77 friend class txStackIterator;
michael@0 78 };
michael@0 79
michael@0 80 class txStackIterator
michael@0 81 {
michael@0 82 public:
michael@0 83 /**
michael@0 84 * Creates an iterator for the given stack.
michael@0 85 *
michael@0 86 * @param aStack the stack to create an iterator for.
michael@0 87 */
michael@0 88 inline
michael@0 89 txStackIterator(txStack* aStack) : mStack(aStack),
michael@0 90 mPosition(0)
michael@0 91 {
michael@0 92 }
michael@0 93
michael@0 94 /**
michael@0 95 * Returns true if there is more objects on the stack.
michael@0 96 *
michael@0 97 * @return .
michael@0 98 */
michael@0 99 inline bool hasNext()
michael@0 100 {
michael@0 101 return (mPosition < mStack->Length());
michael@0 102 }
michael@0 103
michael@0 104 /**
michael@0 105 * Returns the next object pointer from the stack.
michael@0 106 *
michael@0 107 * @return .
michael@0 108 */
michael@0 109 inline void* next()
michael@0 110 {
michael@0 111 if (mPosition == mStack->Length()) {
michael@0 112 return nullptr;
michael@0 113 }
michael@0 114 return mStack->ElementAt(mPosition++);
michael@0 115 }
michael@0 116
michael@0 117 private:
michael@0 118 txStack* mStack;
michael@0 119 uint32_t mPosition;
michael@0 120 };
michael@0 121
michael@0 122 #endif /* txStack_h___ */

mercurial