summaryrefslogtreecommitdiff
path: root/contrib/utility/Test/ReferenceCounting/DefaultImpl/default_impl.cpp
blob: 971751b6677411995ef33eb1d195f4ddc9bae73b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// file      : Test/ReferenceCounting/DefaultImpl/default_impl.cpp
// author    : Boris Kolpackov <boris@kolpackov.net>
// copyright : Copyright (c) 2002-2003 Boris Kolpackov
// license   : http://kolpackov.net/license.html

#include "Utility/ReferenceCounting/DefaultImpl.hpp"

using namespace Utility::ReferenceCounting;

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


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;
  }

public:
  void
  lock ()
  {
    lock_i ();
  }

private:
  bool dummy_;
  bool& destroyed_;
};

struct E {};

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

int main ()
{
  try
  {
    // DefaultImpl
    //
    {
      Impl* a (new Impl);

      postcondition (a->refcount_value () == 1);

      a->remove_ref ();
    }

    // ~DefaultImpl
    //
    {
      Impl* a (new Impl);
      a->remove_ref ();
    }

    // add_ref
    //
    {
      Impl* a (new Impl);

      a->add_ref ();

      postcondition (a->refcount_value () == 2);

      a->remove_ref ();
      a->remove_ref ();
    }


    // remove_ref
    //
    {
      bool destroyed (false);
      Impl* a (new Impl (destroyed));

      a->add_ref ();
      a->remove_ref ();

      postcondition (destroyed == false && a->refcount_value () == 1);

      a->remove_ref ();

      postcondition (destroyed == true);
    }


    // refcount_value
    //
    {
      Impl* a (new Impl);

      postcondition (a->refcount_value () == 1);
    }

    // lock_i
    //
    {
      Impl* a (new Impl);
      a->lock ();
    }
  }
  catch (...)
  {
    return -1;
  }
}
//$Id$