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

2023-12-27 18:55:25
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()

文章来源:https://blog.csdn.net/CSDN_WHB/article/details/135250471
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。