boost::intrusive_ptr的用法

2020-02-13  本文已影响0人  JasonLiThirty

视频教程:https://www.bilibili.com/video/av88980020/

boost::intrusive_ptr和shared_ptr的区别

boost::intrusive_ptr的使用

#include <boost/intrusive_ptr.hpp>

boost::intrusive_ptr的例子

#ifndef REFERENCE_COUNTER_H
#define REFERENCE_COUNTER_H

#include <assert.h>
#include <atomic>
#include <iostream>

class ReferenceCounter
{
public:
    friend void intrusive_ptr_add_ref(ReferenceCounter *p)
    {
        std::cout << "Call intrusive_ptr_add_ref" << std::endl;
        assert(p);
        assert(p->ref_count >= 0);
        ++p->ref_count;
    }

    friend void intrusive_ptr_release(ReferenceCounter *p)
    {
        std::cout << "Call intrusive_ptr_release" << std::endl;
        assert(p);
        assert(p->ref_count > 0);
        if (--p->ref_count == 0)
        {
            delete p;
        }
    }

    ReferenceCounter() :ref_count(0)
    {
        std::cout << "Reference Counter Constructor" << std::endl;
    }

    ReferenceCounter(const ReferenceCounter  &other)
    {
        std::cout << "Reference Counter Copy Constructor" << std::endl;
    }

    ReferenceCounter& operator=(const ReferenceCounter &other)
    {
        std::cout << "Reference Counter Assignment Operator" << std::endl;
    }
    
    ~ReferenceCounter() 
    {
        std::cout << "Reference Counter Destructor" << std::endl;
    };

    int RefCount()
    {
        return ref_count;
    }

private:
    std::atomic_int ref_count;
};
#endif
#ifndef PROCESS_DATA_H
#define PROCESS_DATA_H
#include "ReferenceCounter.h"
#include<string>
#include<iostream>

class ProcessData : public ReferenceCounter
{
public:
    ProcessData(int id, std::string info) :m_id(id), m_info(info) 
    {
        std::cout << "Process Data Constructor" << std::endl;
    }

    ProcessData(const ProcessData &other)
    {
        std::cout << "Process Data Copy Constructor" << std::endl;
        m_id = other.m_id;
        m_info = other.m_info;
    }

    const ProcessData operator=(const ProcessData &other)
    {
        std::cout << "Process Data Assignment Operator" << std::endl;
        m_id = other.m_id;
        m_info = other.m_info;
    }

    ~ProcessData()
    {
        std::cout << "Process Data Destructor" << std::endl;
    }

private:
    int m_id;
    std::string m_info;
};

#endif 
#include "ProcessData.h"
#include <boost/intrusive_ptr.hpp>


int main()
{
    boost::intrusive_ptr<ProcessData> ptr(new ProcessData(1, "a"));
    std::cout << "******************"<< std::endl;
    std::cout << "ref_count = " << ptr->RefCount() << std::endl;
    std::cout << "******************" << std::endl;
    {
        boost::intrusive_ptr<ProcessData> ptrCopy(ptr.get());
        std::cout << "ref_count after copy constructed = " << ptrCopy->RefCount() << std::endl;
    }

    std::cout << "******************" << std::endl;
    std::cout << "ref_count = " << ptr->RefCount() << std::endl;
    std::cout << "******************" << std::endl;

    {
        boost::intrusive_ptr<ProcessData> ptrAssignment = ptr;
        std::cout << "ref_count after assignment = " << ptrAssignment->RefCount() << std::endl;
    }

    std::cout << "******************" << std::endl;
    std::cout << "ref_count = " << ptr->RefCount() << std::endl;
    std::cout << "******************" << std::endl;

    {
        boost::intrusive_ptr<ProcessData> ptrWeak(ptr.get(), false);
        std::cout << "ref_count after construct weak_ptr = " << ptrWeak->RefCount() << std::endl;
    }

    std::cout << "******************" << std::endl;
    std::cout << "ref_count = " << ptr->RefCount() << std::endl;
    std::cout << "******************" << std::endl;
    
    system("pause");
    return 0;
}

//output
Reference Counter Constructor
Process Data Constructor
Call intrusive_ptr_add_ref
******************
ref_count = 1
******************
Call intrusive_ptr_add_ref
ref_count after copy constructed = 2
Call intrusive_ptr_release
******************
ref_count = 1
******************
Call intrusive_ptr_add_ref
ref_count after assignment = 2
Call intrusive_ptr_release
******************
ref_count = 1
******************
ref_count after construct weak_ptr = 1
Call intrusive_ptr_release
Reference Counter Destructor
******************
ref_count = -572662307
******************
上一篇 下一篇

猜你喜欢

热点阅读