SharedHandle.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. template<class T>
  39. class SharedHandle {
  40. template<class T1>
  41. friend std::ostream& operator<<(std::ostream& o, const SharedHandle<T1>& sp);
  42. template<class T1, class T2>
  43. friend bool operator==(const SharedHandle<T1>& t1,
  44. const SharedHandle<T2>& t2);
  45. template<class T1, class T2>
  46. friend bool operator!=(const SharedHandle<T1>& t1,
  47. const SharedHandle<T2>& t2);
  48. template<class T1, class T2>
  49. friend bool operator<(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2);
  50. private:
  51. T* obj;
  52. int* ucount;
  53. public:
  54. SharedHandle():obj(new T()), ucount(new int(1)) {}
  55. SharedHandle(T* obj):obj(obj), ucount(new int(1)) {}
  56. SharedHandle(const SharedHandle& t):obj(t.get()), ucount(t.getRefCount()) {
  57. ++*ucount;
  58. }
  59. template<class S>
  60. SharedHandle(const SharedHandle<S>& t):obj(t.get()), ucount(t.getRefCount()) {
  61. ++*ucount;
  62. }
  63. ~SharedHandle() {
  64. if(--*ucount == 0) {
  65. delete obj;
  66. delete ucount;
  67. }
  68. }
  69. SharedHandle& operator=(const SharedHandle& t) {
  70. ++*t.getRefCount();
  71. if(--*ucount == 0) {
  72. delete obj;
  73. delete ucount;
  74. }
  75. obj = t.get();
  76. ucount = t.getRefCount();
  77. return *this;
  78. }
  79. template<class S>
  80. SharedHandle& operator=(const SharedHandle<S>& t) {
  81. ++*t.getRefCount();
  82. if(--*ucount == 0) {
  83. delete obj;
  84. delete ucount;
  85. }
  86. obj = t.get();
  87. ucount = t.getRefCount();
  88. return *this;
  89. }
  90. T* operator->() { return obj; }
  91. T* operator->() const { return obj; }
  92. T* get() const {
  93. return obj;
  94. }
  95. int* getRefCount() const {
  96. return ucount;
  97. }
  98. };
  99. template<class T>
  100. std::ostream& operator<<(std::ostream& o, const SharedHandle<T>& sp) {
  101. o << *sp.obj;
  102. return o;
  103. }
  104. template<class T1, class T2>
  105. bool operator==(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
  106. return *t1.obj == *t2.obj;
  107. }
  108. template<class T1, class T2>
  109. bool operator!=(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
  110. return *t1.obj != *t2.obj;
  111. }
  112. template<class T1, class T2>
  113. bool operator<(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
  114. return *t1.obj < *t2.obj;
  115. }
  116. #endif // _D_SHARED_HANDLE_H_