strict_ptr.cpp

来自「最新的版本ACE-5.6.8,刚从外文网上搬下,与大家分享.」· C++ 代码 · 共 219 行

CPP
219
字号
// file      : Test/ReferenceCounting/StrictPtr/strict_ptr.cpp
// author    : Boris Kolpackov <boris@kolpackov.net>
// copyright : Copyright (c) 2002-2003 Boris Kolpackov
// license   : http://kolpackov.net/license.html

#include "Utility/ReferenceCounting/StrictPtr.hpp"
#include "Utility/ReferenceCounting/DefaultImpl.hpp"

using namespace Utility::ReferenceCounting;

struct Base : public virtual Interface
{
  virtual
  ~Base () throw ()
  {
  }
};

typedef
StrictPtr<Base>
BasePtr;

class Impl : public virtual Base,
             public virtual DefaultImpl <>
{
public:
  Impl (bool& destroyed)
      : dummy_ (false),
        destroyed_ (destroyed)
  {
  }

  Impl ()
      : dummy_ (false),
        destroyed_ (dummy_)
  {
  }

  virtual
  ~Impl () throw ()
  {
    destroyed_ = true;
  }

private:
  bool dummy_;
  bool& destroyed_;
};

typedef
StrictPtr<Impl>
ImplPtr;

struct E {};

void postcondition (bool p)
{
  if (!p) throw E ();
}

int main ()
{
  try
  {
    // StrictPtr ()
    //
    {
      BasePtr a;

      postcondition (a.in () == 0);
    }

    // StrictPtr (Type*)
    //
    {
      Impl* a (new Impl);
      ImplPtr b (a);

      postcondition (b.in () == a && a->refcount_value () == 1);
    }

    // StrictPtr (StrictPtr<Type> const&)
    //
    {
      ImplPtr a (new Impl);
      ImplPtr b (a);

      postcondition (a.in () == b.in () && a->refcount_value () == 2);
    }

    // StrictPtr (StrictPtr<Other> const&)
    //
    {
      ImplPtr a (new Impl);
      BasePtr b (a);

      postcondition (b.in () == static_cast<Base*>(a.in ()) &&
                     b->refcount_value () == 2);
    }

    // ~StrictPtr
    //
    {
      bool destroyed (false);
      {
        ImplPtr a (new Impl (destroyed));
      }

      postcondition (destroyed == true);
    }

    // operator= (Type* ptr)
    //
    {
      Impl* a (new Impl);
      ImplPtr b;
      b = a;

      postcondition (b.in () == a && a->refcount_value () == 1);
    }

    // operator= (StrictPtr<Type> const&)
    //
    {
      ImplPtr a (new Impl);
      ImplPtr b;
      b = a;

      postcondition (b.in () == a.in () && a->refcount_value () == 2);
    }

    // operator= (StrictPtr<Other> const&)
    //
    {
      ImplPtr a (new Impl);
      BasePtr b;
      b = a;

      postcondition (b.in () == static_cast<Base*>(a.in ()) &&
                     b->refcount_value () == 2);
    }

    // operator==
    //
    {
      Impl* a (new Impl);
      ImplPtr b (a);
      bool r (b.in () == a);

      postcondition (r == true);
    }

    // operator!=
    //
    {
      Impl* a (new Impl);
      ImplPtr b (a);
      bool r (b.in () != a);

      postcondition (r == false);
    }

    // operator->
    //
    {
      Impl* a (new Impl);
      ImplPtr b (a);
      Impl* c (b.operator-> ());

      postcondition (a == c);
    }

    // in
    //
    {
      Impl* a (new Impl);
      ImplPtr b (a);
      Impl* c (b.in ());

      postcondition (a == c);
    }

    // retn
    //
    {
      Impl* a (new Impl);
      ImplPtr b (a);
      Impl* c (b.retn ());

      postcondition (a == c);

      b = a; // give ownership back
    }

    // add_ref
    //
    {
      ImplPtr a (new Impl);
      ImplPtr b (add_ref (a));

      postcondition (a.in () == b.in () && b->refcount_value () == 2);
    }

    // strict_cast
    //
    {
      BasePtr a (new Impl);
      ImplPtr b (strict_cast<Impl>(a));

      postcondition (b != 0 && b->refcount_value () == 2);
    }
  }
  catch (...)
  {
    return -1;
  }
}
//$Id: strict_ptr.cpp 80826 2008-03-04 14:51:23Z wotte $

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?