stack.hh

Go to the documentation of this file.
00001 /*
00002    The lestes compiler suite
00003    Copyright (C) 2002, 2003, 2004, 2005 Miroslav Tichy
00004    Copyright (C) 2002, 2003, 2004, 2005 Petr Zika
00005    Copyright (C) 2002, 2003, 2004, 2005 Vojtech Hala
00006    Copyright (C) 2002, 2003, 2004, 2005 Jiri Kosina
00007    Copyright (C) 2002, 2003, 2004, 2005 Pavel Sanda
00008    Copyright (C) 2002, 2003, 2004, 2005 Jan Zouhar
00009    Copyright (C) 2002, 2003, 2004, 2005 Rudolf Thomas
00010 
00011    This program is free software; you can redistribute it and/or modify
00012    it under the terms of the GNU General Public License as published by
00013    the Free Software Foundation; version 2 of the License.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    See the full text of the GNU General Public License version 2, and
00021    the limitations in the file doc/LICENSE.
00022 
00023    By accepting the license the licensee waives any and all claims
00024    against the copyright holder(s) related in whole or in part to the
00025    work, its use, and/or the inability to use it.
00026  
00027  */
00028 #ifndef lestes__std___stack_hh___included
00029 #define lestes__std___stack_hh___included
00030 
00031 #include <lestes/common.hh>
00032 #include <lestes/std/reflect.hh>
00033 #include <lestes/std/objectize.hh>
00034 #include <lestes/std/data_types.hh>
00035 #include <lestes/std/list.hh>
00036 #include <lestes/std/pair.hh>
00037 #include <lestes/std/lassert.hh>
00038 //#include <lestes/std/collection_refl.hh>
00039 #include <stack>
00040 #include <iterator>
00041 #include <algorithm>
00042 
00043 /*! \file
00044   \brief Collectible ::std::stack
00045   \author TMA
00046 
00047  */
00048 
00049 package(lestes);
00050 package(std);
00051 
00052 /*!
00053   \brief Collectible stack of entities
00054 
00055   An analogon for STL stack class tailored to be compatible with our garbage
00056   collector. Includes namely marking routine and factory method.
00057 
00058   In addition to ::std::stack an iterator is provided.  The sense of the
00059   iteration is reversed with respect to the underlying container.  Thus the
00060   ``normal'' iteration starts at the stack top, which is the back of the
00061   underlying container.
00062 
00063  */
00064 template <class T, class Container = list<T> >
00065 class stack : public object {
00066 public:
00067         typedef typename Container::value_type value_type;
00068         typedef typename Container::size_type size_type;
00069         typedef Container container_type;
00070         typedef typename Container::const_reverse_iterator const_iterator;
00071         typedef typename Container::reverse_iterator iterator;
00072         typedef typename Container::const_iterator const_reverse_iterator;
00073         typedef typename Container::iterator reverse_iterator;
00074 
00075         static ptr < stack < T > > create(ptr < Container > container = Container::create())
00076         { return new stack < T > (container); }
00077 protected:
00078         srp < Container > c;
00079         /*explicit*/ stack<T, Container >(ptr < Container > container)
00080                 : c(container) {}
00081 private:
00082         static ptr < object::reflection_list > reflection;
00083 public:
00084         virtual ptr<object::reflection_list> reflection_get() const;
00085         virtual ptr<object::field_list_list> field_values_get() const;
00086         virtual void gc_mark(void)
00087         { c.gc_mark(); return object::gc_mark(); }
00088 
00089         bool empty() const
00090         { return c->empty(); }
00091         size_type size() const
00092         { return c->size(); }
00093         value_type& top()
00094         { lassert(!empty()); return c->back(); }
00095         const value_type& top() const
00096         { lassert(!empty()); return c->back(); }
00097         void push(const value_type& x)
00098         { c->push_back(x); }
00099         void pop()
00100         { lassert(!empty()); c->pop_back(); }
00101 
00102         const_iterator begin() const
00103         { return c->rbegin(); }
00104         iterator begin()
00105         { return c->rbegin(); }
00106         const_iterator end() const
00107         { return c->rend(); }
00108         iterator end()
00109         { return c->rend(); }
00110 
00111         const_reverse_iterator rbegin() const
00112         { return c->begin(); }
00113         reverse_iterator rbegin()
00114         { return c->begin(); }
00115         const_reverse_iterator rend() const
00116         { return c->end(); }
00117         reverse_iterator rend()
00118         { return c->end(); }
00119 };
00120 
00121 template < typename T, typename C >
00122 ptr < object::reflection_list > stack < T, C > :: reflection;
00123 
00124 template< typename T, typename C >
00125 ptr<object::reflection_list> stack< T, C >::reflection_get() const
00126 {
00127         if (!reflection) {
00128                 typedef class_reflection::field_metadata md;
00129                 typedef class_reflection::field_metadata_list mdlist;
00130                 ptr<mdlist> mdl = mdlist::create();
00131                 mdl->push_back( md::create( "c", "Collection &lt; T &gt;" ) );
00132                 reflection = reflection_list::create( object::reflection_get() );
00133                 reflection->push_back( class_reflection::create( "stack_of_T", mdl ) );
00134         }
00135         return reflection;
00136 }
00137 
00138 template< typename T, typename C >
00139 ptr<object::field_list_list> stack< T, C >::field_values_get() const
00140 {
00141         ptr<field_list_list> result = object::field_values_get();
00142         result->push_back( value_list::create() );
00143         result->back()->push_back( c );
00144         return result;
00145 }
00146 
00147 end_package(std);
00148 end_package(lestes);
00149 
00150 #endif  // lestes__std___stack_hh___included
00151 /* vim: set ft=lestes : */

Generated on Mon Feb 12 18:23:36 2007 for lestes by doxygen 1.5.1-20070107