• 计算机图形学与计算几何经典必备书单整理,下载链接可参考:https://www.stubbornhuang.com/1256/

  • 如果觉得本站的内容有帮助,可以考虑打赏博主哦!

  • 本站由于前段时间遭受到大量临时和国外邮箱注册,所以对可注册的邮箱类型进行了限制!

  • 本站会放置Google广告用于维持域名以及网站服务器费用。

  • 问题反馈可发送邮件到stubbornhuang@qq.com

  • 欢迎大家交换友链,可在https://www.stubbornhuang.com/申请友情链接进行友链交换申请!

  • 感谢大家访问本站,希望本站的内容可以帮助到大家!

  • 在本站开通年度VIP,无限制下载本站资源和阅读本站文章

  • 工资「喂饱肚子」,副业「养活灵魂」!

C++11 – 委托机制的实现TinyDelegate

C++ 发布于2021-09-09 阅读 3,455次 0次评论 0次点赞 本文共1791个字,阅读需要5分钟。

1 使用C++11实现委托机制

1.1 TinyDelegate

1.1.1 代码

TinyDelegate.hpp

#ifndef TINY_DELEGATE_H
#define TINY_DELEGATE_H

#include <functional>
#include <chrono>
#include <string>
#include <list>
#include<algorithm>


template<typename ... arguments>
class TinyDelegate
{
public:
    TinyDelegate() = default;
    virtual~TinyDelegate() = default;

public: 
    template<typename callable>
    TinyDelegate& operator += (const callable& func)
    {
        m_Funtion_List.push_back(std::function<void(arguments...)>(func));
        return *this;
    }

    template<typename Class, typename Method>
    TinyDelegate& operator += (const std::function<void(arguments...)>& func)
    {
        m_Funtion_List.push_back(func);
        return *this;
    }

    //TinyDelegate& operator -= (const std::function<void(arguments...)>& func)
    //{
    //  void(*const* ptr)(arguments...) = func.target<void(*)(arguments...)>();

    //  for (auto iter = m_Funtion_List.begin();iter != m_Funtion_List.end();iter++)
    //  {
    //      if (ptr)
    //      {
    //          if (*ptr == *iter)
    //          {
    //              m_Funtion_List.erase(iter);
    //              break;
    //          }
    //      }
    //  }
    //}

    void operator()(arguments... params)
    {
        for (auto func : m_Funtion_List)
        {
            func(params...);
        }
    }


private:
    std::list<std::function<void(arguments...)>> m_Funtion_List;
};

#endif

1.1.2 测试代码

#include <iostream>
#include <thread>
#include "TinyDelegate.hpp"

void print()
{
    std::cout << "print" << std::endl;
}

void print_string(const std::string& str)
{
    std::cout << "print_string : " << str << std::endl;
}

void add()
{
    std::cout << "add" << std::endl;
}

class Test
{
public:
    Test() = default;
    virtual~Test() = default;

public:
    void add()
    {
        std::cout << "Test::add" << std::endl;
    }
};


int main()
{
    Test test;
    TinyDelegate<> t_Delegate;

    t_Delegate += print;
    t_Delegate += add;
    t_Delegate += std::bind(&Test::add, test);
    t_Delegate += []() { std::cout << "lambda" << std::endl; };

    t_Delegate();


    TinyDelegate<std::string> t_Delegate_string;

    t_Delegate_string += print_string;

    t_Delegate_string("hello");

    getchar();

    return 0;
}

运行结果:

C++11 - 委托机制的实现TinyDelegate-第0张图片

1.1.3 缺陷

由于std::function无法进行比较,暂时未实现-=操作符的重载,暂时未提供通过-=操作符删除委托的功能。也就是目前版本只能增加委托函数,不能删除委托函数。

欢迎扫码关注我的微信公众号,及时获取文章更新

微信公众号二维码

本文作者:StubbornHuang

版权声明:本文为站长原创文章,如果转载请注明原文链接!

原文标题:C++11 – 委托机制的实现TinyDelegate

原文链接:https://www.stubbornhuang.com/1672/

发布于:2021年09月09日 16:59:44

修改于:2023年06月26日 21:17:39

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

文章末尾
上一篇
C++11 - 使用std::thread在类内部以成员函数作为多线程函数执行异步操作
C++
下一篇
资源分享 - Real-Time 3D Character Animation with Visual C++ 英文PDF下载
计算几何与计算机图形学资源
当前分类随机文章推荐

发表评论

您必须 [ 登录 ] 才能发表留言!

关注我们的公众号

微信公众号