interface.cpp
来自「ACE编程的一本经典BIBLE的源代码,喜欢网络编程的别错过」· C++ 代码 · 共 105 行
CPP
105 行
// file : Test/ReferenceCounting/Interface/interface.cpp
// author : Boris Kolpackov <boris@kolpackov.net>
// copyright : Copyright (c) 2002-2003 Boris Kolpackov
// license : http://kolpackov.net/license.html
#include "Utility/ReferenceCounting/Interface.hpp"
using namespace Utility::ReferenceCounting;
struct Obj : public virtual Interface
{
Obj ()
: ref_count_ (1)
{
}
virtual
~Obj () throw ()
{
}
public:
virtual void
add_ref () const throw ()
{
add_ref_i ();
}
virtual void
remove_ref () const throw ()
{
if (remove_ref_i ()) delete this;
}
virtual count_t
refcount_value () const throw ()
{
return refcount_value_i ();
}
protected:
virtual void
add_ref_i () const throw ()
{
++ref_count_;
}
virtual bool
remove_ref_i () const throw ()
{
return --ref_count_ == 0;
}
virtual count_t
refcount_value_i () const throw ()
{
return ref_count_;
}
private:
mutable count_t ref_count_;
};
struct E {};
void postcondition (bool p) throw (E)
{
if (!p) throw E ();
}
int main ()
{
try
{
// add_ref
//
{
Obj* a (new Obj);
Obj* b (add_ref (a));
postcondition (a == b && a->refcount_value () == 2);
a->remove_ref ();
b->remove_ref ();
}
{
Obj* a (0);
Obj* b (add_ref (a));
postcondition (b == 0);
}
}
catch (...)
{
return -1;
}
}
//interface.cpp,v 1.1 2005/05/24 04:33:12 turkaye Exp
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?