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;
};