value_ptr_test_4.cc
Go to the documentation of this file.
1 #include "cetlib/value_ptr.h"
2 
3 #include <cassert>
4 
5 class base {
6 private:
7  int i;
8 
9 protected:
10  base(base const& o) : i(o.i)
11  {
12  ++n_alive;
13  ++n_born;
14  }
15 
16 public:
17  static int n_alive;
18  static int n_born;
19 
20  base() : i(0)
21  {
22  ++n_alive;
23  ++n_born;
24  }
25  explicit base(int i) : i(i)
26  {
27  ++n_alive;
28  ++n_born;
29  }
30 
31  virtual base* clone() const = 0;
32 
33  virtual ~base() noexcept { --n_alive; }
34 
35  bool
36  operator==(base const& o) const
37  {
38  return i == o.i;
39  }
40  bool
41  isSame(base const& o) const
42  {
43  return &o == this;
44  }
45 
46 }; // base
47 
48 int base::n_alive = 0;
49 int base::n_born = 0;
50 
51 // ----------------------------------------------------------------------
52 
53 class derived : public base {
54 private:
55  int i;
56 
57 protected:
58  derived(derived const& s) : base(s), i(s.i)
59  {
60  ++n_alive;
61  ++n_born;
62  }
63 
64 public:
65  static int n_alive;
66  static int n_born;
67  static int n_cloned;
68 
69  derived() : i(0)
70  {
71  ++n_alive;
72  ++n_born;
73  }
74  explicit derived(int i) : base(i), i(i)
75  {
76  ++n_alive;
77  ++n_born;
78  }
79 
80  derived*
81  clone() const override
82  {
83  ++n_cloned;
84  return new derived(*this);
85  }
86 
87  ~derived() noexcept { --n_alive; }
88 
89  bool
90  operator==(derived const& o) const
91  {
92  return this->base::operator==(o) && i == o.i;
93  }
94  bool
95  isSame(derived const& o) const
96  {
97  return &o == this;
98  }
99 
100 }; // derived
101 
102 int derived::n_alive = 0;
103 int derived::n_born = 0;
104 int derived::n_cloned = 0;
105 
106 // ----------------------------------------------------------------------
107 
108 int
110 {
111  assert(base::n_alive == 0);
112 
113  {
114  cet::value_ptr<base> a(new derived(10));
115  assert(base::n_alive == 1);
116  assert(derived::n_alive == 1);
117  assert(derived::n_cloned == 0);
119  assert(base::n_alive == 2);
120  assert(derived::n_alive == 2);
121  assert(derived::n_cloned == 1);
122 
123  assert(*a == *b);
124  assert(a->isSame(*b) == false);
125  } // a and b destroyed
126 
127  assert(base::n_alive == 0);
128  assert(derived::n_alive == 0);
129 
130 } // main()
static int n_born
static int n_alive
virtual base * clone() const =0
derived(derived const &s)
base(base const &o)
derived * clone() const override
base(int i)
bool isSame(base const &o) const
bool isSame(derived const &o) const
const double a
static int n_alive
derived(int i)
virtual ~base() noexcept
bool operator==(base const &o) const
static int n_born
static int n_cloned
static bool * b
Definition: config.cpp:1043
bool operator==(derived const &o) const
int main()
static QCString * s
Definition: config.cpp:1042
~derived() noexcept