fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <memory>
  4.  
  5. using namespace std;
  6.  
  7. template<class T, class A = allocator<T> >
  8. class FooPtr
  9. {
  10. typedef T FOO, *LPFOO;
  11. typedef A ALLO;
  12. typedef map<LPFOO ,int> REFMAP, *LPREFMAP;
  13.  
  14. public:
  15. FooPtr() : _ptr(NULL), id(counter()) {
  16. cout << "FooPtr() : " << id << " ... " << _ptr << endl;
  17. }
  18.  
  19. FooPtr(LPFOO ptr) : _ptr(ptr), id(counter()) {
  20. if (_ptr) {
  21. REFMAP& m = refmap();
  22. cout << "FooPtr(FOO) : " << id << " ... " << _ptr
  23. << "[" << m[_ptr] << " -> " << (m[_ptr] + 1) << "]" << endl;
  24. m[_ptr]++;
  25. } else {
  26. cout << "FooPtr(FOO) : " << id << " ... " << _ptr << endl;
  27. }
  28. }
  29.  
  30. FooPtr(const FooPtr& other) : _ptr(other._ptr), id(counter()) {
  31. REFMAP& m = refmap();
  32. if (m[_ptr]) {
  33. cout << "FooPtr(FooPtr) : " << id << " ... " << _ptr
  34. << "[" << m[_ptr] << " -> " << (m[_ptr] + 1) << "]" << endl;
  35. m[_ptr]++;
  36. } else {
  37. cout << "FooPtr(FooPtr) : " << id << " ... " << _ptr
  38. << "[" << m[_ptr] << " -> " << "???" << "]" << endl;
  39. cout << "\t-instance is none!!" << endl;
  40. _ptr = NULL;
  41. }
  42. }
  43.  
  44. ~FooPtr() {
  45. if (_ptr) {
  46. REFMAP& m = refmap();
  47. cout << "~FooPtr() : " << id << " ... " << _ptr
  48. << "[" << m[_ptr] << " -> " << (m[_ptr] - 1) << "]" << endl;
  49. m[_ptr]--;
  50. if (m[_ptr] == 0) {
  51. m.erase(_ptr);
  52. cout << "\t-begin of deallocate-" << endl;
  53. //delete _ptr;
  54. ALLO allo;
  55. allo.destroy(_ptr);
  56. allo.deallocate(_ptr, 0);
  57. cout << "\t-end of deallocate-" << endl;
  58. }
  59. _ptr = NULL;
  60. } else {
  61. cout << "~FooPtr() : " << id << " ... " << _ptr << endl;
  62. }
  63. }
  64.  
  65. FOO& operator * () {
  66. return *_ptr;
  67. }
  68.  
  69. private:
  70. LPFOO _ptr;
  71. int id;
  72.  
  73. static int counter() {
  74. static int c = 0;
  75. return ++c;
  76. }
  77.  
  78. static REFMAP& refmap() {
  79. static REFMAP ref;
  80. return ref;
  81. }
  82.  
  83. };
  84.  
  85. class Hoge
  86. {
  87. public:
  88. Hoge() : id(counter()) { cout << "Hoge: "<< id
  89. << " ... " << this << endl; }
  90. ~Hoge() { cout << "~Hoge: " << id
  91. << " ... " << this << endl; id = -id; }
  92. operator int () {
  93. return id;
  94. }
  95. private:
  96. int id;
  97. static int counter() {
  98. static int c = 0;
  99. return ++c;
  100. }
  101. };
  102.  
  103. FooPtr<Hoge> fuga(FooPtr<Hoge> h1) {
  104. cout << "fuga: " << *h1 << endl;
  105. FooPtr<Hoge> h2(new Hoge);
  106. return h2;
  107. }
  108.  
  109. FooPtr<Hoge>& piyo(FooPtr<Hoge>& h1) {
  110. cout << "piyo: " << *h1 << endl;
  111. FooPtr<Hoge> h2(new Hoge);
  112. return h2;
  113. }
  114.  
  115. int main() {
  116. // your code goes here
  117. Hoge hoge1;
  118. Hoge *hoge2 = new Hoge;
  119. FooPtr<Hoge> foo(new Hoge);
  120. FooPtr<Hoge> bar(foo);
  121. FooPtr<Hoge> zero;
  122.  
  123. cout << "----" << endl;
  124.  
  125. FooPtr<Hoge> baz = fuga(new Hoge);
  126.  
  127. cout << "----" << endl;
  128.  
  129. FooPtr<Hoge> p(new Hoge);
  130. FooPtr<Hoge> foobar = piyo(p);
  131.  
  132. cout << "----" << endl;
  133.  
  134. cout << "hoge1 = " << hoge1 << endl;
  135. cout << "hoge2 = " << *hoge2 << endl;
  136. cout << "foo = " << *foo << endl;
  137. cout << "bar = " << *bar << endl;
  138. cout << "baz = " << *baz << endl;
  139. // cout << "foobar = " << *foobar << endl; // Wrong Access!
  140.  
  141. cout << "****" << endl;
  142.  
  143. delete hoge2;
  144.  
  145. cout << "****" << endl;
  146.  
  147. return 0;
  148. }
Success #stdin #stdout 0s 3440KB
stdin
Standard input is empty
stdout
Hoge: 1 ... 0xbff1baa4
Hoge: 2 ... 0x9273008
Hoge: 3 ... 0x9273018
FooPtr(FOO) : 1 ... 0x9273018[0 -> 1]
FooPtr(FooPtr) : 2 ... 0x9273018[1 -> 2]
FooPtr() : 3 ... 0
----
Hoge: 4 ... 0x9273048
FooPtr(FOO) : 4 ... 0x9273048[0 -> 1]
fuga: 4
Hoge: 5 ... 0x9273078
FooPtr(FOO) : 5 ... 0x9273078[0 -> 1]
~FooPtr() : 4 ... 0x9273048[1 -> 0]
	-begin of deallocate-
~Hoge: 4 ... 0x9273048
	-end of deallocate-
----
Hoge: 6 ... 0x9273048
FooPtr(FOO) : 6 ... 0x9273048[0 -> 1]
piyo: 6
Hoge: 7 ... 0x92730a8
FooPtr(FOO) : 7 ... 0x92730a8[0 -> 1]
~FooPtr() : 7 ... 0x92730a8[1 -> 0]
	-begin of deallocate-
~Hoge: 7 ... 0x92730a8
	-end of deallocate-
FooPtr(FooPtr) : 8 ... 0x92730a8[0 -> ???]
	-instance is none!!
----
hoge1 = 1
hoge2 = 2
foo = 3
bar = 3
baz = 5
****
~Hoge: 2 ... 0x9273008
****
~FooPtr() : 8 ... 0
~FooPtr() : 6 ... 0x9273048[1 -> 0]
	-begin of deallocate-
~Hoge: 6 ... 0x9273048
	-end of deallocate-
~FooPtr() : 5 ... 0x9273078[1 -> 0]
	-begin of deallocate-
~Hoge: 5 ... 0x9273078
	-end of deallocate-
~FooPtr() : 3 ... 0
~FooPtr() : 2 ... 0x9273018[2 -> 1]
~FooPtr() : 1 ... 0x9273018[1 -> 0]
	-begin of deallocate-
~Hoge: 3 ... 0x9273018
	-end of deallocate-
~Hoge: 1 ... 0xbff1baa4