home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / cppbeta / ibmcli / istrtest.hp_ / ISTRTEST.HPP
Encoding:
C/C++ Source or Header  |  1992-10-26  |  3.8 KB  |  140 lines

  1. /*==============================================================================
  2.  Module Name: istrtest.hpp
  3.  
  4.  Description: 
  5.    Declaration of class IStringTest, class of object encapsulating
  6.    function used to "test" IString objects using various indexOf...
  7.    functions.
  8.  
  9.  Copyright: (C) Copyright IBM Corporation 1992
  10.             All Rights Reserved
  11.             Licensed Materials = Property of IBM
  12.  
  13.  Notes:
  14.  
  15.  Related Topics:
  16.  
  17.  History:
  18.    flag yymmdd who description
  19.    ---- ------ --- -----------------------------------------------------------
  20.         920717 law Initial ICLUI version
  21. ==============================================================================*/
  22. #if !defined( _ISTRTEST_ )
  23.    #define _ISTRTEST_
  24.  
  25. #if !defined( _IBASETYPE_ )
  26.    #include <ibasetyp.hpp>
  27. #endif
  28.  
  29. extern "C"
  30.   {
  31.   typedef int (*PStringTestFunc)(int);
  32.   }
  33.  
  34. /*------------------------------------------------------------------------------
  35.  Class Name: IStringTest
  36.  
  37.  Class Hierarchy:
  38.    IStringTest
  39.  
  40.  Description:
  41.    IString test object; a wrapper for a function or member function
  42.    to be applied to the contents of an IString object.
  43.  
  44.  Usage:
  45.    Objects of this class provide a "string test function" that can be
  46.    applied to IStrings via its member functions:
  47.      indexOf
  48.      indexOfAnyOf
  49.      indexOfAnyBut
  50.      lastIndexOf
  51.      lastIndexOfAnyOf
  52.      lastIndexOfAnyBut
  53.  
  54.    The basic IStringTest class constructor requires the address of a
  55.    character test function such as those provided in the standard
  56.    library's <ctype.h> (e.g., isalnum, ispunct).  This is to permit
  57.    convenient application of such tests.  For example,
  58.       aString.indexOfAnyOf( isalnum )
  59.    would return the index of the first character in aString that is
  60.    alphanumeric.
  61.  
  62.    One can derive from this class to enable more sophisticated
  63.    testing criteria.
  64.  
  65.    There is essentially a single virtual function provided by this
  66.    class:
  67.       test - Test a given character, returning true or false.
  68.    Actually, there are both const and non-const versions of this
  69.    function.  The former is applied by IString when a const IStringTest
  70.    reference is provided, the latter when a non-const IStringTest
  71.    argument is given.
  72.  
  73.    The non-const version defaults to invocation of the const version.
  74.    Thus, one typically has only to re-implement the const version.
  75.    The other would only need to be re-implemented if the StringTest
  76.    derived class requires that the receiver be modified as a result
  77.    the test function.
  78.  
  79.    An example of such a derived class would be a "test" that returned
  80.    true only on the n-th occurrence of some letter.  Such a derived
  81.    class might be implemented as:
  82.  
  83.      class Nth : public IStringTest {
  84.  
  85.      public:
  86.  
  87.      Nth( unsigned int n, char c )
  88.        : IStringTest(0), num(n), value(c)
  89.        {
  90.        }
  91.      Boolean test( char aChar )
  92.        {
  93.        if ( aChar == value )
  94.          if ( num-- )
  95.            return true;
  96.        return false;
  97.        }
  98.  
  99.      protected:
  100.  
  101.      char value;
  102.      unsigned int num;
  103.      };
  104.  
  105.    Such a class would be used as follows:
  106.      Nth fifthA( 5, 'A' );  a
  107.      unsigned pos5thA = aString.indexOf( fifthA );
  108.  
  109.   Notes:
  110.     1. This class is a definite candidate for use of templates so that
  111.        it can better support C++ class member functions.
  112.  
  113.   Related Topics:
  114.     IString
  115. ------------------------------------------------------------------------------*/
  116. class IStringTest {
  117.  
  118. public:
  119.  
  120. IStringTest( PStringTestFunc pFunc )
  121.   : pTest( pFunc )
  122.   {
  123.   }
  124. virtual Boolean
  125.   test( char aChar ) const
  126.   {
  127.   return pTest( aChar );
  128.   }
  129. virtual Boolean
  130.   test( char aChar )
  131.   {
  132.   return ( (const IStringTest &) (*this) ).test( aChar );
  133.   }
  134.  
  135. protected:
  136.  
  137. PStringTestFunc pTest;
  138. };
  139. #endif // _ISTRTEST_
  140.