]> git.lyx.org Git - lyx.git/blob - src/support/limited_stack.h
Add forgotten zlib includes
[lyx.git] / src / support / limited_stack.h
1 // -*- C++ -*-
2 /**
3  * \file limited_stack.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef LIMITED_STACK_H
13 #define LIMITED_STACK_H
14
15 #include <deque>
16
17
18 namespace lyx {
19
20 /**
21  * limited_stack - A stack of limited size.
22  *
23  * Like a normal stack, but elements falling out
24  * of the bottom are destructed.
25  */
26 template <typename T>
27 class limited_stack {
28 public:
29         typedef std::deque<T> container_type;
30         typedef typename container_type::value_type value_type;
31         typedef typename container_type::size_type size_type;
32         typedef typename container_type::const_iterator const_iterator;
33
34         /// limit is the maximum size of the stack
35         limited_stack(size_type limit = 100) {
36                 limit_ = limit;
37         }
38
39         /// Return the top element.
40         value_type & top() {
41                 return c_.front();
42         }
43
44         /// Pop and throw away the top element.
45         void pop() {
46                 c_.pop_front();
47         }
48
49         /// Return true if the stack is empty.
50         bool empty() const {
51                 return c_.empty();
52         }
53
54         /// Clear all elements, deleting them.
55         void clear() {
56                 c_.clear();
57         }
58
59         /// Push an item on to the stack, deleting the
60         /// bottom item on overflow.
61         void push(value_type const & v) {
62                 c_.push_front(v);
63                 if (c_.size() > limit_) {
64                         c_.pop_back();
65                 }
66         }
67
68         /// Direct read access to intermediate elements.
69         T const & operator[](size_type pos) const {
70                 return c_[pos];
71         }
72
73         /// Read access to used size.
74         size_type size() const {
75                 return c_.size();
76         }
77
78         const_iterator begin() const {
79                 return c_.begin();
80         }
81
82         const_iterator end() const {
83                 return c_.end();
84         }
85
86 private:
87         /// Internal contents.
88         container_type c_;
89         /// The maximum number elements stored.
90         size_type limit_;
91 };
92
93 // Make pointer type an error.
94 template <typename T>
95 class limited_stack<T*>;
96
97
98 } // namespace lyx
99
100 #endif // LIMITED_STACK_H