【发布时间】:2012-05-12 04:33:43
【问题描述】:
我最近开始将许多现有的类迁移到使用智能指针,我有一些关于如何移植一些我认为可以从使用智能指针中受益的代码的问题(但我当然可能错了) .我通过其头文件有一个 UtlMemBuffer 缓冲区管理器类,如下所示。
本质上,这个类拥有一个由 void*/length 对组成的缓冲区向量。缓冲区管理是通过辅助方法实现的(UtlMemBuffer::append - 其实现也如下所示)。
我想让这个类使用新的 C++11 智能指针,以便可以很好地定义所有权并最小化重新分配。要使用此类,客户端代码通常将其自己的原始指针缓冲区/长度传递给构造函数,或者它可以调用 append。最终,当 UtlMemBuffer 的析构函数被调用时,它会释放自己的副本,确保在调用者负责其副本时没有内存泄漏。我认为不是传入原始指针,而是使用 std::shared_ptr 将消除对这种类型的双缓冲区所有权的需要(即调用者将负责更新 std: :shared_ptr 并将其传递给 UtlMembuffer。
在我看来,最大的挑战是支持读取方法(接口类似于文件的工作方式),我需要通过原始指针将展平的内存传回。也许更好的设计方法是让我回传一个 std::unique_ptr,它是通过展平 shared_ptrs 的内部集合创建的。我不太确定最好的方法是什么,尽管我必须更改相当多的当前代码,这些代码使用该类来采用这种方法。
调用者是否都应该为 ex 传递 std::shared_ptr 指针。我想知道进行这种转换的最佳方法是什么。
我对这个智能指针业务很陌生,所以任何建议都将不胜感激。谢谢
/**
* Smart Buffer class
*/
class UtlMemBuffer
{
public:
// default constructor
UtlMemBuffer(
const UtlPath& rBufferPath = UtlPath::ssNull,
const void* pBytes = NULL,
const size_t& rBufLength = 0);
// copy constructor
UtlMemBuffer(const UtlMemBuffer& rhs);
// move constructor
UtlMemBuffer(UtlMemBuffer&& rhs);
inline void swap(UtlMemBuffer& rhs) throw() {
// enable ADL (not necessary in our case, but good practice)
using std::swap;
// no need to swap base members - as we are topmost class
swap(mBufferPath, rhs.mBufferPath);
swap(mBufferLength, rhs.mBufferLength);
swap(mBufferBlocks, rhs.mBufferBlocks);
}
// unified assignment operator
UtlMemBuffer& operator=(UtlMemBuffer rhs);
// destructor - pure virtual
virtual ~UtlMemBuffer();
// add buffer to this one
virtual OsStatus append(
const void* pBytes,
const size_t& rBufLength,
size_t& rBufLengthWritten);
// comparator
bool operator==(const UtlMemBuffer& rhs) const;
// comparator
bool operator<(const UtlMemBuffer& rhs) const;
// determine the size of the buffer
size_t size() const;
/**
* comparable interface
*
* @returns 0 if equal, negative val if less than &
* negative value if greater.
*/
virtual int compareTo(const UtlMemBuffer& rhs) const;
/** copy the bytes into the designated buffer */
OsStatus read (void* pReturnBuffer,
const size_t& rMemBufferOffset,
const size_t& rReturnBufferLength,
size_t& rBytesRead) const;
// free existing linked list of blocks
void clear();
// path property
void setBufferPath(const UtlPath& rBufferPath);
UtlPath getBufferPath() const;
private:
typedef std::vector< std::pair<void*, size_t> > MemBufInfo;
// the name of the buffer (sort of file name)
UtlPath mBufferPath;
// this is updated whenever we append data
size_t mBufferLength;
// here we have a collection of received appends (effectively blocks)
MemBufInfo mBufferBlocks;
};
这是管理对缓冲块向量的访问的辅助方法。如您所见,它重新分配原始指针并将它们存储在 mBufferBlocks 成员中。
OsStatus
UtlMemBuffer::append(const void* pBytes,
const size_t& rBufLength,
size_t& rBytesWritten)
{
rBytesWritten = 0;
if (pBytes != NULL) {
void* block = new char [rBufLength];
memcpy(block, pBytes, rBufLength);
mBufferBlocks.push_back(std::make_pair(block, rBufLength));
rBytesWritten = rBufLength;
mBufferLength += rBufLength;
}
return OS_SUCCESS;
}
【问题讨论】:
标签: c++ c++11 shared-ptr smart-pointers unique-ptr