This article demonstrates a test example of the Loki functor template class in the wince environment. It mainly demonstrates the usage of the following functor:
- Use the function to initialize LOKI: functor
- Use functor to initialize LOKI: functor
- Use the class member function to initialize LOKI: functor
- Use other LOKI: functor to initialize LOKI: functor
- LOKI: functor
These examples are mainly derived from the example code in the book (modern c ++ design), I added some implementation code, as well as adaptive modifications to the Loki-0.1.6 version. Go to sf.net to download the Loki-0.1.6 (the latest version ).
The following shows the sample code:
- # Include "stdafx. H "//
- # Include <Loki/functor. h>
- # Include <Loki/typelistmacros. h>
- # Include <iostream>
- // Using namespace Loki;
- // A function
- Void function (INT m)
- {
- STD: cout <"Void function (" <m <");" <STD: Endl;
- }
- // A functor
- Struct somefunctor {
- Void operator () (INT m ){
- STD: cout <"somfunction: Operator () (" <m <");" <STD: Endl;
- }
- };
- // A class member function
- Struct someclass {
- Void memberfunction (INT m ){
- STD: cout <"someclass: memberfunction (" <m <");" <STD: Endl;
- }
- };
- Void testfunctors ()
- {
- // Initialize with a function
- LOKI: functor <void, loki_typelist_1 (INT)> cmd1 (function );
- // Initialize with a functor
- Somefunctor FN;
- LOKI: functor <void, loki_typelist_1 (INT)> cmd2 (FN );
- // Initialize with a pointer to member function
- // And a pointer to member function
- Someclass myobject;
- LOKI: functor <void, loki_typelist_1 (INT)> cmd3 (& myobject, & someclass: memberfunction );
- // Initialize a functor with another
- // (Copying)
- LOKI: functor <void, loki_typelist_1 (INT)> limit 4 (cmd3 );
- LOKI: functor <void, loki_typelist_1 (INT)> lifecycle 5 (chain (cmd1, cmd2 ));
- // Run
- STD: cout <"Call cmd1 ()..." <STD: Endl;
- Cmd1 (1 );
- STD: cout <"Call cmd2 ()..." <STD: Endl;
- Cmd2 (2 );
- STD: cout <"Call cmd3 ()..." <STD: Endl;
- Cmd3 (3 );
- STD: cout <"Call done 4 ()..." <STD: Endl;
- Limit 4 (4 );
- STD: cout <"Call done 5 ()..." <STD: Endl;
- Limit 5 (5 );
- }
The running result is as follows:
Call cmd1 ()...
Void function (1 );
Call cmd2 ()...
Somfunction: Operator () (2 );
Call cmd3 ()...
Someclass: memberfunction (3 );
Call limit 4 ()...
Someclass: memberfunction (4 );
Call limit 5 ()...
Void function (5 );
Somfunction: Operator () (5 );
The above example is run in the simulator. Since the instruction set (instructionset) has been specified as armvi during compilation, I believe it can also be run on the real machine.
In addition, Versions later than s60 3rd ed support openc/C ++, and the testing results under Symbian (simulator/real machine) will be displayed when the value is blank in a period of time ).