本文演示一个在WINCE环境下Loki之Functor模板类的测试例子。主要演示了下面几种Functor的用法:
下面贴出示例代码:
运行结果如下:
以上例子在模拟器下运行通过,由于在编译时已经指定指令集(instructionset)为ARMVI,相信也可以在真机上运行。
另,S60 3rd Ed以后的版本均支持OpenC/C++,过一段时间得空时将给出Symbian下的测试结果(模拟器/真机)。
- 用函数初始化Loki::Functor
- 用functor初始化Loki::Functor
- 用类成员函数初始化Loki::Functor
- 用其它Loki::Functor初始化Loki::Functor
- 用Loki的串接函数(Chain)初始化Loki::Functor
下面贴出示例代码:
- #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)> cmd4(cmd3);
- Loki::Functor<void, LOKI_TYPELIST_1(int)> cmd5(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 cmd4()..." << std::endl;
- cmd4(4);
- std::cout << "call cmd5()..." << std::endl;
- cmd5(5);
- }
call cmd1()...
void Function(1);
call cmd2()...
SomFunction::operator()(2);
call cmd3()...
SomeClass::MemberFunction(3);
call cmd4()...
SomeClass::MemberFunction(4);
call cmd5()...
void Function(5);
SomFunction::operator()(5);
以上例子在模拟器下运行通过,由于在编译时已经指定指令集(instructionset)为ARMVI,相信也可以在真机上运行。
另,S60 3rd Ed以后的版本均支持OpenC/C++,过一段时间得空时将给出Symbian下的测试结果(模拟器/真机)。