Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

xst_stack.h

Go to the documentation of this file.
00001 
00002 // Common Text Transformation Library
00003 // Copyright (C) 1997-2006 by Igor Kholodov. 
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the
00017 // Free Software Foundation, Inc.,
00018 // 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 //
00020 // mailto:cttl@users.sourceforge.net
00021 // http://sourceforge.net/projects/cttl/
00023 
00033 // xst_stack.h
00034 
00035 #ifndef _XST_STACK_H_INCLUDED_
00036 #define _XST_STACK_H_INCLUDED_
00037 
00038 namespace cttl_impl {
00039 
00040 
00053 template< typename StackT >
00054 struct xst_stack {
00055 
00056     // allow any lambda primitive to access stack primitive implementation
00057     template< typename D > friend struct xst_lambda_wrap;
00058 
00059     // allow any lambda primitive to access translator implementation
00060     template< typename D, typename T > friend struct xst_translator;
00061 
00062     enum {
00064         const_value_,
00065 
00067         depth_ = 1,
00068 
00070         capacity_ = 1,
00071 
00073         primitive_id_ = primitive_id_stack
00074     };
00075     
00077     typedef typename xst_storage_adaptor< StackT >::value_type stack_T;
00078 
00080     typedef typename stack_T::value_type value_T;
00081 
00083     typedef value_T dereferenced_value_T;
00084 
00086     typedef xst_stack< StackT > left_T;
00087 
00089     typedef xst_stack< StackT > right_T;
00090     
00092     typedef xst_stack< typename xst_storage_adaptor< StackT >::reference > reference_T;
00093 
00095     xst_stack()
00096     {
00097     }
00098 
00100     xst_stack( StackT stack_ )
00101         :
00102     m_stack( stack_ )
00103     {
00104     }
00105 
00112     xst_stack( value_T const& )
00113     {
00114     }
00115 
00117     xst_stack( xst_stack< StackT > const& other_ )
00118         :
00119     m_stack( other_.m_stack )
00120     {
00121     }
00122 
00124     reference_T make_reference() const
00125     {
00126         return reference_T( const_cast< xst_stack< StackT >* >( this )->m_stack.value() );
00127     }
00128     
00130     template<
00131         int LocationT
00132     >
00133     dereferenced_value_T& dereferenced_value( xst_lambda_wrap< xst_const_scalar< LocationT > > subscript_ )
00134     {
00135         return top( subscript_ );
00136     }
00137 
00139     // Direct access to stack primitive
00141 
00143     left_T& left_lambda()
00144     {
00145         return *this;
00146     }
00147 
00148 
00150     left_T const& left_lambda() const
00151     {
00152         return *this;
00153     }
00154 
00156     right_T& right_lambda()
00157     {
00158         return *this;
00159     }
00160 
00162     right_T const& right_lambda() const
00163     {
00164         return *this;
00165     }
00166 
00167 
00169     // subscript traversal algorithms
00171 
00173     template< typename FunctorT >
00174     static void subscript_top_down( FunctorT& functor_ )
00175     {
00176         functor_( const_scalar( 0 ) );
00177     }
00178 
00180     template< typename FunctorT >
00181     static void subscript_bottom_up( FunctorT& functor_ )
00182     {
00183         functor_( const_scalar( 0 ) );
00184     }
00185 
00187     // lambda compound traversal algorithms
00189 
00191     template< typename FunctorT >
00192     void traverse_bottom_up( FunctorT& functor_ )
00193     {
00194         functor_( *this );
00195     }
00196     
00198     template< typename FunctorT >
00199     void traverse_top_down( FunctorT& functor_ )
00200     {
00201         functor_( *this );
00202     }
00203     
00205     template< typename FunctorT >
00206     void traverse_bottom_up( FunctorT& functor_ ) const
00207     {
00208         functor_( *this );
00209     }
00210     
00212     template< typename FunctorT >
00213     void traverse_top_down( FunctorT& functor_ ) const
00214     {
00215         functor_( *this );
00216     }
00217     
00218 
00220     // Lambda primitive stack interface support
00222 
00224     template<
00225         int LocationT,
00226         typename InputValueT
00227     >
00228     void
00229     push(
00230         xst_lambda_wrap< xst_const_scalar< LocationT > >,
00231         InputValueT const& data_
00232         )
00233     {
00234         CTTL_TRACE_DEPOSIT_TEXT( "->" );
00235         CTTL_TRACE_DEPOSIT_TEXT( xst_storage_adaptor< StackT >::trace_char() );
00236         CTTL_TRACE_DEPOSIT_TEXT( "push$" );
00237         CTTL_TRACE_DEPOSIT_TEXT( xst_non_scalar_traits< InputValueT >::identity( data_ ) );
00238         CTTL_TRACE_DEPOSIT_TEXT( ' ' );
00239         xpush_descend< LocationT >( data_ );
00240     }
00241 
00243     template<
00244         int LocationT
00245     >
00246     void
00247     pop(
00248         xst_lambda_wrap< xst_const_scalar< LocationT > >
00249         )
00250         
00251     {
00252         xpop_descend< LocationT >();
00253     }
00254 
00256     template<
00257         int LocationT
00258     >
00259     value_T const& top(
00260         xst_lambda_wrap< xst_const_scalar< LocationT > >
00261         )
00262          const
00263     {
00264         return xtop_descend< LocationT >();
00265     }
00266 
00268     template<
00269         int LocationT
00270     >
00271     value_T& top(
00272         xst_lambda_wrap< xst_const_scalar< LocationT > >
00273         )
00274         
00275     {
00276         return xtop_descend< LocationT >();
00277     }
00278 
00280     template<
00281         int LocationT
00282     >
00283     size_t
00284     size(
00285         xst_lambda_wrap< xst_const_scalar< LocationT > >
00286         )
00287         const
00288     {
00289         return xsize_at_location< LocationT >();
00290     }
00291 
00293     template<
00294         int LocationT
00295     >
00296     stack_T const*
00297     stack_ptr(
00298         xst_lambda_wrap< xst_const_scalar< LocationT > >
00299         )
00300         const
00301     {
00302         return xstack_ptr_at_location< LocationT >();
00303     }
00304 
00306     template<
00307         int LocationT
00308     >
00309     stack_T*
00310     stack_ptr(
00311         xst_lambda_wrap< xst_const_scalar< LocationT > >
00312         )
00313     {
00314         return xstack_ptr_at_location< LocationT >();
00315     }
00316 
00317 #ifdef CTTL_LAMBDA_REFLECTION
00318 
00319     typedef xst_stack< StackT > reflection_T;
00320 
00322     reflection_T reflection() const
00323     {
00324         return *this;
00325     }
00326 #endif // CTTL_LAMBDA_REFLECTION
00327 
00328 private:
00330     // Implementation
00332 
00334     xst_storage_adaptor< StackT > m_stack;
00335 
00337     template< int LocationT, typename InputValueT >
00338     void xpush_at_location( InputValueT const& data_ )
00339     {
00340         assert( !LocationT );
00341         m_stack.value().push( data_ );
00342     }
00343 
00345     template< int LocationT >
00346     void xpop_at_location()
00347     {
00348         assert( !LocationT );
00349         CTTL_TRACE_DEPOSIT_TEXT( xst_storage_adaptor< StackT >::trace_char() );
00350         CTTL_TRACE_DEPOSIT_TEXT( "pop$" );
00351         assert( m_stack.const_value().size() );
00352         CTTL_TRACE_DEPOSIT_TEXT( xst_non_scalar_traits< value_T >::identity( m_stack.const_value().top() ) );
00353         CTTL_TRACE_DEPOSIT_TEXT( ' ' );
00354         m_stack.value().pop();
00355     }
00356 
00358     template< int LocationT >
00359     value_T const& xtop_at_location() const
00360     {
00361         assert( !LocationT );
00362         CTTL_TRACE_DEPOSIT_TEXT( xst_storage_adaptor< StackT >::trace_char() );
00363         CTTL_TRACE_DEPOSIT_TEXT( '$' );
00364         assert( m_stack.const_value().size() );
00365         CTTL_TRACE_DEPOSIT_TEXT( xst_non_scalar_traits< value_T >::identity( m_stack.const_value().top() ) );
00366         CTTL_TRACE_DEPOSIT_TEXT( ' ' );
00367         return m_stack.const_value().top();
00368     }
00369 
00371     template< int LocationT, typename InputValueT >
00372     void xpush_descend( InputValueT const& data_ )
00373     {
00374         xpush_at_location< LocationT >( data_ );
00375     }
00376 
00378     template< int LocationT >
00379     void xpop_descend()
00380     {
00381         xpop_at_location< LocationT >();
00382     }
00383 
00385     template< int LocationT >
00386     value_T const& xtop_descend() const
00387     {
00388         return xtop_at_location< LocationT >();
00389     }
00390 
00392     // subscript traversal algorithms
00394 
00396     template< typename SubscriptLambdaT, typename FunctorT >
00397     static void xsubscript_top_down_subtree_switch( SubscriptLambdaT subscript_, FunctorT& functor_ )
00398     {
00399         functor_( subscript_ );
00400     }
00401 
00403     template< int LocationT, typename FunctorT >
00404     static void xsubscript_top_down_descend( FunctorT& )
00405     {
00406         assert( false );
00407     }
00408 
00410     template< int LocationT, typename SubscriptLambdaT, typename FunctorT >
00411     static void xsubscript_top_down_subtree_descend( SubscriptLambdaT, FunctorT& )
00412     {
00413         assert( false );
00414     }
00415 
00417     // subscript bottom up traversal
00419 
00421     template< typename SubscriptLambdaT, typename FunctorT >
00422     static void xsubscript_bottom_up_subtree_switch( SubscriptLambdaT subscript_, FunctorT& functor_ )
00423     {
00424         functor_( subscript_ );
00425     }
00426 
00428     template< int LocationT, typename FunctorT >
00429     static void xsubscript_bottom_up_descend( FunctorT& )
00430     {
00431         assert( false );
00432     }
00433 
00435     template< int LocationT, typename SubscriptLambdaT, typename FunctorT >
00436     static void xsubscript_bottom_up_subtree_descend( SubscriptLambdaT, FunctorT& )
00437     {
00438         assert( false );
00439     }
00440 
00442     // Lambda primitive stack interface support
00444 
00446     template< typename SubscriptLambdaT, typename InputValueT >
00447     void xpush_sub_split( SubscriptLambdaT, InputValueT const& )
00448     {
00449         assert( false );
00450     }
00451 
00453     template< int LocationT, typename SubscriptLambdaT, typename InputValueT >
00454     void xpush_sub_descend( SubscriptLambdaT, InputValueT const& )
00455     {
00456         assert( false );
00457     }
00458 
00460     template< typename SubscriptLambdaT >
00461     void xpop_sub_split( SubscriptLambdaT )
00462     {
00463         assert( false );
00464     }
00465 
00467     template< int LocationT, typename SubscriptLambdaT >
00468     void xpop_sub_descend( SubscriptLambdaT )
00469     {
00470         assert( false );
00471     }
00472 
00474     template< typename SubscriptLambdaT >
00475     value_T const& xtop_sub_split( SubscriptLambdaT ) const
00476     {
00477         assert( false );
00478         return m_stack.const_value().top();
00479     }
00480 
00482     template< int LocationT, typename SubscriptLambdaT >
00483     value_T const& xtop_sub_descend( SubscriptLambdaT ) const
00484     {
00485         assert( false );
00486         return m_stack.const_value().top();
00487     }
00488 
00490     template< int LocationT >
00491     value_T& xtop_at_location()
00492     {
00493         assert( !LocationT );
00494         CTTL_TRACE_DEPOSIT_TEXT( "->" );
00495         CTTL_TRACE_DEPOSIT_TEXT( xst_storage_adaptor< StackT >::trace_char() );
00496         CTTL_TRACE_DEPOSIT_TEXT( '$' );
00497         assert( m_stack.const_value().size() );
00498         CTTL_TRACE_DEPOSIT_TEXT( xst_non_scalar_traits< value_T >::identity( m_stack.const_value().top() ) );
00499         CTTL_TRACE_DEPOSIT_TEXT( ' ' );
00500         return m_stack.value().top();
00501     }
00502 
00504     template< int LocationT >
00505     value_T& xtop_descend()
00506     {
00507         return xtop_at_location< LocationT >();
00508     }
00509 
00511     template< typename SubscriptLambdaT >
00512     value_T& xtop_sub_split( SubscriptLambdaT )
00513     {
00514         assert( false );
00515         return xtop_descend< 0 >();
00516     }
00517 
00519     template< int LocationT, typename SubscriptLambdaT >
00520     value_T& xtop_sub_descend( SubscriptLambdaT )
00521     {
00522         assert( false );
00523         return xtop_descend< 0 >();
00524     }
00525 
00527     template< int LocationT >
00528     size_t xsize_at_location() const
00529     {
00530         assert( !LocationT );
00531         return m_stack.const_value().size();
00532     }
00533 
00535     template< int LocationT >
00536     size_t xsize_descend() const
00537     {
00538         return xsize_at_location< LocationT >();
00539     }
00540 
00542     template< typename SubscriptLambdaT >
00543     size_t xsize_sub_split( SubscriptLambdaT ) const
00544     {
00545         assert( false );
00546         return 0;   // compile time optimization
00547     }
00548 
00550     template< int LocationT, typename SubscriptLambdaT >
00551     size_t xsize_sub_descend( SubscriptLambdaT ) const
00552     {
00553         assert( false );
00554         return 0;   // compile time optimization
00555     }
00556 
00558     template< int LocationT >
00559     stack_T const* xstack_ptr_at_location() const
00560     {
00561         assert( !LocationT );
00562         return &m_stack.const_value();
00563     }
00564 
00566     template< int LocationT >
00567     stack_T const* xstack_ptr_descend() const
00568     {
00569         return xstack_ptr_at_location< LocationT >();
00570     }
00571 
00573     template< typename SubscriptLambdaT >
00574     stack_T const* xstack_ptr_sub_split( SubscriptLambdaT ) const
00575     {
00576         assert( false );
00577         return NULL;    // compile time optimization
00578     }
00579 
00581     template< int LocationT, typename SubscriptLambdaT >
00582     stack_T const* xstack_ptr_sub_descend( SubscriptLambdaT ) const
00583     {
00584         assert( false );
00585         return NULL;    // compile time optimization
00586     }
00587 
00588 
00590     template< int LocationT >
00591     stack_T* xstack_ptr_at_location()
00592     {
00593         assert( !LocationT );
00594         return &m_stack.value();
00595     }
00596 
00598     template< int LocationT >
00599     stack_T* xstack_ptr_descend()
00600     {
00601         return xstack_ptr_at_location< LocationT >();
00602     }
00603 
00605     template< typename SubscriptLambdaT >
00606     stack_T* xstack_ptr_sub_split( SubscriptLambdaT )
00607     {
00608         assert( false );
00609         return NULL;    // compile time optimization
00610     }
00611 
00613     template< int LocationT, typename SubscriptLambdaT >
00614     stack_T* xstack_ptr_sub_descend( SubscriptLambdaT )
00615     {
00616         assert( false );
00617         return NULL;    // compile time optimization
00618     }
00619 
00620 
00621 };  // xst_stack
00622 
00623 
00624 }   // namespace cttl_impl
00625 
00626 
00627 #endif //_XST_STACK_H_INCLUDED_

Generated on Thu Nov 2 17:48:24 2006 for CTTL Lambda Expression by  doxygen 1.3.9.1