Skip to content

Commit e79e91f

Browse files
committed
add for tm_ostream
1 parent 8b1d80f commit e79e91f

File tree

2 files changed

+17
-54
lines changed

2 files changed

+17
-54
lines changed

System/IO/tm_ostream.cpp

+13-42
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* Routines for abstract base class
2222
******************************************************************************/
2323

24-
tm_ostream_rep::tm_ostream_rep () : ref_count (0) {}
24+
tm_ostream_rep::tm_ostream_rep () {}
2525
tm_ostream_rep::~tm_ostream_rep () {}
2626
void
2727
tm_ostream_rep::flush () {}
@@ -116,21 +116,21 @@ std_ostream_rep::flush () {
116116

117117
class buffered_ostream_rep : public tm_ostream_rep {
118118
public:
119-
tm_ostream_rep* master;
120-
string buf;
119+
tm_ostream master;
120+
string buf;
121121

122122
public:
123-
buffered_ostream_rep (tm_ostream_rep* master);
123+
buffered_ostream_rep (tm_ostream master);
124124
~buffered_ostream_rep ();
125125

126126
bool is_writable () const;
127127
void write (const char*);
128128
};
129129

130-
buffered_ostream_rep::buffered_ostream_rep (tm_ostream_rep* master2)
130+
buffered_ostream_rep::buffered_ostream_rep (tm_ostream master2)
131131
: master (master2) {}
132132

133-
buffered_ostream_rep::~buffered_ostream_rep () { DEC_COUNT (master); }
133+
buffered_ostream_rep::~buffered_ostream_rep () {}
134134

135135
bool
136136
buffered_ostream_rep::is_writable () const {
@@ -146,33 +146,9 @@ buffered_ostream_rep::write (const char* s) {
146146
* Abstract user interface
147147
******************************************************************************/
148148

149-
tm_ostream::tm_ostream () : rep (tm_new<std_ostream_rep> ()) {
150-
INC_COUNT (this->rep);
151-
}
152-
tm_ostream::tm_ostream (char* s) : rep (tm_new<std_ostream_rep> (s)) {
153-
INC_COUNT (this->rep);
154-
}
155-
tm_ostream::tm_ostream (FILE* f) : rep (tm_new<std_ostream_rep> (f)) {
156-
INC_COUNT (this->rep);
157-
}
158-
tm_ostream::tm_ostream (const tm_ostream& x) : rep (x.rep) {
159-
INC_COUNT (this->rep);
160-
}
161-
tm_ostream::tm_ostream (tm_ostream_rep* rep2) : rep (rep2) {
162-
INC_COUNT (this->rep);
163-
}
164-
tm_ostream::~tm_ostream () { DEC_COUNT (this->rep); }
165-
tm_ostream_rep*
166-
tm_ostream::operator->() {
167-
return rep;
168-
}
169-
tm_ostream&
170-
tm_ostream::operator= (tm_ostream x) {
171-
INC_COUNT (x.rep);
172-
DEC_COUNT (this->rep);
173-
this->rep= x.rep;
174-
return *this;
175-
}
149+
tm_ostream::tm_ostream () : base (make<std_ostream_rep> ()) {}
150+
tm_ostream::tm_ostream (char* s) : base (make<std_ostream_rep> (s)) {}
151+
tm_ostream::tm_ostream (FILE* f) : base (make<std_ostream_rep> (f)) {}
176152
bool
177153
tm_ostream::operator== (tm_ostream& out) {
178154
return (&out == this);
@@ -190,25 +166,20 @@ tm_ostream::flush () {
190166

191167
void
192168
tm_ostream::buffer () {
193-
rep= tm_new<buffered_ostream_rep> (rep);
194-
INC_COUNT (rep);
169+
*this= tm_ostream (make<buffered_ostream_rep> (*this));
195170
}
196171

197172
string
198173
tm_ostream::unbuffer () {
199174
buffered_ostream_rep* ptr= (buffered_ostream_rep*) rep;
200-
rep = ptr->master;
201-
string r = ptr->buf;
202-
INC_COUNT (rep);
203-
DEC_COUNT (ptr);
175+
string r = ptr->buf;
176+
*this = ptr->master;
204177
return r;
205178
}
206179

207180
void
208181
tm_ostream::redirect (tm_ostream x) {
209-
INC_COUNT (x.rep);
210-
DEC_COUNT (this->rep);
211-
this->rep= x.rep;
182+
*this= x;
212183
}
213184

214185
/******************************************************************************

System/IO/tm_ostream.hpp

+4-12
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313
#define OUT_STREAM_HPP
1414

1515
// #include "url.hpp"
16+
#include "sharedptr.hpp"
1617
#include "string.hpp"
1718
#include <cstdio>
1819
class tm_ostream;
1920

2021
class tm_ostream_rep {
21-
public:
22-
int ref_count;
23-
2422
public:
2523
tm_ostream_rep ();
2624
virtual ~tm_ostream_rep ();
@@ -33,9 +31,8 @@ class tm_ostream_rep {
3331
friend class tm_ostream;
3432
};
3533

36-
class tm_ostream {
37-
public:
38-
tm_ostream_rep* rep;
34+
class tm_ostream : public counted_ptr<tm_ostream_rep> {
35+
using base::counted_ptr;
3936

4037
public:
4138
static tm_ostream private_cout;
@@ -47,12 +44,7 @@ class tm_ostream {
4744
tm_ostream ();
4845
tm_ostream (char*);
4946
tm_ostream (FILE*);
50-
tm_ostream (const tm_ostream&);
51-
tm_ostream (tm_ostream_rep*);
52-
~tm_ostream ();
53-
tm_ostream_rep* operator->();
54-
tm_ostream& operator= (tm_ostream x);
55-
bool operator== (tm_ostream&);
47+
bool operator== (tm_ostream&);
5648

5749
void clear ();
5850
void flush ();

0 commit comments

Comments
 (0)