CPPMyth
Library to interoperate with MythTV server
mythsharedptr.h
1 /*
2  * Copyright (C) 2015 Jean-Luc Barriere
3  *
4  * This Program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2, or (at your option)
7  * any later version.
8  *
9  * This Program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; see the file COPYING. If not, write to
16  * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
17  * MA 02110-1301 USA
18  * http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21 
22 #ifndef MYTHSHAREDPTR_H
23 #define MYTHSHAREDPTR_H
24 
25 #include "mythintrinsic.h"
26 
27 #include <cstddef> // for NULL
28 
29 namespace Myth
30 {
31 
32  template<class T>
33  class shared_ptr
34  {
35  public:
36 
37  shared_ptr() : p(NULL), c(NULL) { }
38 
39  explicit shared_ptr(T* s) : p(s), c(NULL)
40  {
41  if (p != NULL)
42  {
43  c = new IntrinsicCounter(1);
44  }
45  }
46 
47  shared_ptr(const shared_ptr& s) : p(s.p), c(s.c)
48  {
49  if (c != NULL)
50  if (c->Increment() < 2)
51  {
52  c = NULL;
53  p = NULL;
54  }
55  }
56 
57  shared_ptr& operator=(const shared_ptr& s)
58  {
59  if (this != &s)
60  {
61  reset();
62  p = s.p;
63  c = s.c;
64  if (c != NULL)
65  if (c->Increment() < 2)
66  {
67  c = NULL;
68  p = NULL;
69  }
70  }
71  return *this;
72  }
73 
74 #if __cplusplus >= 201103L
75  shared_ptr& operator=(shared_ptr&& s)
76  {
77  if (this != &s)
78  swap(s);
79  return *this;
80  }
81 #endif
82 
83  ~shared_ptr()
84  {
85  reset();
86  }
87 
88  void reset()
89  {
90  if (c != NULL)
91  if (c->Decrement() == 0)
92  {
93  delete p;
94  delete c;
95  }
96  c = NULL;
97  p = NULL;
98  }
99 
100  void reset(T* s)
101  {
102  if (p != s)
103  {
104  reset();
105  if (s != NULL)
106  {
107  p = s;
108  c = new IntrinsicCounter(1);
109  }
110  }
111  }
112 
113  T *get() const
114  {
115  return (c != NULL) ? p : NULL;
116  }
117 
118  void swap(shared_ptr<T>& s)
119  {
120  T *tmp_p = p;
121  IntrinsicCounter *tmp_c = c;
122  p = s.p;
123  c = s.c;
124  s.p = tmp_p;
125  s.c = tmp_c;
126  }
127 
128  unsigned use_count() const
129  {
130  return (unsigned) (c != NULL ? c->GetValue() : 0);
131  }
132 
133  T *operator->() const
134  {
135  return get();
136  }
137 
138  T& operator*() const
139  {
140  return *get();
141  }
142 
143  operator bool() const
144  {
145  return p != NULL;
146  }
147 
148  bool operator!() const
149  {
150  return p == NULL;
151  }
152 
153  protected:
154  T *p;
155  IntrinsicCounter *c;
156  };
157 
158 }
159 
160 #endif /* MYTHSHAREDPTR_H */
This is the main namespace that encloses all public classes.
Definition: mythcontrol.h:29