可变参数的函数模板设计回调函数

mytest.h

#include <iostream>
#include <functional>
#include <vector>
#include <unordered_map>
#include <initializer_list>
#include <string>

namespace mycode
{
    using t_callback_fun = std::function<void(const std::string&, const std::string&)>;
    using dwnsigChange = std::function<void(const std::string &, const float &)>;

    static std::unordered_multimap<std::string, t_callback_fun> topic_cbk_fun_map;
    static std::unordered_map<std::string, std::vector<dwnsigChange> *> downSigChange;

    template<typename... T>
    void DummyWrapper(T... t){}

    std::string _registerSigChangeFun(const std::string &topic, dwnsigChange fun);
    template<typename... Ts>
    void registerSigChangeFun(dwnsigChange fun, Ts... args)
    {
        DummyWrapper(_registerSigChangeFun(args, fun)...);
    }


    std::string insert_callbk_fun(const std::string &topic, const t_callback_fun& fun);
    template <typename... Ts>
    void registerSigChangeFun(t_callback_fun fun, Ts... args)
    {
        DummyWrapper(insert_callbk_fun(args, fun)...);
    }

    void callbackTopicChanged(const std::string &topic, const float &val);
    void callbackTopicChanged(const std::string &topic, const std::string &msg);
}

mytest.cpp

#include "../include/mytest.h"

std::string mycode::_registerSigChangeFun(const std::string &topic, mycode::dwnsigChange fun)
{
    if(downSigChange.count(topic) == 0){    //如果没找到对应的字符串key,则创建新的添加进map中
        std::vector<dwnsigChange> *callbackVec = new std::vector<dwnsigChange>();
        callbackVec->push_back(fun);
        downSigChange[topic] = callbackVec;
    }
    else                                    //如果找到了对应的字符串key,则在该key下对应的alue对应的vector
        downSigChange[topic]->push_back(fun);
    return topic;
}

std::string mycode::insert_callbk_fun(const std::string &topic, const t_callback_fun &fun)
{
    topic_cbk_fun_map.insert(std::make_pair(topic, fun));
    return topic;
}

void mycode::callbackTopicChanged(const std::string &topic, const float &val)
{
    if(downSigChange.count(topic) == 0){
        std::cout << "topic no exit call: " << topic << std::endl;
        return;
    }
    std::vector<dwnsigChange> &cbkVec = *downSigChange[topic];
    for(auto fun : cbkVec){
        fun(topic, val);
    }
}

void mycode::callbackTopicChanged(const std::string &topic, const std::string &msg)
{
    auto it_pair = topic_cbk_fun_map.equal_range(topic);        //返回范围
    for(auto it = it_pair.first; it != it_pair.second; ++it)
        it->second(topic, msg);
}

test1.h

#include <iostream>
#include <string>

class test1
{
public:
    void test1fun(std::string topic, float val);    
};

test1.cpp

#include "../include/test1.h"

void test1::test1fun(std::string topic, float val)
{
    std::cout << "topic = " << topic << std::endl;
    std::cout << "val = " << val << std::endl;
}

test2.h

#include <iostream>
#include <string>

class test2
{
public:
    void test2fun(std::string topic, std::string msg);
};

test2.cpp

#include "../include/test2.h"

void test2::test2fun(std::string topic, std::string msg)
{
    std::cout << "topic = " << topic << std::endl;
    std::cout << "msg = " << msg << std::endl;
}

main.cpp

#include <iostream>
#include "../include/test1.h"
#include "../include/test2.h"
#include <memory>
#include "../include/mytest.h"
#include <string>

using std::cout;
using std::endl;
using std::cin;

std::shared_ptr<test1> t1 = nullptr;
std::shared_ptr<test2> t2 = nullptr;

int main(int argc, char *argv[])
{
    std::cout << "Hello world!" << std::endl;

    t1 = std::make_shared<test1>();
    mycode::registerSigChangeFun(std::bind(&test1::test1fun, t1, std::placeholders::_1, std::placeholders::_2),
                                "topic_test1",
                                "test1_topic",
                                "hello_test1");

    t2 = std::make_shared<test2>();
    mycode::registerSigChangeFun(std::bind(&test2::test2fun, t2, std::placeholders::_1, std::placeholders::_2),
                                "topic_test2",
                                "test2_topic",
                                "hello_test2");

    int input = 0;
    static int nCout = 0;
    while(1)
    {
        std::cin >> input;
        if(input < 0){
            std::cout << "请输入大于0的数" << std::endl;
            continue;
        }
        else{
            if(input == 0){
                std::cout << "程序运行结束" << std::endl;
                break;
            }
        }
        nCout++;
        int yushu = nCout % 3;
        switch (yushu)
        {
        case 0:
            if(input % 2 == 0)
                mycode::callbackTopicChanged("topic_test1", input);
            else
                mycode::callbackTopicChanged("topic_test2", ("input str = " + std::to_string(input)));
            break;
        case 1:
            if(input % 2 == 0)
                mycode::callbackTopicChanged("test1_topic", input);
            else
                mycode::callbackTopicChanged("test2_topic", ("input str = " + std::to_string(input)));
            break;
        case 2:
            if(input % 2 == 0)
                mycode::callbackTopicChanged("hello_test1", input);
            else
                mycode::callbackTopicChanged("hello_test2", ("input str = " + std::to_string(input)));   
            break;
        default:
            break;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值