Home | History | Annotate | Download | only in data
      1 # C++ skeleton for Bison
      2 
      3 # Copyright (C) 2002-2012 Free Software Foundation, Inc.
      4 
      5 # This program is free software: you can redistribute it and/or modify
      6 # it under the terms of the GNU General Public License as published by
      7 # the Free Software Foundation, either version 3 of the License, or
      8 # (at your option) any later version.
      9 #
     10 # This program is distributed in the hope that it will be useful,
     11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 # GNU General Public License for more details.
     14 #
     15 # You should have received a copy of the GNU General Public License
     16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 m4_pushdef([b4_copyright_years],
     19            [2002-2012])
     20 
     21 b4_output_begin([b4_dir_prefix[]stack.hh])
     22 b4_copyright([Stack handling for Bison parsers in C++],
     23              [2002-2012])[
     24 
     25 /**
     26  ** \file ]b4_dir_prefix[stack.hh
     27  ** Define the ]b4_namespace_ref[::stack class.
     28  */
     29 
     30 ]b4_cpp_guard_open([b4_dir_prefix[]stack.hh])[
     31 
     32 # include <deque>
     33 
     34 ]b4_namespace_open[
     35   template <class T, class S = std::deque<T> >
     36   class stack
     37   {
     38   public:
     39     // Hide our reversed order.
     40     typedef typename S::reverse_iterator iterator;
     41     typedef typename S::const_reverse_iterator const_iterator;
     42 
     43     stack () : seq_ ()
     44     {
     45     }
     46 
     47     stack (unsigned int n) : seq_ (n)
     48     {
     49     }
     50 
     51     inline
     52     T&
     53     operator [] (unsigned int i)
     54     {
     55       return seq_[i];
     56     }
     57 
     58     inline
     59     const T&
     60     operator [] (unsigned int i) const
     61     {
     62       return seq_[i];
     63     }
     64 
     65     inline
     66     void
     67     push (const T& t)
     68     {
     69       seq_.push_front (t);
     70     }
     71 
     72     inline
     73     void
     74     pop (unsigned int n = 1)
     75     {
     76       for (; n; --n)
     77         seq_.pop_front ();
     78     }
     79 
     80     inline
     81     unsigned int
     82     height () const
     83     {
     84       return seq_.size ();
     85     }
     86 
     87     inline const_iterator begin () const { return seq_.rbegin (); }
     88     inline const_iterator end () const { return seq_.rend (); }
     89 
     90   private:
     91     S seq_;
     92   };
     93 
     94   /// Present a slice of the top of a stack.
     95   template <class T, class S = stack<T> >
     96   class slice
     97   {
     98   public:
     99     slice (const S& stack, unsigned int range)
    100       : stack_ (stack)
    101       , range_ (range)
    102     {
    103     }
    104 
    105     inline
    106     const T&
    107     operator [] (unsigned int i) const
    108     {
    109       return stack_[range_ - i];
    110     }
    111 
    112   private:
    113     const S& stack_;
    114     unsigned int range_;
    115   };
    116 ]b4_namespace_close[
    117 
    118 ]b4_cpp_guard_close([b4_dir_prefix[]stack.hh])
    119 b4_output_end()
    120 
    121 m4_popdef([b4_copyright_years])
    122