Commit 2c9501c4a98b60f0cdf7b37415db544b2f8555b2
1 parent
36081a4f
Removed buggy operator=(char*) from string.
Showing
27 changed files
with
2 additions
and
1359 deletions
examples04/04-rcstring/rcstring.h
... | ... | @@ -15,7 +15,6 @@ rcstring(); |
15 | 15 | rcstring(const char*); |
16 | 16 | rcstring(const rcstring&); |
17 | 17 | ~rcstring(); |
18 | -rcstring& operator=(const char*); | |
19 | 18 | rcstring& operator=(const rcstring&); |
20 | 19 | rcstring& operator+=(const rcstring &); |
21 | 20 | rcstring operator+(const rcstring &) const; |
... | ... | @@ -54,20 +53,6 @@ struct rcstring::rctext |
54 | 53 | n--; |
55 | 54 | return t; |
56 | 55 | }; |
57 | - void assign(unsigned int nsize, const char *p) | |
58 | - { | |
59 | - if(size!=nsize) | |
60 | - { | |
61 | - char* ns=new char[nsize+1]; | |
62 | - size = nsize; | |
63 | - strncpy(ns,p,size); | |
64 | - delete [] s; | |
65 | - s = ns; | |
66 | - } | |
67 | - else | |
68 | - strncpy(s,p,size); | |
69 | - s[size]='\0'; | |
70 | - } | |
71 | 56 | private: |
72 | 57 | rctext(const rctext&); |
73 | 58 | rctext& operator=(const rctext&); |
... | ... | @@ -103,19 +88,6 @@ rcstring::rcstring(const char* s) |
103 | 88 | data=new rctext(strlen(s),s); |
104 | 89 | } |
105 | 90 | |
106 | -rcstring& rcstring::operator=(const char* s) | |
107 | -{ | |
108 | - if(data->n==1) | |
109 | - data->assign(strlen(s),s); | |
110 | - else | |
111 | - { | |
112 | - rctext* t = new rctext(strlen(s),s); | |
113 | - data->n--; | |
114 | - data= t; | |
115 | - }; | |
116 | - return *this; | |
117 | -} | |
118 | - | |
119 | 91 | ostream& operator << (ostream& o, const rcstring& s) |
120 | 92 | { |
121 | 93 | return o<<s.data->s; | ... | ... |
examples04/05-rcstring/rcstring.h
... | ... | @@ -16,7 +16,6 @@ rcstring(); |
16 | 16 | rcstring(const char*); |
17 | 17 | rcstring(const rcstring&); |
18 | 18 | ~rcstring(); |
19 | -rcstring& operator=(const char*); | |
20 | 19 | rcstring& operator=(const rcstring&); |
21 | 20 | rcstring& operator+=(const rcstring &); |
22 | 21 | rcstring operator+(const rcstring &) const; |
... | ... | @@ -54,20 +53,6 @@ struct rcstring::rctext |
54 | 53 | n--; |
55 | 54 | return t; |
56 | 55 | }; |
57 | - void assign(unsigned int nsize, const char *p) | |
58 | - { | |
59 | - if(size!=nsize) | |
60 | - { | |
61 | - char* ns=new char[nsize+1]; | |
62 | - size = nsize; | |
63 | - strncpy(ns,p,size); | |
64 | - delete [] s; | |
65 | - s = ns; | |
66 | - } | |
67 | - else | |
68 | - strncpy(s,p,size); | |
69 | - s[size]='\0'; | |
70 | - } | |
71 | 56 | private: |
72 | 57 | rctext(const rctext&); |
73 | 58 | rctext& operator=(const rctext&); |
... | ... | @@ -126,19 +111,6 @@ rcstring::rcstring(const char* s) |
126 | 111 | data=new rctext(strlen(s),s); |
127 | 112 | } |
128 | 113 | |
129 | -rcstring& rcstring::operator=(const char* s) | |
130 | -{ | |
131 | - if(data->n==1) | |
132 | - data->assign(strlen(s),s); | |
133 | - else | |
134 | - { | |
135 | - rctext* t = new rctext(strlen(s),s); | |
136 | - data->n--; | |
137 | - data= t; | |
138 | - }; | |
139 | - return *this; | |
140 | -} | |
141 | - | |
142 | 114 | ostream& operator << (ostream& o, const rcstring& s) |
143 | 115 | { |
144 | 116 | return o<<s.data->s; | ... | ... |
examples04/06-threads/makefile
1 | -all: threads1 threads2 volatile1 volatile2 threads1_cxx11 threads2_cxx11 volatile1_cxx11 volatile1_cxx11.s volatile2_cxx11 volatile2_cxx11.s | |
1 | +all: threads1 threads1_cxx11 threads2_cxx11 volatile1_cxx11 volatile1_cxx11.s volatile2_cxx11 volatile2_cxx11.s | |
2 | 2 | |
3 | 3 | threads1: threads1.o |
4 | 4 | g++ -g $^ -lpthread -o$@ |
... | ... | @@ -6,12 +6,6 @@ threads1: threads1.o |
6 | 6 | threads1.o: threads1.cpp |
7 | 7 | g++ -g -c -Wall -pedantic $< -o $@ |
8 | 8 | |
9 | -threads2: threads2.o | |
10 | - g++ -g $^ -o $@ -lpthread | |
11 | - | |
12 | -threads2.o: threads2.cpp rcstring.h | |
13 | - g++ -g -c -Wall -pedantic $< -o $@ | |
14 | - | |
15 | 9 | threads1_cxx11: threads1_cxx11.cpp |
16 | 10 | g++ -g -std=c++11 -pthread -Wall -pedantic $< -o $@ |
17 | 11 | |
... | ... | @@ -30,27 +24,8 @@ volatile2_cxx11: volatile2_cxx11.cpp |
30 | 24 | volatile2_cxx11.s: volatile2_cxx11.cpp |
31 | 25 | g++ -g -std=c++11 -pthread -Wall -pedantic -S -O2 $< -o $@ |
32 | 26 | |
33 | -volatile1: volatile1.o | |
34 | - g++ -g $^ -o $@ -lpthread | |
35 | - | |
36 | -volatile1.o: volatile1.s | |
37 | - g++ -g -c $< -o $@ | |
38 | - | |
39 | -volatile1.s: volatile1.cpp | |
40 | - g++ -O2 -S -g -c -Wall -pedantic $< -o $@ | |
41 | - | |
42 | - | |
43 | -volatile2: volatile2.o | |
44 | - g++ -g $^ -o $@ -lpthread | |
45 | - | |
46 | -volatile2.o: volatile2.s | |
47 | - g++ -g -c $< -o $@ | |
48 | - | |
49 | -volatile2.s: volatile2.cpp | |
50 | - g++ -O2 -S -g -c -Wall -pedantic $< -o $@ | |
51 | - | |
52 | 27 | |
53 | 28 | .PHONY: clean |
54 | 29 | |
55 | 30 | clean: |
56 | - -rm threads1.o threads1 threads2.o threads2 volatile1.o volatile1 volatile2.o volatile2 volatile1.s volatile2.s threads1_cxx11 threads2_cxx11 volatile1_cxx11 volatile1_cxx11.s volatile2_cxx11 volatile2_cxx11.s | |
31 | + -rm threads1.o threads1 threads1_cxx11 threads2_cxx11 volatile1_cxx11 volatile1_cxx11.s volatile2_cxx11 volatile2_cxx11.s | ... | ... |
examples04/06-threads/rcstring.h
... | ... | @@ -16,7 +16,6 @@ rcstring(); |
16 | 16 | rcstring(const char*); |
17 | 17 | rcstring(const rcstring&); |
18 | 18 | ~rcstring(); |
19 | -rcstring& operator=(const char*); | |
20 | 19 | rcstring& operator=(const rcstring&); |
21 | 20 | rcstring& operator+=(const rcstring &); |
22 | 21 | rcstring operator+(const rcstring &) const; |
... | ... | @@ -55,21 +54,6 @@ struct rcstring::rctext |
55 | 54 | n--; |
56 | 55 | return t; |
57 | 56 | }; |
58 | - void assign(unsigned int nsize, const char *p) | |
59 | - { | |
60 | - if(size!=nsize) | |
61 | - { | |
62 | - char* ns=new char[size+1]; | |
63 | - size = nsize; | |
64 | - strncpy(ns,p,size); | |
65 | - delete [] s; | |
66 | - size = nsize; | |
67 | - s = ns; | |
68 | - } | |
69 | - else | |
70 | - strncpy(s,p,size); | |
71 | - s[size]='\0'; | |
72 | - } | |
73 | 57 | private: |
74 | 58 | rctext(const rctext&); |
75 | 59 | rctext& operator=(const rctext&); |
... | ... | @@ -129,19 +113,6 @@ rcstring::rcstring(const char* s) |
129 | 113 | data=new rctext(strlen(s),s); |
130 | 114 | } |
131 | 115 | |
132 | -rcstring& rcstring::operator=(const char* s) | |
133 | -{ | |
134 | - if(data->n==1) | |
135 | - data->assign(strlen(s),s); | |
136 | - else | |
137 | - { | |
138 | - rctext* t = new rctext(strlen(s),s); | |
139 | - data->n--; | |
140 | - data= t; | |
141 | - }; | |
142 | - return *this; | |
143 | -} | |
144 | - | |
145 | 116 | ostream& operator << (ostream& o, const rcstring& s) |
146 | 117 | { |
147 | 118 | return o<<s.data->s; | ... | ... |
examples04/06-threads/threads2.cpp deleted
100644 → 0
1 | -#include <stdio.h> | |
2 | -#include <pthread.h> | |
3 | -#include "rcstring.h" | |
4 | - | |
5 | - | |
6 | -rcstring s ("ala"); | |
7 | - | |
8 | -void * | |
9 | -thread1 (void *p) | |
10 | -{ | |
11 | - int *pp = (int *) p; | |
12 | - printf ("Parameter of thread1: %d\n", *pp); | |
13 | - *pp = 1; | |
14 | - for (unsigned int i = 0; i < 100000; i++) | |
15 | - { | |
16 | - printf ("Hello from thread 1\n"); | |
17 | - rcstring s1 (s); | |
18 | - } | |
19 | - pthread_exit (p); | |
20 | -} | |
21 | - | |
22 | -void * | |
23 | -thread2 (void *p) | |
24 | -{ | |
25 | - int *pp = (int *) p; | |
26 | - printf ("Parameter of thread2: %d\n", *pp); | |
27 | - *pp = 2; | |
28 | - for (unsigned int i = 0; i < 100000; i++) | |
29 | - { | |
30 | - printf ("Hello from thread 2\n"); | |
31 | - rcstring s1 (s); | |
32 | - } | |
33 | - pthread_exit (p); | |
34 | -} | |
35 | - | |
36 | - | |
37 | -int | |
38 | -main () | |
39 | -{ | |
40 | - pthread_t t1; | |
41 | - pthread_t t2; | |
42 | - int *retval1; | |
43 | - int *retval2; | |
44 | - int r1 = 10, r2 = 11; | |
45 | - | |
46 | - | |
47 | - pthread_create (&t1, NULL, thread1, &r1); | |
48 | - pthread_create (&t2, NULL, thread2, &r2); | |
49 | - | |
50 | - pthread_join (t1, (void **) &retval1); | |
51 | - pthread_join (t2, (void **) &retval2); | |
52 | - | |
53 | - printf ("%d\n", *retval1); | |
54 | - printf ("%d\n", *retval2); | |
55 | - printf ("RefCount=%d\n", s.getRefCount ()); | |
56 | -} |
examples04/06-threads/volatile1.cpp deleted
100644 → 0
1 | -#include <stdio.h> | |
2 | -#include <pthread.h> | |
3 | - | |
4 | -int val = 0; | |
5 | - | |
6 | -void * | |
7 | -fun1 (void *) | |
8 | -{ | |
9 | - val = 0; | |
10 | - while (1) | |
11 | - if (val != 0) | |
12 | - break; | |
13 | - printf ("out of the loop\n"); | |
14 | - pthread_exit (NULL); | |
15 | -} | |
16 | - | |
17 | - | |
18 | -int | |
19 | -main () | |
20 | -{ | |
21 | - pthread_t w; | |
22 | - | |
23 | - pthread_create (&w, NULL, fun1, NULL); | |
24 | - val = 1; | |
25 | - pthread_join (w, NULL); | |
26 | -} |
examples04/06-threads/volatile2.cpp deleted
100644 → 0
1 | -#include <stdio.h> | |
2 | -#include <pthread.h> | |
3 | - | |
4 | -volatile int val = 0; | |
5 | - | |
6 | -void * | |
7 | -fun1 (void *) | |
8 | -{ | |
9 | - val = 0; | |
10 | - while (1) | |
11 | - if (val != 0) | |
12 | - break; | |
13 | - printf ("out of the loop\n"); | |
14 | - pthread_exit (NULL); | |
15 | -} | |
16 | - | |
17 | - | |
18 | -int | |
19 | -main () | |
20 | -{ | |
21 | - pthread_t w; | |
22 | - | |
23 | - pthread_create (&w, NULL, fun1, NULL); | |
24 | - val = 1; | |
25 | - pthread_join (w, NULL); | |
26 | -} |
examples04/07-threads2/makefile deleted
100644 → 0
1 | -all: threads1 threads2 threads3 threads3 threads4 threads5 | |
2 | - | |
3 | -threads1: threads1.o | |
4 | - g++ -g $^ -o $@ -lpthread | |
5 | - | |
6 | -threads1.o: threads1.cpp rcstring1.h | |
7 | - g++ -g -c -Wall -pedantic $< -o $@ | |
8 | - | |
9 | -threads2: threads2.o | |
10 | - g++ -g $^ -o $@ -lpthread | |
11 | - | |
12 | -threads2.o: threads2.cpp rcstring2.h | |
13 | - g++ -g -c -Wall -pedantic $< -o $@ | |
14 | - | |
15 | -threads3: threads3.o | |
16 | - g++ -g $^ -o $@ -lpthread | |
17 | - | |
18 | -threads3.o: threads3.cpp rcstring3.h | |
19 | - g++ -g -c -Wall -pedantic $< -o $@ | |
20 | - | |
21 | -threads4: threads4.o | |
22 | - g++ -g $^ -o $@ -lpthread | |
23 | - | |
24 | -threads4.o: threads4.cpp | |
25 | - g++ -g -c -Wall -pedantic $< -o $@ | |
26 | - | |
27 | -threads5: threads5.o | |
28 | - g++ -g $^ -o $@ -lpthread | |
29 | - | |
30 | -threads5.o: threads5.cpp mystring.h | |
31 | - g++ -g -c -Wall -pedantic $< -o $@ | |
32 | - | |
33 | - | |
34 | -.PHONY: clean | |
35 | - | |
36 | -clean: | |
37 | - -rm threads1.o threads1 threads2.o threads2 threads3.o threads3 threads4.o threads4 threads5.o threads5 |
examples04/07-threads2/rcstring2.h deleted
100644 → 0
1 | -#ifndef __RCSTRING_H__ | |
2 | -#define __RCSTRING_H__ | |
3 | -#include <string.h> | |
4 | -#include <stdio.h> | |
5 | -#include <pthread.h> | |
6 | -#include <iostream> | |
7 | -using namespace std; | |
8 | - | |
9 | -class rcstring | |
10 | -{ | |
11 | - struct rctext; | |
12 | - rctext *data; | |
13 | -public: | |
14 | - class Range | |
15 | - { | |
16 | - }; | |
17 | - class Cref; | |
18 | - rcstring (); | |
19 | - rcstring (const char *); | |
20 | - rcstring (const rcstring &); | |
21 | - ~rcstring (); | |
22 | - rcstring & operator= (const char *); | |
23 | - rcstring & operator= (const rcstring &); | |
24 | - rcstring & operator+= (const rcstring &); | |
25 | - rcstring operator+ (const rcstring &) const; | |
26 | - friend ostream & operator<< (ostream &, const rcstring &); | |
27 | - void check (unsigned int i) const; | |
28 | - char read (unsigned int i) const; | |
29 | - void write (unsigned int i, char c); | |
30 | - char operator[] (unsigned int i) const; | |
31 | - Cref operator[] (unsigned int i); | |
32 | - unsigned int getRefCount (); | |
33 | -}; | |
34 | - | |
35 | -struct rcstring::rctext | |
36 | -{ | |
37 | - char *s; | |
38 | - unsigned int size; | |
39 | - unsigned int n; | |
40 | - pthread_mutex_t mutex; | |
41 | - | |
42 | - rctext (unsigned int nsize, const char *p) | |
43 | - { | |
44 | - n = 1; | |
45 | - size = nsize; | |
46 | - s = new char[size + 1]; | |
47 | - strncpy (s, p, size); | |
48 | - s[size] = '\0'; | |
49 | - pthread_mutex_init (&mutex, NULL); | |
50 | - }; | |
51 | - ~rctext () | |
52 | - { | |
53 | - delete[]s; | |
54 | - pthread_mutex_destroy(&mutex); | |
55 | - }; | |
56 | - unsigned int AtomicRead() | |
57 | - { | |
58 | - unsigned int retval; | |
59 | - pthread_mutex_lock(&mutex); | |
60 | - retval = n; | |
61 | - pthread_mutex_unlock(&mutex); | |
62 | - return retval; | |
63 | - }; | |
64 | - | |
65 | - unsigned int AtomicIncrement() | |
66 | - { | |
67 | - pthread_mutex_lock(&mutex); | |
68 | - unsigned int retval=++n; | |
69 | - pthread_mutex_unlock(&mutex); | |
70 | - return retval; | |
71 | - }; | |
72 | - | |
73 | - unsigned int AtomicDecrement() | |
74 | - { | |
75 | - pthread_mutex_lock(&mutex); | |
76 | - unsigned int retval=--n; | |
77 | - pthread_mutex_unlock(&mutex); | |
78 | - return retval; | |
79 | - }; | |
80 | - | |
81 | - void assign (unsigned int nsize, const char *p) | |
82 | - { | |
83 | - if (size != nsize) | |
84 | - { | |
85 | - char *ns = new char[size + 1]; | |
86 | - size = nsize; | |
87 | - strncpy (ns, p, size); | |
88 | - delete[]s; | |
89 | - size = nsize; | |
90 | - s = ns; | |
91 | - } | |
92 | - else | |
93 | - strncpy (s, p, size); | |
94 | - s[size] = '\0'; | |
95 | - } | |
96 | - | |
97 | -private: | |
98 | - rctext (const rctext &); | |
99 | - rctext & operator= (const rctext &); | |
100 | -}; | |
101 | - | |
102 | -class | |
103 | - rcstring::Cref | |
104 | -{ | |
105 | - friend class rcstring; | |
106 | - rcstring & s; | |
107 | - int i; | |
108 | - Cref (rcstring & ss, unsigned int ii): s (ss), i (ii) {}; | |
109 | -public: | |
110 | - operator char () const | |
111 | - { | |
112 | - return s.read (i); | |
113 | - } | |
114 | - rcstring::Cref & | |
115 | - operator = (char c) | |
116 | - { | |
117 | - s.write (i, c); | |
118 | - return *this; | |
119 | - }; | |
120 | - rcstring::Cref & operator = (const Cref & ref) | |
121 | - { | |
122 | - return operator= ((char) ref); | |
123 | - }; | |
124 | -}; | |
125 | -inline | |
126 | -rcstring::rcstring () | |
127 | -{ | |
128 | - data = new rctext (0, ""); | |
129 | -} | |
130 | - | |
131 | -inline | |
132 | -rcstring::rcstring (const rcstring & x) | |
133 | -{ | |
134 | - x.data->AtomicIncrement(); | |
135 | - data = x.data; | |
136 | -} | |
137 | - | |
138 | -inline | |
139 | -rcstring::~rcstring () | |
140 | -{ | |
141 | - if(data->AtomicDecrement()==0) | |
142 | - delete data; | |
143 | -} | |
144 | - | |
145 | -rcstring & rcstring::operator= (const rcstring & x) | |
146 | -{ | |
147 | - rctext* olddata=data; | |
148 | - x.data->AtomicIncrement(); | |
149 | - data = x.data; | |
150 | - if(olddata->AtomicDecrement()==0) | |
151 | - delete olddata; | |
152 | - return *this; | |
153 | -} | |
154 | - | |
155 | -rcstring::rcstring (const char *s) | |
156 | -{ | |
157 | - data = new rctext (strlen (s), s); | |
158 | -} | |
159 | - | |
160 | -rcstring & rcstring::operator= (const char *s) | |
161 | -{ | |
162 | - if (data->AtomicRead() == 1) | |
163 | - data->assign (strlen (s), s); | |
164 | - else | |
165 | - { | |
166 | - rctext* olddata=data; | |
167 | - data = new rctext (strlen (s), s); | |
168 | - if(olddata->AtomicDecrement()==0) | |
169 | - delete olddata; | |
170 | - }; | |
171 | - return *this; | |
172 | -} | |
173 | - | |
174 | -ostream & operator << (ostream & o, const rcstring & s) | |
175 | -{ | |
176 | - return o << s.data->s; | |
177 | -} | |
178 | - | |
179 | -rcstring & rcstring::operator+= (const rcstring & s) | |
180 | -{ | |
181 | - int newsize = data->size + s.data->size; | |
182 | - rctext * newdata = new rctext (newsize, data->s); | |
183 | - strcat (newdata->s, s.data->s); | |
184 | - rctext* olddata=data; | |
185 | - data=newdata; | |
186 | - if(olddata->AtomicDecrement()==0) | |
187 | - delete olddata; | |
188 | - return *this; | |
189 | -} | |
190 | - | |
191 | -rcstring | |
192 | -rcstring::operator+ (const rcstring & s) const | |
193 | -{ | |
194 | - return rcstring (*this) += s; | |
195 | -} | |
196 | - | |
197 | -inline void | |
198 | -rcstring::check (unsigned int i) const | |
199 | -{ | |
200 | - if (data->size <= i) | |
201 | - throw Range (); | |
202 | -} | |
203 | - | |
204 | -inline char | |
205 | -rcstring::read (unsigned int i) const | |
206 | -{ | |
207 | - return data->s[i]; | |
208 | -} | |
209 | - | |
210 | -inline void | |
211 | -rcstring::write (unsigned int i, char c) | |
212 | -{ | |
213 | - if (data->AtomicRead() > 1) | |
214 | - { | |
215 | - rctext* newdata=new rctext(data->size,data->s); | |
216 | - rctext* olddata=data; | |
217 | - data=newdata; | |
218 | - if(olddata->AtomicDecrement() == 0) | |
219 | - delete olddata; | |
220 | - } | |
221 | - data->s[i] = c; | |
222 | -} | |
223 | - | |
224 | -char | |
225 | -rcstring::operator[] (unsigned int i) const | |
226 | -{ | |
227 | - check (i); | |
228 | - return data->s[i]; | |
229 | -} | |
230 | - | |
231 | -rcstring::Cref rcstring::operator[](unsigned int i) | |
232 | -{ | |
233 | - check (i); | |
234 | - return Cref (*this, i); | |
235 | -} | |
236 | - | |
237 | - | |
238 | -unsigned int | |
239 | -rcstring::getRefCount () | |
240 | -{ | |
241 | - return data->AtomicRead();; | |
242 | -} | |
243 | - | |
244 | -#endif /* __RCSTRING_H__ */ |
examples04/07-threads2/rcstring3.h deleted
100644 → 0
1 | -#ifndef __RCSTRING_H__ | |
2 | -#define __RCSTRING_H__ | |
3 | -#include <string.h> | |
4 | -#include <stdio.h> | |
5 | -#include <ext/atomicity.h> | |
6 | -using namespace __gnu_cxx; | |
7 | -#include <iostream> | |
8 | -using namespace std; | |
9 | - | |
10 | -class rcstring | |
11 | -{ | |
12 | - struct rctext; | |
13 | - rctext *data; | |
14 | -public: | |
15 | - class Range | |
16 | - { | |
17 | - }; | |
18 | - class Cref; | |
19 | - rcstring (); | |
20 | - rcstring (const char *); | |
21 | - rcstring (const rcstring &); | |
22 | - ~rcstring (); | |
23 | - rcstring & operator= (const char *); | |
24 | - rcstring & operator= (const rcstring &); | |
25 | - rcstring & operator+= (const rcstring &); | |
26 | - rcstring operator+ (const rcstring &) const; | |
27 | - friend ostream & operator<< (ostream &, const rcstring &); | |
28 | - void check (unsigned int i) const; | |
29 | - char read (unsigned int i) const; | |
30 | - void write (unsigned int i, char c); | |
31 | - char operator[] (unsigned int i) const; | |
32 | - Cref operator[] (unsigned int i); | |
33 | - unsigned int getRefCount (); | |
34 | -}; | |
35 | - | |
36 | -struct rcstring::rctext | |
37 | -{ | |
38 | - char *s; | |
39 | - unsigned int size; | |
40 | - _Atomic_word n; | |
41 | - | |
42 | - rctext (unsigned int nsize, const char *p) | |
43 | - { | |
44 | - n = 1; | |
45 | - size = nsize; | |
46 | - s = new char[size + 1]; | |
47 | - strncpy (s, p, size); | |
48 | - s[size] = '\0'; | |
49 | - }; | |
50 | - ~rctext () | |
51 | - { | |
52 | - delete[]s; | |
53 | - }; | |
54 | - unsigned int AtomicRead() | |
55 | - { | |
56 | - return __exchange_and_add(&n,0); | |
57 | - }; | |
58 | - | |
59 | - unsigned int AtomicIncrement() | |
60 | - { | |
61 | - return __exchange_and_add(&n,1)+1; | |
62 | - }; | |
63 | - | |
64 | - unsigned int AtomicDecrement() | |
65 | - { | |
66 | - return __exchange_and_add(&n,-1)-1; | |
67 | - }; | |
68 | - | |
69 | - void assign (unsigned int nsize, const char *p) | |
70 | - { | |
71 | - if (size != nsize) | |
72 | - { | |
73 | - char *ns = new char[size + 1]; | |
74 | - size = nsize; | |
75 | - strncpy (ns, p, size); | |
76 | - delete[]s; | |
77 | - size = nsize; | |
78 | - s = ns; | |
79 | - } | |
80 | - else | |
81 | - strncpy (s, p, size); | |
82 | - s[size] = '\0'; | |
83 | - } | |
84 | - | |
85 | -private: | |
86 | - rctext (const rctext &); | |
87 | - rctext & operator= (const rctext &); | |
88 | -}; | |
89 | - | |
90 | -class | |
91 | - rcstring::Cref | |
92 | -{ | |
93 | - friend class rcstring; | |
94 | - rcstring & s; | |
95 | - int i; | |
96 | - Cref (rcstring & ss, unsigned int ii): s (ss), i (ii) {}; | |
97 | -public: | |
98 | - operator char () const | |
99 | - { | |
100 | - return s.read (i); | |
101 | - } | |
102 | - rcstring::Cref & | |
103 | - operator = (char c) | |
104 | - { | |
105 | - s.write (i, c); | |
106 | - return *this; | |
107 | - }; | |
108 | - rcstring::Cref & operator = (const Cref & ref) | |
109 | - { | |
110 | - return operator= ((char) ref); | |
111 | - }; | |
112 | -}; | |
113 | -inline | |
114 | -rcstring::rcstring () | |
115 | -{ | |
116 | - data = new rctext (0, ""); | |
117 | -} | |
118 | - | |
119 | -inline | |
120 | -rcstring::rcstring (const rcstring & x) | |
121 | -{ | |
122 | - x.data->AtomicIncrement(); | |
123 | - data = x.data; | |
124 | -} | |
125 | - | |
126 | -inline | |
127 | -rcstring::~rcstring () | |
128 | -{ | |
129 | - if(data->AtomicDecrement()==0) | |
130 | - delete data; | |
131 | -} | |
132 | - | |
133 | -rcstring & rcstring::operator= (const rcstring & x) | |
134 | -{ | |
135 | - rctext* olddata=data; | |
136 | - x.data->AtomicIncrement(); | |
137 | - data = x.data; | |
138 | - if(olddata->AtomicDecrement()==0) | |
139 | - delete olddata; | |
140 | - return *this; | |
141 | -} | |
142 | - | |
143 | -rcstring::rcstring (const char *s) | |
144 | -{ | |
145 | - data = new rctext (strlen (s), s); | |
146 | -} | |
147 | - | |
148 | -rcstring & rcstring::operator= (const char *s) | |
149 | -{ | |
150 | - if (data->AtomicRead() == 1) | |
151 | - data->assign (strlen (s), s); | |
152 | - else | |
153 | - { | |
154 | - rctext* olddata=data; | |
155 | - data = new rctext (strlen (s), s); | |
156 | - if(olddata->AtomicDecrement()==0) | |
157 | - delete olddata; | |
158 | - }; | |
159 | - return *this; | |
160 | -} | |
161 | - | |
162 | -ostream & operator << (ostream & o, const rcstring & s) | |
163 | -{ | |
164 | - return o << s.data->s; | |
165 | -} | |
166 | - | |
167 | -rcstring & rcstring::operator+= (const rcstring & s) | |
168 | -{ | |
169 | - int newsize = data->size + s.data->size; | |
170 | - rctext * newdata = new rctext (newsize, data->s); | |
171 | - strcat (newdata->s, s.data->s); | |
172 | - rctext* olddata=data; | |
173 | - data=newdata; | |
174 | - if(olddata->AtomicDecrement()==0) | |
175 | - delete olddata; | |
176 | - return *this; | |
177 | -} | |
178 | - | |
179 | -rcstring | |
180 | -rcstring::operator+ (const rcstring & s) const | |
181 | -{ | |
182 | - return rcstring (*this) += s; | |
183 | -} | |
184 | - | |
185 | -inline void | |
186 | -rcstring::check (unsigned int i) const | |
187 | -{ | |
188 | - if (data->size <= i) | |
189 | - throw Range (); | |
190 | -} | |
191 | - | |
192 | -inline char | |
193 | -rcstring::read (unsigned int i) const | |
194 | -{ | |
195 | - return data->s[i]; | |
196 | -} | |
197 | - | |
198 | -inline void | |
199 | -rcstring::write (unsigned int i, char c) | |
200 | -{ | |
201 | - if (data->AtomicRead() > 1) | |
202 | - { | |
203 | - rctext* newdata=new rctext(data->size,data->s); | |
204 | - rctext* olddata=data; | |
205 | - data=newdata; | |
206 | - if(olddata->AtomicDecrement() == 0) | |
207 | - delete olddata; | |
208 | - } | |
209 | - data->s[i] = c; | |
210 | -} | |
211 | - | |
212 | -char | |
213 | -rcstring::operator[] (unsigned int i) const | |
214 | -{ | |
215 | - check (i); | |
216 | - return data->s[i]; | |
217 | -} | |
218 | - | |
219 | -rcstring::Cref rcstring::operator[](unsigned int i) | |
220 | -{ | |
221 | - check (i); | |
222 | - return Cref (*this, i); | |
223 | -} | |
224 | - | |
225 | - | |
226 | -unsigned int | |
227 | -rcstring::getRefCount () | |
228 | -{ | |
229 | - return data->AtomicRead();; | |
230 | -} | |
231 | - | |
232 | -#endif /* __RCSTRING_H__ */ |
examples04/07-threads2/threads1.cpp deleted
100644 → 0
1 | -#include <stdio.h> | |
2 | -#include "rcstring1.h" | |
3 | - | |
4 | - | |
5 | -rcstring s ("ala"); | |
6 | - | |
7 | -void * | |
8 | -thread1 (void *p) | |
9 | -{ | |
10 | - int *pp = (int *) p; | |
11 | - printf ("Parameter of thread1: %d\n", *pp); | |
12 | - *pp = 1; | |
13 | - for (unsigned int i = 0; i < 100000; i++) | |
14 | - { | |
15 | - rcstring s1 (s); | |
16 | - } | |
17 | - pthread_exit (p); | |
18 | -} | |
19 | - | |
20 | -void * | |
21 | -thread2 (void *p) | |
22 | -{ | |
23 | - int *pp = (int *) p; | |
24 | - printf ("Parameter of thread2: %d\n", *pp); | |
25 | - *pp = 2; | |
26 | - for (unsigned int i = 0; i < 100000; i++) | |
27 | - { | |
28 | - rcstring s1 (s); | |
29 | - } | |
30 | - pthread_exit (p); | |
31 | -} | |
32 | - | |
33 | - | |
34 | -int | |
35 | -main () | |
36 | -{ | |
37 | - pthread_t t1; | |
38 | - pthread_t t2; | |
39 | - int *retval1; | |
40 | - int *retval2; | |
41 | - int r1 = 10, r2 = 11; | |
42 | - | |
43 | - | |
44 | - pthread_create (&t1, NULL, thread1, &r1); | |
45 | - pthread_create (&t2, NULL, thread2, &r2); | |
46 | - | |
47 | - pthread_join (t1, (void **) &retval1); | |
48 | - pthread_join (t2, (void **) &retval2); | |
49 | - | |
50 | - printf ("%d\n", *retval1); | |
51 | - printf ("%d\n", *retval2); | |
52 | - printf ("RefCount=%d\n", s.getRefCount ()); | |
53 | -} |
examples04/07-threads2/threads2.cpp deleted
100644 → 0
1 | -#include <stdio.h> | |
2 | -#include <pthread.h> | |
3 | -#include "rcstring2.h" | |
4 | - | |
5 | - | |
6 | -rcstring s ("ala"); | |
7 | - | |
8 | -void * | |
9 | -thread1 (void *p) | |
10 | -{ | |
11 | - int *pp = (int *) p; | |
12 | - printf ("Parameter of thread1: %d\n", *pp); | |
13 | - *pp = 1; | |
14 | - for (unsigned int i = 0; i < 100000; i++) | |
15 | - { | |
16 | - rcstring s1 (s); | |
17 | - } | |
18 | - pthread_exit (p); | |
19 | -} | |
20 | - | |
21 | -void * | |
22 | -thread2 (void *p) | |
23 | -{ | |
24 | - int *pp = (int *) p; | |
25 | - printf ("Parameter of thread2: %d\n", *pp); | |
26 | - *pp = 2; | |
27 | - for (unsigned int i = 0; i < 100000; i++) | |
28 | - { | |
29 | - rcstring s1 (s); | |
30 | - } | |
31 | - pthread_exit (p); | |
32 | -} | |
33 | - | |
34 | - | |
35 | -int | |
36 | -main () | |
37 | -{ | |
38 | - pthread_t t1; | |
39 | - pthread_t t2; | |
40 | - int *retval1; | |
41 | - int *retval2; | |
42 | - int r1 = 10, r2 = 11; | |
43 | - | |
44 | - | |
45 | - pthread_create (&t1, NULL, thread1, &r1); | |
46 | - pthread_create (&t2, NULL, thread2, &r2); | |
47 | - | |
48 | - pthread_join (t1, (void **) &retval1); | |
49 | - pthread_join (t2, (void **) &retval2); | |
50 | - | |
51 | - printf ("%d\n", *retval1); | |
52 | - printf ("%d\n", *retval2); | |
53 | - printf ("RefCount=%d\n", s.getRefCount ()); | |
54 | -} |
examples04/07-threads2/threads3.cpp deleted
100644 → 0
1 | -#include <stdio.h> | |
2 | -#include <pthread.h> | |
3 | -#include "rcstring3.h" | |
4 | - | |
5 | - | |
6 | -rcstring s ("ala"); | |
7 | - | |
8 | -void * | |
9 | -thread1 (void *p) | |
10 | -{ | |
11 | - int *pp = (int *) p; | |
12 | - printf ("Parameter of thread1: %d\n", *pp); | |
13 | - *pp = 1; | |
14 | - for (unsigned int i = 0; i < 100000; i++) | |
15 | - { | |
16 | - rcstring s1 (s); | |
17 | - } | |
18 | - pthread_exit (p); | |
19 | -} | |
20 | - | |
21 | -void * | |
22 | -thread2 (void *p) | |
23 | -{ | |
24 | - int *pp = (int *) p; | |
25 | - printf ("Parameter of thread2: %d\n", *pp); | |
26 | - *pp = 2; | |
27 | - for (unsigned int i = 0; i < 100000; i++) | |
28 | - { | |
29 | - rcstring s1 (s); | |
30 | - } | |
31 | - pthread_exit (p); | |
32 | -} | |
33 | - | |
34 | - | |
35 | -int | |
36 | -main () | |
37 | -{ | |
38 | - pthread_t t1; | |
39 | - pthread_t t2; | |
40 | - int *retval1; | |
41 | - int *retval2; | |
42 | - int r1 = 10, r2 = 11; | |
43 | - | |
44 | - | |
45 | - pthread_create (&t1, NULL, thread1, &r1); | |
46 | - pthread_create (&t2, NULL, thread2, &r2); | |
47 | - | |
48 | - pthread_join (t1, (void **) &retval1); | |
49 | - pthread_join (t2, (void **) &retval2); | |
50 | - | |
51 | - printf ("%d\n", *retval1); | |
52 | - printf ("%d\n", *retval2); | |
53 | - printf ("RefCount=%d\n", s.getRefCount ()); | |
54 | -} |
examples04/07-threads2/threads4.cpp deleted
100644 → 0
1 | -#include <stdio.h> | |
2 | -#include <string> | |
3 | -using namespace std; | |
4 | - | |
5 | - | |
6 | -string s ("ala"); | |
7 | - | |
8 | -void * | |
9 | -thread1 (void *p) | |
10 | -{ | |
11 | - int *pp = (int *) p; | |
12 | - printf ("Parameter of thread1: %d\n", *pp); | |
13 | - *pp = 1; | |
14 | - for (unsigned int i = 0; i < 100000; i++) | |
15 | - { | |
16 | - string s1 (s); | |
17 | - } | |
18 | - pthread_exit (p); | |
19 | -} | |
20 | - | |
21 | -void * | |
22 | -thread2 (void *p) | |
23 | -{ | |
24 | - int *pp = (int *) p; | |
25 | - printf ("Parameter of thread2: %d\n", *pp); | |
26 | - *pp = 2; | |
27 | - for (unsigned int i = 0; i < 100000; i++) | |
28 | - { | |
29 | - string s1 (s); | |
30 | - } | |
31 | - pthread_exit (p); | |
32 | -} | |
33 | - | |
34 | - | |
35 | -int | |
36 | -main () | |
37 | -{ | |
38 | - pthread_t t1; | |
39 | - pthread_t t2; | |
40 | - int *retval1; | |
41 | - int *retval2; | |
42 | - int r1 = 10, r2 = 11; | |
43 | - | |
44 | - | |
45 | - pthread_create (&t1, NULL, thread1, &r1); | |
46 | - pthread_create (&t2, NULL, thread2, &r2); | |
47 | - | |
48 | - pthread_join (t1, (void **) &retval1); | |
49 | - pthread_join (t2, (void **) &retval2); | |
50 | - | |
51 | - printf ("%d\n", *retval1); | |
52 | - printf ("%d\n", *retval2); | |
53 | -} |
examples04/07-threads2/threads5.cpp deleted
100644 → 0
1 | -#include <stdio.h> | |
2 | -#include "mystring.h" | |
3 | - | |
4 | -string s ("ala"); | |
5 | - | |
6 | -void * | |
7 | -thread1 (void *p) | |
8 | -{ | |
9 | - int *pp = (int *) p; | |
10 | - printf ("Parameter of thread1: %d\n", *pp); | |
11 | - *pp = 1; | |
12 | - for (unsigned int i = 0; i < 100000; i++) | |
13 | - { | |
14 | - string s1 (s); | |
15 | - } | |
16 | - pthread_exit (p); | |
17 | -} | |
18 | - | |
19 | -void * | |
20 | -thread2 (void *p) | |
21 | -{ | |
22 | - int *pp = (int *) p; | |
23 | - printf ("Parameter of thread2: %d\n", *pp); | |
24 | - *pp = 2; | |
25 | - for (unsigned int i = 0; i < 100000; i++) | |
26 | - { | |
27 | - string s1 (s); | |
28 | - } | |
29 | - pthread_exit (p); | |
30 | -} | |
31 | - | |
32 | - | |
33 | -int | |
34 | -main () | |
35 | -{ | |
36 | - pthread_t t1; | |
37 | - pthread_t t2; | |
38 | - int *retval1; | |
39 | - int *retval2; | |
40 | - int r1 = 10, r2 = 11; | |
41 | - | |
42 | - | |
43 | - pthread_create (&t1, NULL, thread1, &r1); | |
44 | - pthread_create (&t2, NULL, thread2, &r2); | |
45 | - | |
46 | - pthread_join (t1, (void **) &retval1); | |
47 | - pthread_join (t2, (void **) &retval2); | |
48 | - | |
49 | - printf ("%d\n", *retval1); | |
50 | - printf ("%d\n", *retval2); | |
51 | -} |
examples04/08-threads2_cxx11/makefile renamed to examples04/07-threads2_cxx11/makefile
examples04/07-threads2/mystring.h renamed to examples04/07-threads2_cxx11/mystring.h
examples04/07-threads2/rcstring1.h renamed to examples04/07-threads2_cxx11/rcstring1.h
... | ... | @@ -16,7 +16,6 @@ rcstring(); |
16 | 16 | rcstring(const char*); |
17 | 17 | rcstring(const rcstring&); |
18 | 18 | ~rcstring(); |
19 | -rcstring& operator=(const char*); | |
20 | 19 | rcstring& operator=(const rcstring&); |
21 | 20 | rcstring& operator+=(const rcstring &); |
22 | 21 | rcstring operator+(const rcstring &) const; |
... | ... | @@ -55,21 +54,6 @@ struct rcstring::rctext |
55 | 54 | n--; |
56 | 55 | return t; |
57 | 56 | }; |
58 | - void assign(unsigned int nsize, const char *p) | |
59 | - { | |
60 | - if(size!=nsize) | |
61 | - { | |
62 | - char* ns=new char[size+1]; | |
63 | - size = nsize; | |
64 | - strncpy(ns,p,size); | |
65 | - delete [] s; | |
66 | - size = nsize; | |
67 | - s = ns; | |
68 | - } | |
69 | - else | |
70 | - strncpy(s,p,size); | |
71 | - s[size]='\0'; | |
72 | - } | |
73 | 57 | private: |
74 | 58 | rctext(const rctext&); |
75 | 59 | rctext& operator=(const rctext&); |
... | ... | @@ -128,19 +112,6 @@ rcstring::rcstring(const char* s) |
128 | 112 | data=new rctext(strlen(s),s); |
129 | 113 | } |
130 | 114 | |
131 | -rcstring& rcstring::operator=(const char* s) | |
132 | -{ | |
133 | - if(data->n==1) | |
134 | - data->assign(strlen(s),s); | |
135 | - else | |
136 | - { | |
137 | - rctext* t = new rctext(strlen(s),s); | |
138 | - data->n--; | |
139 | - data= t; | |
140 | - }; | |
141 | - return *this; | |
142 | -} | |
143 | - | |
144 | 115 | ostream& operator << (ostream& o, const rcstring& s) |
145 | 116 | { |
146 | 117 | return o<<s.data->s; | ... | ... |
examples04/08-threads2_cxx11/rcstring2.h renamed to examples04/07-threads2_cxx11/rcstring2.h
... | ... | @@ -19,7 +19,6 @@ public: |
19 | 19 | rcstring (const char *); |
20 | 20 | rcstring (const rcstring &); |
21 | 21 | ~rcstring (); |
22 | - rcstring & operator= (const char *); | |
23 | 22 | rcstring & operator= (const rcstring &); |
24 | 23 | rcstring & operator+= (const rcstring &); |
25 | 24 | rcstring operator+ (const rcstring &) const; |
... | ... | @@ -76,22 +75,6 @@ struct rcstring::rctext |
76 | 75 | return retval; |
77 | 76 | }; |
78 | 77 | |
79 | - void assign (unsigned int nsize, const char *p) | |
80 | - { | |
81 | - if (size != nsize) | |
82 | - { | |
83 | - char *ns = new char[size + 1]; | |
84 | - size = nsize; | |
85 | - strncpy (ns, p, size); | |
86 | - delete[]s; | |
87 | - size = nsize; | |
88 | - s = ns; | |
89 | - } | |
90 | - else | |
91 | - strncpy (s, p, size); | |
92 | - s[size] = '\0'; | |
93 | - } | |
94 | - | |
95 | 78 | private: |
96 | 79 | rctext (const rctext &); |
97 | 80 | rctext & operator= (const rctext &); |
... | ... | @@ -155,20 +138,6 @@ rcstring::rcstring (const char *s) |
155 | 138 | data = new rctext (strlen (s), s); |
156 | 139 | } |
157 | 140 | |
158 | -rcstring & rcstring::operator= (const char *s) | |
159 | -{ | |
160 | - if (data->AtomicRead() == 1) | |
161 | - data->assign (strlen (s), s); | |
162 | - else | |
163 | - { | |
164 | - rctext* olddata=data; | |
165 | - data = new rctext (strlen (s), s); | |
166 | - if(olddata->AtomicDecrement()==0) | |
167 | - delete olddata; | |
168 | - }; | |
169 | - return *this; | |
170 | -} | |
171 | - | |
172 | 141 | ostream & operator << (ostream & o, const rcstring & s) |
173 | 142 | { |
174 | 143 | return o << s.data->s; | ... | ... |
examples04/08-threads2_cxx11/rcstring3.h renamed to examples04/07-threads2_cxx11/rcstring3.h
... | ... | @@ -19,7 +19,6 @@ public: |
19 | 19 | rcstring (const char *); |
20 | 20 | rcstring (const rcstring &); |
21 | 21 | ~rcstring (); |
22 | - rcstring & operator= (const char *); | |
23 | 22 | rcstring & operator= (const rcstring &); |
24 | 23 | rcstring & operator+= (const rcstring &); |
25 | 24 | rcstring operator+ (const rcstring &) const; |
... | ... | @@ -65,22 +64,6 @@ struct rcstring::rctext |
65 | 64 | return --n; |
66 | 65 | }; |
67 | 66 | |
68 | - void assign (unsigned int nsize, const char *p) | |
69 | - { | |
70 | - if (size != nsize) | |
71 | - { | |
72 | - char *ns = new char[size + 1]; | |
73 | - size = nsize; | |
74 | - strncpy (ns, p, size); | |
75 | - delete[]s; | |
76 | - size = nsize; | |
77 | - s = ns; | |
78 | - } | |
79 | - else | |
80 | - strncpy (s, p, size); | |
81 | - s[size] = '\0'; | |
82 | - } | |
83 | - | |
84 | 67 | private: |
85 | 68 | rctext (const rctext &); |
86 | 69 | rctext & operator= (const rctext &); |
... | ... | @@ -144,20 +127,6 @@ rcstring::rcstring (const char *s) |
144 | 127 | data = new rctext (strlen (s), s); |
145 | 128 | } |
146 | 129 | |
147 | -rcstring & rcstring::operator= (const char *s) | |
148 | -{ | |
149 | - if (data->AtomicRead() == 1) | |
150 | - data->assign (strlen (s), s); | |
151 | - else | |
152 | - { | |
153 | - rctext* olddata=data; | |
154 | - data = new rctext (strlen (s), s); | |
155 | - if(olddata->AtomicDecrement()==0) | |
156 | - delete olddata; | |
157 | - }; | |
158 | - return *this; | |
159 | -} | |
160 | - | |
161 | 130 | ostream & operator << (ostream & o, const rcstring & s) |
162 | 131 | { |
163 | 132 | return o << s.data->s; | ... | ... |
examples04/08-threads2_cxx11/threads1.cpp renamed to examples04/07-threads2_cxx11/threads1.cpp
examples04/08-threads2_cxx11/threads2.cpp renamed to examples04/07-threads2_cxx11/threads2.cpp
examples04/08-threads2_cxx11/threads3.cpp renamed to examples04/07-threads2_cxx11/threads3.cpp
examples04/08-threads2_cxx11/threads4.cpp renamed to examples04/07-threads2_cxx11/threads4.cpp
examples04/08-threads2_cxx11/threads5.cpp renamed to examples04/07-threads2_cxx11/threads5.cpp
examples04/08-threads2_cxx11/mystring.h deleted
100644 → 0
1 | -#ifndef __MYSTRING_H__ | |
2 | -#define __MYSTRING_H__ | |
3 | - | |
4 | -#include <string.h> | |
5 | -#include <iostream> | |
6 | - | |
7 | -class string | |
8 | -{ | |
9 | - char *data; | |
10 | - unsigned len; | |
11 | -public: | |
12 | - string ():data (0), len(0) | |
13 | - { | |
14 | - }; | |
15 | - ~string () | |
16 | - { | |
17 | - delete[]data; | |
18 | - }; | |
19 | - string (const char *s) | |
20 | - { | |
21 | - if (s) | |
22 | - { | |
23 | - len=strlen(s); | |
24 | - data = new char[len + 1]; | |
25 | - strcpy (data, s); | |
26 | - } | |
27 | - else | |
28 | - { | |
29 | - data = 0; | |
30 | - len = 0; | |
31 | - } | |
32 | - }; | |
33 | - | |
34 | - string (const string & src) | |
35 | - { | |
36 | - if (src.data) | |
37 | - { | |
38 | - data = new char[src.len + 1]; | |
39 | - len = src.len; | |
40 | - strcpy (data, src.data); | |
41 | - } | |
42 | - else | |
43 | - { | |
44 | - data = 0; | |
45 | - len = 0; | |
46 | - } | |
47 | - }; | |
48 | - string & operator= (const string & src) | |
49 | - { | |
50 | - if (this != &src) | |
51 | - { | |
52 | - delete[]data; | |
53 | - if (src.data) | |
54 | - { | |
55 | - data = new char[src.len + 1]; | |
56 | - strcpy (data, src.data); | |
57 | - } | |
58 | - else | |
59 | - { | |
60 | - data = 0; | |
61 | - len = 0; | |
62 | - } | |
63 | - }; | |
64 | - return *this; | |
65 | - } | |
66 | -}; | |
67 | - | |
68 | - | |
69 | -#endif /* __string_H__ */ |
examples04/08-threads2_cxx11/rcstring1.h deleted
100644 → 0
1 | -#ifndef __RCSTRING_H__ | |
2 | -#define __RCSTRING_H__ | |
3 | -#include <string.h> | |
4 | -#include <stdio.h> | |
5 | -#include <malloc.h> | |
6 | -#include <iostream> | |
7 | -using namespace std; | |
8 | - | |
9 | -class rcstring{ | |
10 | - struct rctext; | |
11 | - rctext* data; | |
12 | -public: | |
13 | -class Range{}; | |
14 | -class Cref; | |
15 | -rcstring(); | |
16 | -rcstring(const char*); | |
17 | -rcstring(const rcstring&); | |
18 | -~rcstring(); | |
19 | -rcstring& operator=(const char*); | |
20 | -rcstring& operator=(const rcstring&); | |
21 | -rcstring& operator+=(const rcstring &); | |
22 | -rcstring operator+(const rcstring &) const; | |
23 | -friend ostream& operator<<(ostream&, const rcstring&); | |
24 | -void check (unsigned int i) const; | |
25 | -char read(unsigned int i) const; | |
26 | -void write(unsigned int i, char c); | |
27 | -char operator[](unsigned int i) const; | |
28 | -Cref operator[](unsigned int i); | |
29 | -unsigned int getRefCount(); | |
30 | -}; | |
31 | - | |
32 | -struct rcstring::rctext | |
33 | -{ | |
34 | - char* s; | |
35 | - unsigned int size; | |
36 | - volatile unsigned int n; | |
37 | - | |
38 | - rctext(unsigned int nsize, const char* p) | |
39 | - { | |
40 | - n=1; | |
41 | - size=nsize; | |
42 | - s=new char[size+1]; | |
43 | - strncpy(s,p,size); | |
44 | - s[size]='\0'; | |
45 | - }; | |
46 | - ~rctext() | |
47 | - { | |
48 | - delete [] s; | |
49 | - }; | |
50 | - rctext* detach() | |
51 | - { | |
52 | - if(n==1) | |
53 | - return this; | |
54 | - rctext* t=new rctext(size, s); | |
55 | - n--; | |
56 | - return t; | |
57 | - }; | |
58 | - void assign(unsigned int nsize, const char *p) | |
59 | - { | |
60 | - if(size!=nsize) | |
61 | - { | |
62 | - char* ns=new char[size+1]; | |
63 | - size = nsize; | |
64 | - strncpy(ns,p,size); | |
65 | - delete [] s; | |
66 | - size = nsize; | |
67 | - s = ns; | |
68 | - } | |
69 | - else | |
70 | - strncpy(s,p,size); | |
71 | - s[size]='\0'; | |
72 | - } | |
73 | -private: | |
74 | - rctext(const rctext&); | |
75 | - rctext& operator=(const rctext&); | |
76 | -}; | |
77 | - | |
78 | -class rcstring::Cref | |
79 | -{ | |
80 | - friend class rcstring; | |
81 | - rcstring& s; | |
82 | - int i; | |
83 | - Cref (rcstring& ss, unsigned int ii): s(ss), i(ii) {}; | |
84 | -public: | |
85 | - operator char() const | |
86 | - { | |
87 | - cout << "operator char() const"<<endl; | |
88 | - return s.read(i); | |
89 | - } | |
90 | - rcstring::Cref& operator = (char c) | |
91 | - { | |
92 | - cout << "void operator = (char c)"<<endl; | |
93 | - s.write(i,c); | |
94 | - return *this; | |
95 | - }; | |
96 | - rcstring::Cref& operator = (const Cref& ref) | |
97 | - { | |
98 | - return operator= ((char)ref); | |
99 | - }; | |
100 | -}; | |
101 | -inline rcstring::rcstring() | |
102 | - { | |
103 | - data = new rctext(0,""); | |
104 | - } | |
105 | - | |
106 | -inline rcstring::rcstring(const rcstring& x) | |
107 | - { | |
108 | - x.data->n++; | |
109 | - data=x.data; | |
110 | - } | |
111 | -inline rcstring::~rcstring() | |
112 | -{ | |
113 | - if(--data->n==0) | |
114 | - delete data; | |
115 | -} | |
116 | - | |
117 | -rcstring& rcstring::operator=(const rcstring & x) | |
118 | -{ | |
119 | - x.data->n++; | |
120 | - if(--data->n == 0) | |
121 | - delete data; | |
122 | - data=x.data; | |
123 | - return *this; | |
124 | -} | |
125 | - | |
126 | -rcstring::rcstring(const char* s) | |
127 | -{ | |
128 | - data=new rctext(strlen(s),s); | |
129 | -} | |
130 | - | |
131 | -rcstring& rcstring::operator=(const char* s) | |
132 | -{ | |
133 | - if(data->n==1) | |
134 | - data->assign(strlen(s),s); | |
135 | - else | |
136 | - { | |
137 | - rctext* t = new rctext(strlen(s),s); | |
138 | - data->n--; | |
139 | - data= t; | |
140 | - }; | |
141 | - return *this; | |
142 | -} | |
143 | - | |
144 | -ostream& operator << (ostream& o, const rcstring& s) | |
145 | -{ | |
146 | - return o<<s.data->s; | |
147 | -} | |
148 | - | |
149 | -rcstring& rcstring::operator+=(const rcstring & s) | |
150 | -{ | |
151 | -int newsize=data->size+s.data->size; | |
152 | -rctext *newdata=new rctext(newsize,data->s); | |
153 | -strcat(newdata->s,s.data->s); | |
154 | -if(--data->n==0) | |
155 | - delete data; | |
156 | -data = newdata; | |
157 | -return *this; | |
158 | -} | |
159 | - | |
160 | -rcstring rcstring::operator+(const rcstring & s) const | |
161 | -{ | |
162 | - return rcstring(*this)+=s; | |
163 | -} | |
164 | - | |
165 | -inline void rcstring::check (unsigned int i) const | |
166 | -{ | |
167 | -if(data->size<=i) | |
168 | - throw Range(); | |
169 | -} | |
170 | -inline char rcstring::read(unsigned int i) const | |
171 | -{ | |
172 | - return data->s[i]; | |
173 | -} | |
174 | -inline void rcstring::write(unsigned int i, char c) | |
175 | -{ | |
176 | - data = data->detach(); | |
177 | - data->s[i] = c; | |
178 | -} | |
179 | - | |
180 | -char rcstring::operator[](unsigned int i) const | |
181 | -{ | |
182 | - cout << "char rcstring::operator[](unsigned int i) const"<<endl; | |
183 | - check(i); | |
184 | - return data->s[i]; | |
185 | -} | |
186 | - | |
187 | -rcstring::Cref rcstring::operator[](unsigned int i) | |
188 | -{ | |
189 | - cout << "Cref rcstring::operator[](unsigned int i)"<<endl; | |
190 | - check(i); | |
191 | - return Cref(*this,i); | |
192 | -} | |
193 | - | |
194 | - | |
195 | -unsigned int | |
196 | -rcstring::getRefCount() | |
197 | -{ | |
198 | - return data->n; | |
199 | -} | |
200 | - | |
201 | -#endif /* __RCSTRING_H__ */ |