/** * * @file TestFunctorInOut.cpp * * @authors M. Laporte, D. Mathieu * * @date 07/12/2011 * * @version V1.0 * * @brief functor abstrait * **/ #include <string> #include <iostream> #include <cctype> // ispunct(), islower(), isalpha() // tolower(), toupper() #include <cassert> using namespace std; namespace { class ITraitCar { protected : mutable unsigned myCpt; public : ITraitCar (void) : myCpt (0) {} unsigned getCpt (void) const noexcept { return myCpt; } void setCpt (unsigned cpt) noexcept { myCpt = cpt; } virtual ~ITraitCar (void) {} virtual int operator () (int caract) const = 0; }; // ITraitCar class CToUpper : public ITraitCar { public : virtual ~CToUpper (void) {} virtual int operator () (int caract) const { ++myCpt; return toupper (caract); } // operateur() }; // CToUpper class CIgnPunct : public ITraitCar { public : virtual ~CIgnPunct (void) {} virtual int operator () (int caract) const { return ispunct (caract) ? ++myCpt, ' ' : caract; } // operateur() }; // CIgnPunct class CToLower : public ITraitCar { public : virtual ~CToLower (void) {} virtual int operator () (int caract) const { if (isalpha (caract)) ++myCpt; return tolower (caract); } // operateur() }; // CToLower string & moulinette (string & str, const ITraitCar & transf) { for (string::size_type i (str.size ()); i--; ) str [i] = transf (str [i]); return str; } // moulinette() void testFunctor (void) { cout << "FunctorInOut : "; string ligne ("Phrase ... Avec,,:,; pOnCtUaTiOn"); string minusc ("phrase ... avec,,:,; ponctuation"); string majusc ("PHRASE ... AVEC,,:,; PONCTUATION"); string sansPunct ("PHRASE AVEC PONCTUATION"); ToLower toLower; assert (minusc == moulinette (ligne, toLower )); assert (21 == toLower.getCpt()); ToUpper toUpper; assert (majusc == moulinette (ligne, toUpper )); assert (32 == toUpper.getCpt()); IgnPunct ignPunct; assert (sansPunct == moulinette (ligne, ignPunct)); assert ( 8 == ignPunct.getCpt()); cout << "OK\n"; } // testFunctor() } // namespace int main (void) { /* */ testFunctor (); /* */ return 0; } // main()