尝试写一个智能指针把
1
#include
"
SharedPtr.h
"
2 #include " gtest/gtest.h "
3
4 using namespace std;
5
6 TEST(SharedPtr_test, TestConstructer)
7 {
8 SharedPtr<int> s1;
9 EXPECT_EQ(NULL, s1.get());
10 EXPECT_EQ(0, s1.use_count());
11
12 int *pi = new int;
13 SharedPtr<int> s2(pi);
14 EXPECT_EQ(pi, s2.get());
15 EXPECT_EQ(1, s2.use_count());
16} ;
17
18 TEST(SharedPtr_test, TestAssign)
19 {
20 SharedPtr<int> s1;
21
22 int *pi = new int;
23 SharedPtr<int> s2(pi);
24
25 s1 = s2;
26 EXPECT_EQ(s1.get(), s2.get());
27 EXPECT_EQ(s1.use_count(), s2.use_count());
28
29 EXPECT_EQ(pi, s1.get());
30 EXPECT_EQ(2, s1.use_count());
31
32 int *p3 = new int;
33 SharedPtr<int> s3(p3);
34 s1 = s3;
35 EXPECT_EQ(s1.get(), s3.get());
36 EXPECT_EQ(s1.use_count(), s3.use_count());
37
38 EXPECT_EQ(pi, s2.get());
39 EXPECT_EQ(1, s2.use_count());
40
41 s2 = s3;
42 EXPECT_EQ(s1.get(), s2.get());
43 EXPECT_EQ(3, s2.use_count());
44 EXPECT_EQ(s1.use_count(), s2.use_count());
45
46
47 SharedPtr<int> s4;
48 SharedPtr<int> s5;
49 s4 = s5;
50 EXPECT_EQ(s4.get(), s5.get());
51 EXPECT_EQ(s4.use_count(), s5.use_count());
52
53 SharedPtr<int> s6;
54 SharedPtr<int> s7(s6);
55 EXPECT_EQ(s6.get(), s7.get());
56 EXPECT_EQ(s6.use_count(), s7.use_count());
57
58 SharedPtr<int> s8;
59 SharedPtr<int> s9;
60 s8 = s9;
61 int *p10 = new int;
62 *p10 = 10;
63 SharedPtr<int> s10(p10);
64 s8 = s10;
65 EXPECT_NE(s8.get(), s9.get());
66 EXPECT_NE(s8.use_count(), s9.use_count());
67 (*s10)--;
68 EXPECT_EQ(9, *(s8.get()));
69
70} ;
71
72 // TEST(SharedPtr_test, TestNULL)
73 // {
74 // SharedPtr<int> s1;
75 //
76 // try{
77 // s1.operator *();
78 // }
79 // catch(exception e)
80 // {
81 // EXPECT_EQ(e.what(), "There is a null pointer");
82 // }
83 //
84 //
85 // try{
86 // s1.operator ->();
87 // }
88 // catch(exception e)
89 // {
90 // EXPECT_EQ(e.what(), "There is a null pointer");
91 // }
92 // }
93
94 TEST(SharedPtr_test, TestOperatorEqual)
95 {
96 SharedPtr<int> s1;
97 SharedPtr<int> s2;
98 SharedPtr<double> s3;
99 SharedPtr<string> s4;
100
101 int *p1 = new int;
102 SharedPtr<int> s5(p1);
103 SharedPtr<int> s6(s5);
104 SharedPtr<int> s7 = s6;
105 SharedPtr<int> s8;
106 s8 = s7;
107 int *p2 = new int;
108 SharedPtr<int> s9(p2);
109
110
111 EXPECT_EQ(true, s1 == s2);
112 EXPECT_EQ(false, s1 == s3);
113 EXPECT_EQ(false, s1 == s4);
114
115
116 EXPECT_EQ(true, s5 == s6);
117 EXPECT_EQ(false, s5 == s2);
118 EXPECT_EQ(true, s5 == s7);
119 EXPECT_EQ(true, s5 == s8);
120 EXPECT_EQ(false, s5 == s9);
121}
122
123 TEST(SharedPtr_test, TestOperatorNotEqual)
124 {
125 SharedPtr<int> s1;
126 SharedPtr<int> s2;
127 SharedPtr<double> s3;
128 SharedPtr<string> s4;
129
130 int *p1 = new int;
131 SharedPtr<int> s5(p1);
132 SharedPtr<int> s6(s5);
133 SharedPtr<int> s7 = s6;
134 SharedPtr<int> s8;
135 s8 = s7;
136 int *p2 = new int;
137 SharedPtr<int> s9(p2);
138
139
140 EXPECT_NE(true, s1 != s2);
141 EXPECT_NE(false, s1 != s3);
142 EXPECT_NE(false, s1 != s4);
143
144 EXPECT_NE(true, s5 != s6);
145 EXPECT_NE(false, s5 != s2);
146 EXPECT_NE(true, s5 != s7);
147 EXPECT_NE(true, s5 != s8);
148 EXPECT_NE(false, s5 != s9);
149}
150
151 TEST(SharedPtr_test, TestOther)
152 {
153 int *pi = new int;
154 SharedPtr<int> s1(pi);
155 EXPECT_EQ(pi, s1.get());
156 EXPECT_EQ(1, s1.use_count());
157
158 *s1 = 10;
159 EXPECT_EQ(10, (*(s1.get())));
160 EXPECT_EQ(10, (*s1));
161
162 UserCount *puc = new UserCount(19);
163 SharedPtr<UserCount> sc(puc);
164 EXPECT_EQ(puc, &(*sc));
165 EXPECT_EQ(puc, sc.get());
166 EXPECT_EQ(19, sc->GetCount());
167} ;
168
169 int main( int argc, char ** argv)
170 {
171 testing::InitGoogleTest(&argc, argv);
172
173 return RUN_ALL_TESTS();
174 return 0;
175}
2 #include " gtest/gtest.h "
3
4 using namespace std;
5
6 TEST(SharedPtr_test, TestConstructer)
7 {
8 SharedPtr<int> s1;
9 EXPECT_EQ(NULL, s1.get());
10 EXPECT_EQ(0, s1.use_count());
11
12 int *pi = new int;
13 SharedPtr<int> s2(pi);
14 EXPECT_EQ(pi, s2.get());
15 EXPECT_EQ(1, s2.use_count());
16} ;
17
18 TEST(SharedPtr_test, TestAssign)
19 {
20 SharedPtr<int> s1;
21
22 int *pi = new int;
23 SharedPtr<int> s2(pi);
24
25 s1 = s2;
26 EXPECT_EQ(s1.get(), s2.get());
27 EXPECT_EQ(s1.use_count(), s2.use_count());
28
29 EXPECT_EQ(pi, s1.get());
30 EXPECT_EQ(2, s1.use_count());
31
32 int *p3 = new int;
33 SharedPtr<int> s3(p3);
34 s1 = s3;
35 EXPECT_EQ(s1.get(), s3.get());
36 EXPECT_EQ(s1.use_count(), s3.use_count());
37
38 EXPECT_EQ(pi, s2.get());
39 EXPECT_EQ(1, s2.use_count());
40
41 s2 = s3;
42 EXPECT_EQ(s1.get(), s2.get());
43 EXPECT_EQ(3, s2.use_count());
44 EXPECT_EQ(s1.use_count(), s2.use_count());
45
46
47 SharedPtr<int> s4;
48 SharedPtr<int> s5;
49 s4 = s5;
50 EXPECT_EQ(s4.get(), s5.get());
51 EXPECT_EQ(s4.use_count(), s5.use_count());
52
53 SharedPtr<int> s6;
54 SharedPtr<int> s7(s6);
55 EXPECT_EQ(s6.get(), s7.get());
56 EXPECT_EQ(s6.use_count(), s7.use_count());
57
58 SharedPtr<int> s8;
59 SharedPtr<int> s9;
60 s8 = s9;
61 int *p10 = new int;
62 *p10 = 10;
63 SharedPtr<int> s10(p10);
64 s8 = s10;
65 EXPECT_NE(s8.get(), s9.get());
66 EXPECT_NE(s8.use_count(), s9.use_count());
67 (*s10)--;
68 EXPECT_EQ(9, *(s8.get()));
69
70} ;
71
72 // TEST(SharedPtr_test, TestNULL)
73 // {
74 // SharedPtr<int> s1;
75 //
76 // try{
77 // s1.operator *();
78 // }
79 // catch(exception e)
80 // {
81 // EXPECT_EQ(e.what(), "There is a null pointer");
82 // }
83 //
84 //
85 // try{
86 // s1.operator ->();
87 // }
88 // catch(exception e)
89 // {
90 // EXPECT_EQ(e.what(), "There is a null pointer");
91 // }
92 // }
93
94 TEST(SharedPtr_test, TestOperatorEqual)
95 {
96 SharedPtr<int> s1;
97 SharedPtr<int> s2;
98 SharedPtr<double> s3;
99 SharedPtr<string> s4;
100
101 int *p1 = new int;
102 SharedPtr<int> s5(p1);
103 SharedPtr<int> s6(s5);
104 SharedPtr<int> s7 = s6;
105 SharedPtr<int> s8;
106 s8 = s7;
107 int *p2 = new int;
108 SharedPtr<int> s9(p2);
109
110
111 EXPECT_EQ(true, s1 == s2);
112 EXPECT_EQ(false, s1 == s3);
113 EXPECT_EQ(false, s1 == s4);
114
115
116 EXPECT_EQ(true, s5 == s6);
117 EXPECT_EQ(false, s5 == s2);
118 EXPECT_EQ(true, s5 == s7);
119 EXPECT_EQ(true, s5 == s8);
120 EXPECT_EQ(false, s5 == s9);
121}
122
123 TEST(SharedPtr_test, TestOperatorNotEqual)
124 {
125 SharedPtr<int> s1;
126 SharedPtr<int> s2;
127 SharedPtr<double> s3;
128 SharedPtr<string> s4;
129
130 int *p1 = new int;
131 SharedPtr<int> s5(p1);
132 SharedPtr<int> s6(s5);
133 SharedPtr<int> s7 = s6;
134 SharedPtr<int> s8;
135 s8 = s7;
136 int *p2 = new int;
137 SharedPtr<int> s9(p2);
138
139
140 EXPECT_NE(true, s1 != s2);
141 EXPECT_NE(false, s1 != s3);
142 EXPECT_NE(false, s1 != s4);
143
144 EXPECT_NE(true, s5 != s6);
145 EXPECT_NE(false, s5 != s2);
146 EXPECT_NE(true, s5 != s7);
147 EXPECT_NE(true, s5 != s8);
148 EXPECT_NE(false, s5 != s9);
149}
150
151 TEST(SharedPtr_test, TestOther)
152 {
153 int *pi = new int;
154 SharedPtr<int> s1(pi);
155 EXPECT_EQ(pi, s1.get());
156 EXPECT_EQ(1, s1.use_count());
157
158 *s1 = 10;
159 EXPECT_EQ(10, (*(s1.get())));
160 EXPECT_EQ(10, (*s1));
161
162 UserCount *puc = new UserCount(19);
163 SharedPtr<UserCount> sc(puc);
164 EXPECT_EQ(puc, &(*sc));
165 EXPECT_EQ(puc, sc.get());
166 EXPECT_EQ(19, sc->GetCount());
167} ;
168
169 int main( int argc, char ** argv)
170 {
171 testing::InitGoogleTest(&argc, argv);
172
173 return RUN_ALL_TESTS();
174 return 0;
175}
上面是测试
下面是代码
1
#ifndef __SSA_SHARED_PTR__
2 #define __SSA_SHARED_PTR__
3
4 #include < stdexcept >
5
6 using namespace std;
7
8 class NullPointerException: exception {
9public:
10 NullPointerException():exception("There is a null pointer"){};
11} ;
12
13 template < typename T >
14 class UserObject {
15 T* pObj;
16
17public:
18 UserObject(T* v):pObj(v){};
19 UserObject(const UserObject<T>& rl){
20 this->pObj = rl.pObj;
21 };
22
23 UserObject &operator=(const UserObject<T>& rl){
24 this->pObj = rl.pObj;
25 };
26
27 ~UserObject(){delete pObj;};
28
29 T & operator*() const {
30 if(pObj == NULL) {
31 throw NullPointerException();
32 }
33 return *pObj;
34 }
35
36 T * operator->() const {
37 if(pObj == NULL) {
38 throw NullPointerException();
39 }
40 return pObj;
41 }
42
43 T * get() const {
44 return pObj;
45 }
46} ;
47
48 class UserCount {
49public:
50
51 UserCount():_userCount(1){};
52 UserCount(size_t count):_userCount(count){};
53 size_t AddRef() {
54 _userCount++;
55 return _userCount;
56 }
57
58 size_t Release() {
59 _userCount--;
60 return _userCount;
61 }
62
63 size_t GetCount() {
64 return _userCount;
65 }
66
67private:
68 size_t _userCount;
69} ;
70
71 template < typename T >
72 class SharedPtr {
73public:
74 SharedPtr(){
75 pObj = new UserObject<T>(NULL);
76 pn = new UserCount(0);
77 };
78
79 SharedPtr(T* v){
80 pObj = new UserObject<T>(v);
81 pn = new UserCount(1);
82 };
83
84 SharedPtr(const SharedPtr<T> &rl){
85 this->pObj = rl.pObj;
86 rl.pn->AddRef();
87 this->pn = rl.pn;
88 }
89
90 ~SharedPtr(){
91 if(this->pn == NULL) {
92 return;
93 }
94
95 if(this->pn->Release() == 0) {
96 delete this->pObj;
97 delete this->pn;
98 }
99 };
100
101 T & operator*() const {
102 return pObj->operator *();
103 }
104
105 T * operator->() const {
106 return pObj->operator ->();
107 }
108
109 T * get() const {
110 return pObj->get();
111 }
112
113 std::size_t use_count() const {
114 if(pn==NULL) return 0;
115 return pn->GetCount();
116 }
117
118 SharedPtr<T>& operator=(SharedPtr<T> &rv){
119
120 if(this->pn->Release() == 0 )
121 {
122 delete this->pObj;
123 }
124
125 this->pObj = rv.pObj;
126 rv.pn->AddRef();
127 this->pn = rv.pn;
128
129 return *this;
130 }
131
132 template<typename T>
133 friend bool operator==(SharedPtr<T> &lv, SharedPtr<T> &rv);
134 template<typename T>
135 friend bool operator==(const SharedPtr<T> &lv, const SharedPtr<T> &rv);
136 template<typename T, typename U>
137 friend bool operator==(SharedPtr<T> &lv, SharedPtr<U> &rv);
138 template<typename T, typename U>
139 friend bool operator==(const SharedPtr<T> &lv, const SharedPtr<U> &rv);
140
141 template<typename T>
142 friend bool operator!=(SharedPtr<T> &lv, SharedPtr<T> &rv);
143 template<typename T>
144 friend bool operator!=(const SharedPtr<T> &lv, const SharedPtr<T> &rv);
145 template<typename T, typename U>
146 friend bool operator!=(SharedPtr<T> &lv, SharedPtr<U> &rv);
147 template<typename T, typename U>
148 friend bool operator!=(const SharedPtr<T> &lv, const SharedPtr<U> &rv);
149
150private:
151 UserObject<T> * pObj;
152 UserCount* pn;
153} ;
154
155 template < typename T >
156 bool operator == (SharedPtr < T > & lv, SharedPtr < T > & rv)
157 {
158 return lv.get() == rv.get();
159}
160
161 template < typename T >
162 bool operator == ( const SharedPtr < T > & lv, const SharedPtr < T > & rv)
163 {
164 return lv.get() == rv.get();
165}
166
167 template < typename T, typename U >
168 bool operator == (SharedPtr < T > & lv, SharedPtr < U > & rv)
169 {
170 return false;
171}
172
173 template < typename T, typename U >
174 bool operator == ( const SharedPtr < T > & lv, const SharedPtr < U > & rv)
175 {
176 return false;
177}
178
179 template < typename T >
180 bool operator != (SharedPtr < T > & lv, SharedPtr < T > & rv)
181 {
182 return lv.get() != rv.get();
183}
184
185 template < typename T >
186 bool operator != ( const SharedPtr < T > & lv, const SharedPtr < T > & rv)
187 {
188 return lv.get() != rv.get();
189}
190
191 template < typename T, typename U >
192 bool operator != (SharedPtr < T > & lv, SharedPtr < U > & rv)
193 {
194 return true;
195}
196
197 template < typename T, typename U >
198 bool operator != ( const SharedPtr < T > & lv, const SharedPtr < U > & rv)
199 {
200 return true;
201}
202
203 #endif // __SSA_SHARED_PTR__
2 #define __SSA_SHARED_PTR__
3
4 #include < stdexcept >
5
6 using namespace std;
7
8 class NullPointerException: exception {
9public:
10 NullPointerException():exception("There is a null pointer"){};
11} ;
12
13 template < typename T >
14 class UserObject {
15 T* pObj;
16
17public:
18 UserObject(T* v):pObj(v){};
19 UserObject(const UserObject<T>& rl){
20 this->pObj = rl.pObj;
21 };
22
23 UserObject &operator=(const UserObject<T>& rl){
24 this->pObj = rl.pObj;
25 };
26
27 ~UserObject(){delete pObj;};
28
29 T & operator*() const {
30 if(pObj == NULL) {
31 throw NullPointerException();
32 }
33 return *pObj;
34 }
35
36 T * operator->() const {
37 if(pObj == NULL) {
38 throw NullPointerException();
39 }
40 return pObj;
41 }
42
43 T * get() const {
44 return pObj;
45 }
46} ;
47
48 class UserCount {
49public:
50
51 UserCount():_userCount(1){};
52 UserCount(size_t count):_userCount(count){};
53 size_t AddRef() {
54 _userCount++;
55 return _userCount;
56 }
57
58 size_t Release() {
59 _userCount--;
60 return _userCount;
61 }
62
63 size_t GetCount() {
64 return _userCount;
65 }
66
67private:
68 size_t _userCount;
69} ;
70
71 template < typename T >
72 class SharedPtr {
73public:
74 SharedPtr(){
75 pObj = new UserObject<T>(NULL);
76 pn = new UserCount(0);
77 };
78
79 SharedPtr(T* v){
80 pObj = new UserObject<T>(v);
81 pn = new UserCount(1);
82 };
83
84 SharedPtr(const SharedPtr<T> &rl){
85 this->pObj = rl.pObj;
86 rl.pn->AddRef();
87 this->pn = rl.pn;
88 }
89
90 ~SharedPtr(){
91 if(this->pn == NULL) {
92 return;
93 }
94
95 if(this->pn->Release() == 0) {
96 delete this->pObj;
97 delete this->pn;
98 }
99 };
100
101 T & operator*() const {
102 return pObj->operator *();
103 }
104
105 T * operator->() const {
106 return pObj->operator ->();
107 }
108
109 T * get() const {
110 return pObj->get();
111 }
112
113 std::size_t use_count() const {
114 if(pn==NULL) return 0;
115 return pn->GetCount();
116 }
117
118 SharedPtr<T>& operator=(SharedPtr<T> &rv){
119
120 if(this->pn->Release() == 0 )
121 {
122 delete this->pObj;
123 }
124
125 this->pObj = rv.pObj;
126 rv.pn->AddRef();
127 this->pn = rv.pn;
128
129 return *this;
130 }
131
132 template<typename T>
133 friend bool operator==(SharedPtr<T> &lv, SharedPtr<T> &rv);
134 template<typename T>
135 friend bool operator==(const SharedPtr<T> &lv, const SharedPtr<T> &rv);
136 template<typename T, typename U>
137 friend bool operator==(SharedPtr<T> &lv, SharedPtr<U> &rv);
138 template<typename T, typename U>
139 friend bool operator==(const SharedPtr<T> &lv, const SharedPtr<U> &rv);
140
141 template<typename T>
142 friend bool operator!=(SharedPtr<T> &lv, SharedPtr<T> &rv);
143 template<typename T>
144 friend bool operator!=(const SharedPtr<T> &lv, const SharedPtr<T> &rv);
145 template<typename T, typename U>
146 friend bool operator!=(SharedPtr<T> &lv, SharedPtr<U> &rv);
147 template<typename T, typename U>
148 friend bool operator!=(const SharedPtr<T> &lv, const SharedPtr<U> &rv);
149
150private:
151 UserObject<T> * pObj;
152 UserCount* pn;
153} ;
154
155 template < typename T >
156 bool operator == (SharedPtr < T > & lv, SharedPtr < T > & rv)
157 {
158 return lv.get() == rv.get();
159}
160
161 template < typename T >
162 bool operator == ( const SharedPtr < T > & lv, const SharedPtr < T > & rv)
163 {
164 return lv.get() == rv.get();
165}
166
167 template < typename T, typename U >
168 bool operator == (SharedPtr < T > & lv, SharedPtr < U > & rv)
169 {
170 return false;
171}
172
173 template < typename T, typename U >
174 bool operator == ( const SharedPtr < T > & lv, const SharedPtr < U > & rv)
175 {
176 return false;
177}
178
179 template < typename T >
180 bool operator != (SharedPtr < T > & lv, SharedPtr < T > & rv)
181 {
182 return lv.get() != rv.get();
183}
184
185 template < typename T >
186 bool operator != ( const SharedPtr < T > & lv, const SharedPtr < T > & rv)
187 {
188 return lv.get() != rv.get();
189}
190
191 template < typename T, typename U >
192 bool operator != (SharedPtr < T > & lv, SharedPtr < U > & rv)
193 {
194 return true;
195}
196
197 template < typename T, typename U >
198 bool operator != ( const SharedPtr < T > & lv, const SharedPtr < U > & rv)
199 {
200 return true;
201}
202
203 #endif // __SSA_SHARED_PTR__
希望大家能帮我提一些意见 谢谢