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