SmartPtr实作

Boost.SmartPtr 的快速 (CI) 测试 Boost.SmartPtr 的快速测试实现功能C++实现代码 实现功能 Boost.SmartPtr 的快速 (CI) 测试 C++实现代码 #include <boost/smart_ptr.hpp> #include <boost/core/lightweight_test.hpp> class X { private: X( X const & ); X & operator=( X const & ); public: 阅读详情

#ifndef _KOK_SMARTPTR_H_
#define _KOK_SMARTPTR_H_

#pragma once

#include <cstdlib>
#include <assert.h>
#include <algorithm>

namespace KOK
{
    template<class P>
    class CheckAssert
    {
    protected:
        void OnRef(const P& pointer)
        {
            assert(pointer !=NULL);
        }
        void OnDeRef(const P& pointer)
        {
            assert(pointer !=NULL);
        }
        void Swap(CheckAssert&){};

        CheckAssert(){};
        CheckAssert(const CheckAssert&){};

        template<class U>
        CheckAssert(const CheckAssert<U>&){};
    };
    template<class T>
    class DefaultStorage
    {
    public:
        typedef T* StoredType;
        typedef T* PointerType;
        typedef T& RefType;
    protected:
        DefaultStorage():pointee_(Default()){};
        DefaultStorage(StoredType pType):pointee_(pType){};
        DefaultStorage(const DefaultStorage& pType){};

        template<class H>
        DefaultStorage(const DefaultStorage<H>& pType){};

        void Swap(DefaultStorage& other)
        {
            std::swap(pointee_, other.pointee_);
        };
        void Destroy()
        {
            delete pointee_;
        };
        static StoredType Default()
        {
            return 0;
        }
    public:
        RefType operator *() const
        {
            return *pointee_;
        }
        PointerType operator ->() const
        {
            return pointee_;
        }
        template<class U>
        friend typename DefaultStorage<U>::StoredType GetPtrImpl(const DefaultStorage<U>& wrap);

        template<class U>
        friend typename DefaultStorage<U>::RefType GetRef(const DefaultStorage<U>& wrap);

        template<class U>
        friend typename DefaultStorage<U>::StoredType& GetPtrRef(DefaultStorage<U>& wrap);

        template<class U>
        friend typename const DefaultStorage<U>::StoredType& GetPtrRef(const DefaultStorage<U>& wrap);

    private:
        StoredType pointee_;

    };
    template<class U>
    inline typename DefaultStorage<U>::StoredType GetPtrImpl(const DefaultStorage<U>& wrap)
    {
        return wrap.pointee_;
    };
    template<class U>
    inline typename DefaultStorage<U>::RefType GetRef(const DefaultStorage<U>& wrap)
    {
        return *wrap.pointee_;
    };
    template<class U>
    inline typename DefaultStorage<U>::StoredType& GetPtrRef(DefaultStorage<U>& wrap)
    {
        return wrap.pointee_;
    };
    template<class U>
    inline typename const DefaultStorage<U>::StoredType& GetPtrRef(const DefaultStorage<U>& wrap)
    {
        return wrap.pointee_;
    };
    template<class P>
    class RefCount
    {
    private:
        unsigned int* count_;
    protected:
        RefCount(const RefCount& other):count_(other.count_){};
        template<class P1>
        RefCount(const RefCount<P1>& other):count_(reinterpret_cast<const RefCount&>(other).count_){};

        RefCount():count_(new unsigned int(1))
        {
            assert(count_ != NULL);
        };
        P Clone(const P& val)
        {
            ++*count_;
            return val;
        }
        bool Release()
        {
            if(--*count_ == 0)
            {
                delete count_;
                return true;
            }
            return false;
        };
        void Reset()
        {
            *count_ = 1;
        };
        void Swap(RefCount& other)
        {
            std::swap(count_, other.count_);
        };
        enum {destroyCopy = false};
    };

