随便写了个shared_ptr

//
// Created by bomeng on 6/14/23.
//

#ifndef DEMO_MY_SHARED_PTR_H
#define DEMO_MY_SHARED_PTR_H

#include <stdexcept>

namespace bomeng {
    template<typename T>
    class Ref {
    private:
        int r_count;
        T *obj;

    public:
        explicit Ref(T *target)
                : obj(target), r_count(1) {}

        ~Ref() {
            delete obj;
            obj = nullptr;
        }

        inline void increase() { r_count++; }

        inline void reduce() { r_count--; }

        T *get() { return obj; }

        int getCount() { return r_count; }
    };

    template<class T>
    class shared_ptr {
    private:
        Ref<T> *ref;

    private:
        void deleteRef() {
            if (ref) {
                ref->reduce();
                if (ref->getCount() == 0)
                    delete ref;
                ref = nullptr;
            }
        }

    public:

        shared_ptr()
                : ref(nullptr) {}

        explicit shared_ptr(T *ptr)
                : ref(new Ref<T>(ptr)) {}

        ~shared_ptr() {
            deleteRef();
        }

        //拷贝构造
        shared_ptr(const shared_ptr<T> &s_ptr)
                : ref(s_ptr.ref) {
            if (ref)
                ref->increase();
        }

        //拷贝赋值
        shared_ptr<T> &operator=(const shared_ptr<T> &s_ptr) {
            deleteRef();

            ref = s_ptr.ref;
            if (ref)
                ref->increase();
        }

        //移动构造
        shared_ptr(shared_ptr<T> &&s_ptr)
                : ref(s_ptr.ref) {
            s_ptr.ref = nullptr;
        }

        //移动赋值
        shared_ptr<T> &operator=(shared_ptr<T> &&s_ptr) {
            deleteRef();

            ref = s_ptr.ref;
            s_ptr.ref = nullptr;
        }

    public:
        void reset(T *target = nullptr) {
            deleteRef();

            if (target)
                ref = new Ref<T>(target);
        }

        T &operator*() {
            if (ref == nullptr)
                throw std::runtime_error("ERROR dereference a null pointer!");
            return *(ref->get());
        }

        T *operator->() {
            if (ref == nullptr)
                throw std::runtime_error("ERROR dereference a null pointer!");
            return ref->get();
        }

        int use_count() {
            if (ref)
                return ref->getCount();
            return 0;
        }

        T *get() { return ref->get(); }

    public:
        template<typename T2>
        friend bool operator==(const shared_ptr<T2> &s_ptr1, const shared_ptr<T2> &s_ptr2);

        template<typename T2>
        friend bool operator!=(const shared_ptr<T2> &s_ptr1, const shared_ptr<T2> &s_ptr2);

        template<typename T2>
        friend bool operator==(const shared_ptr<T2> &s_ptr, std::nullptr_t);

        template<typename T2>
        friend bool operator!=(const shared_ptr<T2> &s_ptr, std::nullptr_t);

        template<typename T2>
        friend bool operator==(std::nullptr_t, const shared_ptr<T2> &s_ptr);

        template<typename T2>
        friend bool operator!=(std::nullptr_t, const shared_ptr<T2> &s_ptr);
    };

    template<typename T>
    bool operator==(const shared_ptr<T> &s_ptr1, const shared_ptr<T> &s_ptr2) {
        return s_ptr1.ref == s_ptr2.ref;
    }

    template<typename T>
    bool operator!=(const shared_ptr<T> &s_ptr1, const shared_ptr<T> &s_ptr2) {
        return s_ptr1.ref != s_ptr2.ref;
    }

    template<typename T>
    bool operator==(const shared_ptr<T> &s_ptr, std::nullptr_t) {
        return s_ptr.ref == nullptr;
    }

    template<typename T>
    bool operator!=(const shared_ptr<T> &s_ptr, std::nullptr_t) {
        return s_ptr.ref != nullptr;
    }

    template<typename T>
    bool operator==(std::nullptr_t, const shared_ptr<T> &s_ptr) {
        return s_ptr.ref == nullptr;
    }

    template<typename T>
    bool operator!=(std::nullptr_t, const shared_ptr<T> &s_ptr) {
        return s_ptr.ref != nullptr;
    }

}

#endif //DEMO_MY_SHARED_PTR_H

全部评论
川大✌太强啦
点赞 回复 分享
发布于 2023-09-25 11:17 四川

相关推荐

牛客51274894...:照片认真的吗,找个专门拍证件照的几十块钱整端正点吧,要不就别加照片
点赞 评论 收藏
分享
评论
2
2
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
11438次浏览 98人参与
# 你的实习产出是真实的还是包装的? #
2014次浏览 42人参与
# MiniMax求职进展汇总 #
24213次浏览 310人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7687次浏览 43人参与
# 简历第一个项目做什么 #
31792次浏览 344人参与
# 重来一次,我还会选择这个专业吗 #
433635次浏览 3926人参与
# 米连集团26产品管培生项目 #
6117次浏览 216人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187256次浏览 1122人参与
# 牛客AI文生图 #
21456次浏览 238人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152508次浏览 888人参与
# 研究所笔面经互助 #
118985次浏览 577人参与
# 简历中的项目经历要怎么写? #
310464次浏览 4224人参与
# AI时代,哪些岗位最容易被淘汰 #
63991次浏览 832人参与
# 面试紧张时你会有什么表现? #
30527次浏览 188人参与
# 你今年的平均薪资是多少? #
213192次浏览 1039人参与
# 你怎么看待AI面试 #
180248次浏览 1261人参与
# 高学历就一定能找到好工作吗? #
64348次浏览 620人参与
# 你最满意的offer薪资是哪家公司? #
76602次浏览 374人参与
# 我的求职精神状态 #
448216次浏览 3129人参与
# 正在春招的你,也参与了去年秋招吗? #
363619次浏览 2638人参与
# 腾讯音乐求职进展汇总 #
160708次浏览 1112人参与
# 校招笔试 #
471467次浏览 2964人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务