-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle.h
174 lines (167 loc) · 4.48 KB
/
handle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#ifndef _HANDLE_H_
#define _HENDLE_H_
#include <iostream>
#include "base.h"
#include "rgb.h"
#include "point.h"
#include "circle.h"
#include "rect.h"
#include "tri.h"
// #include "desktop.h"
using namespace std;
class Handle{
// friend class Desktop;
// friend Desktop& operator<< (Desktop& o, const Handle& h);
public:
// Handle(){}
Handle(){}; //why要new
Handle(const Handle& h): p(h.p) {
++(p->count);
} //為甚麼是傳Handle& 因為我不是要他的值,是她這個記憶體裡面的東西
Handle(Point a,int _radius,RGB _rgb): p(new Circle(a,_radius,_rgb)){};
Handle(Point a,int width,int height,RGB _rgb): p(new Rect(a,width,height,_rgb)){};
Handle(Point a,int left,int right,int height,RGB _rgb): p(new Tri(a,left,right,height,_rgb)){};
~Handle() {
unhook();
}
void draw(Bitmap& map,int method) const {
// this->p->draw(map,method);
// map.setPixel(p->ref,p->color,method);
this->p->draw(map,method);
}
Handle operator=(Handle h){
if(p == NULL){
p = h.p;
++(p->count);
}
else if (p != h.p) {
unhook();
p = h.p;
++(p->count);
}
// this->p = NULL;
// this->p = h.p;
return *this;
// this->p = NULL;
// Handle newH;
// newH.p=h.p->copy();
// *this = newH;
// return *this;
}
Handle operator+(Point a){
Handle newH;
newH.p=this->p->copy();
newH.p->ref += a;
return newH;
}
Handle operator+=(Point a){
if (p->count > 1) {
Base* other = p->copy();
unhook();
p = other;
}
this->p->ref += a;
return *this;
}
Handle operator*(double scale){
Handle h;
h.p = this->p->copy();
h.p->operator*=(scale);
return h;
}
Handle operator*=(double scale){
if (p->count > 1) {
Base* other = p->copy();
unhook();
p = other;
}
this->p->operator*=(scale);
return *this;
}
Handle setColor(RGB rgb){
if (p->count > 1) {
Base* other = p->copy();
unhook();
p = other;
}
this->p->setColor(rgb);
return *this;
}
private:
void unhook() {
--(p->count);
if (p->count == 0)
delete p;
}
Base* p = NULL;
};
#endif
// #ifndef _HANDLE_H_
// #define _HENDLE_H_
// #include <iostream>
// #include "base.h"
// #include "rgb.h"
// #include "point.h"
// #include "circle.h"
// #include "rect.h"
// #include "tri.h"
// // #include "desktop.h"
// using namespace std;
// class Handle{
// // friend class Desktop;
// // friend Desktop& operator<< (Desktop& o, const Handle& h);
// public:
// Handle(){}
// Handle(const Handle& h): p(h.p) {
// ++(p->count);
// } //為甚麼是傳Handle& 因為
// Handle(Point a,int _radius,RGB _rgb): p(new Circle(a,_radius,_rgb)){};
// Handle(Point a,int width,int height,RGB _rgb): p(new Rect(a,width,height,_rgb)){};
// Handle(Point a,int left,int right,int height,RGB _rgb): p(new Tri(a,left,right,height,_rgb)){};
// ~Handle() {}
// void draw(Bitmap& map,int method) const {
// // this->p->draw(map,method);
// // map.setPixel(p->ref,p->color,method);
// this->p->draw(map,method);
// }
// Handle operator=(Handle h){
// this->p = NULL;
// this->p = h.p;
// return *this;
// // this->p = NULL;
// // Handle newH;
// // newH.p=h.p->copy();
// // *this = newH;
// // return *this;
// }
// Handle operator+(Point a){
// Handle newH;
// newH.p=this->p->copy();
// newH.p->ref += a;
// return newH;
// }
// Handle operator+=(Point a){
// Handle newH;
// newH.p=this->p->copy();
// newH.p->ref += a;
// *this = newH;
// return *this;
// }
// Handle operator*(double scale){
// Handle h;
// h.p = this->p->copy();
// h.p->operator*=(scale);
// return h;
// }
// Handle operator*=(double scale){
// this->p->operator*=(scale);
// return *this;
// }
// Handle setColor(RGB rgb){
// this->p->setColor(rgb);
// return *this;
// }
// private:
// Base* p = NULL;
// };
// #endif