SharedHandle.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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, typename S>
  218. SharedHandle<T>
  219. static_pointer_cast(const SharedHandle<S>& t) {
  220. return SharedHandle<T>(t, static_cast<T*>(t.get()));
  221. }
  222. template<typename T>
  223. std::ostream& operator<<(std::ostream& o, const SharedHandle<T>& sp) {
  224. o << *sp.obj_;
  225. return o;
  226. }
  227. template<typename T1, typename T2>
  228. bool operator==(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
  229. return *t1.obj_ == *t2.obj_;
  230. }
  231. template<typename T1, typename T2>
  232. bool operator!=(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
  233. return *t1.obj_ != *t2.obj_;
  234. }
  235. template<typename T1, typename T2>
  236. bool operator<(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
  237. return *t1.obj_ < *t2.obj_;
  238. }
  239. template<typename T>
  240. class WeakHandle {
  241. private:
  242. template<typename T1>
  243. friend std::ostream& operator<<(std::ostream& o, const WeakHandle<T1>& sp);
  244. template<typename T1, typename T2>
  245. friend bool operator==(const WeakHandle<T1>& t1,
  246. const WeakHandle<T2>& t2);
  247. template<typename T1, typename T2>
  248. friend bool operator!=(const WeakHandle<T1>& t1,
  249. const WeakHandle<T2>& t2);
  250. template<typename T1, typename T2>
  251. friend bool operator<(const WeakHandle<T1>& t1, const WeakHandle<T2>& t2);
  252. template<typename S> friend class WeakHandle;
  253. T* obj_;
  254. WeakCount ucount_;
  255. public:
  256. WeakHandle():obj_(0), ucount_(WeakRef()) {}
  257. explicit WeakHandle(T* obj):obj_(obj), ucount_(StrongRef()) {}
  258. WeakHandle(const WeakHandle& t):obj_(t.obj_), ucount_(t.ucount_) {}
  259. template<typename S>
  260. WeakHandle(const SharedHandle<S>& t):obj_(t.obj_), ucount_(t.ucount_) {}
  261. template<typename S>
  262. WeakHandle(const WeakHandle<S>& t, T* p):
  263. obj_(p), ucount_(t.ucount_) {}
  264. ~WeakHandle() {}
  265. WeakHandle& operator=(const WeakHandle& t) {
  266. ucount_.reassign(t.ucount_);
  267. obj_ = t.obj_;
  268. return *this;
  269. }
  270. template<typename S>
  271. WeakHandle& operator=(const SharedHandle<S>& t) {
  272. ucount_.reassign(t.ucount_);
  273. obj_ = t.obj_;
  274. return *this;
  275. }
  276. template<typename S>
  277. WeakHandle& operator=(const WeakHandle<S>& t) {
  278. ucount_.reassign(t.ucount_);
  279. obj_ = t.obj_;
  280. return *this;
  281. }
  282. T* operator->() { return obj_; }
  283. T* operator->() const { return obj_; }
  284. T* get() const {
  285. if(isNull()) {
  286. return 0;
  287. } else {
  288. return obj_;
  289. }
  290. }
  291. size_t getRefCount() const {
  292. return ucount_.getRefCount();
  293. }
  294. void reset() {
  295. *this = WeakHandle();
  296. }
  297. bool isNull() const {
  298. return ucount_.getRefCount() == 0 || obj_ == 0;
  299. }
  300. };
  301. template<typename T>
  302. std::ostream& operator<<(std::ostream& o, const WeakHandle<T>& sp) {
  303. o << *sp.obj_;
  304. return o;
  305. }
  306. template<typename T1, typename T2>
  307. bool operator==(const WeakHandle<T1>& t1, const WeakHandle<T2>& t2) {
  308. return *t1.obj_ == *t2.obj_;
  309. }
  310. template<typename T1, typename T2>
  311. bool operator!=(const WeakHandle<T1>& t1, const WeakHandle<T2>& t2) {
  312. return *t1.obj_ != *t2.obj_;
  313. }
  314. template<typename T1, typename T2>
  315. bool operator<(const WeakHandle<T1>& t1, const WeakHandle<T2>& t2) {
  316. return *t1.obj_ < *t2.obj_;
  317. }
  318. template<typename T, typename S>
  319. WeakHandle<T>
  320. dynamic_pointer_cast(const WeakHandle<S>& t) {
  321. if(T* p = dynamic_cast<T*>(t.get())) {
  322. return WeakHandle<T>(t, p);
  323. } else {
  324. return WeakHandle<T>();
  325. }
  326. }
  327. } // namespace aria2
  328. #endif // _D_SHARED_HANDLE_H_