Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / LinkedStack.h
blob44a1e3e679d4f0d2366ebdf82a59d96b5a337023
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef LinkedStack_h
32 #define LinkedStack_h
34 #include "wtf/FastAllocBase.h"
35 #include "wtf/OwnPtr.h"
37 namespace WTF {
39 template <typename T>
40 class LinkedStack {
41 WTF_MAKE_FAST_ALLOCATED(LinkedStack);
42 public:
43 LinkedStack() : m_size(0) { }
45 // Iterative cleanup to prevent stack overflow problems.
46 ~LinkedStack()
48 OwnPtr<Node> ptr = m_head.release();
49 while (ptr)
50 ptr = ptr->m_next.release();
53 bool isEmpty();
55 void push(const T&);
56 const T& peek();
57 void pop();
59 size_t size();
61 // This inner class used to be private but is now public on account of a
62 // possible MSVC bug. It can be made private again if we get rid of
63 // WTF_MAKE_FAST_ALLOCATED ever.
64 class Node {
65 WTF_MAKE_FAST_ALLOCATED(LinkedStack::Node);
66 public:
67 Node(const T&, PassOwnPtr<Node> next);
69 T m_data;
70 OwnPtr<Node> m_next;
73 private:
74 OwnPtr<Node> m_head;
75 size_t m_size;
78 template <typename T>
79 LinkedStack<T>::Node::Node(const T& data, PassOwnPtr<Node> next)
80 : m_data(data)
81 , m_next(next)
85 template <typename T>
86 inline bool LinkedStack<T>::isEmpty()
88 return !m_head;
91 template <typename T>
92 inline void LinkedStack<T>::push(const T& data)
94 m_head = adoptPtr(new Node(data, m_head.release()));
95 ++m_size;
98 template <typename T>
99 inline const T& LinkedStack<T>::peek()
101 return m_head->m_data;
104 template <typename T>
105 inline void LinkedStack<T>::pop()
107 ASSERT(m_head && m_size);
108 m_head = m_head->m_next.release();
109 --m_size;
112 template <typename T>
113 inline size_t LinkedStack<T>::size()
115 return m_size;
120 using WTF::LinkedStack;
122 #endif