boost::any + std::shared_ptr结合使用

struct Type1 {
    void Print() {
        LOG(INFO) << "===Type1::Print()";
    }
    ~Type1() {
        LOG(INFO) << "===~Type1()";
    }
};

struct Type2 {
    void Print() {
        LOG(INFO) << "===Type2::Print()";
    }
    ~Type2() {
        LOG(INFO) << "===~Type2()";
    }
};

struct Type3 {
    void Print() {
        LOG(INFO) << "===Type3::Print()";
    }
    ~Type3() {
        LOG(INFO) << "===~Type3()";
    }
};

// 验证boost::any::type类型校验; 验证shared_ptr引用计数
void PrintUseCount(un_map_any& uma, const std::string& prefix) {  // NOLINT
    // boost::any anyone;
    std::stringstream ss_debug;
    for (auto it = uma.begin(); it != uma.end(); ++it) {
        auto& anyone = (it->second);
        if (anyone.type() == typeid(std::shared_ptr<Type1>)) {
            // 引用计数+1  值:3
            auto ptr_tmp = boost::any_cast<std::shared_ptr<Type1>>(it->second);
            ss_debug << "Type1:" << ptr_tmp.use_count() << "; ";
        } else if (anyone.type() == typeid(std::shared_ptr<Type2>)) {
            auto ptr_tmp = boost::any_cast<std::shared_ptr<Type2>>(it->second);
            ss_debug << "Type2:" << ptr_tmp.use_count() << "; ";
        } else if (anyone.type() == typeid(std::shared_ptr<Type3>)) {
            auto ptr_tmp = boost::any_cast<std::shared_ptr<Type3>>(it->second);
            ss_debug << "Type3:" << ptr_tmp.use_count() << "; ";
        } else {
            ss_debug << "unknown type:" << anyone.type().name()
                     << "; help1:" << typeid(std::shared_ptr<Type1>).name()
                     << "; help2:" << typeid(std::shared_ptr<Type2>).name()
                     << "; help3:" << typeid(std::shared_ptr<Type3>).name();
        }
    }
    LOG(INFO) << prefix << ss_debug.str();
}

void unmap_test_entry(un_map_any& uma) {  // NOLINT
    PrintUseCount(uma, "[unmap_test_entry]after assign ");
}

//根据类型进行显示
template<typename ValueType>
void UseElement(un_map_any& la) {  // NOLINT
    auto iter = la.find("p2");
    if (iter == la.end()) {
        return;
    }
    auto ptr = boost::any_cast<ValueType>(iter->second);
    if (nullptr != ptr) {
        ptr->Print();
    } else {
        LOG(INFO) << "===ptr is nullptr";
    }
}

int main() {
    un_map_any la;
    auto p1 = std::make_shared<Type1>();  // 引用计数+1  值:1
    la["p1"] = p1;  // 引用计数+1  值:2
    auto p2 = std::make_shared<Type2>();
    la["p2"] = p2;
    auto p3 = std::make_shared<Type3>();
    la["p3"] = p3;
    PrintUseCount(la, "before test entry");
    unmap_test_entry(la);
    PrintUseCount(la, "[main] after unmap_test_entry");
    UseElement<std::shared_ptr<Type2>>(la);
    PrintUseCount(la, "[main] after UseElement");
}

输出:

I1227 16:48:35.399410 11572 main.cc:146] before test entryType3:3; Type2:3; Type1:3; 
I1227 16:48:35.399492 11572 main.cc:146] [unmap_test_entry]after assign Type3:3; Type2:3; Type1:3; 
I1227 16:48:35.399498 11572 main.cc:146] [main] after unmap_test_entryType3:3; Type2:3; Type1:3; 
I1227 16:48:35.399503 11572 main.cc:109] ===Type2::Print()
I1227 16:48:35.399508 11572 main.cc:146] [main] after UseElementType3:3; Type2:3; Type1:3; 
Complete
I1227 16:48:35.399514 11572 main.cc:121] ===~Type3()
I1227 16:48:35.399518 11572 main.cc:112] ===~Type2()
I1227 16:48:35.399523 11572 main.cc:103] ===~Type1()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值