C++ 实战: 好用的std::variant

早期我们用union来存变量,但是预先不知道存储的是那种类型.

C++新特性中,提供std::variant来满足新场景

直接干货


#include <unordered_map>
#include <variant>

namespace your_own_sample
{

class SampleTest
{
public:
    explicit SampleTest() = default;
    SampleTest(const std::string &address, uint16_t port);

    ~SampleTest() override = default;

    using Module = std::string;
    using Path = std::string;
    using Data = std::string;

    using CallBackA = std::function<void(const std::error_code &status,
                                          const Module &module,
                                          const Path &path,
                                          Data *data)>;
    void subscribeCallBackA(const Module &module, const Path &path, const CallBackA &opercb) final;
    using CallBackB = std::function<void(const std::error_code &status,
                                        const Module &module,
                                        const Path &path,
                                        uint32_t message_id)>;
    void subscribeCallBackB(const Module &module,
                            const Path &path,
                            const CallBackB &opercb);
private:
    using subCB = std::variant<CallBackA, CallBackB>;

    std::unordered_map<Path, std::pair<Module, subCB>> subscribe_info_;

    void handleSubAsOperationalDPResponse(const Response &resp);
};

void SampleTest::handleSubAsOperationalDPResponse(const Response &resp)
{
    auto iter = subscribe_info_.find(resp.path(0));
    if (iter != subscribe_info_.end())
    {
        if (std::holds_alternative<CallBackA>(iter->second.second))
        {
            Data data;
            auto callback = std::get<CallBackA>(iter->second.second);
            callback(your_own_api::SUCCESS, resp.module_name(), resp.path(0), &data);
        }
        else if (std::holds_alternative<CallBackB>(iter->second.second))
        {
            auto callback = std::get<CallBackB>(iter->second.second);
            callback(your_own_api::SUCCESS, resp.module_name(), resp.path(0), resp.message_id());
        }
    }
}


void SampleTest::subscribeCallBackA(const Module &module,
                                                const Path &path,
                                                const CallBackA &callBackA)
{
    subscribe_info_.insert({path, {module, callBackA}});
}

void SampleTest::subscribeCallBackB(const Module &module,
                                    const Path &path,
                                    const CallBackB &callBackB)
{
    subscribe_info_.insert({path, {module, callBackB}});
}

}

通过存储的类型不同,来确定该调用哪种回调函数,上述样例只是其中最简单的应用,我们在refine老代码的时候,可以快炫酷的用用,看看效果。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值