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

xtl_base.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 
00039 // xtl_base.h
00040 
00041 #ifndef _CTTL_XTLBASE_H_INCLUDED_
00042 #define _CTTL_XTLBASE_H_INCLUDED_
00043 
00044 #include "coreutils.h"
00045 #include "xtl_trace.h"
00046 
00047 namespace cttl_impl {
00048 
00050 typedef int ( *iswhat_T )( int );
00051 
00053 typedef int ( *iswwhat_T )( wint_t );
00054 
00064 template< typename ExprT >
00065 struct xtl_wrap {
00066 
00067     // compile-time
00068 
00070     ExprT m_expr;
00071 
00073     xtl_wrap()
00074     : m_expr( ExprT() )
00075     {
00076 //      CTTL_TRACE_MESSAGE( CTTL_TRACE_TYPEID( &m_expr ) );
00077     }
00078 
00080     template< typename GenericT >
00081     xtl_wrap( GenericT const& x_ )
00082     : m_expr( ExprT( x_ ) )
00083     {
00084 //      CTTL_TRACE_MESSAGE( CTTL_TRACE_TYPEID( &m_expr ) );
00085     }
00086 
00088     xtl_wrap( xtl_wrap< ExprT > const& other_ )
00089     : m_expr( other_.m_expr )
00090     {
00091     }
00092 
00111     template< typename UniverseT >
00112     size_t match( UniverseT& edge_ )
00113     {
00114         return m_expr.match( edge_ );
00115     }
00116 
00135     template< typename UniverseT >
00136     size_t find( UniverseT& edge_ )
00137     {
00138         return m_expr.find( edge_ );
00139     }
00140 
00159     template< typename UniverseT >
00160     size_t bang_find( UniverseT& edge_ )
00161     {
00162         return m_expr.bang_find( edge_ );
00163     }
00164 
00183     template< typename UniverseT >
00184     size_t runtime_match( UniverseT& edge_ )
00185     {
00186         if ( edge_.parent().container().m_flags.test( xtl_flag_runtime_bang_find ) ) {
00187             edge_.parent().container().m_flags.clear( xtl_flag_runtime_bang_find );
00188             return m_expr.bang_find( edge_ );
00189 
00190         } else if ( edge_.parent().container().m_flags.test( xtl_flag_runtime_find ) ) {
00191             edge_.parent().container().m_flags.clear( xtl_flag_runtime_find );
00192             return m_expr.find( edge_ );
00193         }
00194         
00195         return m_expr.match( edge_ );
00196     }
00197 
00198 };  // xtl_wrap
00199 
00200 
00216 template< typename EdgeT, typename ExprT >
00217 class xtl_edge {
00218 
00219 private:
00221     EdgeT m_edge;
00222 
00224     ExprT m_expr;
00225 
00226 public:
00227     // compile-time
00228 
00230     xtl_edge( EdgeT const& edge_,  ExprT const& expr_ )
00231     : m_edge( edge_ ), m_expr( expr_ )
00232     {
00233     }
00234 
00235     // run-time
00236 
00259     template< typename UniverseT >
00260     size_t match( UniverseT& edge_ )
00261     {
00262         CTTL_TRACE_LEVEL_MATCH( 'e' );
00263         size_t match_offset = m_expr.match( edge_ );
00264         if ( match_offset != UniverseT::string_T::npos ) {
00265             m_edge.first.offset( match_offset );
00266             m_edge.second.offset( edge_.first.offset() );
00267             CTTL_TRACE_EDGE_RESULT_TRUE( 'e' );
00268             return match_offset;
00269         }
00270 
00271         CTTL_TRACE_EDGE_RESULT_FALSE( 'e', m_edge.first.identity() );
00272         return UniverseT::string_T::npos;
00273     }
00274     
00297     template< typename UniverseT >
00298     size_t find( UniverseT& edge_ )
00299     {
00300         CTTL_TRACE_LEVEL_FIND( 'e' );
00301         size_t match_offset = m_expr.find( edge_ );
00302         if ( match_offset != UniverseT::string_T::npos ) {
00303             m_edge.first.offset( match_offset );
00304             m_edge.second.offset( edge_.first.offset() );
00305             CTTL_TRACE_EDGE_RESULT_TRUE( 'e' );
00306             return match_offset;
00307         }
00308 
00309         CTTL_TRACE_EDGE_RESULT_FALSE( 'e', m_edge.first.identity() );
00310         return UniverseT::string_T::npos;
00311     }
00312     
00335     template< typename UniverseT >
00336     size_t bang_find( UniverseT& edge_ )
00337     {
00338         CTTL_TRACE_LEVEL_BANG( 'e' );
00339         size_t match_offset = m_expr.bang_find( edge_ );
00340         if ( match_offset != UniverseT::string_T::npos ) {
00341             m_edge.first.offset( match_offset );
00342             m_edge.second.offset( edge_.first.offset() );
00343             CTTL_TRACE_EDGE_RESULT_TRUE( 'e' );
00344             return match_offset;
00345         }
00346 
00347         CTTL_TRACE_EDGE_RESULT_FALSE( 'e', m_edge.first.identity() );
00348         return UniverseT::string_T::npos;
00349     }
00350     
00351 };  // xtl_edge
00352 
00353 
00367 template< typename NodeT, typename ExprT >
00368 class xtl_node {
00369 
00370 private:
00372     NodeT m_node;
00373 
00375     ExprT m_expr;
00376 
00377 public:
00378     // compile-time
00379 
00381     xtl_node( NodeT const& node_,  ExprT const& expr_ )
00382     : m_node( node_ ), m_expr( expr_ )
00383     {
00384     }
00385 
00386     // run-time
00387 
00410     template< typename UniverseT >
00411     size_t match( UniverseT& edge_ )
00412     {
00413         typedef typename UniverseT::strict_edge_T strict_universe_T;
00414 
00415         CTTL_TRACE_LEVEL_MATCH( 'n' );
00416         size_t match_offset = m_expr.match( edge_ );
00417         if ( match_offset != UniverseT::string_T::npos ) {
00418             m_node.offset( match_offset );
00419             // strict_universe_T needed, otherwise a custom policy
00420             // would have to have a default constructor:
00421             CTTL_TRACE_NODE_RESULT_TRUE( 'n', strict_universe_T( m_node.parent(), m_node.identity(), edge_.first.identity() ) );
00422             return match_offset;
00423         }
00424 
00425         CTTL_TRACE_NODE_RESULT_FALSE( 'n', m_node.identity() );
00426         return UniverseT::string_T::npos;
00427     }
00428     
00451     template< typename UniverseT >
00452     size_t find( UniverseT& edge_ )
00453     {
00454         typedef typename UniverseT::strict_edge_T strict_universe_T;
00455 
00456         CTTL_TRACE_LEVEL_FIND( 'n' );
00457         size_t match_offset = m_expr.find( edge_ );
00458         if ( match_offset != UniverseT::string_T::npos ) {
00459             m_node.offset( match_offset );
00460             // strict_universe_T needed, otherwise a custom policy
00461             // would have to have a default constructor:
00462             CTTL_TRACE_NODE_RESULT_TRUE( 'n', strict_universe_T( m_node.parent(), m_node.identity(), edge_.first.identity() ) );
00463             return match_offset;
00464         }
00465 
00466         CTTL_TRACE_NODE_RESULT_FALSE( 'n', m_node.identity() );
00467         return UniverseT::string_T::npos;
00468     }
00469     
00492     template< typename UniverseT >
00493     size_t bang_find( UniverseT& edge_ )
00494     {
00495         typedef typename UniverseT::strict_edge_T strict_universe_T;
00496 
00497         CTTL_TRACE_LEVEL_BANG( 'n' );
00498         size_t match_offset = m_expr.bang_find( edge_ );
00499         if ( match_offset != UniverseT::string_T::npos ) {
00500             m_node.offset( match_offset );
00501             // strict_universe_T needed, otherwise a custom policy
00502             // would have to have a default constructor:
00503             CTTL_TRACE_NODE_RESULT_TRUE( 'n', strict_universe_T( m_node.parent(), m_node.identity(), edge_.first.identity() ) );
00504             return match_offset;
00505         }
00506 
00507         CTTL_TRACE_NODE_RESULT_FALSE( 'n', m_node.identity() );
00508         return UniverseT::string_T::npos;
00509     }
00510     
00511 };  // xtl_node
00512 
00513 
00514 }   // namespace cttl_impl
00515 
00516 #endif // _CTTL_XTLBASE_H_INCLUDED_

Generated on Thu Nov 2 17:44:06 2006 for Common Text Transformation Library by  doxygen 1.3.9.1