☰
C++仿SKData实现C原生指针管理
2026/11/2 6:09:08 网站建设 项目流程
template<typename T, typename SizeT = size_t> class HBData { public: HBData() : data(nullptr), size(0), isDeepCopy(false) {} HBData(const T* other_data, size_t other_size, bool release) : data(other_data), size(other_size), isDeepCopy(release) {} HBData(const HBData& other) { if(isDeepCopy && data) delete[] data; data = new T[other.size]; auto len = sizeof(T) * other.size; memcpy_s(data, len, other.data, len); size = other.size; isDeepCopy = true; } ~HBData() { if(isDeepCopy && data) delete[] data; } public: const T* data; SizeT size; bool isDeepCopy; };
template <typename T, typename SizeT = size_t> class HBData { public: HBData() : data(nullptr), size(0) {} HBData(const T *ptr, SizeT len) : data(ptr), size(len), ref(std::make_shared<int>(1)) {} HBData(const HBData &other) { *this = other; } ~HBData() { if (!ref) return; if (1 != ref.use_count()) return; delete[] data; } HBData deepCopy() const { auto newData = new T[size]; HBData hbData(newData, size); auto len = sizeof(T) * size; memcpy_s(newData, len, data, len); return hbData; } public: const T *data; SizeT size; std::shared_ptr<int> ref; };

https://chromium.googlesource.com/external/skia/include/+/f2e084053cd1dbd21fa3cece80d70045b2b0b83b/core/SkData.h

C++自定义智能指针-CSDN博客


创作不易,小小的支持一下吧!

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询