- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我最近开始将我现有的许多类迁移到使用智能指针,我有几个关于如何移植我认为可以从使用智能指针中受益的代码的问题(但我当然可能是错的) .我有一个 UtlMemBuffer 缓冲区管理器类,如下所示,通过其头文件显示。
本质上,此类拥有一个由 void*/length 对组成的缓冲区 vector 。缓冲区管理是通过辅助方法(UtlMemBuffer::append - 其实现如下所示)实现的。
我想让这个类使用新的 C++11 智能指针,以便可以很好地定义所有权并最大限度地减少重新分配。要使用此类,客户端代码通常将其自己的原始指针缓冲区/长度传递给构造函数,或者它可以调用 append。最终,当 UtlMemBuffer 的析构函数被调用时,它释放自己的拷贝,确保在调用者负责其拷贝时没有内存泄漏。我认为使用 std::shared_ptr 不是传递原始指针,而是消除对这种类型的双缓冲区所有权的需要(即调用者将负责更新 std: :shared_ptr 并将其传递给 UtlMembuffer。
我认为最大的挑战是支持读取方法(接口(interface)类似于文件的工作方式),我需要通过原始指针传回扁平内存。也许更好的设计方法是传回一个 std::unique_ptr,它是我通过展平 shared_ptr 的内部集合创建的。我不太确定最好的方法是什么,我将不得不更改大量使用该类的当前代码来采用该方法。
调用者是否都应该为 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;
};
这是管理对缓冲区 block vector 的访问的辅助方法。如您所见,它重新分配原始指针并将它们存储在 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* 来表示内存字节,那么您可以简单地存储 vector 而不是成对的 void*/len。
这就是我在这里可能会做的。假设您不必对该内存空间中的对象进行任何实际破坏,只需转换为 char* 并粘贴在一个 vector 中。
关于c++ - 使用智能指针管理缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10418199/
我会尽可能地解释我正在做的事情,以获得最好的可能的建议/解决方案。这一切都是在 java 中完成的。 我的客户有一个基于 SWING 的桌面应用程序,它将使用 WebStart 加载。我被指派为用户帐
看来这个page包含 Azure CLI 支持的与 Azure API 管理相关的所有功能。但它没有展示如何使用 Azure CLI 管理用户、产品、证书、订阅和 API 等实体。 Azure CLI
我设置了一个 Hadoop 1.2.x 版本,双节点集群。第一节点(NameNode、Jobtracker)和第二节点(Secondary NameNode、Datanode、TaskTracker)
对于内容驱动的网站,设计好坏的关键是关系型数据库。在这个教程中,我们已经使用了MySQL关系型数据库管理系统(RDBMS)建立了我们的数据库。对于网站的开发者来说,MySQL是一个较受欢迎的选择,这
在尝试运行MariaDB之前,首先确定其当前状态,运行或关闭。 有三个选项用于启动和停止MariaDB – 运行mysqld(MariaDB脚本)。 运行mysqld_safe启动脚本。
我在管理界面中遇到 StackedInlines 前缀的问题。我会尝试发布所有必要的代码。 models.py(简要) ##### Base classes class BaseItem(models
我是新来的。到目前为止,我一直在使用 MVC 模型并使用基本的 session 管理模型,即在 session 中存储一个 token 并检查每个请求。 我正在尝试对lift做同样的事情,但我的 se
我在 win 服务中使用 NHiberante。有时我得到 System.ObjectDisposedException: Session is closed! Object name: 'ISess
我正在尝试使用 HtmlUnit 登录 Facebook 页面并查看其 HTML 内容。我正在尝试通过 HtmlUnit 填写登录凭据,但在单击提交按钮时我没有看到正在执行的 session 。 在
我正在为一个相当大的项目开发一个带有 reactjs 的前端,该项目有两个主要接口(interface)。主站点的前端和管理员的前端。 我应该将它们开发为两个不同的项目还是 reactjs 中的一个项
短版 我有一个使用插件基础结构的应用程序。插件具有可配置的属性,可帮助它们了解如何完成工作。插件按配置文件分组以定义如何完成任务,配置文件存储在由 DataContractSerializer 序列化
如何管理 iPhone 应用程序中的用户 session ?我在应用程序的第一页上从用户那里获取了用户名和密码。用户可以随时注销。如何像其他 Web 应用程序一样在 iPhone 应用程序中存储 se
我正在使用 Azure API 管理,其中包含第三方论坛 (Discourse) 的链接。 api管理提供的默认登录系统用于注册用户。我想知道是否可以对 api 管理和论坛使用单点登录,这样用户就不必
我正在使用 Wordpress 建立一个网站,并且我想利用它的 session 。但我没有找到任何插件,甚至文档。在我开始破解之前有什么建议或引用吗? 注意:我问的是 WP 是否以及如何使用标准 PH
我已阅读《Azure in Action》一书中的以下内容:“在 Windows Azure 中,状态服务器或进程外 session 状态提供程序,不支持” 谁能告诉我为什么不支持这个。他们在书中没有
我有一个内联表单集,我想排除一些模型对象在表单集中显示。 例如。模型 B 具有模型 A 的外键,因此它是 1:n(A 对象有许多 B 对象)关系。现在在 A 管理编辑页面上,我已经获得了 B 的内联。
我正在开发一个基于 session 的项目。我在想,与银行类似,我会创建一张支票并为用户提供阻止 session 超时的能力。 我正在考虑创建一个 setInterval 来检查需要身份验证的空白页面
我正在为一位拥有 Magento 商店的客户工作。里面塞满了产品,但这些产品的名称有点乱。他并没有坚持一种命名约定,而是多年来使用了不同的约定。因此,每当他使用“管理”->“管理产品”部分中的“名称”
我使用大约十几个 XSLT 文件来提供大量输出格式。目前,用户必须知道导出的文件格式的扩展名,例如RTF、HTML、TXT。 我还想使用参数来允许更多选项。如果我可以将元数据嵌入 XSL 文件本身,那
我已阅读《Azure in Action》一书中的以下内容:“在 Windows Azure 中,状态服务器或进程外 session 状态提供程序,不支持” 谁能告诉我为什么不支持这个。他们在书中没有
我是一名优秀的程序员,十分优秀!