home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / IBMCLASS / ISTRTEST.HPP < prev    next >
C/C++ Source or Header  |  1993-10-22  |  9KB  |  212 lines

  1. #ifndef _ISTRTEST_
  2. #define _ISTRTEST_
  3. /*******************************************************************************
  4. * FILE NAME: istrtest.hpp                                                      *
  5. *                                                                              *
  6. * DESCRIPTION:                                                                 *
  7. *   This file contains the declaration(s) of the class(es):                    *
  8. *     IStringTest - Abstract "string test" class.                              *
  9. *                                                                              *
  10. * COPYRIGHT:                                                                   *
  11. *   Licensed Materials - Property of IBM                                       *
  12. *   (C) Copyright IBM Corporation 1992, 1993                                   *
  13. *   All Rights Reserved                                                        *
  14. *   US Government Users Restricted Rights - Use, duplication, or               *
  15. *   disclosure                                                                 *
  16. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  17. *                                                                              *
  18. *******************************************************************************/
  19. #ifndef _IVBASE_
  20.   #include <ivbase.hpp>
  21. #endif
  22.  
  23. /*----------------------------------------------------------------------------*/
  24. /* Align classes on four byte boundary.                                       */
  25. /*----------------------------------------------------------------------------*/
  26. #pragma pack(4)
  27.  
  28. extern "C"
  29.   {
  30.   typedef IBase::Boolean _Optlink ICStrTestFn( int );
  31.   }
  32.  
  33. class IStringTest : public IVBase {
  34. typedef IVBase
  35.   Inherited;
  36. /*******************************************************************************
  37.   The IStringTest class defines the basic protocol for test objects that can
  38.   be passed to IStrings (or I0Strings) to assist in performing various
  39.   testing and searching functions.  It also provides concrete implementation
  40.   for the common case of using a plain C function for such testing.
  41.  
  42.   A derived template class, IStringTestMemberFn, is provided to permit the
  43.   use of member functions of any class on the IString functions that support
  44.   IStringTest.
  45.  
  46.   Derived classes should re-implement the test virtual function to test
  47.   characters passed by the IString and return the appropriate result.
  48.  
  49.   A constructor for this class accepts a pointer to a C function that accepts
  50.   an integer as an argument and returns a Boolean.  Such functions can be
  51.   used anywhere an IStringTest can be used.  Note that this is the type of
  52.   the standard C library is.... functions that check the type of C characters.
  53.  
  54.   Example:
  55.     // Strip leading alphanumerics from aString...
  56.     IString
  57.       aString;
  58.     ...
  59.     aString.stripLeading( isalnum ); // Uses C library isalnum function.
  60. *******************************************************************************/
  61. public:
  62. /*------------------------------ Function Types --------------------------------
  63.   CFunction   - Pointer to the C function that accepts an integer argument
  64.                 and returns Boolean.
  65. ------------------------------------------------------------------------------*/
  66. typedef ICStrTestFn
  67.   CFunction;
  68.  
  69. /*------------------------------- Constructor ---------------------------------
  70.   An object of this class can be constructed with a pointer to the C
  71.   function to be used to implement the test member function.
  72. ------------------------------------------------------------------------------*/
  73.   IStringTest ( CFunction   &cFunc );
  74.   ~IStringTest ( );
  75.  
  76. /*--------------------------------- Testing ------------------------------------
  77.   test - This function tests the argument integer (character) and returns
  78.          true or false as returned by the C function provided at construction.
  79.          Derived classes should override this function to implement their own
  80.          testing function.
  81. ------------------------------------------------------------------------------*/
  82. virtual Boolean
  83.   test ( int c ) const;
  84.  
  85. protected:
  86. /*------------------------------ Implementation --------------------------------
  87.   FnType - Enumeration describing the various flavors of functions
  88.            supported.
  89.              user   - User-defined.
  90.              c      - C.
  91.              cpp    - C++ static or non-member function.
  92.              memFn  - C++ member function.
  93.              cMemFn - Const C++ member function.
  94.  
  95.   data   - Data member union, varying by FnType:
  96.              cFn   - Pointer to a C function.
  97.              user  - Pointer to an arbitrary derived-class data (if FnType is
  98.                      neither c nor cpp).
  99. ------------------------------------------------------------------------------*/
  100. enum FnType { user, c, cpp, memFn, cMemFn };
  101. FnType
  102.   type;
  103. union { CFunction *cFn; void *user; } data;
  104.  
  105. /*--------------------- Protected Constructor ----------------------------------
  106.   This protected constructor can be used by derived classes to
  107.   reuse the space for the C/C++ function pointer.
  108. ------------------------------------------------------------------------------*/
  109.   IStringTest ( FnType  type,
  110.                 void   *userData );
  111.  
  112. }; // class IStringTest
  113.  
  114. template < class T >
  115. class IStringTestMemberFn : public IStringTest {
  116. /*******************************************************************************
  117.   The template class IStringTestMemberFn is used to provide an
  118.   IStringTest-type wrapper for particular C++ member functions.  This permits
  119.   such member functions to be used in conjunction with the IString (and
  120.   I0String) functions that accept an IStringTest object as an argument.
  121.  
  122.   The template class argument is the class name T of the class whose member
  123.   function is to be wrappered.
  124.  
  125.   The constructor for the object requires two things:
  126.      - An instance of class T.
  127.      - A pointer to a member function of the class T.  This member function
  128.        will be applied to the object specified on the constructor to test
  129.        each character passed to the test function of this class.  The member
  130.        function must accept a single integer argument and return Boolean.
  131.  
  132.   Both const and non-const member functions are supported.  The latter
  133.   require that a non-const member be specified as the first argument.
  134.  
  135.   Example:
  136.     // Any arbitrary class with appropriate function...
  137.     struct StringTester {
  138.     Boolean
  139.       charTest( int c ) const;
  140.     };
  141.     // StringTester object...
  142.     StringTester
  143.       tester;
  144.     // Test aString using tester object...
  145.     IString
  146.       aString;
  147.     ...
  148.     aString
  149.       .includes( IStringTestMemberFn<StringTester>( tester,
  150.                                                     StringTester::charTest ) );
  151.  
  152.   Customization (Template Arguments)
  153.   Class IStringTestMemberFn is a template class instantiated with the
  154.   template arguments:
  155.  
  156.     class T
  157. *******************************************************************************/
  158. public:
  159. /*------------------------------ Related Types ---------------------------------
  160.   ConstFn    - Const member function of the appropriate type.
  161.   NonconstFn - Non-const member function of the appropriate type.
  162. ------------------------------------------------------------------------------*/
  163. typedef Boolean
  164.  ( T::*ConstFn )( int ) const;
  165. typedef Boolean
  166.  ( T::*NonconstFn )( int );
  167.  
  168. /*------------------------------- Constructors ---------------------------------
  169.   There are two constructors, one supporting const member functions, the
  170.   other non-const member functions.  The constructors also require an object
  171.   of the class T (non-const object for non-const member functions).
  172. ------------------------------------------------------------------------------*/
  173.   IStringTestMemberFn ( const T    &object,
  174.                         ConstFn     constFn )
  175.     : IStringTest( user, (void*)&object ),
  176.       cMF( constFn )
  177.     {
  178.     }
  179.   IStringTestMemberFn ( T          &object,
  180.                         NonconstFn  nonconstFn )
  181.     : IStringTest( user, (void*)&object ),
  182.       ncMF( nonconstFn )
  183.     {
  184.     }
  185.  
  186. /*-------------------------------- Overrides -----------------------------------
  187.   test - Overridden to dispatch a member function against an object.
  188. ------------------------------------------------------------------------------*/
  189. virtual Boolean
  190.   test ( int c ) const
  191.     {
  192.     if ( this->type == cMemFn )
  193.       return ((const T*)data.user->*cMF)( c );
  194.     else
  195.       return ((T*)data.user->*ncMF)( c );
  196.     }
  197.  
  198. private: /*------------------------ PRIVATE ----------------------------------*/
  199. union { ConstFn cMF; NonconstFn ncMF; };
  200. };
  201.  
  202. /*----------------------------------------------------------------------------*/
  203. /* Resume compiler default packing.                                           */
  204. /*----------------------------------------------------------------------------*/
  205. #pragma pack()
  206.  
  207. #ifndef I_NO_INLINES
  208.   #include <istrtest.inl>
  209. #endif
  210.  
  211. #endif // _ISTRTEST_
  212.