SharedHandle.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #ifndef _D_SHARED_HANDLE_H_
  23. #define _D_SHARED_HANDLE_H_
  24. #include <ostream>
  25. template<class T>
  26. class SharedHandle {
  27. template<class T1>
  28. friend std::ostream& operator<<(std::ostream& o, const SharedHandle<T1>& sp);
  29. template<class T1, class T2>
  30. friend bool operator==(const SharedHandle<T1>& t1,
  31. const SharedHandle<T2>& t2);
  32. template<class T1, class T2>
  33. friend bool operator!=(const SharedHandle<T1>& t1,
  34. const SharedHandle<T2>& t2);
  35. template<class T1, class T2>
  36. friend bool operator<(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2);
  37. private:
  38. T* obj;
  39. int* ucount;
  40. public:
  41. SharedHandle():obj(new T()), ucount(new int(1)) {}
  42. SharedHandle(T* obj):obj(obj), ucount(new int(1)) {}
  43. template<class S>
  44. SharedHandle(const SharedHandle<S>& t):obj(t.get()), ucount(t.getRefCount()) {
  45. ++*ucount;
  46. }
  47. ~SharedHandle() {
  48. if(--*ucount == 0) {
  49. delete obj;
  50. delete ucount;
  51. }
  52. }
  53. template<class S>
  54. SharedHandle<T>& operator=(const SharedHandle<S>& t) {
  55. ++*t.getRefCount();
  56. if(--*ucount == 0) {
  57. delete obj;
  58. delete ucount;
  59. }
  60. obj = t.get();
  61. ucount = t.getRefCount();
  62. return *this;
  63. }
  64. T* operator->() { return obj; }
  65. T* operator->() const { return obj; }
  66. T* get() const {
  67. return obj;
  68. }
  69. int* getRefCount() const {
  70. return ucount;
  71. }
  72. };
  73. template<class T>
  74. std::ostream& operator<<(std::ostream& o, const SharedHandle<T>& sp) {
  75. o << *sp.obj;
  76. return o;
  77. }
  78. template<class T1, class T2>
  79. bool operator==(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
  80. return *t1.obj == *t2.obj;
  81. }
  82. template<class T1, class T2>
  83. bool operator!=(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
  84. return *t1.obj != *t2.obj;
  85. }
  86. template<class T1, class T2>
  87. bool operator<(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
  88. return *t1.obj < *t2.obj;
  89. }
  90. #endif // _D_SHARED_HANDLE_H_