这个功能有没有更好的实现方式?

心中那自由的世界 2013-06-29 05:12:42

我有一组类,名字分别是,分别对应协议的各种请求,其中有一种请求是不需要解消息体的,直接将消息体转发就可以了。这种请求有七八个之多。
Common, ProtoReqA, ProtoReqB, ProtoReqC, ProtoSaveBody....
其中有
typedef ProtoSaveBody ProtoReqX;
typedef ProtoSaveBody ProtoReqY;
typedef ProtoSaveBody ProtoReqZ;


因为我在内存管理中使用了placement new,写了两个宏,一个用来NEW,一个用来DEL。

其中在对ProtoSaveBody这种类型的ProtoReqX调析构的时候,出现了一个不太好解决的问题。代码流程如下:
t = new(malloc(FIX_SIZE(namespaceA::ProtoReqX))) namespaceA::ProtoReqX(param1, param2);
.......
namespaceA::ProtoReqX* t = (namespaceA::ProtoReqX*) (decoder);
PL_DEL(ProtoReqX, t);
free(t);

因为ProtoReqX的析构函数本质上是~ProtoSaveBody,所以这里编译通不过。现在我只好写成
PL_DEL(ProtoSaveBody, t);

在gcc下,可以用typeof来搞定,在vc下怎么整?

另外,有没有人将boost::smart_ptr与boost::pool配套使用的?还是说内存池与smart_ptr都自己写了。
...全文
187 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
橡木疙瘩 2013-06-29
  • 打赏
  • 举报
回复
typedef ProtoSaveBody ProtoReqX 改成 #define ProtoReqX ProtoSaveBody 应该就可以了。
www_adintr_com 2013-06-29
  • 打赏
  • 举报
回复
引用 8 楼 119365374 的回复:
template<class Y, class D> shared_ptr(Y * p, D d);-->就是使用这个constructor?
是的
  • 打赏
  • 举报
回复
引用 3 楼 adlay 的回复:
boost 的智能指针是可以指定怎么删除的, 可以和内存池配合使用哦,
我看了一下boost的文档 destructor ~shared_ptr(); // never throws Effects: If *this is empty, or shares ownership with another shared_ptr instance (use_count() > 1), there are no side effects. Otherwise, if *this owns a pointer p and a deleter d, d(p) is called. Otherwise, *this owns a pointer p, and delete p is called. Throws: nothing. template<class Y, class D> shared_ptr(Y * p, D d);-->就是使用这个constructor?
www_adintr_com 2013-06-29
  • 打赏
  • 举报
回复
引用 4 楼 119365374 的回复:
[quote=引用 1 楼 adlay 的回复:] 用模板撒:

template <typename T>
void PL_DEL(T* x)
{
	x->~T();
}

模板参数会推导, 不用给出, 只需要 PL_DEL(t) 就行了.
项目限制使用模板,如果用gcc typeof的话,都不需要写模板了。还有别的办法嘛。
#include <iostream>
#include <typeinfo>

class A {
};
int main()
{
    A* a;
    typeof(a) b = new A();
    std::cout << typeid(b).name() << "\n";
}
[/quote] 方法倒是多: 1. 换项目 2. 把你的模板代码放到 boost 的文件下面, 假装不是你写的. 3. 换编译器, VC2010 就支持 typeof 了 4. boost 当中也有 typeof, 当然, 它是使用模板来实现的.
  • 打赏
  • 举报
回复
引用 5 楼 taodm 的回复:
项目不限制使用boost,竟然限制使用模板?够白痴级的设定了。 typedef ProtoSaveBody ProtoReqX; typedef ProtoSaveBody ProtoReqY; typedef ProtoSaveBody ProtoReqZ; 从设计上不会是一个好的设计的。虽然它一定程度可行。
boost也是裁剪过的,仅限于一两个模块使用。结贴了。
taodm 2013-06-29
  • 打赏
  • 举报
回复
项目不限制使用boost,竟然限制使用模板?够白痴级的设定了。 typedef ProtoSaveBody ProtoReqX; typedef ProtoSaveBody ProtoReqY; typedef ProtoSaveBody ProtoReqZ; 从设计上不会是一个好的设计的。虽然它一定程度可行。
  • 打赏
  • 举报
回复
引用 1 楼 adlay 的回复:
用模板撒:

template <typename T>
void PL_DEL(T* x)
{
	x->~T();
}

模板参数会推导, 不用给出, 只需要 PL_DEL(t) 就行了.
项目限制使用模板,如果用gcc typeof的话,都不需要写模板了。还有别的办法嘛。
#include <iostream>
#include <typeinfo>

class A {
};
int main()
{
    A* a;
    typeof(a) b = new A();
    std::cout << typeid(b).name() << "\n";
}
www_adintr_com 2013-06-29
  • 打赏
  • 举报
回复
boost 的智能指针是可以指定怎么删除的, 可以和内存池配合使用哦,
taodm 2013-06-29
  • 打赏
  • 举报
回复
既然boost都会用了,竟然还不能把PL_DEL(ProtoSaveBody, t);写成PL_DEL(t);?
www_adintr_com 2013-06-29
  • 打赏
  • 举报
回复
用模板撒:

template <typename T>
void PL_DEL(T* x)
{
	x->~T();
}

模板参数会推导, 不用给出, 只需要 PL_DEL(t) 就行了.

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