SharedHandle.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 <ostream>
  38. class RefCount {
  39. public:
  40. RefCount():totalRefCount(0), strongRefCount(0) {}
  41. RefCount(int32_t totalRefCount, int32_t strongRefCount)
  42. :totalRefCount(totalRefCount), strongRefCount(strongRefCount) {}
  43. int32_t totalRefCount;
  44. int32_t strongRefCount;
  45. };
  46. template<typename T>
  47. class SharedHandle;
  48. template<typename T>
  49. class WeakHandle;
  50. template<typename T>
  51. class SharedHandle {
  52. private:
  53. template<typename T1>
  54. friend std::ostream& operator<<(std::ostream& o, const SharedHandle<T1>& sp);
  55. template<typename T1, typename T2>
  56. friend bool operator==(const SharedHandle<T1>& t1,
  57. const SharedHandle<T2>& t2);
  58. template<typename T1, typename T2>
  59. friend bool operator!=(const SharedHandle<T1>& t1,
  60. const SharedHandle<T2>& t2);
  61. template<typename T1, typename T2>
  62. friend bool operator<(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2);
  63. T* obj;
  64. RefCount* ucount;
  65. void releaseReference() {
  66. if(--ucount->strongRefCount == 0) {
  67. delete obj;
  68. obj = 0;
  69. }
  70. if(--ucount->totalRefCount == 0) {
  71. delete ucount;
  72. ucount = 0;
  73. }
  74. }
  75. public:
  76. SharedHandle():obj(new T()), ucount(new RefCount(1, 1)) {}
  77. SharedHandle(T* obj):obj(obj), ucount(new RefCount(1, 1)) {}
  78. SharedHandle(const SharedHandle& t):obj(t.get()), ucount(t.getRefCount()) {
  79. ++ucount->totalRefCount;
  80. ++ucount->strongRefCount;
  81. }
  82. template<typename S>
  83. SharedHandle(const SharedHandle<S>& t) {
  84. obj = dynamic_cast<T*>(t.get());
  85. if(obj) {
  86. ucount = t.getRefCount();
  87. ++ucount->totalRefCount;
  88. ++ucount->strongRefCount;
  89. } else {
  90. ucount = new RefCount(1, 1);
  91. }
  92. }
  93. ~SharedHandle() {
  94. releaseReference();
  95. }
  96. SharedHandle& operator=(const SharedHandle& t) {
  97. ++t.getRefCount()->totalRefCount;
  98. ++t.getRefCount()->strongRefCount;
  99. releaseReference();
  100. obj = t.get();
  101. ucount = t.getRefCount();
  102. return *this;
  103. }
  104. template<typename S>
  105. SharedHandle& operator=(const SharedHandle<S>& t) {
  106. T* to = dynamic_cast<T*>(t.get());
  107. if(to) {
  108. ++t.getRefCount()->totalRefCount;
  109. ++t.getRefCount()->strongRefCount;
  110. releaseReference();
  111. obj = to;
  112. ucount = t.getRefCount();
  113. } else {
  114. releaseReference();
  115. obj = 0;
  116. ucount = new RefCount(1, 1);
  117. }
  118. return *this;
  119. }
  120. T* operator->() { return obj; }
  121. T* operator->() const { return obj; }
  122. T* get() const {
  123. return obj;
  124. }
  125. RefCount* getRefCount() const {
  126. return ucount;
  127. }
  128. bool isNull() const {
  129. return obj == 0;
  130. }
  131. };
  132. template<typename T>
  133. std::ostream& operator<<(std::ostream& o, const SharedHandle<T>& sp) {
  134. o << *sp.obj;
  135. return o;
  136. }
  137. template<typename T1, typename T2>
  138. bool operator==(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
  139. return *t1.obj == *t2.obj;
  140. }
  141. template<typename T1, typename T2>
  142. bool operator!=(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
  143. return *t1.obj != *t2.obj;
  144. }
  145. template<typename T1, typename T2>
  146. bool operator<(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
  147. return *t1.obj < *t2.obj;
  148. }
  149. template<typename T>
  150. class WeakHandle {
  151. private:
  152. template<typename T1>
  153. friend std::ostream& operator<<(std::ostream& o, const WeakHandle<T1>& sp);
  154. template<typename T1, typename T2>
  155. friend bool operator==(const WeakHandle<T1>& t1,
  156. const WeakHandle<T2>& t2);
  157. template<typename T1, typename T2>
  158. friend bool operator!=(const WeakHandle<T1>& t1,
  159. const WeakHandle<T2>& t2);
  160. template<typename T1, typename T2>
  161. friend bool operator<(const WeakHandle<T1>& t1, const WeakHandle<T2>& t2);
  162. T* obj;
  163. RefCount* ucount;
  164. void releaseReference() {
  165. if(--ucount->totalRefCount == 0) {
  166. delete ucount;
  167. ucount = 0;
  168. }
  169. }
  170. public:
  171. WeakHandle():obj(0), ucount(new RefCount(1, 0)) {}
  172. WeakHandle(T* obj):obj(obj), ucount(new RefCount(1, 1)) {}
  173. WeakHandle(const WeakHandle& t):obj(t.get()), ucount(t.getRefCount()) {
  174. ++ucount->totalRefCount;
  175. }
  176. template<typename S>
  177. WeakHandle(const SharedHandle<S>& t):obj(t.get()), ucount(t.getRefCount()) {
  178. obj = dynamic_cast<T*>(t.get());
  179. if(obj) {
  180. ucount = t.getRefCount();
  181. ++ucount->totalRefCount;
  182. } else {
  183. ucount = new RefCount(1, 0);
  184. }
  185. }
  186. template<typename S>
  187. WeakHandle(const WeakHandle<S>& t) {
  188. obj = dynamic_cast<T*>(t.get());
  189. if(obj) {
  190. ucount = t.getRefCount();
  191. ++ucount->totalRefCount;
  192. } else {
  193. ucount = new RefCount(1, 0);
  194. }
  195. }
  196. ~WeakHandle() {
  197. releaseReference();
  198. }
  199. WeakHandle& operator=(const WeakHandle& t) {
  200. ++t.getRefCount()->totalRefCount;
  201. releaseReference();
  202. obj = t.get();
  203. ucount = t.getRefCount();
  204. return *this;
  205. }
  206. template<typename S>
  207. WeakHandle& operator=(const SharedHandle<S>& t) {
  208. T* to = dynamic_cast<T*>(t.get());
  209. if(to) {
  210. ++t.getRefCount()->totalRefCount;
  211. releaseReference();
  212. obj = to;
  213. ucount = t.getRefCount();
  214. } else {
  215. releaseReference();
  216. obj = 0;
  217. ucount = new RefCount(1, 0);
  218. }
  219. return *this;
  220. }
  221. template<typename S>
  222. WeakHandle& operator=(const WeakHandle<S>& t) {
  223. T* to = dynamic_cast<T*>(t.get());
  224. if(to) {
  225. ++t.getRefCount()->totalRefCount;
  226. releaseReference();
  227. obj = to;
  228. ucount = t.getRefCount();
  229. } else {
  230. releaseReference();
  231. obj = 0;
  232. ucount = new RefCount(1, 0);
  233. }
  234. return *this;
  235. }
  236. T* operator->() { return obj; }
  237. T* operator->() const { return obj; }
  238. T* get() const {
  239. if(isNull()) {
  240. return 0;
  241. } else {
  242. return obj;
  243. }
  244. }
  245. RefCount* getRefCount() const {
  246. return ucount;
  247. }
  248. bool isNull() const {
  249. return ucount->strongRefCount == 0 || obj == 0;
  250. }
  251. };
  252. template<typename T>
  253. std::ostream& operator<<(std::ostream& o, const WeakHandle<T>& sp) {
  254. o << *sp.obj;
  255. return o;
  256. }
  257. template<typename T1, typename T2>
  258. bool operator==(const WeakHandle<T1>& t1, const WeakHandle<T2>& t2) {
  259. return *t1.obj == *t2.obj;
  260. }
  261. template<typename T1, typename T2>
  262. bool operator!=(const WeakHandle<T1>& t1, const WeakHandle<T2>& t2) {
  263. return *t1.obj != *t2.obj;
  264. }
  265. template<typename T1, typename T2>
  266. bool operator<(const WeakHandle<T1>& t1, const WeakHandle<T2>& t2) {
  267. return *t1.obj < *t2.obj;
  268. }
  269. #endif // _D_SHARED_HANDLE_H_