    template<class T,
        template<class> class StoragePolicy = DefaultStorage,
        template<class> class OwnershipPolicy = RefCount,
        template<class> class CheckErrorPolicy = CheckAssert>
    class SmartPtr : public StoragePolicy<T>,
        public OwnershipPolicy<typename StoragePolicy<T>::StoredType>,
        public CheckErrorPolicy<typename StoragePolicy<T>::StoredType>
    {
    public:
        typedef typename StoragePolicy<T>::StoredType StoredType;
        typedef typename StoragePolicy<T>::PointerType PointerType;
        typedef typename StoragePolicy<T>::RefType RefType;

        template<bool B, class X, class Y>
        struct Select
        {
        };
        template<class X, class Y>
        struct Select<false, X, Y>
        {
            typedef Y type;
        };
        template<class X, class Y>
        struct Select<true, X, Y>
        {
            typedef Y type;
        };

    private:
        typedef StoragePolicy<T> SP;
        typedef OwnershipPolicy<StoredType> OP;
        typedef CheckErrorPolicy<StoredType> CP;

    public:
        typedef typename Select<OP::destroyCopy, SmartPtr<T, StoragePolicy, OwnershipPolicy, CheckErrorPolicy>, const SmartPtr<T, StoragePolicy, OwnershipPolicy, CheckErrorPolicy> >::type CpyArg;
        SmartPtr(StoredType ptr):SP(ptr)
        {
            assert(NULL != ptr);
        };

        SmartPtr()
        {
        };

        SmartPtr(CpyArg& copyPtr):SP(copyPtr), OP(copyPtr), CP(copyPtr)
        {
            GetPtrRef(*this) = OP::Clone(GetPtrImpl(copyPtr));
        };
        template<class T1,
            template<class> class SP1,
            template<class> class OP1,
            template<class> class CP1>
        SmartPtr(SmartPtr<T1, SP1, OP1, CP1>& copyPtr):SP(copyPtr), OP(copyPtr), CP(copyPtr)
        {
            GetPtrRef(*this) = OP::Clone(GetPtrImpl(copyPtr));
        };
        template<class T1,
            template<class> class SP1,
            template<class> class OP1,
            template<class> class CP1>
        SmartPtr(const SmartPtr<T1, SP1, OP1, CP1>& copyPtr):SP(copyPtr), OP(copyPtr), CP(copyPtr)
        {
            GetPtrRef(*this) = OP::Clone(GetPtrImpl(copyPtr));
        };
        RefType operator *()
        {
            CP::OnDeRef(GetPtrImpl(*this));
            return SP::operator*();
        };
        PointerType operator ->()
        {
            CP::OnDeRef(GetPtrImpl(*this));
            return SP::operator->();
        };
        template<class T1,
            template<class> class SP1,
            template<class> class OP1,
            template<class> class CP1>
        SmartPtr& operator =(SmartPtr<T1, SP1, OP1, CP1>& rhs)
        {
            SmartPtr temp(rhs);
            temp.Swap(*this);
            return *this;
        };
        template<class T1,
            template<class> class SP1,
            template<class> class OP1,
            template<class> class CP1>
        SmartPtr& operator =(const SmartPtr<T1, SP1, OP1, CP1>& rhs)
        {
            SmartPtr temp(rhs);
            temp.Swap(*this);
            return *this;
        };
        SmartPtr& operator =(CpyArg& rhs)
        {
            SmartPtr temp(rhs);
            temp.Swap(*this);
            return *this;
        };
        void Swap(SmartPtr& other)
        {
            OP::Swap(other);
            SP::Swap(other);
            CP::Swap(other);
        };
        ~SmartPtr()
        {
            if(OP::Release())
                SP::Destroy();
        };
    private:
        class Tester
        {
            void operator delete(void*);
        };
    public:
        operator Tester*()
        {
            if(GetPtrImpl(*this) == 0)
                return 0;
            static Tester test;
            return &test;
        };
        template<class T1,
            template<class> class SP1,
            template<class> class OP1,
            template<class> class CP1>
        friend void Release(SmartPtr<T1, SP1, OP1, CP1>& des, typename SmartPtr<T1, SP1, OP1, CP1>::StoredType& p);

        template<class U>
        friend inline bool operator ==(const SmartPtr& lhs, const U* rhs)
        {
            return GetPtrImpl(lhs) == rhs;
        };
        template<class U>
        friend inline bool operator ==(const U* lhs, const SmartPtr& rhs)
        {
            return rhs == lhs;
        };
        friend inline bool operator ==(const SmartPtr& lhs, const T* rhs)
        {
            return GetPtrImpl(lhs) == rhs;
        };
        friend inline bool operator ==(const T* lhs, const SmartPtr& rhs)
        {
            return rhs == lhs;
        };

        template<class U>
        friend inline bool operator !=(const SmartPtr& lhs, const U* rhs)
        {
            return !(lhs == rhs);
        };
        template<class U>
        friend inline bool operator !=(const U* lhs, const SmartPtr& rhs)
        {
            return rhs != lhs;
        };
        friend inline bool operator !=(const SmartPtr& lhs, const T* rhs)
        {
            return !(lhs == rhs);
        };
        friend inline bool operator !=(const T* lhs, const SmartPtr& rhs)
        {
            return rhs != lhs;
        };

    };

