]> git.lyx.org Git - lyx.git/blob - src/support/limited_stack.h
14eef6e0c65dbbde96e278203c2faecbe6ec34d9
[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 /**
16  * limited_stack - a stack of limited size
17  *
18  * Like a normal stack, but elements falling out
19  * of the bottom are destructed.
20  */
21 template <typename T>
22 class limited_stack {
23 public:
24         typedef std::list<T> container_type;
25         typedef typename container_type::value_type value_type;
26         typedef typename container_type::size_type size_type;
27  
28         /// limit is the maximum size of the stack
29         limited_stack(size_type limit = 100) {
30                 limit_ = limit;
31         }
32
33         /// return the top element
34         value_type top() {
35                 return c_.front();
36         }
37
38         /// pop and throw away the top element
39         void pop() {
40                 c_.pop_front();
41         }
42  
43         /// return true if the stack is empty
44         bool empty() const {
45                 return c_.size() == 0;
46         }
47
48         /// clear all elements, deleting them
49         void clear() {
50                 while (!c_.empty()) {
51                         c_.pop_back();
52                 }
53         }
54  
55         /// push an item on to the stack, deleting the
56         /// bottom item on overflow.
57         void push(value_type const & v) {
58                 c_.push_front(v);
59                 if (c_.size() > limit_) {
60                         c_.pop_back(); 
61                 }
62         }
63  
64 private:
65         /// internal contents
66         container_type c_;
67         /// the maximum number elements stored
68         size_type limit_;
69 };
70
71 // make pointer type an error.
72 template <typename T>
73 class limited_stack<T*>;
74  
75 #endif // LIMITED_STACK_H