]> git.lyx.org Git - lyx.git/blobdiff - src/support/limited_stack.h
Remove non-copyable idioms
[lyx.git] / src / support / limited_stack.h
index 0c5c093c54e12f8b69b8b9bfb1cc5007bc4f6f1b..b17eeae19dbca92efecaedff761a8f40d73d8744 100644 (file)
@@ -6,16 +6,19 @@
  *
  * \author John Levon
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #ifndef LIMITED_STACK_H
 #define LIMITED_STACK_H
 
-#include <list>
+#include <deque>
+
+
+namespace lyx {
 
 /**
- * limited_stack - a stack of limited size
+ * limited_stack - A stack of limited size.
  *
  * Like a normal stack, but elements falling out
  * of the bottom are destructed.
 template <typename T>
 class limited_stack {
 public:
-       typedef std::list<T> container_type;
+       typedef std::deque<T> container_type;
        typedef typename container_type::value_type value_type;
        typedef typename container_type::size_type size_type;
+       typedef typename container_type::const_iterator const_iterator;
 
        /// limit is the maximum size of the stack
        limited_stack(size_type limit = 100) {
                limit_ = limit;
        }
 
-       /// return the top element
-       value_type top() {
+       /// Return the top element.
+       value_type top() {
                return c_.front();
        }
 
-       /// pop and throw away the top element
+       /// Pop and throw away the top element.
        void pop() {
                c_.pop_front();
        }
 
-       /// return true if the stack is empty
+       /// Return true if the stack is empty.
        bool empty() const {
-               return c_.size() == 0;
+               return c_.empty();
        }
 
-       /// clear all elements, deleting them
+       /// Clear all elements, deleting them.
        void clear() {
-               while (!c_.empty()) {
-                       c_.pop_back();
-               }
+               c_.clear();
        }
 
-       /// push an item on to the stack, deleting the
+       /// Push an item on to the stack, deleting the
        /// bottom item on overflow.
        void push(value_type const & v) {
                c_.push_front(v);
@@ -63,15 +65,36 @@ public:
                }
        }
 
+       /// Direct read access to intermediate elements.
+       T const & operator[](size_type pos) const {
+               return c_[pos];
+       }
+
+       /// Read access to used size.
+       size_type size() const {
+               return c_.size();
+       }
+
+       const_iterator begin() const {
+               return c_.begin();
+       }
+
+       const_iterator end() const {
+               return c_.end();
+       }
+
 private:
-       /// internal contents
+       /// Internal contents.
        container_type c_;
-       /// the maximum number elements stored
+       /// The maximum number elements stored.
        size_type limit_;
 };
 
-// make pointer type an error.
+// Make pointer type an error.
 template <typename T>
 class limited_stack<T*>;
 
+
+} // namespace lyx
+
 #endif // LIMITED_STACK_H