    template<class T1,
        template<class> class SP1,
        template<class> class OP1,
        template<class> class CP1>
    inline void Release(SmartPtr<T1, SP1, OP1, CP1>& des, typename SmartPtr<T1, SP1, OP1, CP1>::StoredType& p)
    {
        p = GetPtrImpl(des);
        GetPtrRef(des) = SP1<T1>::Default();
    };
    template<class T1,
        template<class> class SP1,
        template<class> class OP1,
        template<class> class CP1,
    class T2,
        template<class> class SP2,
        template<class> class OP2,
        template<class> class CP2>
    inline bool operator ==(const SmartPtr<T1, SP1, OP1, CP1>& lhs, const SmartPtr<T2, SP2, OP2, CP2>& rhs)
    {
        return GetPtrImpl(lhs) == GetPtrImpl(rhs);
    };
    template<class T1,
        template<class> class SP1,
        template<class> class OP1,
        template<class> class CP1,
    class T2,
        template<class> class SP2,
        template<class> class OP2,
        template<class> class CP2>
    inline bool operator !=(const SmartPtr<T1, SP1, OP1, CP1>& lhs, const SmartPtr<T2, SP2, OP2, CP2>& rhs)
    {
        return !(lhs == rhs);
    };
}
#endif

 

Boost.SmartPtr 快速测试 代码中分别测试了 Boost.SmartPtr 中的 shared_ptr、weak_ptr 和 unique_ptr。对于 unique_ptr,我们测试了其释放内存的正确性。Boost.SmartPtr 是 Boost 库中提供的一组智能指针,可以自动管理内存,避免手动申请和释放内存时出现的问题。可以看到,Boost.SmartPtr 中的智能指针在测试中表现良好,符合预期的功能和效果。通过使用这些智能指针,我们可以避免手动管理内存时出现的错误,提高代码的可靠性和安全性。 阅读详情

相关推荐

智能指针Boost.SmartPtr的快速测试编程

除了shared_ptr,Boost.SmartPtr还提供了其他类型的智能指针,例如scoped_ptr和intrusive_ptr。intrusive_ptr是一种使用引用计数的智能指针,但与shared_ptr不同的是,它要求被管理的对象实现特定的接口。请注意,在实际编程中,除了Boost.SmartPtr库,C++11及更高版本还提供了自己的智能指针类型std::shared_ptr和std::unique_ptr,它们也是非常有用的工具。当超出p的作用域时,shared_ptr会自动释放内存。

LogicGuruX的博客 108

c++ - 第21节 - 智能指针

为什么需要智能指针。

qq_45113223的博客 1262

smartPtr.h

智能指针,写的类似于std,带引用技术功能的

智能指针 smart_ptr

什么是内存泄漏:内存泄漏指因为疏忽或错误造成程序未能释放已经不再使用的内存的情况。内存泄漏并不是指内存在物理上的消失,而是应用程序分配某段内存后,因为设计错误,失去了对该段内存的控制,因而造成了内存的浪费。内存泄漏的危害:长期运行的程序出现内存泄漏,影响很大,如操作系统、后台服务等等,出现内存泄漏会导致响应越来越慢,最终卡死。

qq_68006585的博客 1413

smartptr——基于自动引用计数的智能指针

智能指针相信大家听说过吧,我理解的智能指针,既是一个C++模板类,重载了指针操作符(->)和(*)操作符,而使它的实例可以"用指针的形式去调用"。 它可以接管任意类型的指针,所以你只需要放心大胆的new出任意对象,然后用智能指针去接管它,而不用担心在什么地方去delete,这个对象会最后一个接管它的智能指针释放它或者被销毁时而被delete;如果贪图方便,你甚至可以在调用智能指针的构造函数时去new出你的对象。 内部实现的关键在于:自动地址引用计数、操作符重载。 可以实现任意类型(基本数据类型、自定义类型)的指针地址的自动引用计数。

JMeter压测Dubbo接口的完整落地方案(适配3.x)

Dubbo作为主流Java微服务RPC框架,其接口压测长期面临协议私有、注册发现复杂、序列化耦合度高等技术门槛。理解Dubbo基于TCP长连接、泛化调用与SPI扩展机制的核心原理,是实现稳定压测的前提;技术价值在于绕过HTTP网关直击服务核心链路,保障大促等关键场景的性能可信度;典型应用场景包括电商订单链路压测、风控/库存等内部Dubbo服务SLA验证,以及CI/CD中性能左移实践。本文聚焦Dubbo 3.x版本,提供零第三方插件、可复用、避坑的JMeter Java Sampler落地路径。

