home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cset21v1.zip
/
IBMCPP
/
IBMCLASS
/
ISTRTEST.HPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-22
|
9KB
|
212 lines
#ifndef _ISTRTEST_
#define _ISTRTEST_
/*******************************************************************************
* FILE NAME: istrtest.hpp *
* *
* DESCRIPTION: *
* This file contains the declaration(s) of the class(es): *
* IStringTest - Abstract "string test" class. *
* *
* COPYRIGHT: *
* Licensed Materials - Property of IBM *
* (C) Copyright IBM Corporation 1992, 1993 *
* All Rights Reserved *
* US Government Users Restricted Rights - Use, duplication, or *
* disclosure *
* restricted by GSA ADP Schedule Contract with IBM Corp. *
* *
*******************************************************************************/
#ifndef _IVBASE_
#include <ivbase.hpp>
#endif
/*----------------------------------------------------------------------------*/
/* Align classes on four byte boundary. */
/*----------------------------------------------------------------------------*/
#pragma pack(4)
extern "C"
{
typedef IBase::Boolean _Optlink ICStrTestFn( int );
}
class IStringTest : public IVBase {
typedef IVBase
Inherited;
/*******************************************************************************
The IStringTest class defines the basic protocol for test objects that can
be passed to IStrings (or I0Strings) to assist in performing various
testing and searching functions. It also provides concrete implementation
for the common case of using a plain C function for such testing.
A derived template class, IStringTestMemberFn, is provided to permit the
use of member functions of any class on the IString functions that support
IStringTest.
Derived classes should re-implement the test virtual function to test
characters passed by the IString and return the appropriate result.
A constructor for this class accepts a pointer to a C function that accepts
an integer as an argument and returns a Boolean. Such functions can be
used anywhere an IStringTest can be used. Note that this is the type of
the standard C library is.... functions that check the type of C characters.
Example:
// Strip leading alphanumerics from aString...
IString
aString;
...
aString.stripLeading( isalnum ); // Uses C library isalnum function.
*******************************************************************************/
public:
/*------------------------------ Function Types --------------------------------
CFunction - Pointer to the C function that accepts an integer argument
and returns Boolean.
------------------------------------------------------------------------------*/
typedef ICStrTestFn
CFunction;
/*------------------------------- Constructor ---------------------------------
An object of this class can be constructed with a pointer to the C
function to be used to implement the test member function.
------------------------------------------------------------------------------*/
IStringTest ( CFunction &cFunc );
~IStringTest ( );
/*--------------------------------- Testing ------------------------------------
test - This function tests the argument integer (character) and returns
true or false as returned by the C function provided at construction.
Derived classes should override this function to implement their own
testing function.
------------------------------------------------------------------------------*/
virtual Boolean
test ( int c ) const;
protected:
/*------------------------------ Implementation --------------------------------
FnType - Enumeration describing the various flavors of functions
supported.
user - User-defined.
c - C.
cpp - C++ static or non-member function.
memFn - C++ member function.
cMemFn - Const C++ member function.
data - Data member union, varying by FnType:
cFn - Pointer to a C function.
user - Pointer to an arbitrary derived-class data (if FnType is
neither c nor cpp).
------------------------------------------------------------------------------*/
enum FnType { user, c, cpp, memFn, cMemFn };
FnType
type;
union { CFunction *cFn; void *user; } data;
/*--------------------- Protected Constructor ----------------------------------
This protected constructor can be used by derived classes to
reuse the space for the C/C++ function pointer.
------------------------------------------------------------------------------*/
IStringTest ( FnType type,
void *userData );
}; // class IStringTest
template < class T >
class IStringTestMemberFn : public IStringTest {
/*******************************************************************************
The template class IStringTestMemberFn is used to provide an
IStringTest-type wrapper for particular C++ member functions. This permits
such member functions to be used in conjunction with the IString (and
I0String) functions that accept an IStringTest object as an argument.
The template class argument is the class name T of the class whose member
function is to be wrappered.
The constructor for the object requires two things:
- An instance of class T.
- A pointer to a member function of the class T. This member function
will be applied to the object specified on the constructor to test
each character passed to the test function of this class. The member
function must accept a single integer argument and return Boolean.
Both const and non-const member functions are supported. The latter
require that a non-const member be specified as the first argument.
Example:
// Any arbitrary class with appropriate function...
struct StringTester {
Boolean
charTest( int c ) const;
};
// StringTester object...
StringTester
tester;
// Test aString using tester object...
IString
aString;
...
aString
.includes( IStringTestMemberFn<StringTester>( tester,
StringTester::charTest ) );
Customization (Template Arguments)
Class IStringTestMemberFn is a template class instantiated with the
template arguments:
class T
*******************************************************************************/
public:
/*------------------------------ Related Types ---------------------------------
ConstFn - Const member function of the appropriate type.
NonconstFn - Non-const member function of the appropriate type.
------------------------------------------------------------------------------*/
typedef Boolean
( T::*ConstFn )( int ) const;
typedef Boolean
( T::*NonconstFn )( int );
/*------------------------------- Constructors ---------------------------------
There are two constructors, one supporting const member functions, the
other non-const member functions. The constructors also require an object
of the class T (non-const object for non-const member functions).
------------------------------------------------------------------------------*/
IStringTestMemberFn ( const T &object,
ConstFn constFn )
: IStringTest( user, (void*)&object ),
cMF( constFn )
{
}
IStringTestMemberFn ( T &object,
NonconstFn nonconstFn )
: IStringTest( user, (void*)&object ),
ncMF( nonconstFn )
{
}
/*-------------------------------- Overrides -----------------------------------
test - Overridden to dispatch a member function against an object.
------------------------------------------------------------------------------*/
virtual Boolean
test ( int c ) const
{
if ( this->type == cMemFn )
return ((const T*)data.user->*cMF)( c );
else
return ((T*)data.user->*ncMF)( c );
}
private: /*------------------------ PRIVATE ----------------------------------*/
union { ConstFn cMF; NonconstFn ncMF; };
};
/*----------------------------------------------------------------------------*/
/* Resume compiler default packing. */
/*----------------------------------------------------------------------------*/
#pragma pack()
#ifndef I_NO_INLINES
#include <istrtest.inl>
#endif
#endif // _ISTRTEST_