有人称为命令模式,好比先定义了一个命令(行为+参数),然后把这个命令,加入到排队、传递等策略中,等到合适的时机再操作执行。而我觉得更像机器学习中的placeholder占位模式,把函数的地址和传入参数都绑定了,再手动操作执行:
#include <functional> #include <type_traits> template <typename R=void> struct CommCommand { private: std::function<R()> m_f; public: // 接受可以调用对象的函数封包器 template <class F, class... Args, class=typename std::enable_if<!std::is_member_function_pointer<F>::value>::type> void Wrap(F&& f, Args&&... args) { m_f = [&] {return f(args...); }; } // 接受常量成员函数的封包器 template <class R, class C, class... DArgs, class P, class... Args> void Wrap(R(C::*f)(DArgs...) const, P&& p, Args&&... args) { m_f = [&, f] {return (*p.*f)(args...); }; } // 接受非常量成员函数的函数包装器 template <class R, class C, class... DArgs, class P, class... Args> void Wrap(R(C::*f)(DArgs...), P&& p, Args&&... args) { m_f = [&, f] {return (*p.*f)(args...); }; } R Execute() { return m_f(); } }; struct STA { int m_a; int operator()() { return m_a; } int operator()(int n) { return m_a + n; } int triple0() { return m_a * 3; } int triple(int a) { return m_a * 3 + a; } int triple1() const { return m_a * 3; } const int triple2(int a) const { return m_a * 3 + a; } void triple3() { cout << "triple3" << endl; } }; int add_one(int n) { cout << "add_one" << endl; return n + 1; } void printTest() { cout << "printTest" << endl; } void test() { CommCommand<int> cmd; // 普通函数 cmd.Wrap(add_one, 0); // lambda表达式 cmd.Wrap([](int n) {return n + 1; }, 2); // 接受函数对象 typedef int(*Fun)(int); Fun f = add_one; cmd.Wrap(f, 5); STA t = { 10 }; int x = 3; // 接受成员函数 cmd.Wrap(&STA::triple0, &t); cmd.Wrap(&STA::triple, &t, x); cmd.Wrap(&STA::triple, &t, 3); cmd.Wrap(&STA::triple2, &t, x); auto r = cmd.Execute(); cout << "result:" << r << endl; CommCommand<void> cmd1; cmd1.Wrap(&STA::triple3, &t); cmd1.Wrap(printTest); cmd1.Execute(); } int main(int, char *[]) { test(); system("pause"); return 0; }
另外一种实现:
template <typename Receiver> class Command { public: typedef void(Receiver::*Action)(); Command(Receiver* r, Action a):_receiver(r), _action(a) {} virtual void Execute(); protected: Action _action; Receiver* _receiver; }; template <typename Receiver> void Command<Receiver>::Execute() // 等同实现SimpleCommand子类 { (_receiver->*_action)(); // 可以使用Command中private变量 } template <typename Receiver> class SimpleCommand :Command<Receiver> { public: SimpleCommand(Receiver* r, Action a) :Command<Receiver>(r, a) {} void Execute() { (_receiver->*_action)(); // 不能使用Command中private变量 } }; class MyClass { public: void print() { cout << "MyClass print..." << endl; } }; void test() { MyClass* receiver = new MyClass; Command<MyClass>* cmd = new Command<MyClass>(receiver, &MyClass::print); cmd->Execute(); SimpleCommand<MyClass>* simpleCommand = new SimpleCommand<MyClass>(receiver, &MyClass::print); simpleCommand->Execute(); }
相关推荐
本书的内容来源于C++11项目实践,写作此书的目的是为了和广大读者分享学习和应用C++11的经验和乐趣。本书分为两篇,第一篇主要是介绍一些常用的C++11特性,介绍这些特性是如何优化C++11以前的一些代码的,读者可以...
深入理解 C++11深入理解 C++11 非扫描版, 高清文字版
深入理解C++11:C++11新特性解析与应用》主要内容:[0国0]内[0首0]本全面深入解读C++11新标准的专著,由C++标准委员[0会0]代表和IBM XL编译器中[0国0]开发团队共同撰写。不仅详细阐述了C++11标准的设计原则,而且...
C++高效编程:内存与性能优化(pdf版),深入C++经典好书!
深入理解C++11:C++11新特性解析与应用 PDF 文字版 非扫描
《深入理解C++11:C++11新特性解析与应用》电子书,epub格式。
能较好的说明C++11的特性,起到较好说明功能
祁宇 深入应用C++11 代码优化与工程级应用.pdf
深入理解C++11:C++11新特性解析与应用(文字版).。。。。。。
主要介绍了C++ 11 std::function和std::bind使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Beyond the C++ Standard Library : An Introduction to Boost (Chinese)
[Packt Publishing] C++ Code::Blocks 应用开发教程 (英文版) [Packt Publishing] C++ Application Development with Code::Blocks (E-Book) ☆ 图书概要:☆ Develop advanced applications with Code::Blocks ...
深入理解C++11:C++11新特性解析与应用(高清62M扫描加配套源码),已经验证,放心使用。
C++11:C++11新特性解析与应用,讲c++11的,比较简单
深入理解C++11:C++11新特性解析与应用的源代码
C++设计新思维:泛型编程与设计模式之应用.pdf 带目录书签
Visual C++权威剖析:MFC的原理、机制与开发实例.pdf.讲MFC运行机制的一本书。
深入理解C++11:C++11新特性解析与应用(62M高清扫描版)
深入理解C++11:C++11新特性解析与应用 文字版 epub格式。