SharedHandle.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef _D_SHARED_HANDLE_H_
  36. #define _D_SHARED_HANDLE_H_
  37. #include <iosfwd>
  38. namespace aria2 {
  39. typedef struct StrongRef {} StrongRef;
  40. typedef struct WeakRef {} WeakRef;
  41. class RefCount {
  42. private:
  43. size_t _strongRefCount;
  44. size_t _weakRefCount;
  45. public:
  46. RefCount():_strongRefCount(1), _weakRefCount(1) {}
  47. RefCount(const WeakRef&):_strongRefCount(0), _weakRefCount(1) {}
  48. inline void addRefCount() {
  49. ++_strongRefCount;
  50. ++_weakRefCount;
  51. }
  52. inline void addWeakRefCount() {
  53. ++_weakRefCount;
  54. }
  55. inline void releaseRefCount() {
  56. --_strongRefCount;
  57. --_weakRefCount;
  58. }
  59. inline void releaseWeakRefCount() {
  60. --_weakRefCount;
  61. }
  62. inline size_t getStrongRefCount() { return _strongRefCount; }
  63. inline size_t getWeakRefCount() { return _weakRefCount; }
  64. };
  65. class WeakCount;
  66. class SharedCount {
  67. private:
  68. friend class WeakCount;
  69. RefCount* _refCount;
  70. public:
  71. SharedCount():_refCount(new RefCount()) {}
  72. SharedCount(const SharedCount& s):_refCount(s._refCount)
  73. {
  74. _refCount->addRefCount();
  75. }
  76. ~SharedCount() {
  77. _refCount->releaseRefCount();
  78. if(_refCount->getWeakRefCount() == 0) {
  79. delete _refCount;
  80. }
  81. }
  82. bool reassign(const SharedCount& s) {
  83. s._refCount->addRefCount();
  84. _refCount->releaseRefCount();
  85. if(_refCount->getWeakRefCount() == 0) {
  86. delete _refCount;
  87. _refCount = s._refCount;
  88. return true;
  89. }
  90. size_t thisCount = _refCount->getStrongRefCount();
  91. _refCount = s._refCount;
  92. return thisCount == 0;
  93. }
  94. inline size_t getRefCount() const { return _refCount->getStrongRefCount(); }
  95. };
  96. class WeakCount {
  97. private:
  98. RefCount* _refCount;
  99. public:
  100. WeakCount(const WeakRef& t):_refCount(new RefCount(t)) {}
  101. WeakCount(const StrongRef& t):_refCount(new RefCount()) {}
  102. WeakCount(const WeakCount& w):_refCount(w._refCount)
  103. {
  104. _refCount->addWeakRefCount();
  105. }
  106. WeakCount(const SharedCount& s):_refCount(s._refCount)
  107. {
  108. _refCount->addWeakRefCount();
  109. }
  110. ~WeakCount()
  111. {
  112. _refCount->releaseWeakRefCount();
  113. if(_refCount->getWeakRefCount() == 0) {
  114. delete _refCount;
  115. }
  116. }
  117. bool reassign(const SharedCount& s) {
  118. s._refCount->addWeakRefCount();
  119. _refCount->releaseWeakRefCount();
  120. if(_refCount->getWeakRefCount() == 0) {
  121. delete _refCount;
  122. _refCount = s._refCount;
  123. return true;
  124. }
  125. _refCount = s._refCount;
  126. return false;
  127. }
  128. bool reassign(const WeakCount& s) {
  129. s._refCount->addWeakRefCount();
  130. _refCount->releaseWeakRefCount();
  131. if(_refCount->getWeakRefCount() == 0) {
  132. delete _refCount;
  133. _refCount = s._refCount;
  134. return true;
  135. }
  136. _refCount = s._refCount;
  137. return false;
  138. }
  139. inline size_t getRefCount() const { return _refCount->getStrongRefCount(); }
  140. };
  141. template<typename T>
  142. class SharedHandle;
  143. template<typename T>
  144. class WeakHandle;
  145. template<typename T>
  146. class SharedHandle {
  147. private:
  148. template<typename T1>
  149. friend std::ostream& operator<<(std::ostream& o, const SharedHandle<T1>& sp);
  150. template<typename T1, typename T2>
  151. friend bool operator==(const SharedHandle<T1>& t1,
  152. const SharedHandle<T2>& t2);
  153. template<typename T1, typename T2>
  154. friend bool operator!=(const SharedHandle<T1>& t1,
  155. const SharedHandle<T2>& t2);
  156. template<typename T1, typename T2>
  157. friend bool operator<(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2);
  158. template<typename S> friend class SharedHandle;
  159. template<typename S> friend class WeakHandle;
  160. T* obj;
  161. SharedCount ucount;
  162. public:
  163. SharedHandle():obj(0), ucount() {}
  164. explicit SharedHandle(T* obj):obj(obj), ucount() {}
  165. SharedHandle(const SharedHandle& t):obj(t.obj), ucount(t.ucount) {}
  166. template<typename S>
  167. SharedHandle(const SharedHandle<S>& t):obj(t.obj), ucount(t.ucount) {}
  168. template<typename S>
  169. SharedHandle(const SharedHandle<S>& t, T* p):
  170. obj(p), ucount(t.ucount) {}
  171. ~SharedHandle() {
  172. if(ucount.getRefCount() == 1) {
  173. delete obj;
  174. }
  175. }
  176. SharedHandle& operator=(const SharedHandle& t) {
  177. if(ucount.reassign(t.ucount)) {
  178. delete obj;
  179. }
  180. obj = t.obj;
  181. return *this;
  182. }
  183. template<typename S>
  184. SharedHandle& operator=(const SharedHandle<S>& t) {
  185. if(ucount.reassign(t.ucount)) {
  186. delete obj;
  187. }
  188. obj = t.obj;
  189. return *this;
  190. }
  191. T* operator->() const { return obj; }
  192. T* get() const {
  193. return obj;
  194. }
  195. size_t getRefCount() const {
  196. return ucount.getRefCount();
  197. }
  198. void reset() {
  199. *this = SharedHandle();
  200. }
  201. void reset(T* t) {
  202. *this = SharedHandle(t);
  203. }
  204. bool isNull() const {
  205. return obj == 0;
  206. }
  207. };
  208. template<typename T, typename S>
  209. SharedHandle<T>
  210. dynamic_pointer_cast(const SharedHandle<S>& t) {
  211. if(T* p = dynamic_cast<T*>(t.get())) {
  212. return SharedHandle<T>(t, p);
  213. } else {
  214. return SharedHandle<T>();
  215. }
  216. }
  217. template<typename T>
  218. std::ostream& operator<<(std::ostream& o, const SharedHandle<T>& sp) {
  219. o << *sp.obj;
  220. return o;
  221. }
  222. template<typename T1, typename T2>
  223. bool operator==(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
  224. return *t1.obj == *t2.obj;
  225. }
  226. template<typename T1, typename T2>
  227. bool operator!=(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
  228. return *t1.obj != *t2.obj;
  229. }
  230. template<typename T1, typename T2>
  231. bool operator<(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
  232. return *t1.obj < *t2.obj;
  233. }
  234. template<typename T>
  235. class WeakHandle {
  236. private:
  237. template<typename T1>
  238. friend std::ostream& operator<<(std::ostream& o, const WeakHandle<T1>& sp);
  239. template<typename T1, typename T2>
  240. friend bool operator==(const WeakHandle<T1>& t1,
  241. const WeakHandle<T2>& t2);
  242. template<typename T1, typename T2>
  243. friend bool operator!=(const WeakHandle<T1>& t1,
  244. const WeakHandle<T2>& t2);
  245. template<typename T1, typename T2>
  246. friend bool operator<(const WeakHandle<T1>& t1, const WeakHandle<T2>& t2);
  247. template<typename S> friend class WeakHandle;
  248. T* obj;
  249. WeakCount ucount;
  250. public:
  251. WeakHandle():obj(0), ucount(WeakRef()) {}
  252. explicit WeakHandle(T* obj):obj(obj), ucount(StrongRef()) {}
  253. WeakHandle(const WeakHandle& t):obj(t.obj), ucount(t.ucount) {}
  254. template<typename S>
  255. WeakHandle(const SharedHandle<S>& t):obj(t.obj), ucount(t.ucount) {}
  256. template<typename S>
  257. WeakHandle(const WeakHandle<S>& t, T* p):
  258. obj(p), ucount(t.ucount) {}
  259. ~WeakHandle() {}
  260. WeakHandle& operator=(const WeakHandle& t) {
  261. ucount.reassign(t.ucount);
  262. obj = t.obj;
  263. return *this;
  264. }
  265. template<typename S>
  266. WeakHandle& operator=(const SharedHandle<S>& t) {
  267. ucount.reassign(t.ucount);
  268. obj = t.obj;
  269. return *this;
  270. }
  271. template<typename S>
  272. WeakHandle& operator=(const WeakHandle<S>& t) {
  273. ucount.reassign(t.ucount);
  274. obj = t.obj;
  275. return *this;
  276. }
  277. T* operator->() { return obj; }
  278. T* operator->() const { return obj; }
  279. T* get() const {
  280. if(isNull()) {
  281. return 0;
  282. } else {
  283. return obj;
  284. }
  285. }
  286. size_t getRefCount() const {
  287. return ucount.getRefCount();
  288. }
  289. void reset() {
  290. *this = WeakHandle();
  291. }
  292. bool isNull() const {
  293. return ucount.getRefCount() == 0 || obj == 0;
  294. }
  295. };
  296. template<typename T>
  297. std::ostream& operator<<(std::ostream& o, const WeakHandle<T>& sp) {
  298. o << *sp.obj;
  299. return o;
  300. }
  301. template<typename T1, typename T2>
  302. bool operator==(const WeakHandle<T1>& t1, const WeakHandle<T2>& t2) {
  303. return *t1.obj == *t2.obj;
  304. }
  305. template<typename T1, typename T2>
  306. bool operator!=(const WeakHandle<T1>& t1, const WeakHandle<T2>& t2) {
  307. return *t1.obj != *t2.obj;
  308. }
  309. template<typename T1, typename T2>
  310. bool operator<(const WeakHandle<T1>& t1, const WeakHandle<T2>& t2) {
  311. return *t1.obj < *t2.obj;
  312. }
  313. template<typename T, typename S>
  314. WeakHandle<T>
  315. dynamic_pointer_cast(const WeakHandle<S>& t) {
  316. if(T* p = dynamic_cast<T*>(t.get())) {
  317. return WeakHandle<T>(t, p);
  318. } else {
  319. return WeakHandle<T>();
  320. }
  321. }
  322. } // namespace aria2
  323. #endif // _D_SHARED_HANDLE_H_