Chinaunix首页 | 论坛 | 博客
  • 博客访问: 101868599
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: C/C++

2008-04-16 18:50:05

     来源:Blog    作者:fatalerror99

Widget 的客户像这样使用它的 new-handling capabilities(处理 new 的能力):

void outOfMem(); // decl. of func. to call if mem. alloc.
// for Widget objects fails

Widget::set_new_handler(outOfMem); // set outOfMem as Widget's
// new-handling function

Widget *pw1 = new Widget; // if memory allocation
// fails, call outOfMem

std::string *ps = new std::string; // if memory allocation fails,
// call the global new-handling
// function (if there is one)

Widget::set_new_handler(0); // set the Widget-specific
// new-handling function to
// nothing (i.e., null)

Widget *pw2 = new Widget; // if mem. alloc. fails, throw an
// exception immediately. (There is
// no new- handling function for
// class Widget.)

  无论 class 是什么,实现这个方案的代码都是一样的,所以在其它地方重用它就是一个合理的目标。使它成为可能的一个简单方法是创建一个 "mixin-style" base class(“混合风格”基类),也就是说,一个设计为允许 derived classes(派生类)继承一个单一特定能力(在当前情况下,就是设定一个 class-specific new-handler 的能力)的 base class(基类)。然后把这个 base class(基类)转化为一个 template(模板),以便于你得到针对每一个 inheriting class(继承来的类)的 class data 的不同拷贝。

  这个设计的 base class(基类)部分让 derived classes(派生类)继承它们全都需要的 set_new_handler 和 operator new functions,而这个设计 template(模板)部分确保每一个 inheriting class(继承来的类)得到一个不同的 currentHandler data member(数据成员)。这听起来可能有点复杂,但是代码看上去可靠而且熟悉。实际上,仅有的真正不同是它现在可以用在任何需要它的 class 之上:

template // "mixin-style" base class for
class NewHandlerSupport{
 // class-specific set_new_handler
public: // support

 static std::new_handler set_new_handler(std::new_handler p) throw();
 static void * operator new(std::size_t size) throw(std::bad_alloc);

 ... // other versions of op. new
private:
 static std::new_handler currentHandler;
};

template
std::new_handler
NewHandlerSupport::set_new_handler(std::new_handler p) throw()
{
 std::new_handler oldHandler = currentHandler;
 currentHandler = p;
 return oldHandler;
}

template
void* NewHandlerSupport::operator new(std::size_t size)
throw(std::bad_alloc)
{
 NewHandlerHolder h(std::set_new_handler(currentHandler));
 return ::operator new(size);
}
// this initializes each currentHandler to null
template
std::new_handler NewHandlerSupport::currentHandler = 0;

阅读(241) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~