weixin_33700809的博客 549

C++11---智能指针

智能指针1. 为什么引入智能指针?1.1 内存泄漏2.智能指针的使用及原理2.1 RAII2.2 智能指针的原理3. C++98- - -auto_ptr4. unique_ptr5. shared_ptr5.1 循环引用6. weak_ptr 1. 为什么引入智能指针? void Func() { vector<int*> v; int* p = new int; v.push_back(p); //因为push_back的时候有可能需要申请空间,但是万一申请内存失败了怎么办 v.a

MEANSWER的博客 1705

无法获得下列放可solidworks standard 无法连接到服务器-15,10,10061如何解决?

无法获得下列放可solidworks standard 无法连接到服务器-15,10,10061如何解决?

qq_63539957的博客 2万+

SmartPtr(智能指针)

为什么会有智能指针? 在我们现实生活中有这样一个使用场景,每次我们new或者malloc了一块空间,完了必须需要我们手动delete或者free释放这块空间,否则会出现内存泄漏。但是,实际应用中程序未必都会按照顺序执行到释放空间那一步,有时候会在中间部分抛出异常使执行流发生跳转或者发生其他状况,总之代码未按照我们期望的那样去执行,就有可能导致内存泄漏。这时候我们就引入智能指针(自动化管理指针所指...

ypt 1803

【Godot】实现对话系统

在 Godot 中实现对话系统(Dialogue System)是游戏开发中的常见需求。Godot本身自带的的灵活性和轻量级脚本语言 GDScript 使得实现对话系统变得相对简单。以下是实现一个基础对话系统的示例。(语言为GDScript但高亮选择Python因为类似)通过 Godot 和 GDScript,我们可以给我们的游戏实现一个对话系统。从简单的文本显示到复杂的分支对话,Godot 提供了足够的灵活性来满足我们的需求。如果需要更高级的功能,可以尝试使用现成的插件如 Dialogic。

Octopus2077的博客 2521

调整开关频率处理传导骚扰超标的系统方法

针对某产品在传导骚扰测试中660kHz频点超标问题,分析发现与DC-DC模块开关频率直接相关。通过调整模块开关频率至500kHz,使其避开标准限值严苛频段(0.53-1.8MHz),在不改变硬件设计的情况下实现测试通过。该案例表明,利用EMC标准的频率特性进行"频谱设计"比传统滤波手段更高效,建议在多模块系统中采用1:2:4等频率规划策略,从源头降低噪声耦合风险。这一频率迁移思路为EMC优化提供了新范式。

硬核科技 328

c++智能指针(SmartPtr)简单剖析

一、什么是智能指针?智能指针(smart pointer):是存储指向动态分配(堆)对象指针的类,用于生存期控制,能够确保自动正确的销毁动态分配的对象,防止内存泄露。它的一种通用实现技术是使用引用计数(reference count)。由于c++语言没有自动回收机制,写代码时总要new一个空间,用完之后必须要delete掉,有时候程序代码过于复杂,经常会忘记delete内存,从而导致程序异退出,因此

Sakuramyb马永博的博客 2884

【Dify】知识库智能检索应用

本文介绍了一个基于大语言模型的知识库检索系统,实现从自然语言提问到智能回答的自动化流程。该系统通过知识检索节点定位相关信息,利用重排序模型优化结果,最后由大语言模型生成自然语言答案。核心模型包括InternLM2-7B和BCE重排序模型。工作流程包含输入问题、检索重排序、智能生成和答案输出四个阶段,适用于企业知识管理、客户支持、教育培训等多种场景,能显著提升信息检索效率和准确性。文章还提供了相关开发文档链接,涵盖AIGC工具平台搭建、常见问题解决等内容。

Mr数据杨 1000

郁闷的Loki::SmartPtr

最近做的项目,需要在多线程中管理资源,很自然就想到使用智能指针搭配引用计数的技术构造垃圾收集回收机制。首先考虑的是采用loki中的SmartPtr,并且已经在项目中运作,现在才发现Loki::SmartPtr并不是想象中的灵活好用。问题在于SmartPtr的构造函数(用原始指针构造出智能指针对象):  SmartPtr(const StoredType& p) : SP(p)        {  

醉大饿极的专栏 3107

HFSS实战项目——基片集成波导(SIW)带通滤波器

设计了一款直接耦合的SIW带通滤波器, 采用直接耦合的形式, 工作频率覆盖19~21 GHz, 通带内插入损耗小于1.5 dB, 回波损耗小于-20 dB, 带外抑制特性良好.

2401_87464239的博客 4897

Transformer实战(5)——Hugging Face环境配置与应用详解

本文详细介绍 Transformer 模型的实践应用全流程。首先讲解基于 Anaconda 的环境配置,包括 transformers 库安装及 hf-mirror 镜像加速方案。然后以 BERT 为例,演示分词器使用、文本编码及 fill-mask 等任务实现,对比 PyTorch/TensorFlow 框架差异。接着展示 Hugging Face 社区模型应用,包括 GPT-2 文本生成和零样本分类。最后扩展至多模态领域,使用 CLIP 模型实现零样本图像分类。

盼小辉丶的博客 1万+

Loki 智能指针 SmartPtr 的使用

Loki SmartPtr C++ 智能指针

xuxiangwin的专栏 2423

9篇3章7节:基于 R 语言的 FAERS 数据提取与合并

本文主要探讨了 FAERS 数据提取与合并的操作流程。首先,通过程序化方法从本地存储的 FAERS ASCII 文件中提取指定季度或范围内的单药报告数据,并可通过参数设置控制数据存储位置、处理方式、计算资源分配以及报告者职业类别等。随后,通过批量读取的方式对多个季度的 DRUG 文件进行循环处理,并将其合并为一个综合数据框。

MD Code 1262

SmartPtr

1.拥有权的管理策略  深层拷贝(deep copy):拷贝SmartPtr的时候也拷贝其所指之物  临写拷贝(copy on write):第一次修改所指对象的时候拷贝,由于无法区分哪些操作修改所指对象,所以很难实现。  引用计数(reference counting):记录对象被引用的次数,次数为0时删除该对象。缺点,怕出现循环引用的情况。  引用链接(reference linking):我

majcos的专栏 847

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

smartptr讲义

文章来源:https://www.jianshu.com/p/f94846969d86 为什么会有智能指针? 在我们现实生活中有这样一个使用场景,每次我们new或者malloc了一块空间,完了必须需要我们手动delete或者free释放这块空间,否则会出现内存泄漏。但是,实际应用中程序未必都会按照顺序执行到释放空间那一步,有时候会在中间部分抛出异常使执行流发生跳转或者发生其他状况,总之代码未按照我...

十年代码写十年 210

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

C++学习之模板应用 ---array、smartptr、stack

有array(数组)、smartptr(智能指针)、stack(栈); 一、array: 1、我们实现了指针访问和下标访问。C++中指针访问常用迭代器来代替。 2、这里我们也揭示了 size_type、iterator、[] 、begin()、end()、size()的真实面目; Array.hpp 1 #ifndef ARRAY_H_ 2 #define ARRAY_H_ ...

weixin_30765505的博客 110

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

对SmartPtr的一次重要修订!

这次修改主要针对如下几个问题:(1)防止指向同一个对象的智能指针之间的意外赋值;(2)使智能指针能够随意地放置在各种容器中,比如STL容器、数组等;(3)能够对存放智能指针的容器进行排序、合并、切分等等操作,比如调用list.sort()或者std::sort()等算法。修改后的代码如下:     // 非侵入式引用计数器管理对象接口templatecl

春风化雪的专栏 1969

smartPtr指针的实现

编写一个智能指针类。智能指针是一种数据类型,一般用模板实现,模拟指针行为的同时还提供自动来及回收机制。它会自动记录SmartPointer&lt;T*&gt;对象的引用计数,一旦T类型对象的引用计数为零,就会释放该对象。 解法: 智能指针跟普通指针一样,但它借由自动化内存管理保证了安全性,避免了诸如悬挂指针、内存泄漏和分配失败等问题。 智能指针必须为给定对象的所有引用维护单一引用计数。 实...

weixin_33788244的博客 196

BOOST c++库学习 之 Smart Ptr库智能指针库入门实战指南 以及 使用boost.smartptr库以及使用普通指针且不释放内存的对比例程(需要实际运行,然后观察电脑内存的变化)

自动内存管理库通过引用计数、作用域管理等机制,实现了动态内存的自动管理。智能指针可以在超出作用域或不再需要时自动释放内存,避免手动管理可能引发的错误。Boost库中的智能指针提供了相较于普通指针更安全、更高效的内存管理方案。它们自动化的内存管理、生命周期控制、线程安全特性等显著减少了内存泄漏、悬挂指针、循环引用等常见问题,使得C++开发更加健壮和高效。因此,推荐在C++编程中尽量使用智能指针代替普通指针,特别是在管理动态分配的资源时。

weixin_44251074的博客 870
上一篇: RBTree实作
下一篇: hashtable
daeees
博客等级 码龄19年 6粉丝 51原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值