CPPMyth
Library to interoperate with MythTV server
Loading...
Searching...
No Matches
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// Compatibility with C++98 remains
26#include <cstddef> // for NULL
27
28namespace Myth
29{
30 namespace OS {
31 class Atomic;
32 }
33
34 class shared_ptr_base
35 {
36 private:
37 OS::Atomic* pc;
38 OS::Atomic* spare;
39 protected:
40 virtual ~shared_ptr_base();
41 shared_ptr_base();
42 shared_ptr_base(const shared_ptr_base& s);
43 shared_ptr_base& operator=(const shared_ptr_base& s);
44 bool clear_counter(); /* returns true if destroyed */
45 void reset_counter(); /* initialize a new count */
46 void swap_counter(shared_ptr_base& s);
47 int get_count() const;
48 bool is_null() const { return (pc == NULL); }
49 };
50
51
52 template<class T>
53 class shared_ptr : private shared_ptr_base
54 {
55 private:
56 T *p;
57 public:
58
59 shared_ptr()
60 : shared_ptr_base()
61 , p(NULL) { }
62
63 explicit shared_ptr(T* s)
64 : shared_ptr_base()
65 , p(s)
66 {
67 if (s != NULL)
68 shared_ptr_base::reset_counter();
69 }
70
71 shared_ptr(const shared_ptr& s)
72 : shared_ptr_base(s)
73 , p(s.p)
74 {
75 if (shared_ptr_base::is_null())
76 p = NULL;
77 }
78
79 shared_ptr& operator=(const shared_ptr& s)
80 {
81 if (this != &s)
82 {
83 reset();
84 p = s.p;
85 shared_ptr_base::operator = (s);
86 if (shared_ptr_base::is_null())
87 p = NULL;
88 }
89 return *this;
90 }
91
92#if __cplusplus >= 201103L
93 shared_ptr& operator=(shared_ptr&& s) noexcept
94 {
95 if (this != &s)
96 swap(s);
97 return *this;
98 }
99#endif
100
101 ~shared_ptr()
102 {
103 reset();
104 }
105
106 void reset()
107 {
108 if (shared_ptr_base::clear_counter())
109 delete p;
110 p = NULL;
111 }
112
113 void reset(T* s)
114 {
115 if (p != s)
116 {
117 reset();
118 p = s;
119 if (s != NULL)
120 shared_ptr_base::reset_counter();
121 }
122 }
123
124 T *get() const
125 {
126 return p;
127 }
128
129 void swap(shared_ptr<T>& s)
130 {
131 T* _p = p;
132 p = s.p;
133 s.p = _p;
134 shared_ptr_base::swap_counter(s);
135 if (shared_ptr_base::is_null())
136 p = NULL;
137 }
138
139 int use_count() const
140 {
141 return shared_ptr_base::get_count();
142 }
143
144 T *operator->() const
145 {
146 return get();
147 }
148
149 T& operator*() const
150 {
151 return *get();
152 }
153
154 operator bool() const
155 {
156 return p != NULL;
157 }
158
159 bool operator!() const
160 {
161 return p == NULL;
162 }
163 };
164
165}
166
167#endif /* MYTHSHAREDPTR_H */
This is the main namespace that encloses all public classes.
Definition mythcontrol.h:30