]> git.lyx.org Git - lyx.git/blob - src/support/limited_stack.h
DocBook: simplify code to handle abstracts.
[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) : limit_(limit) {}
36
37         /// Return the top element.
38         value_type & top() {
39                 return c_.front();
40         }
41
42         /// Pop and throw away the top element.
43         void pop() {
44                 c_.pop_front();
45         }
46
47         /// Return true if the stack is empty.
48         bool empty() const {
49                 return c_.empty();
50         }
51
52         /// Clear all elements, deleting them.
53         void clear() {
54                 c_.clear();
55         }
56
57         /// Push an item on to the stack, deleting the
58         /// bottom item on overflow.
59         void push(value_type const & v) {
60                 c_.push_front(v);
61                 if (c_.size() > limit_) {
62                         c_.pop_back();
63                 }
64         }
65
66         /// Direct read access to intermediate elements.
67         T const & operator[](size_type pos) const {
68                 return c_[pos];
69         }
70
71         /// Read access to used size.
72         size_type size() const {
73                 return c_.size();
74         }
75
76         const_iterator begin() const {
77                 return c_.begin();
78         }
79
80         const_iterator end() const {
81                 return c_.end();
82         }
83
84 private:
85         /// Internal contents.
86         container_type c_;
87         /// The maximum number elements stored.
88         size_type limit_;
89 };
90
91 // Make pointer type an error.
92 template <typename T>
93 class limited_stack<T*>;
94
95
96 } // namespace lyx
97
98 #endif // LIMITED_STACK_H