/** * * @file TestFunctorAbstrait.cpp * * @authors M. Laporte * * @date 07/05/2018 * * @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 { public : virtual ~ITraitCar (void) {} virtual int operator () (int caract) const = 0; }; // ITraitCar class ToLower : public ITraitCar { public : virtual ~ToLower (void) {} virtual int operator () (int caract) const noexcept { return tolower (caract); } // operateur() }; // ToLower class ToUpper : public ITraitCar { public : virtual ~ToUpper (void) {} virtual int operator () (int caract) const noexcept { return toupper (caract); } // operateur() }; // ToUpper class IgnPunct : public ITraitCar { public : virtual ~IgnPunct (void) {} virtual int operator () (int caract) const noexcept { return ispunct (caract) ? ' ' : caract; } // operateur() }; // IgnPunct 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 << "Functor abstrait : "; string ligne ("azert:;,.?GFDSQ"); string minusc ("azert:;,.?gfdsq"); string majusc ("AZERT:;,.?GFDSQ"); string sansPunct ("AZERT GFDSQ"); assert (minusc == moulinette (Ligne, ToLower ())); assert (majusc == moulinette (Ligne, ToUpper ())); assert (sansPunct == moulinette (Ligne, IgnPunct())); cout << "OK\n"; } // testFunctor() } // namespace int main (void) { /* */ testFunctor (); /* */ return 0; } // main()