fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class ChinkoException
  5. {
  6. public:
  7. operator char * () const { return "MuriPo!"; }
  8. };
  9.  
  10. template <class T>
  11. class Chinko
  12. {
  13. private:
  14. T* pointer = NULL;
  15. long *counter = NULL;
  16. public:
  17. Chinko() {}
  18. Chinko(T* t) {
  19. pointer = t;
  20. counter = new long(1);
  21. cout << "Erection!" << endl;
  22. }
  23. Chinko(const Chinko& chinko) {
  24. pointer = chinko.pointer;
  25. counter = chinko.counter;
  26. (*counter)++;
  27. }
  28. ~Chinko() { reset(); }
  29. T& operator * () const {
  30. if (pointer == NULL) throw ChinkoException();
  31. return *(pointer);
  32. }
  33. operator bool () const { return (counter != NULL); }
  34. T* operator -> () const { return pointer; }
  35. T* get() const { return pointer; }
  36. void swap(Chinko& chinko) {
  37. long *cnt = counter;
  38. T* t = pointer;
  39. pointer = chinko.pointer;
  40. counter = chinko.counter;
  41. chinko.counter = cnt;
  42. chinko.pointer = t;
  43. }
  44. void reset() {
  45. if (counter == NULL) return;
  46. (*counter)--;
  47. if (*counter == 0) {
  48. delete counter;
  49. if (pointer != NULL) delete pointer;
  50. cout << "Impotence..." << endl;
  51. }
  52. counter = NULL;
  53. pointer = NULL;
  54. }
  55. void reset(T* t) {
  56. reset();
  57. pointer = t;
  58. counter = new long(1);
  59. }
  60. long use_count() const {
  61. if (counter == NULL) return 0;
  62. return *counter;
  63. }
  64. bool unique() const {
  65. if (counter == NULL) return false;
  66. return (*counter == 1);
  67. }
  68.  
  69. };
  70.  
  71. class Value
  72. {
  73. private:
  74. int value;
  75. public:
  76. Value(int v) : value(v) {}
  77. ~Value() { cout << "Oh! God!" << endl; }
  78. int getValue() const { return value; }
  79. void setValue(int v) { value = v; }
  80. operator int () const { return value; }
  81. };
  82.  
  83. int main() {
  84. cout << "start" << endl;
  85. try {
  86. {
  87. Chinko<int> chinko(new int);
  88. cout << "chinko: " << *chinko << endl;
  89. cout << "unique: " << chinko.unique() << endl;
  90. cout << "count: " << chinko.use_count() << endl;
  91. Chinko<int> bokki = chinko;
  92. cout << "bokki: " << *bokki << endl;
  93. *chinko = 100;
  94. cout << "chinko: " << *chinko << endl;
  95. cout << "bokki: " << *bokki << endl;
  96. cout << "unique: " << chinko.unique() << endl;
  97. cout << "count: " << chinko.use_count() << endl;
  98. Chinko<int> shasei = bokki;
  99. cout << "shasei: " << *shasei << endl;
  100. cout << "unique: " << chinko.unique() << endl;
  101. cout << "count: " << chinko.use_count() << endl;
  102. bokki.reset();
  103. cout << "unique: " << chinko.unique() << endl;
  104. cout << "count: " << chinko.use_count() << endl;
  105. }
  106. {
  107. Chinko<Value> val(new Value(777));
  108. cout << val->getValue() << endl;
  109. val->setValue(999);
  110. cout << *val << endl;
  111. val.reset(new Value(1111));
  112. cout << *val << endl;
  113. }
  114.  
  115. } catch (ChinkoException& ex) {
  116. cout << ex << endl;
  117. }
  118. cout << "finish" << endl;
  119. return 0;
  120. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
start
Erection!
chinko: 0
unique: 1
count: 1
bokki: 0
chinko: 100
bokki: 100
unique: 0
count: 2
shasei: 100
unique: 0
count: 3
unique: 0
count: 2
Impotence...
Erection!
777
999
Oh! God!
Impotence...
1111
Oh! God!
Impotence...
finish