标签:des style blog io ar color os sp java
The idea is to create a Ptr type that acts like a reference in Java.
And A Garbage Collector (MemMgr) type that acts like a garbage collector in Java.
Just a toy. :D
main.cpp
1 #include "MemMgr.h" 2 3 #include <iostream> 4 5 using namespace std; 6 7 class Box 8 { 9 public: 10 void dosomething() { cout << "Box dosomething" << endl; } 11 Box() { cout << "Box cons" << endl; } 12 ~Box() { cout << "Box des" << endl; } 13 }; 14 15 Ptr<Box> global; 16 MemMgr mgr; 17 18 19 Ptr<Box> func() { 20 Ptr<Box> pb = mgr.regist(new Box()); 21 return pb; 22 } 23 24 25 26 int main() 27 { 28 Ptr<Box> p = func(); 29 p->dosomething(); 30 (*p).dosomething(); 31 Ptr<Box> p2 = p; 32 p2->dosomething(); 33 cout << "end of main" << endl; 34 global = p2; 35 return 0; 36 }
MemMgr.h
1 #ifndef MEMMGR_H 2 #define MEMMGR_H 3 4 #include <map> 5 #include <cstdlib> 6 #include <set> 7 8 template <typename TYPE> 9 class Ptr; 10 11 /** 12 MemMgr take the idea of Garbage Collector 13 from the Java language. It‘s just much simple 14 and limited. 15 */ 16 class MemMgr 17 { 18 template <typename T> friend class Ptr; 19 private: 20 typedef unsigned long count; 21 template <typename T> void login(T* ptr_obj); 22 template <typename T> void logout(T* ptr_obj); 23 std::map<void*, count> cmap; 24 public: 25 MemMgr(); 26 /** 27 Client is responsible to ensure obj is in the heap, 28 and make sure only use Ptr objects rather than ordinary 29 pointers when manipulating objects managed by MemMgr. 30 31 If MemMgr is destroyed before any Ptr object managed 32 by it, all Ptr objects managed by that MemMgr are all 33 dangled pointers, this eventually leads to memory leak. 34 So it‘s crucial to make sure MemMgr is not destroyed 35 before ALL Ptr objects managed by it are destroyed. 36 37 Otherwise the behavior of the MemMgr is undefined. 38 */ 39 template <typename T> Ptr<T> regist(T *obj); 40 ~MemMgr(); 41 42 }; 43 44 /** 45 Ptr acts like a reference in java. 46 */ 47 template <typename TYPE> 48 class Ptr { 49 50 friend class MemMgr; 51 52 public: 53 Ptr& operator=(const Ptr<TYPE> ©) 54 { 55 if(copy) { 56 logout(); 57 obj = copy.obj; 58 mgr = copy.mgr; 59 copy.mgr->login(&obj); 60 } // else leaves obj and mgr NULL 61 return *this; 62 } 63 TYPE* operator->() { return obj; } 64 TYPE& operator*() { return *obj; } 65 const TYPE* operator->() const { return obj; } 66 const TYPE& operator*() const { return *obj; } 67 operator bool() const { return ( (obj != NULL) && (mgr != NULL) ); } 68 69 Ptr(): obj(NULL), mgr(NULL) {} 70 Ptr(const Ptr ©): obj(NULL), mgr(NULL) 71 { 72 if(copy) { 73 obj = copy.obj; 74 mgr = copy.mgr; 75 copy.mgr->login(obj); 76 } 77 } 78 ~Ptr() 79 { 80 logout(); 81 } 82 private: 83 Ptr(TYPE *_obj, MemMgr *_mgr): obj(_obj), mgr(_mgr) 84 { 85 mgr->login(obj); 86 } 87 void logout() { 88 if (*this) { 89 mgr->logout(obj); obj = NULL; mgr = NULL; 90 } 91 } 92 TYPE *obj; 93 MemMgr *mgr; 94 }; 95 96 97 template <typename T> Ptr<T> MemMgr::regist(T *obj) 98 { 99 return Ptr<T>(obj, this); 100 } 101 102 template <typename T> 103 void MemMgr::login(T* ptr_obj) 104 { 105 std::map<void*, count>::iterator iter = cmap.find(ptr_obj); 106 if (iter != cmap.end()) { 107 ++(iter->second); 108 } else { 109 cmap.insert(std::pair<void*, count>(ptr_obj, 1)); 110 } 111 } 112 113 template <typename T> 114 void MemMgr::logout(T* ptr_obj) 115 { 116 std::map<void*, count>::iterator iter = cmap.find(ptr_obj); 117 if (iter != cmap.end()) { 118 --(iter->second); 119 if (iter->second == 0) { 120 T *p = (T*)(iter->first); 121 delete p; 122 } 123 } 124 } 125 126 #endif // MEMMGR_H
MemMgr.cpp
1 #include "MemMgr.h" 2 3 #include <iostream> 4 5 using namespace std; 6 7 MemMgr::MemMgr() 8 { 9 cout << "MemMgr cons" << endl; 10 } 11 12 MemMgr::~MemMgr() 13 { 14 cout << "MemMgr des" << endl; 15 }
c++ simple "Garbage Collector"
标签:des style blog io ar color os sp java
原文地址:http://www.cnblogs.com/qrlozte/p/4114386.html