我最近开始将我现有的许多类迁移到使用智能指针上,我对如何移植一些我认为可以从使用智能指针中受益的代码有一些问题(当然,我可能是错的)。我有一个UtlMemBuffer缓冲区管理器类,通过它的头文件显示如下。
本质上,这个类拥有一个由void*/length对组成的缓冲区向量。缓冲区管理是通过一个helper方法实现的(utlmembuffer::append-它的实现如下所示)。
我想让这个类使用新的C++11智能指针,这样可以很好地定义所有权并最小化重新分配。要使用此类,客户端代码通常将自己的原始指针buffer/length传入构造函数,或者可以调用append。最终,当调用UtlMemBuffer的析构函数时,它释放自己的副本,确保调用方负责其副本时没有内存泄漏。我认为,使用std::shared_ptr不是传入原始指针,而是消除了对这种类型的双重缓冲区所有权的需要(即调用方负责获取std::shared_ptr并将其传递给UTLMemBuffer)。
在我看来,最大的挑战是支持read方法(接口类似于文件的工作方式),在这里我需要通过一个原始指针传递回扁平化的内存。也许更好的设计方法是传回一个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;
};
这里是helper方法,它管理对缓冲区块向量的访问。如您所见,它重新分配原始指针并将它们存储在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;
}
如果作者简单地使用char*来表示内存字节,就像通常一样,那么您可以简单地存储向量,而不是void*/len成对。
这就是我在这里可能会做的。假设你不需要对这个内存空间中的对象进行任何实际的破坏,只需将其转换为Char*并插入一个矢量即可。