⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 interface.cpp

📁 ACE编程的一本经典BIBLE的源代码,喜欢网络编程的别错过
💻 CPP
字号:
// 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -