home *** CD-ROM | disk | FTP | other *** search
- /*==============================================================================
- Module Name: istrtest.hpp
-
- Description:
- Declaration of class IStringTest, class of object encapsulating
- function used to "test" IString objects using various indexOf...
- functions.
-
- Copyright: (C) Copyright IBM Corporation 1992
- All Rights Reserved
- Licensed Materials = Property of IBM
-
- Notes:
-
- Related Topics:
-
- History:
- flag yymmdd who description
- ---- ------ --- -----------------------------------------------------------
- 920717 law Initial ICLUI version
- ==============================================================================*/
- #if !defined( _ISTRTEST_ )
- #define _ISTRTEST_
-
- #if !defined( _IBASETYPE_ )
- #include <ibasetyp.hpp>
- #endif
-
- extern "C"
- {
- typedef int (*PStringTestFunc)(int);
- }
-
- /*------------------------------------------------------------------------------
- Class Name: IStringTest
-
- Class Hierarchy:
- IStringTest
-
- Description:
- IString test object; a wrapper for a function or member function
- to be applied to the contents of an IString object.
-
- Usage:
- Objects of this class provide a "string test function" that can be
- applied to IStrings via its member functions:
- indexOf
- indexOfAnyOf
- indexOfAnyBut
- lastIndexOf
- lastIndexOfAnyOf
- lastIndexOfAnyBut
-
- The basic IStringTest class constructor requires the address of a
- character test function such as those provided in the standard
- library's <ctype.h> (e.g., isalnum, ispunct). This is to permit
- convenient application of such tests. For example,
- aString.indexOfAnyOf( isalnum )
- would return the index of the first character in aString that is
- alphanumeric.
-
- One can derive from this class to enable more sophisticated
- testing criteria.
-
- There is essentially a single virtual function provided by this
- class:
- test - Test a given character, returning true or false.
- Actually, there are both const and non-const versions of this
- function. The former is applied by IString when a const IStringTest
- reference is provided, the latter when a non-const IStringTest
- argument is given.
-
- The non-const version defaults to invocation of the const version.
- Thus, one typically has only to re-implement the const version.
- The other would only need to be re-implemented if the StringTest
- derived class requires that the receiver be modified as a result
- the test function.
-
- An example of such a derived class would be a "test" that returned
- true only on the n-th occurrence of some letter. Such a derived
- class might be implemented as:
-
- class Nth : public IStringTest {
-
- public:
-
- Nth( unsigned int n, char c )
- : IStringTest(0), num(n), value(c)
- {
- }
- Boolean test( char aChar )
- {
- if ( aChar == value )
- if ( num-- )
- return true;
- return false;
- }
-
- protected:
-
- char value;
- unsigned int num;
- };
-
- Such a class would be used as follows:
- Nth fifthA( 5, 'A' ); a
- unsigned pos5thA = aString.indexOf( fifthA );
-
- Notes:
- 1. This class is a definite candidate for use of templates so that
- it can better support C++ class member functions.
-
- Related Topics:
- IString
- ------------------------------------------------------------------------------*/
- class IStringTest {
-
- public:
-
- IStringTest( PStringTestFunc pFunc )
- : pTest( pFunc )
- {
- }
- virtual Boolean
- test( char aChar ) const
- {
- return pTest( aChar );
- }
- virtual Boolean
- test( char aChar )
- {
- return ( (const IStringTest &) (*this) ).test( aChar );
- }
-
- protected:
-
- PStringTestFunc pTest;
- };
- #endif // _ISTRTEST_