]> git.lyx.org Git - lyx.git/blob - src/support/limited_stack.h
Undo /does/ seem to still work properly :)
[lyx.git] / src / support / limited_stack.h
1 // -*- C++ -*-
2 /**
3  * \file limited_stack.h
4  * Copyright 2002 the LyX Team
5  * Read the file COPYING
6  *
7  * \author John Levon <moz@compsoc.man.ac.uk>
8  */
9
10 #ifndef LIMITED_STACK_H
11 #define LIMITED_STACK_H
12
13 #include <list>
14
15 #include <boost/shared_ptr.hpp>
16  
17 /**
18  * limited_stack - a stack of limited size
19  *
20  * Like a normal stack, but only accepts pointer types,
21  * and bottom elements are deleted on overflow
22  */
23 template <typename T>
24 class limited_stack {
25 public:
26         typedef std::list<T> container_type;
27         typedef typename container_type::value_type value_type;
28         typedef typename container_type::size_type size_type;
29  
30         /// limit is the maximum size of the stack
31         limited_stack(size_type limit = 10) {
32                 limit_ = limit;
33         }
34
35         /// return the top element
36         value_type top() {
37                 return c_.front();
38         }
39
40         /// pop and throw away the top element
41         void pop() {
42                 c_.pop_front();
43         }
44  
45         /// return true if the stack is empty
46         bool empty() const {
47                 return c_.size() == 0;
48         }
49
50         /// clear all elements, deleting them
51         void clear() {
52                 while (!c_.empty()) {
53                         c_.pop_back();
54                 }
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 private:
67         /// internal contents
68         container_type c_;
69         /// the maximum number elements stored
70         size_type limit_;
71 };
72
73 // make pointer type an error.
74 template <typename T>
75 class limited_stack<T*>;
76  
77 #endif // LIMITED_STACK_H